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

/**
 * 加班计划流程批准action
 */
@Slf4j
public class OvertimePlanApproveAction 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 jbry = mainTableData.get("jbry");
        try {
            Map<String,List<Map<String, String>>> detailGroupMap = detailTableData.stream().collect(Collectors.groupingBy(e->Util.null2String(e.get("jbry"))));

            for (Map.Entry<String,List<Map<String, String>>> tableEntry :detailGroupMap.entrySet()){
                List<Map<String, String>> detailGroupByUserList = tableEntry.getValue();
                Map<String, Object> params = Maps.newHashMap();
                if ("".equals(tableEntry.getKey())){
                    params.put("userId",jbry);
                }else {
                    params.put("userId",tableEntry.getKey());
                }
                params.put("submitDate",DateUtil.getCurrentDate());
                params.put("submitStr","ksrq");
                params.put("submitDataList",detailGroupByUserList);

                /**
                 * 考勤周期校验
                 */
                if (detailGroupByUserList == null || detailGroupByUserList.size() == 0){
                    log.error("明细表没有数据！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestid + "22222");
                    requestInfo.getRequestManager().setMessagecontent("明细表没有数据！");
                    return Action.FAILURE_AND_CONTINUE;
                }
                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");
                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 (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;
                }
            }

        }catch (Exception e){
            log.error("OvertimePlanApproveAction error : [{}]",e);
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
