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 GetAskForLeaveCmd extends AbstractCommonCommand<Map<String,Object>> {

    public GetAskForLeaveCmd(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 a.qjry,a.jqlx,a.cxjqj,b.ksrq,b.kssj,b.jsrq,b.jssj,b.qjsc,b.qtj,b.btj from uf_jcl_kq_qjjl a left join uf_jcl_kq_qjjl_dt1 b on a.id = b.mainid where a.qjry=? and b.ksrq>=? and b.ksrq<=? and (b.cxqj=0 or b.cxqj is null)";
        List<Map<String,Object>> askForLeaveList = DbTools.getSqlToList(sql,userId, DateUtil.beforeDay(analysisDate,1),DateUtil.AfterDay(analysisDate,1));
        Map<String,Map<String,Object>> askForLeaveItems = Maps.newHashMap();


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

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

        Set<String> jplxSet = Sets.newHashSet();
        List<Map<String,Object>> haveAskForLeaveList = askForLeaveList.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("cxjqj"))){
                jplxSet.add(e.get("jqlx").toString());
                return true;
            }else if ("1".equals(e.get("cxjqj")) && DateUtil.getTime(kssj).compareTo(DateUtil.getTime(lastjssj)) <0 && DateUtil.getTime(jssj).compareTo(DateUtil.getTime(firstKssj)) >0 ){
                jplxSet.add(e.get("jqlx").toString());
                return true;
            }else {
                return false;
            }
        }).collect(Collectors.toList());
        log.info("haveAskForLeaveList : [{}]",haveAskForLeaveList);



        List<Map<String,Object>> resultList = Lists.newArrayList();
        List<Map<String,Object>> askForLeaveSchedule = Lists.newArrayList();
        resultMap.put("askForLeaveList",haveAskForLeaveList);
        resultMap.put("askForLeaveItems",askForLeaveItems);
        resultMap.put("askForLeaveSchedule",askForLeaveSchedule);
        if (jplxSet.size() == 0){
            return resultMap;
        }

        sql = "select * from uf_jcl_kq_kqxm where id in ("+String.join(",",jplxSet)+")";
        Map<String,List<Map<String,Object>>> collect = DbTools.getSqlToList(sql).stream().collect(Collectors.groupingBy(e->e.get("id").toString()));


        for (Map<String,Object> askForLeaveData : haveAskForLeaveList){
            Map<String,Object> askForLeaveItem = collect.get(askForLeaveData.get("jqlx")).get(0);
            //全天请假
            if (CheckBoxEnum.CHECKED.getKey().equals(askForLeaveData.get("qtj"))){
                Map<String,Object> map = Maps.newHashMap();
                map.put("item",askForLeaveData.get("jqlx"));
                map.put("itemduration",scheduleResult.get(0).get("edsc"));
                resultList.add(map);
                resultMap.put("alldayVaction",resultList);
            }


            askForLeaveItems.put(askForLeaveData.get("jqlx").toString(),askForLeaveItem);

            if (!"".equals(Util.null2String(askForLeaveData.get("kssj"))) && !"".equals(Util.null2String(askForLeaveData.get("jssj")))){
                Map<String,Object> askForLeaveMap = 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",askForLeaveData.get("kssj"));
                    put("dtjssj",askForLeaveData.get("jssj"));
                    put("ksdk",askForLeaveItem.get("fgsbxydk"));
                    put("jsdk",askForLeaveItem.get("lgsbxydk"));
                    put("tqdkfzs","60");
                    put("thdkfzs","60");
                    put("tqlghlfzs",askForLeaveItem.get("tqlghlfzs"));
                    put("thfghlfzs",askForLeaveItem.get("thfghlfzs"));
                }};
                String ksrq = Util.null2String(askForLeaveData.get("ksrq"));
                if (DateUtil.getTime(ksrq).compareTo(DateUtil.getTime(analysisDate)) == 0){
                    askForLeaveMap.put("gsrq",ClassBelongToEnum.NOWDAY.getKey());
                }else if (DateUtil.getTime(ksrq).compareTo(DateUtil.getTime(analysisDate)) < 0){
                    askForLeaveMap.put("gsrq",ClassBelongToEnum.YESTERDAY.getKey());
                }else if (DateUtil.getTime(ksrq).compareTo(DateUtil.getTime(analysisDate)) > 0){
                    askForLeaveMap.put("gsrq",ClassBelongToEnum.NEXTDAY.getKey());
                }
                askForLeaveSchedule.add(askForLeaveMap);
            }
        }


        return resultMap;
    }
}
