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

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

@Slf4j
public class AskForLeaveWorkFlowArchivingAction 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("detailTableData : [{}]",detailTable2);


        //请假人员
        String qjry = mainTableData.get("qjry");
        //实际假期类型
        String jqlx = mainTableData.get("sjqjlx");
        //需要校验假期额度的假期类型
        List<String> checkAmountJqIdList = CommonUtil.getJqInfoWithAmount();
        //第一笔开始时间
        String firstStartDate = detailTableData.get(0).get("ksrq");

        String sql = "select id,jqid,sxrq,ktsc,yxsc,wxsc,yqsxrq,ztsc from uf_jcl_kq_jqye where ygid=? and jqid=? and sxrq<=? and yqsxrq>=? order by sxrq";

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

        Map<String,Object> holidayBalanceMap = holidayBalanceList.stream().collect(Collectors.toMap(e->e.get("id").toString(), e->e));


        try {
            //当前假期类型需要校验剩余额度时则更新假期余额，否则跳过
            if (checkAmountJqIdList.contains(jqlx)) {
                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);

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

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


        return Action.SUCCESS;
    }
}
