package com.engine.attendance.workflow.action;

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

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

/**
 * 出差流程提交检查
 */
@Slf4j
public class EvctionCheckAction 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 ccr = mainTableData.get("ccr");

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

        Map<String,Object> dataMap = makeUpClockInService.getKqCycleTimeIntervalCmd(params);

        List<Map<String,Object>> dateList = (List<Map<String,Object>>)dataMap.get("data");

        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 (dateList.size()==0){
            log.error("申请人员当前时间没有考勤周期，请联系管理员！");
            requestInfo.getRequestManager().setMessageid("11111" + requestid + "22222");
            requestInfo.getRequestManager().setMessagecontent("申请人员当前时间没有考勤周期，请联系管理员！");
            return Action.FAILURE_AND_CONTINUE;
        }

        for (Map<String, String> detailTable:detailTableData){
            String bdkrq = detailTable.get("ksrq");
            boolean mark = true;
            for (Map<String,Object> date:dateList){
                //存在考勤周期内
                String startDate = Util.null2String(date.get("startDate"));
                String endDate = Util.null2String(date.get("endDate"));
                if (DateUtil.getTime(startDate).compareTo(DateUtil.getTime(bdkrq)) <=0 &&
                        DateUtil.getTime(endDate).compareTo(DateUtil.getTime(bdkrq)) >=0){
                    mark = false;
                }
            }
            if (mark){
                //已关账
                log.error("出差日期:{}，已关账无法提交出差",bdkrq);
                requestInfo.getRequestManager().setMessageid("11111" + requestid + "22222");
                requestInfo.getRequestManager().setMessagecontent("出差日期:"+bdkrq+"不在当前月份对应考勤周期内无法再提交出差！");
                return Action.FAILURE_AND_CONTINUE;
            }
        }

        return Action.SUCCESS;
    }
}
