package com.engine.kq.cmd.attendanceEvent;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQLeaveRulesComInfo;
import com.engine.kq.biz.KQReportBiz;
import com.engine.kq.biz.KQSettingsBiz;
import com.engine.kq.enums.KqSplitFlowTypeEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 获取销假信息
 */
public class GetLeaveBackInfoCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetLeaveBackInfoCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> retmap = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        try {
            String resourceId = Util.null2String(params.get("resourceId"));
            String requestid = Util.null2String(params.get("requestid"));
            if (resourceId.length() == 0 || requestid.length() == 0) {
                retmap.put("status", "2");
                retmap.put("message", SystemEnv.getHtmlLabelName(390802, user.getLanguage()));
                return retmap;
            }

            getRequestInfo(requestid, resourceId, retmap,true);

        } catch (Exception e) {
            retmap.put("status", "-1");
            retmap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
            writeLog(e);
        }
        return retmap;
    }

    public void getRequestInfo(String requestid, String resourceId,
                               Map<String, Object> retmap,boolean flag) {

        String leaveback_clear = "0";
        boolean is_leaveback_clear = KQSettingsBiz.is_leaveback_clear();
        if (is_leaveback_clear) {
            leaveback_clear = "1";
        }
        KQLeaveRulesComInfo kqLeaveRulesComInfo = new KQLeaveRulesComInfo();
        List<Object> backList = new ArrayList<>();
        Map<String, Object> backMap = new HashMap<>();
        Map<String, Object> splitMap = new HashMap<>();

        RecordSet rs = new RecordSet();
        String requestSql = "select usedetail,newleavetype,fromdatedb,fromtimedb,todatedb,totimedb,durationdb,durationrule,detailid,dataid "
                + " from " + KqSplitFlowTypeEnum.LEAVE.getTablename() + " where 1=1 and status = 0 and requestid=? and resourceid=? "
                + " group by usedetail,newleavetype,fromdatedb,fromtimedb,todatedb,totimedb,durationdb,durationrule,detailid,dataid ";
        rs.executeQuery(requestSql, requestid, resourceId);
        while (rs.next()) {
            String usedetail = rs.getString("usedetail");
            String newleavetype = rs.getString("newleavetype");
            String fromdatedb = rs.getString("fromdatedb");
            String fromtimedb = rs.getString("fromtimedb");
            String todatedb = rs.getString("todatedb");
            String totimedb = rs.getString("totimedb");
            String durationdb = rs.getString("durationdb");
            String durationrule = rs.getString("durationrule");

            splitMap = new HashMap<>();
            splitMap.put("fromdatedb", fromdatedb);
            splitMap.put("fromtimedb", fromtimedb);
            splitMap.put("todatedb", todatedb);
            splitMap.put("totimedb", totimedb);
            splitMap.put("newleavetype", newleavetype);
            splitMap.put("durationdb", durationdb);
            splitMap.put("re_duration", durationdb);

            Map<String, Object> params = new HashMap<>();
            params.put("newLeaveType", newleavetype);
            params.put("resourceId", resourceId);
            params.put("fromDate", fromdatedb);
            params.put("toDate", todatedb);
            params.put("fromTime", fromtimedb);
            params.put("toTime", totimedb);
            // if(!"1".equalsIgnoreCase(leaveback_clear)){
            //   GetLeaveWorkDurationCmd getLeaveWorkDurationCmd = new GetLeaveWorkDurationCmd(params, user);
            //  Map<String, Object> leave_retmap = getLeaveWorkDurationCmd.execute(null);
            //  if(leave_retmap != null){
            //    String duration = Util.null2String(leave_retmap.get("duration"));
            //重新计算得到按照当前班次计算得到的时长
            //     splitMap.put("re_duration",duration);
            //   }
            // }
            if (durationrule.length() > 0) {
                if ("2".equalsIgnoreCase(durationrule)) {
                    if (splitMap.get("half_changeType") != null) {
                        String tmp_leave = Util.null2String(splitMap.get("half_changeType"));
                        tmp_leave += "," + newleavetype;
                    } else {
                        splitMap.put("half_changeType", newleavetype);
                    }
                }
                if ("4".equalsIgnoreCase(durationrule)) {
                    if (splitMap.get("whole_changeType") != null) {
                        String tmp_leave = Util.null2String(splitMap.get("whole_changeType"));
                        tmp_leave += "," + newleavetype;
                    } else {
                        splitMap.put("whole_changeType", newleavetype);
                    }

                }
            }

            String key = "";
            if ("1".equalsIgnoreCase(usedetail)) {
                key = "detail_" + rs.getString("detailid");
            } else {
                key = "main_" + rs.getString("dataid");
            }
            if (!backMap.containsKey(key)) {
                backList.add(splitMap);
                backMap.put(key, backList);
            }
        }

        if (backList.isEmpty() && flag) {
            new KQReportBiz().reflow(requestid);
            getRequestInfo(requestid, resourceId, retmap,false);//加个布尔判断以防死循环
        }


        if (backList.isEmpty()) {
            retmap.put("status", "2");
            retmap.put("message", SystemEnv.getHtmlLabelName(505779, user.getLanguage()));
        } else {
            retmap.put("backList", backList);
            retmap.put("status", "1");
        }
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

}
