package com.engine.jucailinkq.attendance.attendanceanalysis.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.jucailinkq.common.cmd.GetPersonBySuitOrganzationCmd;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

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

/**
 * 获得当前执行日期的考勤周期内需要分析的日期
 */
@Slf4j
public class GetAttendanceCycleCmd extends AbstractCommonCommand<Map<String,Object>> {

    public GetAttendanceCycleCmd(Map<String,Object> params){
        this.params=params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        List<Map<String,Object>> userList = (List<Map<String,Object>>)params.get("userList");
        String executeDate = Util.null2String(params.get("executeDate"));

        String modeId = Utils.getFormmodeIdMap().get("uf_jcl_kq_kqzqmc");
        params.put("modeId",modeId);
        Map<String,Object> userToDateMap = Maps.newHashMap();
        Map<String,Object> person = new GetPersonBySuitOrganzationCmd(params).execute(commandContext);
        Set<String> attendanceCycleSet = Sets.newHashSet();
        Set<String> subcompanyids = userList.stream().map(e->e.get("subcompanyid1").toString()).collect(Collectors.toSet());
        Map<String,String> subcompanyToAttendanceCycleMap = Maps.newHashMap();
        for (String subcompanyid :subcompanyids){
            String attendanceCycle = Utils.getDefaultAttendanceCycle(subcompanyid);
            subcompanyToAttendanceCycleMap.put(subcompanyid,attendanceCycle);
        }

        for (Map<String,Object> userMap : userList){
            String id = Util.null2String(userMap.get("id"));
            String subcompanyid1 =Util.null2String(userMap.get("subcompanyid1"));
            String attendanceCycle = person.get(id)==null?subcompanyToAttendanceCycleMap.get(subcompanyid1):person.get(id).toString();
            if ("".equals(attendanceCycle)){
                continue;
            }
            userMap.put("attendanceCycle",attendanceCycle);
            attendanceCycleSet.add(attendanceCycle);
        }
        String sql = "select id,mc,ksrq,jsrq from uf_jcl_kq_kqzq where mc in ("+String.join(",",attendanceCycleSet)+") and (gzrq<=? or (ksrq<=? and gzrq>=?)) and zt <> '2'";
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,executeDate,executeDate,executeDate);
        List<String> ksrqTojsrqList = dataList.stream().map(e->e.get("ksrq")+"&"+e.get("jsrq")).collect(Collectors.toList());
        Map<String,List<Map<String,Object>>> ksrqTojsrqMap = Maps.newHashMap();
        sql = "select rq,ygid from uf_jcl_kq_cqjg where (cqzt=1 or sjzt=0) and rq>=? and rq<=?";
        for (String key:ksrqTojsrqList){
            String ksrq = key.split("&")[0];
            String jsrq = key.split("&")[1];
            ksrqTojsrqMap.put(key,DbTools.getSqlToList(sql,ksrq,jsrq));
        }

        Map<String,List<Map<String,Object>>> dataCollect = dataList.stream().collect(Collectors.groupingBy(e->e.get("mc").toString()));


        for (Map<String,Object> userMap : userList){
            Set<String> dateList = Sets.newHashSet();
            dateList.add(DateUtil.beforeDay(executeDate,1));
            dateList.add(DateUtil.beforeDay(executeDate,2));
            String userId = Util.null2String(userMap.get("id"));
            userToDateMap.put(userId,dateList);

            String attendanceCycle = Util.null2String(userMap.get("attendanceCycle"));
            if (attendanceCycle.equals("")){
                continue;
            }
            List<Map<String,Object>> cycleList = dataCollect.get(attendanceCycle);
            if (cycleList ==null){
                continue;
            }
            for (Map<String,Object> cycleMap :cycleList){
                String key = cycleMap.get("ksrq")+"&"+cycleMap.get("jsrq");
                List<String> attendanceResultList = ksrqTojsrqMap.get(key).stream().filter(e->e.get("ygid").equals(userId))
                        .map(e->e.get("rq").toString()).collect(Collectors.toList());
                dateList.addAll(attendanceResultList);

            }
        }
        log.info("GetAttendanceCycleCmd executeDate : [{}]",executeDate);
        log.info("GetAttendanceCycleCmd userToDateMap : [{}]",userToDateMap);
        return userToDateMap;
    }
}
