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

import com.engine.common.util.ServiceUtil;
import com.engine.jucailinkq.attendance.component.AttendanceCycle.service.AttendanceCycleService;
import com.engine.jucailinkq.attendance.component.AttendanceCycle.service.impl.AttendanceCycleServiceImpl;
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.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;

/**
 * 销差流程批准action
 */
@Slf4j
public class RevokeEvctionApproveAction implements Action {

    private AttendanceCycleService getAttendanceCycleService = ServiceUtil.getService(AttendanceCycleServiceImpl.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 xcr = mainTableData.get("xjr");

        /**
         * 考勤周期检查
         */
        Map<String, Object> params = Maps.newHashMap();
        params.put("userId",xcr);
        params.put("submitDate", DateUtil.getCurrentDate());
        params.put("submitStr","ksrq");
        //筛选出勾选了撤销的明细数据
        detailTableData = detailTableData.stream().filter(e->"1".equals(e.get("cx"))).collect(Collectors.toList());
        params.put("submitDataList",detailTableData);
        if (detailTableData.size() > 0) {
            Map<String,Object> dataMap = getAttendanceCycleService.getKqCycleCheckoutInfoCmd(params);
            List<Map<String,Object>> checkoutList = (List<Map<String,Object>>)dataMap.get("checkoutList");
            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 (checkoutList.size() > 0 || nocycleList.size() > 0){
                String message = "";
                if (nocycleList.size() > 0){
                    message = message +String.join(",",nocycleList)+"未找对对应的考勤周期；";
                }
                if (checkoutList.size() > 0){
                    List<String> list = checkoutList.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;
    }
}
