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;
import java.util.stream.Collectors;

/**
 * 出差流程提交检查
 */
@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());
        params.put("submitStr","ksrq");
        params.put("submitDataList",detailTableData);

        /**
         * 考勤周期校验
         */
        if (detailTableData == null || detailTableData.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");
        List<Map<String,Object>> dateList = (List<Map<String,Object>>)dataMap.get("dataList");

        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;
    }
}
