package com.engine.attendance.component.persongroup.job.scheduling.tactics;

import com.engine.attendance.component.persongroup.commonutil.PersongroupCommonUtil;
import com.engine.attendance.enums.CheckBoxEnum;
import com.engine.attendance.enums.SchedulingApproachEnum;
import com.engine.common.util.CommonUtil;
import com.engine.common.util.DateUtil;
import com.engine.common.util.DbTools;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 轮转周期为天
 */
@Slf4j
public class DayRegularScheduling implements RegularScheduling{
    @Override
    public boolean support(int personClassfiy) {
        if (personClassfiy == 0){
            return true;
        }
        return false;
    }

    @Override
    public void compute(Map<String, Object> data) {
        log.info("import DayRegularScheduling :[{}]",data);
        //主表数据id
        String id = Util.null2String(data.get("id"));
        //规律排班模块Id
        String modeId = Util.null2String(data.get("modeId"));
        //排班结果模块Id
        String formmodeid = Util.null2String(data.get("formmodeid"));
        //生成天数
        int day = Integer.valueOf(Util.null2String(data.get("day")));
        //企业日历
        String qyrl = Util.null2String(data.get("qyrl"));
        //节假日处理 0:改班，1：跳过，2：不处理
        String jjrcl = Util.null2String(data.get("jjrcl"));
        //公休日处理 0：改班，1：跳过，2：不处理
        String gxrcl = Util.null2String(data.get("gxrcl"));
        //生效日期
        String bdate = Util.null2String(data.get("bdate"));
        //失效日期
        String edate = Util.null2String(data.get("edate"));
        //当前日期
        String nowDate = DateUtil.getCurrentDate();
        //排班方式
        String pbfs = Util.null2String(data.get("pbfs"));
        //改班班次
        String gbbc = Util.null2String(data.get("gbbc"));
        //改班班次是否休息
        String gbbcIfRest = CommonUtil.ifrestByClass(gbbc);

        //执行日期
        String executeDate = Util.null2String(data.get("executeDate"));


        //规律排班明细表数据
        String sql = "select a.ksts,a.jsts,a.hxbz,a.bcmc,b.sfxx from uf_jcl_kq_glpb_dt1 a left join uf_jcl_kq_bcxx b on a.bcmc=b.id where a.mainid=?";
        List<Map<String,Object>> detailDataList = DbTools.getSqlToList(sql,id);
        //规律排班适用组织数据
        sql = "select dx,dxlx,aqjb from uf_jcl_syzz where modeid=? and dataid=?";
        List<Map<String,Object>> organizationList = DbTools.getSqlToList(sql,modeId,id);
        //规律排班所用企业日历数据
        sql = "select rlmc,nd,rq,rqlx from uf_jcl_kq_rlxx where rlmc=?";
        List<Map<String,Object>> calendarList = DbTools.getSqlToList(sql,qyrl);
//        Map<String,String> calendarMap = calendarList.stream().collect(Collectors.toMap(e->Util.null2String(e.get("nd")),e->Util.null2String(e.get("id"))));
//        log.info("calendarMap : {}",calendarMap);
        Map<String, List<Map<String,Object>>> detailDataGroupingByYear = calendarList.stream().collect(Collectors.groupingBy(e ->e.get("nd").toString()));
        log.info("detailDataGroupingByYear : {}",detailDataGroupingByYear);
        //循环总天数
        int circulateDays = Integer.valueOf(detailDataList.get(detailDataList.size()-1).get("jsts").toString());
        int betweenDays = PersongroupCommonUtil.getBetweenDays(data);
        log.info("betweenDays : [{}]",betweenDays);
        /**按照天数规律循环遍历插入*/
        //从第几天开始循环
        int leaveDay = betweenDays%circulateDays + 1;
        int count = 0;
        List<Map<String,Object>> insertDataList = Lists.newArrayList();
        while (count <day){
            if (circulateDays < leaveDay){
                leaveDay = 1;
            }
            String bcrq = DateUtil.AfterDay(executeDate,count);
            if (DateUtil.getBetWeenDays(bcrq,edate) <=0){
                //超过规律有效期
                break;
            }
            for (Map<String,Object> map : detailDataList){
                int ksts = Integer.valueOf(Util.null2String(map.get("ksts")));
                int jsts = Integer.valueOf(Util.null2String(map.get("jsts")));
                Map<String,Object> dataMap = Maps.newHashMap();


                dataMap.put("pbtj", SchedulingApproachEnum.RULE.getKey());
                dataMap.put("pbgl",id);
                dataMap.put("bcrq",bcrq);
                if (jsts >= leaveDay && ksts <=leaveDay){
                    String dateTime =bcrq;
                    log.info("dateTime :{},count: {},map : {}",dateTime,count,map);
                    List<Map<String,Object>> detailList = detailDataGroupingByYear.get(dateTime.split("-")[0]);
                    if (detailList == null){
                        continue;
                    }
                    List<Map<String,Object>> nowDateList = detailList.stream().filter(e->dateTime.equals(Util.null2String(e.get("rq")))).collect(Collectors.toList());
                    if (nowDateList.size() ==0){
                        continue;
                    }
                    Map<String,Object> nowDateMap = nowDateList.get(0);
                        //日期类型
                    String rqlx = Util.null2String(nowDateMap.get("rqlx"));
                    dataMap.put("rqlx",rqlx);
                    String sfxx = Util.null2String(map.get("sfxx")).equals("")? CheckBoxEnum.UNCHECKED.getKey():Util.null2String(map.get("sfxx"));
                    dataMap.put("sfxx",sfxx);
                    if ("0".equals(pbfs)){
                        //固定班次
                        dataMap.put("sfdkpp","0");
                        dataMap.put("hxbz",null);
                        dataMap.put("bcxx",map.get("bcmc"));
                    }else if ("1".equals(pbfs)){
                        //打卡匹配
                        dataMap.put("sfdkpp","1");
                        dataMap.put("hxbz",map.get("hxbz"));
                        dataMap.put("bcxx",null);
                    }
                    dataMap.put("pbrq",DateUtil.getCurrentDate());
                    dataMap.put("pbsj",DateUtil.getCurrentTime("yyyy-MM-dd HH:mm").split(" ")[1]);
                    if ("1".equals(rqlx)){
                            //当为节假日时
                        if ("0".equals(jjrcl)){
                            //改班
                            if ("".equals(gbbc)){
                                //休息
                                dataMap.put("sfxx","1");
                                dataMap.put("bcxx",null);
                            }else {
                                dataMap.put("bcxx",gbbc);
                                dataMap.put("sfxx",gbbcIfRest);
                            }
                            insertDataList.add(dataMap);
                        }else if ("1".equals(jjrcl)){
                            //跳过
                            dataMap.put("sfxx","1");
                            dataMap.put("bcxx",null);
                            insertDataList.add(dataMap);
                        }else if ("2".equals(jjrcl)){
                            //不处理
                            insertDataList.add(dataMap);
                        }
                    }else if ("2".equals(rqlx) || "3".equals(rqlx)){
                        if ("0".equals(gxrcl)){
                            //改班
                            if ("".equals(gbbc)){
                                //休息
                                dataMap.put("sfxx","1");
                                dataMap.put("bcxx",null);
                            }else {
                                dataMap.put("bcxx",gbbc);
                                dataMap.put("sfxx",gbbcIfRest);
                            }
                            insertDataList.add(dataMap);
                        }else if ("1".equals(gxrcl)){
                            //跳过
                            dataMap.put("sfxx","1");
                            dataMap.put("bcxx",null);
                            insertDataList.add(dataMap);
                        }else if ("2".equals(gxrcl)){
                            //不处理
                            insertDataList.add(dataMap);
                        }
                    } else if ("0".equals(rqlx) || "4".equals(rqlx)){
                        //当为工作日
                        insertDataList.add(dataMap);
                    }

                }
            }
            count++;
            leaveDay++;
        }

        /**插入排班结果表*/
        PersongroupCommonUtil.insertShiftSchedulingResults(organizationList,insertDataList,formmodeid);

    }
}
