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

import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;

/**
 * @Author: sy
 * @Description: 撤销出差
 * @Date: 2024/3/15
 **/
@Slf4j
public class RevokeLeaveAction implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        log.debug("RevokeLeaveAction,do action on request:" + requestInfo.getRequestid());
        String requestid = requestInfo.getRequestid();
        try {

            // 流程表单主表数据
            HashMap<String, String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);
            String xcr = mainTableData.get("xjr");

            DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
            log.debug("RevokeLeaveAction detailTable : [{}], length : {}", Arrays.toString(detailTable), detailTable.length);
            List<Map<String, String>> updateList = new ArrayList<>();
            //记录需要更改出勤结果数据状态的人员、日期信息
            List<Map<String, String>> empDateList = new ArrayList<>();
            Map<String, String> empDateItem;
            List<String> dateList = new ArrayList<>();
            if (detailTable.length > 0) {
                for (DetailTable dt : detailTable) {
                    //明细表
                    Row[] s = dt.getRow();
                    for (Row r : s) {
                        //行
                        Cell[] h = r.getCell();
                        Map<String, String> itemMap = new HashMap<>();
                        for (Cell c : h) {
                            //列
                            String name = Util.null2String(c.getName());
                            log.debug("RevokeLeaveAction key : {}, value : {}",c.getName(), c.getValue());
                            if ("cxsm".equals(name)) {
                                itemMap.put("cxsm", Util.null2String(c.getValue()));
                            }
                            if ("cxlyid".equals(name)) {
                                itemMap.put("cxlyid", Util.null2String(c.getValue()));
                            }
                            if ("cx".equals(name)) {
                                itemMap.put("cx", Util.null2String(c.getValue()));
                            }
                            if ("ksrq".equals(name)) {
                                itemMap.put("ksrq", Util.null2String(c.getValue()));
                            }
                            if ("jsrq".equals(name)) {
                                itemMap.put("jsrq", Util.null2String(c.getValue()));
                            }
                        }
                        log.debug("RevokeLeaveAction itemMap : [{}]",itemMap);
                        if (Util.null2String(itemMap.get("cx")).equals("1")) {
                            updateList.add(itemMap);

                            dateList = DateUtil.getDatesBetween(itemMap.get("ksrq"), "".equals(Util.null2String(itemMap.get("jsrq"))) ? itemMap.get("ksrq") : itemMap.get("jsrq"));
                            for (String date : dateList) {
                                //收集需要更改出勤结果数据状态的人员、日期信息
                                empDateItem = new HashMap<>();
                                empDateItem.put("empId", xcr);
                                empDateItem.put("cqDate", date);
                                empDateList.add(empDateItem);
                            }
                        }
                    }
                }
            }
            log.debug("RevokeLeaveAction updateList : [{}]",updateList);
            if (updateList.size() > 0) {
                updateList.forEach(f -> {
                    if (StringUtils.isNotBlank(f.get("cxlyid"))) {
                        String sql = "update uf_jcl_kq_ccjl_dt1" +" set "+"cxly=0,cxcc=1,cxsm=?,cxid=? where id = ?";
                        log.debug("RevokeLeaveAction sql : [{}]",sql);
                        DbTools.update(sql, Util.null2String(f.get("cxsm")), requestid, f.get("cxlyid"));
                        log.debug("RevokeLeaveAction cxsm : {}, requestid : {}, cxlyid : {}",Util.null2String(f.get("cxsm")), requestid, f.get("cxlyid"));
                    }
                });

                //更新出勤结果中的数据状态
                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.debug("流程数据报错:RevokeLeaveAction:");
            StringWriter errorsWriter = new StringWriter();
            e.printStackTrace(new PrintWriter(errorsWriter));
            log.debug(errorsWriter.toString());
            requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
            requestInfo.getRequestManager().setMessagecontent("【更改出差撤销状态action】报错，请联系管理员！");
            return Action.FAILURE_AND_CONTINUE;
        }

        return SUCCESS;


    }
}
