package com.engine.kq.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.kq.cmd.shiftmanagement.SaveShiftManagementBaseFormCmd;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.auto.AutoInitWFSet;
import com.weaver.general.Util;

import java.util.*;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 初始化考勤的相关接口
 */
public class KQInitializationBiz {

    /**
     * 初始化班次
     * workTime：上班时间
     * restTime：下班时间
     * excludeRestTime：排除休息时间：0-不勾选、1-勾选
     * restTime_start:休息时间段的开始时间
     * restTime_end:休息时间段的结束时间
     * workDate：工作日期：6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     *
     * @return
     */
    public static Map<String, Object> initShift(Map<String, Object> params, User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        try {
            RecordSet rs = new RecordSet();

            String sql = "select * from kq_initHistory where id=1";
            RecordSet recordSet = new RecordSet();
            recordSet.executeQuery(sql);
            recordSet.next();
            int serialidDB = Util.getIntValue(recordSet.getString("serialid"), 0);
            KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();
            int[] initArrays = kqTimesArrayComInfo.getInitArr();
            String workTime = weaver.general.Util.null2String(params.get("workTime"));
            String restTime = weaver.general.Util.null2String(params.get("restTime"));
            boolean isRest = false;
            String excludeRestTime = weaver.general.Util.null2String(params.get("excludeRestTime"));
            String restTime_start = weaver.general.Util.null2String(params.get("restTime_start"));
            String restTime_end = weaver.general.Util.null2String(params.get("restTime_end"));

            String serail = workTime + "-" + restTime;
            int workTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(workTime);
            int restTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(restTime);
            Arrays.fill(initArrays, workTimeIndex, restTimeIndex, 1);
            if ("1".equalsIgnoreCase(excludeRestTime)) {
                isRest = true;
                int restTime_startIndex = kqTimesArrayComInfo.getArrayindexByTimes(restTime_start);
                int restTime_endIndex = kqTimesArrayComInfo.getArrayindexByTimes(restTime_end);
                Arrays.fill(initArrays, restTime_startIndex, restTime_endIndex, -1);
            }
            int worktime = kqTimesArrayComInfo.getCnt(initArrays, 0, 1440, 1);

            Map<String, Object> retmap = new HashMap<String, Object>();
            JSONArray workArray = new JSONArray();
            JSONObject workObjects = new JSONObject();
            JSONObject workObject = new JSONObject();

            JSONArray restArray = new JSONArray();
            JSONObject restObjects = new JSONObject();

            JSONObject jsonObj = new JSONObject();
            jsonObj.put("subcompanyid", "");
            jsonObj.put("serial", serail);
            jsonObj.put("shiftonoffworkcount", "1");
            jsonObj.put("punchsettings", "0");
            jsonObj.put("isoffdutyfreecheck", "0");
            jsonObj.put("isresttimeopen", isRest ? "1" : "0");
            jsonObj.put("worktime", worktime + "");
            //初始化工作时段
            workObject = new JSONObject();
            workObject.put("across", "0");
            workObject.put("times", workTime);
            workObject.put("mins", "60");
            workObjects.put("start", workObject);

            workObject = new JSONObject();
            workObject.put("across", "0");
            workObject.put("times", restTime);
            workObject.put("mins", "60");
            workObjects.put("end", workObject);

            workObjects.put("record", 0);
            workArray.add(workObjects);
            jsonObj.put("workSections", workArray);
            //初始化休息时段
            jsonObj.put("restTimeSections", restArray);
            if (isRest) {
                restObjects = new JSONObject();
                restObjects.put("resttype", "start");
                restObjects.put("time", restTime_start);
                restArray.add(restObjects);

                restObjects = new JSONObject();
                restObjects.put("resttype", "end");
                restObjects.put("time", restTime_end);
                restArray.add(restObjects);
            }

            SaveShiftManagementBaseFormCmd baseFormCmd = new SaveShiftManagementBaseFormCmd(null, user);
            if (serialidDB <= 0) {
                baseFormCmd.add(retmap, jsonObj);
                String status = weaver.general.Util.null2String(retmap.get("status"));
                String serialid = weaver.general.Util.null2String(retmap.get("id"));
                if ("1".equals(status) && serialid.length() > 0) {
                    resultMap.put("status", "1");
                    resultMap.put("sketch", SystemEnv.getHtmlLabelName(505652, user.getLanguage()));//班次初始化成功
                    List<String> descList = new ArrayList<String>();
                    resultMap.put("description", descList);
                    String updateHistory = "update kq_initHistory set serialid = ? ";
                    rs.executeUpdate(updateHistory, serialid);
                } else {
                    resultMap.put("status", "0");
                    resultMap.put("sketch", SystemEnv.getHtmlLabelName(125960, user.getLanguage()));
                    List<String> descList = new ArrayList<String>();
                    resultMap.put("description", descList);
                }
            } else {
                jsonObj.put("id", serialidDB);
                baseFormCmd.edit(retmap, jsonObj);
                resultMap.put("status", "1");
                resultMap.put("sketch", SystemEnv.getHtmlLabelName(505652, user.getLanguage()));//班次初始化成功
            }
            KQShiftManagementComInfo kqShiftManagementComInfo = new KQShiftManagementComInfo();
            kqShiftManagementComInfo.removeShiftManagementCache();
            resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/shift");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("status", "0");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(125960, user.getLanguage()));
            List<String> descList = new ArrayList<String>();
            resultMap.put("description", descList);
            return resultMap;

        }
    }

    /**
     * 初始化考勤组
     * workTime：上班时间
     * restTime：下班时间
     * excludeRestTime：排除休息时间：0-不勾选、1-勾选
     * restTime_start:休息时间段的开始时间
     * restTime_end:休息时间段的结束时间
     * workDate：工作日期：6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     *
     * @return
     */
    public static Map<String, Object> initGroup(Map<String, Object> params, User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", "1");
        resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000750,weaver.general.Util.getIntValue(user.getLanguage())));//考勤组初始化

        List<String> descList = new ArrayList<String>();
        KQGroupInit kqGroupInit = new KQGroupInit();
        boolean initGroupBase = kqGroupInit.initGroupBase();
        boolean initGroupMembers = kqGroupInit.initGroupMembers();

        String workDate = Util.null2String((String)params.get("workDate"));//工作日期
        boolean initGroupSerial = kqGroupInit.initGroupSerial(workDate);

        //考勤组基本信息
        descList.add(initGroupBase ? SystemEnv.getHtmlLabelName(10000751,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000752,weaver.general.Util.getIntValue(user.getLanguage())));
        //固定班次
        descList.add(initGroupSerial ? SystemEnv.getHtmlLabelName(10000753,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000754,weaver.general.Util.getIntValue(user.getLanguage())));
        //考勤组成员
        descList.add(initGroupMembers ? SystemEnv.getHtmlLabelName(10000755,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000756,weaver.general.Util.getIntValue(user.getLanguage())));

        if (initGroupBase && initGroupSerial && initGroupMembers) {
            new KQReportFieldComInfo().removeCache();
            new KQGroupComInfo().removeCache();
            new KQGroupMemberComInfo().removeCache();
            new KQShiftScheduleComInfo().removeCache();
            new KQFixedSchedulceComInfo().removeCache();
            resultMap.put("status", "1");
        } else {
            resultMap.put("status", "0");
        }
        resultMap.put("description", descList);
        resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/groupSetting");
        return resultMap;
    }

    /**
     * 初始化考勤流程
     * workTime：上班时间
     * restTime：下班时间
     * excludeRestTime：排除休息时间：0-不勾选、1-勾选
     * restTime_start:休息时间段的开始时间
     * restTime_end:休息时间段的结束时间
     * workDate：工作日期：6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     *
     * @return
     */
    public static Map<String, Object> initWorkflow(Map<String, Object> params, User user) {
        RecordSet rs = new RecordSet();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String sql = "select * from kq_initHistory where id=1";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        recordSet.next();

        int leavewf = Util.getIntValue(recordSet.getString("leavewf"), 0);
        int travelwf = Util.getIntValue(recordSet.getString("travelwf"), 0);
        int exitwf = Util.getIntValue(recordSet.getString("exitwf"), 0);
        int overtimewf = Util.getIntValue(recordSet.getString("overtimewf"), 0);
        int changeserialwf = Util.getIntValue(recordSet.getString("changeserialwf"), 0);
        int punchcardwf = Util.getIntValue(recordSet.getString("punchcardwf"), 0);
        int sellingoffwf = Util.getIntValue(recordSet.getString("sellingoffwf"), 0);
        int processchangewf = Util.getIntValue(recordSet.getString("processchangewf"), 0);

        resultMap.put("status", "1");
        resultMap.put("sketch", SystemEnv.getHtmlLabelName(505655,weaver.general.Util.getIntValue(user.getLanguage())));
        AutoInitWFSet autoInitWFSet = new AutoInitWFSet();
        autoInitWFSet.setUser(user);
        autoInitWFSet.setLog(true);

        boolean isLeaveOk = false;
        boolean isEvectionOk = false;
        boolean isOutOk = false;
        boolean isOvertimeOk = false;
        boolean isShiftOk = false;
        boolean isCardOk = false;
        boolean isLeaveBackOk = false;
        boolean isProcessChangeOk = false;

        String curdatetime = DateUtil.getFullDate();
        if (leavewf == 1) {
            isLeaveOk = true;
        } else {
            //请假流程
            isLeaveOk = autoInitWFSet.initWfForKQ(KqSplitFlowTypeEnum.LEAVE.getFlowtype(), SystemEnv.getHtmlLabelName(83393, user.getLanguage()) + curdatetime, "0");
        }

        if (travelwf == 1) {
            isEvectionOk = true;
        } else {
            //出差流程
            isEvectionOk = autoInitWFSet.initWfForKQ(KqSplitFlowTypeEnum.EVECTION.getFlowtype(), SystemEnv.getHtmlLabelName(83394, user.getLanguage()) + curdatetime, "0");
        }

        if (exitwf == 1) {
            isOutOk = true;
        } else {
            //公出流程
            isOutOk = autoInitWFSet.initWfForKQ(KqSplitFlowTypeEnum.OUT.getFlowtype(), SystemEnv.getHtmlLabelName(83395, user.getLanguage()) + curdatetime, "0");
        }

        if (overtimewf == 1) {
            isOvertimeOk = true;
        } else {
            //加班流程
            isOvertimeOk = autoInitWFSet.initWfForKQ(KqSplitFlowTypeEnum.OVERTIME.getFlowtype(), SystemEnv.getHtmlLabelName(83396, user.getLanguage()) + curdatetime, "0");
        }

        if (changeserialwf == 1) {
            isShiftOk = true;
        } else {
            //排班流程
            autoInitWFSet.setIsdetail(true);
            isShiftOk = autoInitWFSet.initWfForKQ(KqSplitFlowTypeEnum.SHIFT.getFlowtype(), SystemEnv.getHtmlLabelName(390737, user.getLanguage()) + curdatetime, "1");
        }

        if (punchcardwf == 1) {
            isCardOk = true;
        } else {
            //补卡流程
            autoInitWFSet.setIsdetail(true);
            isCardOk = autoInitWFSet.initWfForKQ(KqSplitFlowTypeEnum.CARD.getFlowtype(), SystemEnv.getHtmlLabelName(390274, user.getLanguage()) + curdatetime, "1");
        }

        if (sellingoffwf == 1) {
            isLeaveBackOk = true;
        } else {
            //销假流程
            autoInitWFSet.setIsdetail(true);
            isLeaveBackOk = autoInitWFSet.initWfForKQ(KqSplitFlowTypeEnum.LEAVEBACK.getFlowtype(), SystemEnv.getHtmlLabelName(389117, user.getLanguage()) + curdatetime, "1");
        }
        if (processchangewf == 1) {
          isProcessChangeOk = true;
        } else {
            //变更流程
          autoInitWFSet.setIsdetail(true);
          isProcessChangeOk = autoInitWFSet.initWfForKQ(KqSplitFlowTypeEnum.PROCESSCHANGE.getFlowtype(), SystemEnv.getHtmlLabelName(513400, user.getLanguage()) + curdatetime, "1");
        }

        List<String> descList = new ArrayList<String>();
        if (isLeaveOk) {
            String updateHistory = "update kq_initHistory set leavewf = ? ";
            rs.executeUpdate(updateHistory, "1");
            descList.add(SystemEnv.getHtmlLabelName(10000757,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
            autoInitWFSet.cleanAutoInit("0");
            descList.add(SystemEnv.getHtmlLabelName(505685,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        if (isEvectionOk) {
            String updateHistory = "update kq_initHistory set travelwf = ? ";
            rs.executeUpdate(updateHistory, "1");
            descList.add(SystemEnv.getHtmlLabelName(10000758,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
            autoInitWFSet.cleanAutoInit("1");
            descList.add(SystemEnv.getHtmlLabelName(505687,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        if (isOutOk) {
            String updateHistory = "update kq_initHistory set exitwf = ? ";
            rs.executeUpdate(updateHistory, "1");
            descList.add(SystemEnv.getHtmlLabelName(10000759,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
            autoInitWFSet.cleanAutoInit("2");
            descList.add(SystemEnv.getHtmlLabelName(505689,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        if (isOvertimeOk) {
            String updateHistory = "update kq_initHistory set overtimewf = ? ";
            rs.executeUpdate(updateHistory, "1");
            descList.add(SystemEnv.getHtmlLabelName(10000760,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
            autoInitWFSet.cleanAutoInit("3");
            descList.add(SystemEnv.getHtmlLabelName(505691,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        if (isShiftOk) {
            String updateHistory = "update kq_initHistory set changeserialwf = ? ";
            rs.executeUpdate(updateHistory, "1");
            descList.add(SystemEnv.getHtmlLabelName(10000761,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
            autoInitWFSet.cleanAutoInit("5");
            descList.add(SystemEnv.getHtmlLabelName(505693,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        if (isCardOk) {
            String updateHistory = "update kq_initHistory set punchcardwf = ? ";
            rs.executeUpdate(updateHistory, "1");
            descList.add(SystemEnv.getHtmlLabelName(10000762,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
            autoInitWFSet.cleanAutoInit("7");
            descList.add(SystemEnv.getHtmlLabelName(505695,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        if (isLeaveBackOk) {
            String updateHistory = "update kq_initHistory set sellingoffwf = ? ";
            rs.executeUpdate(updateHistory, "1");
            descList.add(SystemEnv.getHtmlLabelName(10000763,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
            autoInitWFSet.cleanAutoInit("6");
            descList.add(SystemEnv.getHtmlLabelName(505697,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        if (isProcessChangeOk) {
          String updateHistory = "update kq_initHistory set processchangewf = ? ";
          rs.executeUpdate(updateHistory, "1");
          descList.add(SystemEnv.getHtmlLabelName(10000764,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
          autoInitWFSet.cleanAutoInit("8");
          descList.add(SystemEnv.getHtmlLabelName(505697,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        if (isLeaveOk && isEvectionOk && isOutOk && isOvertimeOk && isShiftOk && isCardOk && isLeaveBackOk && isProcessChangeOk) {
            resultMap.put("status", "1");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(20196,weaver.general.Util.getIntValue(user.getLanguage())) + "," + SystemEnv.getHtmlLabelName(505658, user.getLanguage()));
        } else {
            resultMap.put("status", "0");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(125960, user.getLanguage()));
        }

        resultMap.put("description", descList);
        resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/wfn/statesShedule");
        return resultMap;
    }

    /**
     * 初始化节假日
     * workTime：上班时间
     * restTime：下班时间
     * excludeRestTime：排除休息时间：0-不勾选、1-勾选
     * restTime_start:休息时间段的开始时间
     * restTime_end:休息时间段的结束时间
     * workDate：工作日期：6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     *
     * @return
     */
    public static Map<String, Object> initHolidaySet(Map<String, Object> params,User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        Calendar today = Calendar.getInstance();
        String currentYear = Util.add0(today.get(Calendar.YEAR), 4);

        String sql = "select * from kq_initHistory where id=1";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        recordSet.next();
        int groupId = Util.getIntValue(recordSet.getString("groupId"), 0);

        /*如果初始化历史记录表中没有记录考勤组ID，则初始化失败*/
        if (groupId == 0) {
            resultMap.put("status", "0");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(505660, user.getLanguage()));
            resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/holidaySetting");
            return resultMap;
        }
        resultMap = KQHolidaySetBiz.initHolidaySet("" + groupId, currentYear,user);
        resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/holidaySetting/" + groupId);
        return resultMap;
    }

    /***
     * 初始化加班规则
     * workTime：上班时间
     * restTime：下班时间
     * excludeRestTime：排除休息时间：0-不勾选、1-勾选
     * restTime_start:休息时间段的开始时间
     * restTime_end:休息时间段的结束时间
     * workDate：工作日期：6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     * @return
     */
    public static Map<String, Object> initOvertimeRules(Map<String, Object> params,User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/overtimeRule");

        int excludeRestTime = Util.getIntValue((String) params.get("excludeRestTime"), 0);
        String restTime_start = Util.null2String((String) params.get("restTime_start"));
        String restTime_end = Util.null2String((String) params.get("restTime_end"));

        String sql = "select * from kq_initHistory where id=1";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        recordSet.next();
        int groupId = Util.getIntValue(recordSet.getString("groupId"), 0);
        int overtimeRuleId = Util.getIntValue(recordSet.getString("overtimeRuleId"), 0);

        /*如果初始化历史记录表中没有记录考勤组ID，则初始化失败*/
        if (groupId == 0) {
            resultMap.put("status", "0");
            resultMap.put("sketch",  SystemEnv.getHtmlLabelName(505659, user.getLanguage()));
            return resultMap;
        }

        /*判断加班规则是否初始化过*/
        boolean initialized = false;
        sql = "select * from kq_overtimeRules where (isDelete is null or isDelete <>1 ) and id=" + overtimeRuleId;
        recordSet.executeQuery(sql);
        initialized = recordSet.getCounts() > 0;

        /*没有初始化过就新建，已经初始化过就更新*/
        if (initialized) {
            sql = "update kq_overtimeRules set name='默认加班规则',groupIds=?,isDelete=0 where id=?";
            recordSet.executeUpdate(sql, groupId, overtimeRuleId);
        } else {
            sql = "insert into kq_OvertimeRules(name,groupIds,isDelete) values(?,?,0)";
            recordSet.executeUpdate(sql, "默认加班规则", groupId);

            String maxIdSql = "select max(id) from kq_OvertimeRules where (isDelete is null or isDelete <>1)";
            recordSet.executeQuery(maxIdSql);
            if (recordSet.next()) {
                overtimeRuleId = Util.getIntValue(recordSet.getString(1), 0);
            }
        }

        if (overtimeRuleId == 0) {
            resultMap.put("status", "1");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000765,weaver.general.Util.getIntValue(user.getLanguage())));
            return resultMap;
        }

        sql = "delete from kq_OvertimeRulesDetail where ruleId=?";
        boolean isok = recordSet.executeUpdate(sql, overtimeRuleId);
      new KQLog().info("kq_OvertimeRulesDetail:"+sql+":overtimeRuleId:"+overtimeRuleId+":isok:"+isok);
        sql = "insert into kq_OvertimeRulesDetail(ruleId,dayType,overtimeEnable,computingMode,startTime,minimumLen,paidLeaveEnable,lenOfOvertime,lenOfLeave,hasRestTime) " +
                "values(?,?,?,?,?,?,?,?,?,?)";
        boolean flag2 = recordSet.executeUpdate(sql, overtimeRuleId, 2, 1, 1, 30, 30, 1, 1, 1, 0);
        boolean flag3 = recordSet.executeUpdate(sql, overtimeRuleId, 3, 1, 1, 0, 30, 1, 1, 1, excludeRestTime);
        if (flag3 && excludeRestTime == 1) {
            String restTimeSql = "delete from kq_OvertimeRestTime where ruleId=? and dayType=?";
            recordSet.executeUpdate(restTimeSql, overtimeRuleId, 3);

            restTimeSql = "insert into kq_OvertimeRestTime(ruleId,dayType,startType,startTime,endType,endTime) values(?,?,?,?,?,?)";
            recordSet.executeUpdate(restTimeSql, overtimeRuleId, 3, 0, restTime_start, 0, restTime_end);
        }
        boolean flag1 = recordSet.executeUpdate(sql, overtimeRuleId, 1, 1, 1, 0, 30, 1, 1, 1, excludeRestTime);
        if (flag1 && excludeRestTime == 1) {
            String restTimeSql = "delete from kq_OvertimeRestTime where ruleId=? and dayType=?";
            recordSet.executeUpdate(restTimeSql, overtimeRuleId, 1);

            restTimeSql = "insert into kq_OvertimeRestTime(ruleId,dayType,startType,startTime,endType,endTime) values(?,?,?,?,?,?)";
            recordSet.executeUpdate(restTimeSql, overtimeRuleId, 1, 0, restTime_start, 0, restTime_end);
        }

        if (flag1 && flag2 && flag3) {
            resultMap.put("status", "1");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000766,weaver.general.Util.getIntValue(user.getLanguage())));
            resultMap.put("description", new String[]{SystemEnv.getHtmlLabelName(10000767,weaver.general.Util.getIntValue(user.getLanguage())), SystemEnv.getHtmlLabelName(10000768,weaver.general.Util.getIntValue(user.getLanguage())), SystemEnv.getHtmlLabelName(10000769,weaver.general.Util.getIntValue(user.getLanguage()))});
        }

        /*在初始化历史记录表中记录下加班规则的ID*/
        sql = "update kq_initHistory set overtimeRuleId=" + overtimeRuleId;
        recordSet.executeUpdate(sql);

        return resultMap;
    }

    /***
     * 初始化出差公出规则
     * workTime：上班时间
     * restTime：下班时间
     * excludeRestTime：排除休息时间：0-不勾选、1-勾选
     * restTime_start:休息时间段的开始时间
     * restTime_end:休息时间段的结束时间
     * workDate：工作日期：6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     * @return
     */
    public static Map<String, Object> initTravelRules(Map<String, Object> params,User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/travelRule");
        String sql = "update kq_travelRules set minimumUnit=1,proportion=8,computingMode=2,hoursToDay=8 where id=1";
        RecordSet recordSet = new RecordSet();
        boolean isTravelRuleOK = recordSet.executeUpdate(sql);//出差规则是否执行成功

        sql = "update kq_exitRules set minimumUnit=1,proportion=8,computingMode=2,hoursToDay=8 where id=1";
        boolean isExitRuleOk = recordSet.executeUpdate(sql);//公出规则是否执行成功

        if (isTravelRuleOK && isExitRuleOk) {
            resultMap.put("status", "1");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000770,weaver.general.Util.getIntValue(user.getLanguage())));
        } else {
            resultMap.put("status", "0");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000771,weaver.general.Util.getIntValue(user.getLanguage())));
        }

        /**
         * 刷新缓存
         */
        KQTravelRulesComInfo travelRulesComInfo = new KQTravelRulesComInfo();//出差规则缓存类
        KQExitRulesComInfo exitRulesComInfo = new KQExitRulesComInfo();//公出规则缓存类
        travelRulesComInfo.removeCache();
        exitRulesComInfo.removeCache();
        return resultMap;
    }

    /**
     * 初始化假期规则
     * workTime：上班时间
     * restTime：下班时间
     * excludeRestTime：排除休息时间：0-不勾选、1-勾选
     * restTime_start:休息时间段的开始时间
     * restTime_end:休息时间段的结束时间
     * workDate：工作日期：6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     *
     * @return
     */
    public static Map<String, Object> initLeaveRules(Map<String, Object> params,User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/holidayRule");
        /**
         * 根据考勤一键初始化记录表判断哪些假期规则已经初始化过了
         */
        String sql = "select * from kq_initHistory where id=1";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        recordSet.next();
        int annualLeave = Util.getIntValue(recordSet.getString("annualLeave"), 0);
        int paidCompassionateLeave = Util.getIntValue(recordSet.getString("paidCompassionateLeave"), 0);
        int paidSickLeave = Util.getIntValue(recordSet.getString("paidSickLeave"), 0);
        int vacationLeave = Util.getIntValue(recordSet.getString("vacationLeave"), 0);
        int compassionateLeave = Util.getIntValue(recordSet.getString("compassionateLeave"), 0);
        int sickLeave = Util.getIntValue(recordSet.getString("sickLeave"), 0);
        int maternityLeave = Util.getIntValue(recordSet.getString("maternityLeave"), 0);
        int paternityLeave = Util.getIntValue(recordSet.getString("paternityLeave"), 0);
        int marriageLeave = Util.getIntValue(recordSet.getString("marriageLeave"), 0);
        int funeralLeave = Util.getIntValue(recordSet.getString("funeralLeave"), 0);
        int breastfeedingLeave = Util.getIntValue(recordSet.getString("breastfeedingLeave"), 0);

        boolean isAnnualLeaveOK = KQLeaveRulesBiz.initAnnualLeave(annualLeave);
        boolean isPaidCompassionateLeaveOK = KQLeaveRulesBiz.initPaidCompassionateLeave(paidCompassionateLeave);
        boolean isPaidSickLeaveOK = KQLeaveRulesBiz.initPaidSickLeave(paidSickLeave);
        boolean isVacationLeaveOK = KQLeaveRulesBiz.initVacationLeave(vacationLeave);
        boolean isCompassionateLeaveOK = KQLeaveRulesBiz.initCompassionateLeave(compassionateLeave);
        boolean isSickLeaveOK = KQLeaveRulesBiz.initSickLeave(sickLeave);
        boolean isMaternityLeaveOK = KQLeaveRulesBiz.initMaternityLeave(maternityLeave);
        boolean isPaternityLeaveOK = KQLeaveRulesBiz.initPaternityLeave(paternityLeave);
        boolean isMarriageLeaveOK = KQLeaveRulesBiz.initMarriageLeave(marriageLeave);
        boolean isFuneralLeaveOK = KQLeaveRulesBiz.initFuneralLeave(funeralLeave);
        boolean isBreastfeedingLeaveOK = KQLeaveRulesBiz.initBreastfeedingLeave(breastfeedingLeave);

        List<String> descList = new ArrayList<String>();//详细描述
        descList.add(isAnnualLeaveOK ? SystemEnv.getHtmlLabelName(10000772,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000773,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isPaidCompassionateLeaveOK ? SystemEnv.getHtmlLabelName(10000774,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000775,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isPaidSickLeaveOK ? SystemEnv.getHtmlLabelName(10000776,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000777,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isVacationLeaveOK ? SystemEnv.getHtmlLabelName(10000778,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000779,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isCompassionateLeaveOK ? SystemEnv.getHtmlLabelName(10000780,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000781,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isSickLeaveOK ? SystemEnv.getHtmlLabelName(10000782,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000783,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isMaternityLeaveOK ? SystemEnv.getHtmlLabelName(10000784,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000785,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isPaternityLeaveOK ? SystemEnv.getHtmlLabelName(10000786,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000787,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isMarriageLeaveOK ? SystemEnv.getHtmlLabelName(10000788,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000789,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isFuneralLeaveOK ? SystemEnv.getHtmlLabelName(10000790,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000791,weaver.general.Util.getIntValue(user.getLanguage())));
        descList.add(isBreastfeedingLeaveOK ? SystemEnv.getHtmlLabelName(10000792,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000793,weaver.general.Util.getIntValue(user.getLanguage())));

        if (isAnnualLeaveOK && isPaidCompassionateLeaveOK && isPaidSickLeaveOK && isVacationLeaveOK && isCompassionateLeaveOK
                && isSickLeaveOK && isMaternityLeaveOK && isPaternityLeaveOK && isMarriageLeaveOK && isFuneralLeaveOK && isBreastfeedingLeaveOK) {
            resultMap.put("status", "1");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000794,weaver.general.Util.getIntValue(user.getLanguage())));
        } else {
            resultMap.put("status", "0");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000795,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        resultMap.put("description", descList);

        /**
         * 刷新缓存
         */
        KQLeaveRulesComInfo leaveRulesComInfo = new KQLeaveRulesComInfo();
        leaveRulesComInfo.removeCache();
        KQLeaveRulesDetailComInfo detailComInfo = new KQLeaveRulesDetailComInfo();
        detailComInfo.removeCache();

        return resultMap;
    }

    /**
     * 初始化假期假期余额成功
     * workTime：上班时间
     * restTime：下班时间
     * excludeRestTime：排除休息时间：0-不勾选、1-勾选
     * restTime_start:休息时间段的开始时间
     * restTime_end:休息时间段的结束时间
     * workDate：工作日期：6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     *
     * @return
     */
    public static Map<String, Object> initBalanceOfLeave(Map<String, Object> params,User user) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("url", "/spa/hrm/staticAttendance4engine/engine.html#/attendance/vacationBalance");
        /**
         * 根据考勤一键初始化记录表判断哪些假期规则已经初始化过了
         */
        String sql = "select * from kq_initHistory where id=1";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        recordSet.next();
        int annualLeave = Util.getIntValue(recordSet.getString("annualLeave"), 0);

        String date = DateUtil.getCurrentDate();

        boolean isAnnualLeaveOK = annualLeave == 0 ? false : KQBalanceOfLeaveBiz.createData("" + annualLeave, date.substring(0, 4), 0, "", "" + user.getUID());

        List<String> descList = new ArrayList<String>();//详细描述
        descList.add(isAnnualLeaveOK ? SystemEnv.getHtmlLabelName(10000796,weaver.general.Util.getIntValue(user.getLanguage())) : SystemEnv.getHtmlLabelName(10000796,weaver.general.Util.getIntValue(user.getLanguage())));

        if (isAnnualLeaveOK) {
            resultMap.put("status", "1");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000797,weaver.general.Util.getIntValue(user.getLanguage())));
        } else {
            resultMap.put("status", "0");
            resultMap.put("sketch", SystemEnv.getHtmlLabelName(10000798,weaver.general.Util.getIntValue(user.getLanguage())));
        }
        resultMap.put("description", descList);
        return resultMap;
    }
}
