package com.engine.kq.cmd.overtimerules;

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

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQGroupComInfo;
import com.engine.kq.biz.KQShiftManagementComInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import weaver.attendance.service.OvertimeRuleComputingModeService;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.filter.XssUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 加班规则--获取新建或编辑的表单
 */
public class GetOvertimeRulesFormCmd extends AbstractCommonCommand<Map<String, Object>> {

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String ruleId = Util.null2String(params.get("ruleId"));//加班规则的ID
        boolean canAdd = HrmUserVarify.checkUserRight("KQOvertimeRulesAdd:Add", user);//是否具有新建权限
        boolean canEdit = HrmUserVarify.checkUserRight("KQOvertimeRulesEdit:Edit", user);//是否具有编辑的权限
        if (!canAdd && !canEdit) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
            return resultMap;
        }
        boolean isEdit = false;
        /*加班规则名称*/
        String name = "";
        /*考勤组的ID*/
        String groupIds = "";

      /**
       * 从这开始，下面的变量都是之前的写法，现在改成动态参数的，下面的这些参数就不用了
       */
      /*工作日是否允许加班：0-不允许、1-允许*/
        int overtimeEnable2 = 1;
        /*工作日的加班方式：1-需审批，以加班流程为准、2-需审批，以打卡为准，但是不能超过加班流程时长、3-无需审批，根据打卡时间计算加班时长*/
        int computingMode2 = 1;
        /*工作日下班多少分钟后开始计算加班*/
        int startTime2 = 30;
        /*工作日上班前多少分钟后开始计算加班*/
        int before_startTime2 = 0;
        /*工作日最小加班时长*/
        int minimumLen2 = 30;
        /*工作日是否允许加班转调休*/
        int paidLeaveEnable2 = 0;
        /*工作日加班转调休比例中的加班时长*/
        double lenOfOvertime2 = 1;
        /*工作日加班转调休比例中的调休时长*/
        double lenOfLeave2 = 1;

        /*工作日加班补偿方式 1-默认加班补偿规则、2-根据员工在加班流程上选择的加班补偿类型进行补偿两种*/
        int paidLeaveEnableType2 = 0;
        /*工作日默认加班补偿方式 里转调休的方式: 1-按加班时长比例转调休时长、2-按加班时长范围设置转调休时长、3-按加班的时间段设置转调休时长*/
        int paidLeaveEnableDefaultType2 = 1;
        /*工作日 根据员工在加班流程上选择的加班补偿类型进行补偿 里转调休的方式: 1-按加班时长比例转调休时长、2-按加班时长范围设置转调休时长、3-按加班的时间段设置转调休时长*/
        int paidLeaveEnableFlowType2 = 1;

        //     按加班时长范围设置转调休时长: 加班时长 ≥* 分钟，转调休时长*分钟
        List<Map<String,String>> balanceLengthDetailLists2 = Lists.newArrayList();
        //      加班时间在19:00后，比例转调休时长 按照1:1
        List<Map<String,String>> balanceTimeDetailLists2 = Lists.newArrayList();
        /*工作日是否有休息时间*/
        int hasRestTime2 = 0;
        /*排除休息时间两种方式 :1、设置休息时间 2、根据加班时长扣除 */
        int restType2=1;
//     工作日 是否开启 自定义跨天加班归属分界点
      int has_cut_point2 = 0;
//     开启 自定义跨天加班归属分界点
      String cut_point2 = "00:00";


        /*休息日是否允许加班：0-不允许、1-允许*/
        int overtimeEnable3 = 1;
        /*休息日的加班方式：1-需审批，以加班流程为准、2-需审批，以打卡为准，但是不能超过加班流程时长、3-无需审批，根据打卡时间计算加班时长*/
        int computingMode3 = 1;
        /*休息日的最小加班时长*/
        int minimumLen3 = 30;
        /*休息日是否允许加班转调休*/
        int paidLeaveEnable3 = 0;
        /*休息日加班转调休比例中的加班时长*/
      double lenOfOvertime3 = 1;
        /*休息日加班转调休比例中的调休时长*/
      double lenOfLeave3 = 1;
        /*休息日是否有休息时间*/
        int hasRestTime3 = 0;

        /*休息日加班补偿方式 1-默认加班补偿规则、2-根据员工在加班流程上选择的加班补偿类型进行补偿两种*/
        int paidLeaveEnableType3 = 0;
        /*休息日默认加班补偿方式 里转调休的方式: 1-按加班时长比例转调休时长、2-按加班时长范围设置转调休时长、3-按加班的时间段设置转调休时长*/
        int paidLeaveEnableDefaultType3 = 1;
        /*休息日 根据员工在加班流程上选择的加班补偿类型进行补偿 里转调休的方式: 1-按加班时长比例转调休时长、2-按加班时长范围设置转调休时长、3-按加班的时间段设置转调休时长*/
        int paidLeaveEnableFlowType3 = 1;

        //     按加班时长范围设置转调休时长: 加班时长 ≥* 分钟，转调休时长*分钟
        List<Map<String,String>> balanceLengthDetailLists3 = Lists.newArrayList();
        //      加班时间在19:00后，比例转调休时长 按照1:1
        List<Map<String,String>> balanceTimeDetailLists3 = Lists.newArrayList();
        /*排除休息时间两种方式 :1、设置休息时间 2、根据加班时长扣除 */
        int restType3=1;

        /*节假日是否允许加班：0-不允许、1-允许*/
        int overtimeEnable1 = 1;
        /*节假日的加班方式：1-需审批，以加班流程为准、2-需审批，以打卡为准，但是不能超过加班流程时长、3-无需审批，根据打卡时间计算加班时长*/
        int computingMode1 = 1;
        /*节假日的最小加班时长*/
        int minimumLen1 = 30;
        /*节假日是否允许加班转调休*/
        int paidLeaveEnable1 = 0;
        /*节假日加班转调休比例中的加班时长*/
      double lenOfOvertime1 = 1;
        /*假节日加班转调休比例中的调休时长*/
      double lenOfLeave1 = 1;
        /*节假日是否有休息时间*/
        int hasRestTime1 = 0;

        /*节假日加班补偿方式 1-默认加班补偿规则、2-根据员工在加班流程上选择的加班补偿类型进行补偿两种*/
        int paidLeaveEnableType1 = 0;
        /*节假日默认加班补偿方式 里转调休的方式: 1-按加班时长比例转调休时长、2-按加班时长范围设置转调休时长、3-按加班的时间段设置转调休时长*/
        int paidLeaveEnableDefaultType1 = 1;
        /*节假日 根据员工在加班流程上选择的加班补偿类型进行补偿 里转调休的方式: 1-按加班时长比例转调休时长、2-按加班时长范围设置转调休时长、3-按加班的时间段设置转调休时长*/
        int paidLeaveEnableFlowType1 = 1;

        //     按加班时长范围设置转调休时长: 加班时长 ≥* 分钟，转调休时长*分钟
        List<Map<String,String>> balanceLengthDetailLists1 = Lists.newArrayList();
        //      加班时间在19:00后，比例转调休时长 按照1:1
        List<Map<String,String>> balanceTimeDetailLists1 = Lists.newArrayList();
        /*排除节假时间两种方式 :1、设置休息时间 2、根据加班时长扣除 */
        int restType1=1;
      /**
       * 到这里结束，这块区间的变量就用不到了，就起到一个文字说明的意义
       */

      //顺序显示，2是工作日，3是休息日，1是节假日
        int[] changetype = {2,3,1};

        Map<String,String> valueMap = Maps.newHashMap();
        if (!ruleId.equals("")) {
            String sql = "select t1.name,t1.shiftIds as groupids,t2.* from kq_OvertimeRules t1 left join kq_OvertimeRulesDetail t2 on t1.id=t2.ruleId where (t1.isDelete is null or t1.isDelete!=1) and t1.id=" + ruleId;
            RecordSet recordSet = new RecordSet();
            recordSet.executeQuery(sql);
            int dayType = 0;//1-工作日、2-休息日、3-节假日
            while (recordSet.next()) {
                isEdit = true;
                name = recordSet.getString("name");
                groupIds = recordSet.getString("groupIds");
                dayType = recordSet.getInt("dayType");
                int detailid = recordSet.getInt("id");
                int overtimeEnable = recordSet.getInt("overtimeEnable");
                int computingMode = recordSet.getInt("computingMode");
                int startTime = recordSet.getInt("startTime");
                int before_startTime = recordSet.getInt("before_startTime");
                int minimumLen = recordSet.getInt("minimumLen");
                int paidLeaveEnable = recordSet.getInt("paidLeaveEnable");
                double lenOfOvertime = recordSet.getDouble("lenOfOvertime");
                double lenOfLeave = recordSet.getDouble("lenOfLeave");
                int hasRestTime = recordSet.getInt("hasRestTime");
                int has_cut_point = recordSet.getInt("has_cut_point");
                String cut_point = recordSet.getString("cut_point");
                if("-1".equalsIgnoreCase(cut_point)){
                  cut_point = "00:00";
                }
                if(has_cut_point < 0){
                  has_cut_point = 0;
                }
                if(has_cut_point == 0){
                  cut_point = "00:00";
                }
                if(before_startTime < 0){
                  before_startTime = 0;
                }
                int paidLeaveEnableType = recordSet.getInt("paidLeaveEnableType");
                int paidLeaveEnableDefaultType = recordSet.getInt("paidLeaveEnableDefaultType");
                int paidLeaveEnableFlowType = recordSet.getInt("paidLeaveEnableFlowType");
                int restTimeType = recordSet.getInt("restTimeType");
                if(paidLeaveEnableType <= 0){
                  paidLeaveEnableType = 1;
                }
                if(paidLeaveEnableDefaultType <= 0){
                  paidLeaveEnableDefaultType = 1;
                }
                if(paidLeaveEnableFlowType <= 0){
                  paidLeaveEnableFlowType = 1;
                }
                if(paidLeaveEnableDefaultType == 1 && paidLeaveEnable == 0){
                  //如果是第一种生成调休方式，且没有开启关联调休，那么默认的加班转调休方式就是第一种
                  paidLeaveEnableDefaultType = 1;
                }
                if(restTimeType <= 0){
                  restTimeType = 1;
                }

                valueMap.put("ID_"+dayType, detailid+"");

                valueMap.put("overtimeEnable"+dayType, overtimeEnable+"");
                valueMap.put("computingMode"+dayType, computingMode+"");
                valueMap.put("startTime"+dayType, startTime+"");
                valueMap.put("before_startTime"+dayType, before_startTime+"");
                valueMap.put("minimumLen"+dayType, minimumLen+"");
                valueMap.put("paidLeaveEnable"+dayType, paidLeaveEnable+"");
                valueMap.put("lenOfOvertime"+dayType, lenOfOvertime+"");
                valueMap.put("lenOfLeave"+dayType, lenOfLeave+"");
                valueMap.put("hasRestTime"+dayType, hasRestTime+"");
                valueMap.put("restTimeType"+dayType, restTimeType+"");
                valueMap.put("has_cut_point"+dayType, has_cut_point+"");
                valueMap.put("cut_point"+dayType, cut_point+"");
                valueMap.put("paidLeaveEnableType"+dayType, paidLeaveEnableType+"");
                valueMap.put("paidLeaveEnableDefaultType"+dayType, paidLeaveEnableDefaultType+"");
                valueMap.put("paidLeaveEnableFlowType"+dayType, paidLeaveEnableFlowType+"");
            }

          for(int i = 0 ; i < changetype.length ; i++) {
            int changetype_index = changetype[i];
            String hasRestTime_index = "hasRestTime"+changetype_index;
            String paidLeaveEnable_index = "paidLeaveEnable"+changetype_index;
            String paidLeaveEnableType_index = "paidLeaveEnableType"+changetype_index;
            String paidLeaveEnableDefaultType_index = "paidLeaveEnableDefaultType"+changetype_index;
            String paidLeaveEnableFlowType_index = "paidLeaveEnableFlowType"+changetype_index;
            if(valueMap.containsKey(hasRestTime_index)){
              String hasRestTime = Util.null2String(valueMap.get(hasRestTime_index));
              if("1".equalsIgnoreCase(hasRestTime)){
                String restTimeType_index = "restTimeType"+changetype_index;
                if(valueMap.containsKey(restTimeType_index)){
                  String restTimeType = Util.null2String(valueMap.get(restTimeType_index));
                  if("1".equalsIgnoreCase(restTimeType)){
//                    排除休息时间
                    List restTimeList = new ArrayList();
                    sql = "select * from kq_OvertimeRestTime where ruleId=? and dayType=? order by dayType,id";
                    recordSet.executeQuery(sql, ruleId,changetype_index);
                    while (recordSet.next()) {
                      String startType = recordSet.getString("startType");
                      String startTime = recordSet.getString("startTime");
                      String endType = recordSet.getString("endType");
                      String endTime = recordSet.getString("endTime");

                      Map<String, Object> restTimeMap = new HashMap<String, Object>();
                      restTimeMap.put("startType", startType);
                      restTimeMap.put("startTime", startTime);
                      restTimeMap.put("endType", endType);
                      restTimeMap.put("endTime", endTime);
                      restTimeList.add(restTimeMap);
                    }
                    if(!restTimeList.isEmpty()){
                      resultMap.put("restTimeRule"+changetype_index, restTimeList);
                    }
                  }else if("2".equalsIgnoreCase(restTimeType)){
//                    排除休息时长
                    List restTimeList = new ArrayList();
                    sql = "select * from kq_OvertimeRestlength where ruleId=? and dayType=? order by dayType,dsporder";
                    recordSet.executeQuery(sql, ruleId,changetype_index);
                    while (recordSet.next()) {
                      String overlength = recordSet.getString("overlength");
                      String cutlength = recordSet.getString("cutlength");

                      Map<String, Object> restTimeMap = new HashMap<String, Object>();
                      restTimeMap.put("overtime", overlength);
                      restTimeMap.put("exclude", cutlength);
                      restTimeList.add(restTimeMap);
                    }
                    if(!restTimeList.isEmpty()){
                      resultMap.put("restTimeRule"+changetype_index, restTimeList);
                    }
                  }
                }
              }
            }
            if(valueMap.containsKey(paidLeaveEnableType_index)){
              String ID_index = "ID_"+changetype_index;
              if(valueMap.containsKey(ID_index)){
                String detailid = Util.null2String(valueMap.get(ID_index));
                String paidLeaveEnableType = Util.null2String(valueMap.get(paidLeaveEnableType_index));
                String paidLeaveEnableDefaultType = Util.null2String(valueMap.get(paidLeaveEnableDefaultType_index));
                String paidLeaveEnableFlowType = Util.null2String(valueMap.get(paidLeaveEnableFlowType_index));
                if("1".equalsIgnoreCase(paidLeaveEnableType)){
                  if(valueMap.containsKey(paidLeaveEnable_index)) {
                    String paidLeaveEnable = Util.null2String(valueMap.get(paidLeaveEnable_index));
                    if("1".equalsIgnoreCase(paidLeaveEnable)){
                      if("2".equalsIgnoreCase(paidLeaveEnableDefaultType)){
                        set_lengthDetailList(detailid,changetype_index,resultMap);
                      }else if("3".equalsIgnoreCase(paidLeaveEnableDefaultType)){
                        set_TimeDetailList(detailid,changetype_index,resultMap);
                      }
                    }
                  }
                }else if("2".equalsIgnoreCase(paidLeaveEnableType)){
                  if("2".equalsIgnoreCase(paidLeaveEnableFlowType)){
                    set_lengthDetailList(detailid,changetype_index,resultMap);
                  }else if("3".equalsIgnoreCase(paidLeaveEnableFlowType)){
                    set_TimeDetailList(detailid,changetype_index,resultMap);
                  }
                }
              }
            }

          }
        }

        List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>();
        Map<String, Object> groupItem = new HashMap<String, Object>();
        List<Object> itemList = new ArrayList<Object>();
        HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
        SearchConditionItem searchConditionItem = null;
        HrmFieldBean hrmFieldBean = null;

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("name");//加班规则名称
        hrmFieldBean.setFieldlabel("388831");
        hrmFieldBean.setFieldhtmltype("1");
        hrmFieldBean.setType("1");
        hrmFieldBean.setViewAttr(3);
        hrmFieldBean.setFieldvalue(name);
        hrmFieldBean.setIsFormField(true);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        searchConditionItem.setRules("required|string");
        itemList.add(searchConditionItem);

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("groupIds");//应用范围--考勤组
        hrmFieldBean.setFieldlabel("389098");
        hrmFieldBean.setFieldhtmltype("3");
        hrmFieldBean.setType("mkqshift");
        hrmFieldBean.setFieldvalue(groupIds);
        hrmFieldBean.setIsFormField(true);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        KQShiftManagementComInfo kqGroupComInfo = new KQShiftManagementComInfo();
        List<Map<String, Object>> replaceDatas = new ArrayList<Map<String, Object>>();
        Map<String, Object> datas = new HashMap<String, Object>();
        String[] groupIdArr = groupIds.split(",");
        for (String groupId : groupIdArr) {
            datas = new HashMap<String, Object>();
            datas.put("id", groupId);
            datas.put("name", kqGroupComInfo.getSerial(groupId));
            replaceDatas.add(datas);
        }
        searchConditionItem.getBrowserConditionParam().setReplaceDatas(replaceDatas);
        /*一个考勤组只能关联一个加班规则，所以新建或者编辑的时候需要对考勤组浏览按钮内的数据进行过滤*/
        Map<String,Object> dataMap = new HashMap<String,Object>();
        Map<String,Object> completeMap = new HashMap<String,Object>();
        String kqGroupIdStr = "";
        String sql = "select * from kq_OvertimeRules where (isDelete is null or isDelete !=1) ";
        if (isEdit) {
            sql += " and id !=" + ruleId;
        }
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        while (recordSet.next()) {
            String kqGroupIds = recordSet.getString("shiftIds");
            if (kqGroupIds.equals("")) {
                continue;
            }
            kqGroupIdStr += "," + kqGroupIds;
        }
        XssUtil xssUtil = new XssUtil();
        if(kqGroupIdStr.length() > 0) {
        	dataMap.put("sqlWhere", xssUtil.put(" id not in (" + kqGroupIdStr.substring(1) + ")"));
            completeMap.put("sqlWhere", xssUtil.put(" id not in (" + kqGroupIdStr.substring(1) + ")"));
        }
        searchConditionItem.getBrowserConditionParam().setDataParams(dataMap);
        searchConditionItem.getBrowserConditionParam().setCompleteParams(completeMap);
        itemList.add(searchConditionItem);

        groupItem.put("title", SystemEnv.getHtmlLabelName(1361, user.getLanguage()));
        groupItem.put("defaultshow", true);
        groupItem.put("items", itemList);
        groupList.add(groupItem);

        for(int i = 0 ; i < changetype.length ; i++){
          int changetype_index = changetype[i];
          groupItem = new HashMap<String, Object>();
          itemList = new ArrayList<Object>();
          String overtimeEnable_key = "overtimeEnable"+changetype_index;
          String overtimeEnable_value = isEdit?Util.null2String(valueMap.get(overtimeEnable_key),"1"):"1";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(overtimeEnable_key);//是否允许加班
          hrmFieldBean.setFieldlabel("388833");
          hrmFieldBean.setFieldhtmltype("4");
          hrmFieldBean.setType("1");
          hrmFieldBean.setFieldvalue(overtimeEnable_value);
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          Map<String, Object> otherParamsMap = new HashMap<String, Object>();
          otherParamsMap.put("display", "switch");
          searchConditionItem.setOtherParams(otherParamsMap);
          itemList.add(searchConditionItem);

          String computingMode_key = "computingMode"+changetype_index;
          String computingMode_value = isEdit?Util.null2String(valueMap.get(computingMode_key),"1"):"1";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(computingMode_key);//加班方式
          hrmFieldBean.setFieldlabel("388834");
          hrmFieldBean.setFieldhtmltype("5");
          hrmFieldBean.setType("1");
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          List<SearchConditionOption> statusOptions = new ArrayList<SearchConditionOption>();
          statusOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(500382, user.getLanguage()),"1".equalsIgnoreCase(computingMode_value)));
          statusOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(500383, user.getLanguage()),"2".equalsIgnoreCase(computingMode_value)));
          statusOptions.add(new SearchConditionOption("4", SystemEnv.getHtmlLabelName(524827, user.getLanguage()),"4".equalsIgnoreCase(computingMode_value)));
          Map<Integer, String> cusModeMap = new OvertimeRuleComputingModeService().getNewComputingMode(user.getLanguage());
          for(Integer key : cusModeMap.keySet()) {
    		  statusOptions.add(new SearchConditionOption(String.valueOf(key), StringUtil.vString(cusModeMap.get(key)),String.valueOf(key).equalsIgnoreCase(computingMode_value)));
    	  }
          statusOptions.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(390837, user.getLanguage()),"3".equalsIgnoreCase(computingMode_value)));
          searchConditionItem.setOptions(statusOptions);
          searchConditionItem.setDetailtype(3);
          itemList.add(searchConditionItem);

          if(changetype_index == 2){
            String startTime_key = "startTime"+changetype_index;
            String startTime_value = isEdit?Util.null2String(valueMap.get(startTime_key),"30"):"30";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(startTime_key);//工作日下班多少分钟后开始计算加班
            hrmFieldBean.setFieldlabel("388842");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setViewAttr(3);
            hrmFieldBean.setFieldvalue(startTime_value);
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|integer");
            searchConditionItem.setMin("0");
            itemList.add(searchConditionItem);

            String before_startTime_key = "before_startTime"+changetype_index;
            String before_startTime_value = isEdit?Util.null2String(valueMap.get(before_startTime_key),"0"):"0";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(before_startTime_key);//工作日上班前多少分钟后开始计算加班
            hrmFieldBean.setFieldlabel("388842");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setViewAttr(3);
            hrmFieldBean.setFieldvalue(before_startTime_value);
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|integer");
            searchConditionItem.setMin("0");
            itemList.add(searchConditionItem);
          }

          String minimumLen_key = "minimumLen"+changetype_index;
          String minimumLen_value = isEdit?Util.null2String(valueMap.get(minimumLen_key),"30"):"30";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(minimumLen_key);//最小加班时长
          hrmFieldBean.setFieldlabel("388843");
          hrmFieldBean.setFieldhtmltype("1");
          hrmFieldBean.setType("2");
          hrmFieldBean.setViewAttr(3);
          hrmFieldBean.setFieldvalue(minimumLen_value);
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          searchConditionItem.setRules("required|integer");
          searchConditionItem.setMin("1");
          itemList.add(searchConditionItem);

          String paidLeaveEnableType_key = "paidLeaveEnableType"+changetype_index;
          String paidLeaveEnableType_value = isEdit?Util.null2String(valueMap.get(paidLeaveEnableType_key),"1"):"1";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(paidLeaveEnableType_key);//加班补偿方式
          hrmFieldBean.setFieldlabel("388838");
          hrmFieldBean.setFieldhtmltype("5");
          hrmFieldBean.setType("1");
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          List<SearchConditionOption> paidLeaveEnableOptions = new ArrayList<SearchConditionOption>();
          paidLeaveEnableOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(524828, user.getLanguage()),"1".equalsIgnoreCase(paidLeaveEnableType_value)));
          paidLeaveEnableOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(524833, user.getLanguage()),"2".equalsIgnoreCase(paidLeaveEnableType_value)));
          searchConditionItem.setOptions(paidLeaveEnableOptions);
          searchConditionItem.setDetailtype(3);
          itemList.add(searchConditionItem);

          String paidLeaveEnableDefaultType_key = "paidLeaveEnableDefaultType"+changetype_index;
          String paidLeaveEnableDefaultType_value = isEdit?Util.null2String(valueMap.get(paidLeaveEnableDefaultType_key),"1"):"1";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(paidLeaveEnableDefaultType_key);//默认加班补偿方式 里转调休的方式
          hrmFieldBean.setFieldlabel("524828");
          hrmFieldBean.setFieldhtmltype("5");
          hrmFieldBean.setType("2");
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          List<SearchConditionOption> paidLeaveDefaultTypeOptions = new ArrayList<SearchConditionOption>();
          paidLeaveDefaultTypeOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(524830, user.getLanguage()),"1".equalsIgnoreCase(paidLeaveEnableDefaultType_value)));
          paidLeaveDefaultTypeOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(524831, user.getLanguage()),"2".equalsIgnoreCase(paidLeaveEnableDefaultType_value)));
//          这个方式只是临时屏蔽，后面可能还是会启用，不要占用这个key是3的
//          paidLeaveDefaultTypeOptions.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(524832, user.getLanguage()),"3".equalsIgnoreCase(paidLeaveEnableDefaultType_value)));
          searchConditionItem.setOptions(paidLeaveDefaultTypeOptions);
          searchConditionItem.setDetailtype(1);
          itemList.add(searchConditionItem);

          String paidLeaveEnableFlowType_key = "paidLeaveEnableFlowType"+changetype_index;
          String paidLeaveEnableFlowType_value = isEdit?Util.null2String(valueMap.get(paidLeaveEnableFlowType_key),"1"):"1";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(paidLeaveEnableFlowType_key);//根据员工在加班流程上选择的加班补偿类型进行补偿
          hrmFieldBean.setFieldlabel("524833");
          hrmFieldBean.setFieldhtmltype("5");
          hrmFieldBean.setType("2");
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          List<SearchConditionOption> paidLeaveFlowTypeOptions = new ArrayList<SearchConditionOption>();
          paidLeaveFlowTypeOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(524830, user.getLanguage()),"1".equalsIgnoreCase(paidLeaveEnableFlowType_value)));
          paidLeaveFlowTypeOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(524831, user.getLanguage()),"2".equalsIgnoreCase(paidLeaveEnableFlowType_value)));
//          这个方式只是临时屏蔽，后面可能还是会启用，不要占用这个key是3的
//          paidLeaveFlowTypeOptions.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(524832, user.getLanguage()),"3".equalsIgnoreCase(paidLeaveEnableFlowType_value)));
          searchConditionItem.setOptions(paidLeaveFlowTypeOptions);
          searchConditionItem.setDetailtype(1);
          itemList.add(searchConditionItem);

          String paidLeaveEnable_key = "paidLeaveEnable"+changetype_index;
          String paidLeaveEnable_value = isEdit?Util.null2String(valueMap.get(paidLeaveEnable_key),"0"):"0";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(paidLeaveEnable_key);//是否允许加班转调休
          hrmFieldBean.setFieldlabel("388838");
          hrmFieldBean.setFieldhtmltype("4");
          hrmFieldBean.setType("1");
          hrmFieldBean.setFieldvalue(paidLeaveEnable_value);
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          itemList.add(searchConditionItem);

          String lenOfOvertime_key = "lenOfOvertime"+changetype_index;
          String lenOfOvertime_value = isEdit?Util.null2String(valueMap.get(lenOfOvertime_key),"1"):"1";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(lenOfOvertime_key);//加班转调休比例中的加班时长
          hrmFieldBean.setFieldlabel("389222");
          hrmFieldBean.setFieldhtmltype("1");
          hrmFieldBean.setType("2");
          hrmFieldBean.setViewAttr(3);
          hrmFieldBean.setFieldvalue(lenOfOvertime_value);
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          searchConditionItem.setRules("required|numeric");
          searchConditionItem.setStep("0.01");
          searchConditionItem.setPrecision(2);
          itemList.add(searchConditionItem);

          String lenOfLeave_key = "lenOfLeave"+changetype_index;
          String lenOfLeave_value = isEdit?Util.null2String(valueMap.get(lenOfLeave_key),"1"):"1";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(lenOfLeave_key);//加班转调休比例中的调休时长
          hrmFieldBean.setFieldlabel("389223");
          hrmFieldBean.setFieldhtmltype("1");
          hrmFieldBean.setType("2");
          hrmFieldBean.setViewAttr(3);
          hrmFieldBean.setFieldvalue(lenOfLeave_value);
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          searchConditionItem.setRules("required|numeric");
          searchConditionItem.setStep("0.01");
          searchConditionItem.setPrecision(2);
          itemList.add(searchConditionItem);

          String hasRestTime_key = "hasRestTime"+changetype_index;
          String hasRestTime_value = isEdit?Util.null2String(valueMap.get(hasRestTime_key),"0"):"0";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(hasRestTime_key);//排除休息时间
          hrmFieldBean.setFieldlabel("388565");
          hrmFieldBean.setFieldhtmltype("4");
          hrmFieldBean.setType("1");
          hrmFieldBean.setFieldvalue(hasRestTime_value);
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          otherParamsMap = new HashMap<String, Object>();
          otherParamsMap.put("display", "switch");
          searchConditionItem.setOtherParams(otherParamsMap);
          itemList.add(searchConditionItem);

          String restTimeType_key = "restTimeType"+changetype_index;
          String restTimeType_value = isEdit?Util.null2String(valueMap.get(restTimeType_key),"1"):"1";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(restTimeType_key);//排除休息时间类型
          hrmFieldBean.setFieldlabel("388565");
          hrmFieldBean.setFieldhtmltype("5");
          hrmFieldBean.setType("1");
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          List<SearchConditionOption> restTimeTypeOptions = new ArrayList<SearchConditionOption>();
          restTimeTypeOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(525905, user.getLanguage()),"1".equalsIgnoreCase(restTimeType_value)));
          restTimeTypeOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(524834, user.getLanguage()),"2".equalsIgnoreCase(restTimeType_value)));
          searchConditionItem.setOptions(restTimeTypeOptions);
          searchConditionItem.setDetailtype(3);
          itemList.add(searchConditionItem);

          String has_cut_point_key = "has_cut_point"+changetype_index;
          String has_cut_point_value = isEdit?Util.null2String(valueMap.get(has_cut_point_key),"0"):"0";
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(has_cut_point_key);//	是否开启 自定义跨天加班归属分界点
          hrmFieldBean.setFieldlabel("524835");
          hrmFieldBean.setFieldhtmltype("4");
          hrmFieldBean.setType("2");
          hrmFieldBean.setFieldvalue(has_cut_point_value);
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          itemList.add(searchConditionItem);

          String cut_point_key = "cut_point"+changetype_index;
          String cut_point_value = isEdit?Util.null2String(valueMap.get(cut_point_key),"00:00"):"00:00";
          if("0".equalsIgnoreCase(has_cut_point_value)){
            cut_point_value = "00:00";
          }
          hrmFieldBean = new HrmFieldBean();
          hrmFieldBean.setFieldname(cut_point_key);//	自定义跨天加班归属分界点
          hrmFieldBean.setFieldlabel("524835");
          hrmFieldBean.setFieldhtmltype("3");
          hrmFieldBean.setType("19");
          hrmFieldBean.setFieldvalue(cut_point_value);
          hrmFieldBean.setIsFormField(true);
          searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
          itemList.add(searchConditionItem);

//          2是工作日，3是休息日，1是节假日
          if(changetype_index == 2){
            groupItem.put("title", SystemEnv.getHtmlLabelName(388841, user.getLanguage()));//工作日加班
          }else if(changetype_index == 3){
            groupItem.put("title", SystemEnv.getHtmlLabelName(389730, user.getLanguage()));//休息日加班
          }else if(changetype_index == 1){
            groupItem.put("title", SystemEnv.getHtmlLabelName(1959, user.getLanguage()));//节假日加班
          }
          groupItem.put("defaultshow", true);
          groupItem.put("items", itemList);
          groupList.add(groupItem);
        }
        resultMap.put("condition", groupList);

        /**
         * 判断调休是否已设置
         */
        sql = "select * from kq_LeaveRules where (isDelete is null or isDelete <>1) and id in (select ruleId from kq_LeaveRulesDetail where (isDelete is null or isDelete <>1) and distributionMode=5)";
        recordSet.executeQuery(sql);
        boolean hasTXRules = recordSet.getCounts()>0;
        resultMap.put("hasTXrules",hasTXRules);
        return resultMap;
    }

  private void set_TimeDetailList(String detailid, int changetype_index, Map<String, Object> resultMap) {
    RecordSet recordSet = new RecordSet();
    List timeDetailList = new ArrayList();
    String sql = "select * from kq_OvertimeBalanceTimeDetail where rulesdetailid=? order by timepoint";
    recordSet.executeQuery(sql, detailid);
    while (recordSet.next()) {
      String timepoint = recordSet.getString("timepoint");
      String lenOfOvertime = recordSet.getString("lenOfOvertime");
      String lenOfLeave = recordSet.getString("lenOfLeave");
      String dsporder = recordSet.getString("dsporder");

      Map<String, Object> timeDetailMap = new HashMap<String, Object>();
      timeDetailMap.put("time", timepoint);
      timeDetailMap.put("left", lenOfOvertime);
      timeDetailMap.put("right", lenOfLeave);
      timeDetailMap.put("dsporder", dsporder);
      timeDetailList.add(timeDetailMap);
    }
    if(!timeDetailList.isEmpty()){
      resultMap.put("paidLeaveEnableTypeRule"+changetype_index, timeDetailList);
    }
  }

  private void set_lengthDetailList(String detailid, int changetype_index,
        Map<String, Object> resultMap){

      RecordSet recordSet = new RecordSet();
      List lengthDetailList = new ArrayList();
      String sql = "select * from kq_OvertimeBalanceLengthDetail where rulesdetailid=? order by overtimelength";
      recordSet.executeQuery(sql, detailid);
      while (recordSet.next()) {
        String overtimelength = recordSet.getString("overtimelength");
        String balancelength = recordSet.getString("balancelength");
        String dsporder = recordSet.getString("dsporder");

        Map<String, Object> lengthDetailMap = new HashMap<String, Object>();
        lengthDetailMap.put("work", overtimelength);
        lengthDetailMap.put("rest", balancelength);
        lengthDetailMap.put("dsporder", dsporder);
        lengthDetailList.add(lengthDetailMap);
      }
      if(!lengthDetailList.isEmpty()){
        resultMap.put("paidLeaveEnableTypeRule"+changetype_index, lengthDetailList);
      }
    }
}
