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

import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
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;

/**
 * @Author: sy
 * @Description: 批量请假-归档节点附加操作
 * @Date: 2024/6/28
 **/
@Slf4j
public class BatchAskForLeaveWorkFlowArchivingAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {

        // 流程表单主表数据
        HashMap<String,String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);
        log.info("mainTableData : [{}]",mainTableData);
        // 流程表单明细表1数据
        List<Map<String, String>> detailTableData = CommonUtil.getDetailTableInfo(requestInfo,0);
        log.info("detailTableData : [{}]",detailTableData);

        //流程表单明细表2数据
        List<Map<String, String>> detailTable2 = CommonUtil.getDetailTableInfo(requestInfo,1);
        log.info("detailTable2Data : [{}]",detailTable2);

        //第一笔开始时间
//        String firstStartDate = detailTableData.get(0).get("ksrq");

        String sql = "select id,jqid,sxrq,ktsc,yxsc,wxsc,yqsxrq,ztsc from uf_jcl_kq_jqye order by sxrq";

        List<Map<String,Object>> holidayBalanceList = DbTools.getSqlToList(sql);

        Map<String,Object> holidayBalanceMap = holidayBalanceList.stream().collect(Collectors.toMap(e->e.get("id").toString(), e->e));
//        log.info("holidayBalanceMap : [{}]",holidayBalanceMap);
        try {
            if (detailTable2.size() > 0) {
                Map<String,Object> balanceMap = Maps.newHashMap();
                for (Map<String, String> data : detailTable2){
                    String jqye = Util.null2String(data.get("jqye"));
                    double sysc = Double.valueOf(data.get("sysc").toString());
                    double havedSysc= balanceMap.get(jqye) ==null?0:Double.valueOf(balanceMap.get(jqye).toString());
                    sysc = Utils.add(sysc,havedSysc);
                    balanceMap.put(jqye,sysc);
                }

                for (Map.Entry<String,Object> entry:balanceMap.entrySet()){
                    String id = entry.getKey();
                    double ztsc = Double.valueOf(entry.getValue().toString());
                    Map<String,Object> map =  (Map<String,Object>)holidayBalanceMap.get(id);
//                    log.info(id + "-map : [{}]",map);
                    double havedztsc = Util.null2String(map.get("ztsc")).equals("") ? 0 : Double.valueOf(map.get("ztsc").toString());
                    double havedyxsc = Util.null2String(map.get("yxsc")).equals("") ? 0 : Double.valueOf(map.get("yxsc").toString());

                    double updatedztsc = Utils.subtract(havedztsc,ztsc);
                    double updatedwxsc = Utils.add(havedyxsc,ztsc);

                    String updateSql = "update uf_jcl_kq_jqye set yxsc=?,ztsc=? where id=?";
                    if (!DbTools.update(updateSql,updatedwxsc,updatedztsc,id)){
                        //更新假期余额失败
                        String message = "更新假期余额失败";
                        log.error(message);
                        requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }

            //记录需要更改出勤结果数据状态的人员、日期信息
            List<Map<String, String>> empDateList = new ArrayList<>();
            Map<String, String> empDateItem;
            List<String> dateList = new ArrayList<>();
            //遍历明细表1
            for (Map<String, String> data : detailTableData) {
                dateList = DateUtil.getDatesBetween(data.get("ksrq"), data.get("jsrq"));
                for (String date : dateList) {
                    //收集需要更改出勤结果数据状态的人员、日期信息
                    empDateItem = new HashMap<>();
                    empDateItem.put("empId", data.get("qjr"));
                    empDateItem.put("cqDate", date);
                    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("BatchAskForLeaveWorkFlowArchivingAction error : [{}]",e);
            return Action.FAILURE_AND_CONTINUE;
        }


        return Action.SUCCESS;
    }
}
