package com.dgtly.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.dgtly.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgtly.system.mapper.AlertConfigurationMapper;
import com.dgtly.system.domain.AlertConfiguration;
import com.dgtly.system.service.IAlertConfigurationService;
import com.dgtly.common.core.text.Convert;

/**
 * 弹框配置Service业务层处理
 * 
 * @author chenyn
 * @date 2023-06-13
 */
@Service
public class AlertConfigurationServiceImpl implements IAlertConfigurationService 
{
    @Autowired
    private AlertConfigurationMapper alertConfigurationMapper;

    /**
     * 查询弹框配置
     * 
     * @param alertId 弹框配置ID
     * @return 弹框配置
     */
    @Override
    public AlertConfiguration selectAlertConfigurationById(Long alertId)
    {
        return alertConfigurationMapper.selectAlertConfigurationById(alertId);
    }

    @Override
    public AlertConfiguration selectAlertConfigurationByAlertStatus(String userType,String salesLevel,String orgCode) {
        List<AlertConfiguration> alertConfigurationList = alertConfigurationMapper.selectAlertConfigurationByAlertStatus(userType);
        if(alertConfigurationList !=null && alertConfigurationList.size()> 0 ){
            Iterator<AlertConfiguration> it = alertConfigurationList.iterator();
            while (it.hasNext()) {
                AlertConfiguration alert = it.next();
                //diy
                if(orgCode ==null || ("").equals(orgCode)){
                    if( alert.getOrgCode() ==null || ("null").equals(alert.getOrgCode())){
                        return alert;
                    }
                }else{
                    List<String>  orgCodes = alertConfigurationMapper.selectOrgCodeByInterface(alert.getSalesLevel(),alert.getOrgCode());
                    if(orgCodes !=null && orgCodes.size()>0){
                        if(orgCodes.contains(orgCode)){
                            return alert;
                        }
                    }
                }
            }
        }
        return  null;
    }

    /**
     * 查询弹框配置列表
     * 
     * @param alertConfiguration 弹框配置
     * @return 弹框配置
     */
    @Override
    public List<AlertConfiguration> selectAlertConfigurationList(AlertConfiguration alertConfiguration)
    {
        return alertConfigurationMapper.selectAlertConfigurationList(alertConfiguration);
    }

    @Override
    public int selectAlertConfigurationListFilter(AlertConfiguration alertConfiguration) {
        Date start1 = alertConfiguration.getAlertStartTime();
        Date end1 = alertConfiguration.getAlertEndTime();
        List<AlertConfiguration> alertConfigurationList= alertConfigurationMapper.selectAlertConfigurationListFilter(alertConfiguration);
        if(alertConfigurationList !=null && alertConfigurationList.size()>0){
            Iterator<AlertConfiguration> it = alertConfigurationList.iterator();
            List<String> orgCodes =new ArrayList<>();
            if(alertConfiguration.getOrgCode() !=null &&  !("null").equals(alertConfiguration.getOrgCode() )  ) {
                 orgCodes = alertConfigurationMapper.selectOrgCode(alertConfiguration.getSalesLevel(), alertConfiguration.getOrgCode());
            }
            while (it.hasNext()) {
                AlertConfiguration alert = it.next();
                    if(orgCodes !=null && orgCodes.size()>0 && alert.getOrgCode() !=null && !("null").equals(alert.getOrgCode())){
                        if(orgCodes.contains(alert.getOrgCode())){
                            Date start2 = alert.getAlertStartTime();
                            Date end2 = alert.getAlertEndTime();
                            if ((start1.after(start2) && start1.before(end2)) ||
                                    (end1.after(start2) && end1.before(end2)) ||
                                    (start2.after(start1) && start2.before(end1)) ||
                                    (end2.after(start1) && end2.before(end1))) {
                               return 1;
                            } else {
                               return 0;
                            }
                        }
                }else{
                        Date start2 = alert.getAlertStartTime();
                        Date end2 = alert.getAlertEndTime();
                        if ((start1.after(start2) && start1.before(end2)) ||
                                (end1.after(start2) && end1.before(end2)) ||
                                (start2.after(start1) && start2.before(end1)) ||
                                (end2.after(start1) && end2.before(end1))) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
            }
        }else{
                    return 0;
        }
        return 0;
    }

    /**
     * 新增弹框配置
     * 
     * @param alertConfiguration 弹框配置
     * @return 结果
     */
    @Override
    public int insertAlertConfiguration(AlertConfiguration alertConfiguration)
    {
        return alertConfigurationMapper.insertAlertConfiguration(alertConfiguration);
    }

    /**
     * 修改弹框配置
     * 
     * @param alertConfiguration 弹框配置
     * @return 结果
     */
    @Override
    public int updateAlertConfiguration(AlertConfiguration alertConfiguration)
    {
        return alertConfigurationMapper.updateAlertConfiguration(alertConfiguration);
    }

    /**
     * 删除弹框配置对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAlertConfigurationByIds(String ids)
    {
        return alertConfigurationMapper.deleteAlertConfigurationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除弹框配置信息
     * 
     * @param alertId 弹框配置ID
     * @return 结果
     */
    @Override
    public int deleteAlertConfigurationById(Long alertId)
    {
        return alertConfigurationMapper.deleteAlertConfigurationById(alertId);
    }

    @Override
    public List<Map<String, Object>> searchProductParamByCustomerCode(String salesLevel, String orgCode) {
        String groupByStr =null;
        if(salesLevel !=null && ("diy_level").equals(salesLevel)){
            groupByStr = "company_code";
        }else if(salesLevel !=null && ("division_level").equals(salesLevel)){
            groupByStr = "company_code";
        }else if(salesLevel !=null && ("company_level").equals(salesLevel)){
            groupByStr = "office_code";
        }else if(salesLevel !=null && ("office_level").equals(salesLevel)){
            groupByStr = "suboffice_code";
        }
        return alertConfigurationMapper.searchProductParamByCustomerCode(salesLevel, orgCode,groupByStr);
    }
}
