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

import com.engine.common.util.ServiceUtil;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 加班计划流程归档action
 * @author sy
 */
@Slf4j
public class OvertimePlanArchivingAction implements Action {

    @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 {

            //记录需要更改出勤结果数据状态的人员、日期信息
            List<Map<String, String>> empDateList = new ArrayList<>();
            Map<String, String> empDateItem;
            List<String> dateList = new ArrayList<>();
            //遍历明细表1
            for (Map<String, String> data : detailTableData) {
                //收集需要更改出勤结果数据状态的人员、日期信息
                empDateItem = new HashMap<>();
                empDateItem.put("empId", data.getOrDefault("jbry", jbry));
                empDateItem.put("cqDate", data.get("gsrq"));
                empDateList.add(empDateItem);
            }
            //更新出勤结果中的数据状态
            String sjztUpdateResult = CommonUtil.updateAttendanceResultInfoStatus(empDateList, "0");
            if (!"".equals(sjztUpdateResult)) {
                log.error("更新出勤结果中的数据状态失败:" + sjztUpdateResult);
                requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                requestInfo.getRequestManager().setMessagecontent("更新出勤结果中的数据状态失败:" + sjztUpdateResult);
                return Action.FAILURE_AND_CONTINUE;
            }

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