package com.dgtly.system.service.impl;

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

import com.alibaba.fastjson2.JSON;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
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(List<String> userTypeList,String orgCode) {
        //查询有效弹窗
        List<AlertConfiguration> alertConfigurationList = alertConfigurationMapper.selectAlertConfigurationByAlertStatus();
        log.info("查询弹框配置列表:{}", JSON.toJSON(alertConfigurationList));
        log.info("用户类型:{}",userTypeList);
        if(alertConfigurationList !=null && alertConfigurationList.size()> 0 ){
            //判断弹框的组织 如果弹框有组织 对比该组织与员工的组织是否一致
            Iterator<AlertConfiguration> it = alertConfigurationList.iterator();
            while (it.hasNext()) {
                AlertConfiguration alert = it.next();
                //diy
                if( alert.getOrgCode() ==null || ("null").equals(alert.getOrgCode())){
                    for (String userType:userTypeList){
                        String [] userTypes = alert.getUserType().split(",");
                        boolean exists = Arrays.stream(userTypes)
                                .anyMatch(str -> str.equals(userType));
                        if (exists){
                            return alert;
                        }
                    }
                }else {
                    List<String>  orgCodes = alertConfigurationMapper.selectOrgCodeByInterface(alert.getSalesLevel(),alert.getOrgCode());
                    if(orgCodes !=null && orgCodes.size()>0){
                        if(orgCodes.contains(orgCode)){
                            for (String userType:userTypeList){
                                String [] userTypes = userType.split(",");
                                boolean exists = Arrays.stream(userTypes)
                                        .anyMatch(str -> str.equals(userType));
                                if (exists){
                                    return alert;
                                }
                            }
                        }
                    }
                }
            }
        }
        return  null;
    }

    @Override
    public AlertConfiguration selectAlertConfiguration(List<String> userTypeList) {
        //查询有效弹窗
        List<AlertConfiguration> alertConfigurationList = alertConfigurationMapper.selectAlertConfigurationByAlertStatus();
        if(alertConfigurationList !=null && alertConfigurationList.size()> 0 ){
            //判断弹框的组织 如果弹框有组织 对比该组织与员工的组织是否一致
            Iterator<AlertConfiguration> it = alertConfigurationList.iterator();
            while (it.hasNext()) {
                AlertConfiguration alert = it.next();
                //diy
                if( alert.getOrgCode() ==null || ("null").equals(alert.getOrgCode())){
                    for (String userType:userTypeList){
                        String [] userTypes = alert.getUserType().split(",");
                        boolean exists = Arrays.stream(userTypes)
                                .anyMatch(str -> str.equals(userType));
                        if (exists){
                            return alert;
                        }
                    }
                }
            }
        }
        return  null;
    }

    @Override
    public AlertConfiguration selectAlert(String alertStatus) {
        //查询有效弹窗
        List<AlertConfiguration> alertConfigurationList = alertConfigurationMapper.selectAlertConfigurationByAlertStatus();
        if(alertConfigurationList !=null && alertConfigurationList.size()> 0 ){
            //判断弹框的组织 如果弹框有组织 对比该组织与员工的组织是否一致
            Iterator<AlertConfiguration> it = alertConfigurationList.iterator();
            while (it.hasNext()) {
                AlertConfiguration alert = it.next();
                //diy
                if( alert.getOrgCode() ==null || ("null").equals(alert.getOrgCode())){
                    String [] userTypes = alert.getUserType().split(",");
                    boolean exists = Arrays.stream(userTypes)
                            .anyMatch(str -> str.equals(alertStatus));
                    if (exists){
                        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(isOverlap(start1, end1, start2, end2)){
                               return 1;
                            }
                        }
                }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;
//                        }
                        if(isOverlap(start1, end1, start2, end2)){
                            return 1;
                        }
                    }

            }
        }else{
                    return 0;
        }

        return 0;
    }




    public static boolean isOverlap(Date start1, Date end1, Date start2, Date end2) {
        // 判断时间段1是否在时间段2之前
        if (end1.before(start2)) {
            return false;
        }
        // 判断时间段2是否在时间段1之前
        if (end2.before(start1)) {
            return false;
        }
        // 如果以上两个条件都不满足，则时间段重叠
        return true;
    }

    /**
     * 新增弹框配置
     * 
     * @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);
    }
}
