package com.engine.attendance.workflow.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.util.CommonUtil;
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 lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class GetKqCycleTimeIntervalCmd extends AbstractCommonCommand<Map<String, Object>> {

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String userId = Util.null2String(params.get("userId"));
        String submitDates = Util.null2String(params.get("submitDate"));
        log.info("GetKqCycleTimeIntervalCmd userId:[{}],submitDates:[{}]",userId,submitDates);
        String modeId = Utils.getFormmodeIdMap().get("uf_jcl_kq_kqzqmc");

        Set<String> attendanceSetIdsSets = CommonUtil.getDataIds(userId,modeId,null,null);
        log.info("attendanceSetIdsSets : {}",attendanceSetIdsSets);


        String sql = "select id,departmentid,subcompanyid1 from hrmresource where id =?";
        Map<String,Object> departMentMap = DbTools.getSqlToMap(sql,userId);
        String deptid = Util.null2String(departMentMap.get("departmentid"));
        String pdeptids = "";
        List<Map<String,Object>> dateList = Lists.newArrayList();
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("status",true);
        resultMap.put("data",dateList);
        try {
            pdeptids = new DepartmentComInfo().getAllParentDepartId(Util.null2String(departMentMap.get("departmentid")), pdeptids);
            pdeptids = deptid + pdeptids;
            log.info("pdeptids : [{}]",pdeptids);
            LocalDateTime nowLocalDateTime = DateUtil.getTime(submitDates);
            int nowyear = nowLocalDateTime.getYear();
            int nowMonth = nowLocalDateTime.getMonth().getValue();

            LocalDateTime beforeLocalDateTime = DateUtil.getTime(submitDates).minusMonths(1);
            int beforeYear = beforeLocalDateTime.getYear();
            int beforeMonth = beforeLocalDateTime.getMonth().getValue();

            sql = "select a.zt,a.ksrq,a.jsrq,a.gzrq,a.gzsj,b.zt dtzt,b.tsbm,b.gzrq dtgzrq,b.gzsj dtgzsj from uf_jcl_kq_kqzq a left join uf_jcl_kq_kqzq_dt1 b on a.id=b.mainid where  a.mc=? and (a.nd=? and a.yf=? or a.nd=? and a.yf=?)";
            if (attendanceSetIdsSets.toArray().length == 0){
                resultMap.put("status",false);
                return resultMap;
            }
            List<Map<String,Object>> list = DbTools.getSqlToList(sql,attendanceSetIdsSets.toArray()[0],nowyear,nowMonth,beforeYear,beforeMonth);

            log.info("GetKqCycleTimeIntervalCmd list : [{}]",list);
            for (Map<String,Object> dataMap : list){
                String tsbm = Util.null2String(dataMap.get("tsbm"));
                if (!"".equals(tsbm) && CommonUtil.ifContainStr(pdeptids,tsbm,",")){
                    //人员在特殊部门里
                    String gzrq = Util.null2String(dataMap.get("dtgzrq")) +" "+Util.null2String(dataMap.get("dtgzsj")) +":00";
                    if (DateUtil.getTime(submitDates).compareTo(DateUtil.getTime(gzrq))<=0){
                        Map<String,Object> date = Maps.newHashMap();
                        date.put("startDate",dataMap.get("ksrq"));
                        date.put("endDate",dataMap.get("jsrq"));
                        dateList.add(date);
                    }
                }
            }

            for (Map<String,Object> dataMap : list){
                String gzrq = Util.null2String(dataMap.get("gzrq")) +" "+Util.null2String(dataMap.get("gzsj")) +":00";
                if (DateUtil.getTime(submitDates).compareTo(DateUtil.getTime(gzrq))<=0){
                    Map<String,Object> date = Maps.newHashMap();
                    List<Map<String,Object>> filterDate = dateList.stream().filter(e->e.get("startDate").equals(dataMap.get("ksrq"))).collect(Collectors.toList());
                    if (filterDate.size() == 0){
                        date.put("startDate",dataMap.get("ksrq"));
                        date.put("endDate",dataMap.get("jsrq"));
                        dateList.add(date);
                    }
                }
            }
        }catch (Exception e){
            log.error("catch error :{}",e);
        }
        log.info("GetKqCycleTimeIntervalCmd dateList : [{}]",dateList);

        return resultMap;
    }
}
