package com.engine.attendance.attendanceanalysis.cmd;

import com.engine.attendance.enums.CheckBoxEnum;
import com.engine.attendance.enums.ClassBelongToEnum;
import com.engine.attendance.enums.ClassSegmentTypeEnum;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.util.DateUtil;
import com.engine.common.util.DbTools;
import com.engine.common.util.Utils;
import com.engine.core.interceptor.CommandContext;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 获得出差数据
 */
@Slf4j
public class GetEvectionCmd extends AbstractCommonCommand<Map<String,Object>> {

    public GetEvectionCmd(Map<String,Object> params){
        this.params=params;
    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String,Object> resultMap = Maps.newHashMap();
        String userId = Util.null2String(params.get("userId"));
        String analysisDate = Util.null2String(params.get("analysisDate"));
        List<Map<String, Object>> scheduleResult =  (List<Map<String, Object>>)params.get("scheduleResult");
        String sql = "select b.ccr,a.cclx,a.ccsd,b.ksrq,b.jsrq,b.kssj,b.jssj,b.ccsc,b.qtcc,b.btcc,b.cxcc from uf_jcl_kq_ccjl a left join uf_jcl_kq_ccjl_dt1 b on a.id = b.mainid where b.ccr=? and b.ksrq>=? and b.ksrq<=? and (b.cxcc=0 or b.cxcc is null)";
        List<Map<String,Object>> evectionList = DbTools.getSqlToList(sql,userId, DateUtil.beforeDay(analysisDate,1),DateUtil.AfterDay(analysisDate,1));
        Map<String,Map<String,Object>> evectionItems = Maps.newHashMap();


        String firstKssj = Utils.getkssjTime(scheduleResult.get(0),analysisDate);

        String lastjssj = Utils.getjssjTime(scheduleResult.get(scheduleResult.size()-1),analysisDate);

        Set<String> cclxSet = Sets.newHashSet();
        List<Map<String,Object>> haveEvectionList = evectionList.stream().filter(e->{
            String kssj = e.get("ksrq")+" "+e.get("kssj");
            String jssj = e.get("jsrq")+" "+e.get("jssj");
            if (e.get("ksrq").equals(analysisDate) && !"1".equals(e.get("ccsd"))){
                cclxSet.add(e.get("cclx").toString());
                return true;
            }else if ("1".equals(e.get("ccsd")) && DateUtil.getTime(kssj).compareTo(DateUtil.getTime(lastjssj)) <0 && DateUtil.getTime(jssj).compareTo(DateUtil.getTime(firstKssj)) >0){
                cclxSet.add(e.get("cclx").toString());
                return true;
            }else {
                return false;
            }
        }).collect(Collectors.toList());
        log.info("haveEvectionList : [{}]",haveEvectionList);
        List<Map<String,Object>> resultList = Lists.newArrayList();
        List<Map<String,Object>> evectionSchedule = Lists.newArrayList();

        resultMap.put("evectionList",haveEvectionList);
        resultMap.put("evectionItems",evectionItems);
        resultMap.put("evectionSchedule",evectionSchedule);
        if (cclxSet.size() == 0){
            return resultMap;
        }
        sql = "select * from uf_jcl_kq_kqxm where id in ("+String.join(",",cclxSet)+")";
        Map<String,List<Map<String,Object>>> collect = DbTools.getSqlToList(sql).stream().collect(Collectors.groupingBy(e->e.get("id").toString()));


        for (Map<String,Object> evectionData : haveEvectionList){
            Map<String,Object> evectionItem = collect.get(evectionData.get("cclx")).get(0);
            //全天出差
            if (CheckBoxEnum.CHECKED.getKey().equals(evectionData.get("qtcc"))){
                Map<String,Object> map = Maps.newHashMap();
                map.put("item",evectionData.get("cclx"));
                map.put("itemduration",scheduleResult.get(0).get("edsc"));
                resultList.add(map);
                resultMap.put("alldayEvection",resultList);
            }


            evectionItems.put(evectionData.get("cclx").toString(),evectionItem);

            if (!"".equals(Util.null2String(evectionData.get("kssj"))) && !"".equals(Util.null2String(evectionData.get("jssj")))){
                Map<String,Object> evectioneMap = new HashMap(){{
                    put("bcxx", scheduleResult.get(0).get("bcxx"));
                    put("bcsdxx", scheduleResult.get(0).get("bcsdxx"));
                    put("edsc", scheduleResult.get(0).get("edsc"));
                    put("bdlx", ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey());
                    put("dtkssj",evectionData.get("kssj"));
                    put("dtjssj",evectionData.get("jssj"));
                    put("ksdk",evectionItem.get("fgsbxydk"));
                    put("jsdk",evectionItem.get("lgsbxydk"));
                    put("tqdkfzs","60");
                    put("thdkfzs","60");
                    put("tqlghlfzs",evectionItem.get("tqlghlfzs"));
                    put("thfghlfzs",evectionItem.get("thfghlfzs"));
                }};
                String ksrq = Util.null2String(evectionData.get("ksrq"));
                if (DateUtil.getTime(ksrq).compareTo(DateUtil.getTime(analysisDate)) == 0){
                    evectioneMap.put("gsrq",ClassBelongToEnum.NOWDAY.getKey());
                }else if (DateUtil.getTime(ksrq).compareTo(DateUtil.getTime(analysisDate)) < 0){
                    evectioneMap.put("gsrq",ClassBelongToEnum.YESTERDAY.getKey());
                }else if (DateUtil.getTime(ksrq).compareTo(DateUtil.getTime(analysisDate)) > 0){
                    evectioneMap.put("gsrq",ClassBelongToEnum.NEXTDAY.getKey());
                }
                evectionSchedule.add(evectionData);
            }
        }

        return resultMap;
    }
}
