package com.engine.jucailinkq.attendance.workflow.action;

import com.engine.jucailinkq.attendance.workflow.service.MakeUpClockInService;
import com.engine.jucailinkq.attendance.workflow.service.impl.MakeUpClockInServiceImpl;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.common.util.ServiceUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

/**
 * 销假流程提交检查
 */
@Slf4j
public class EliminationsAskForLeaveAction implements Action {

    private MakeUpClockInService makeUpClockInService = ServiceUtil.getService(MakeUpClockInServiceImpl.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        // 流程表单主表数据
        HashMap<String,String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);
        // 流程表单明细表数据
        List<Map<String, String>> detailTableData = CommonUtil.getDetailTableInfo(requestInfo,0);
        //销差人
        String xjr = mainTableData.get("xjr");

        Map<String, Object> params = Maps.newHashMap();
        params.put("userId",xjr);
        params.put("submitDate", DateUtil.getCurrentDate());

        detailTableData = detailTableData.stream().filter(e->"1".equals(e.get("cx"))).collect(Collectors.toList());
        List<String> detailIds = detailTableData.stream().map(e->e.get("cxlyid")).collect(Collectors.toList());
        String sql = "select id,ksrq from uf_jcl_kq_qjjl_dt1 where (cxqj is null or  cxqj <> '1') and id in ("+String.join(",",detailIds)+")";
        List<Map<String, Object>> detailList = DbTools.getSqlToList(sql);

        params.put("submitStr","ksrq");
        params.put("submitDataList",detailList);

        if (detailList == null || detailList.size() == 0){
            log.error("未撤销任何假期！");
            requestInfo.getRequestManager().setMessageid("11111" + requestid + "22222");
            requestInfo.getRequestManager().setMessagecontent("未撤销任何假期！");
            return Action.FAILURE_AND_CONTINUE;
        }
        Map<String,Object> dataMap = makeUpClockInService.getKqCycleTimeIntervalCmd(params);
        List<Map<String,Object>> closeList = (List<Map<String,Object>>)dataMap.get("closeList");
        List<String> nocycleList = (List<String>)dataMap.get("nocycleList");

        boolean status = (boolean)dataMap.get("status");
        if (!status){
            log.error("该人员没有考勤周期");
            requestInfo.getRequestManager().setMessageid("11111" + requestid + "22222");
            requestInfo.getRequestManager().setMessagecontent("该人员没有考勤周期！");
            return Action.FAILURE_AND_CONTINUE;
        }
        if (closeList.size() > 0 || nocycleList.size() > 0){
            String message = "";
            if (nocycleList.size() > 0){
                message = message +String.join(",",nocycleList)+"未找对对应的考勤周期；";
            }
            if (closeList.size() > 0){
                List<String> list = closeList.stream().map(e->e.get("rq").toString()).collect(Collectors.toList());
                message = message +String.join(",",list)+"对应的考勤周期的考勤周期已关账";
            }
            requestInfo.getRequestManager().setMessageid("11111" + requestid + "22222");
            requestInfo.getRequestManager().setMessagecontent(message);
            return Action.FAILURE_AND_CONTINUE;
        }


        return Action.SUCCESS;
    }
}
