package com.engine.kq.cmd.shiftmanagement;

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 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.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;

/**
 * 获取班次管理个性化表单
 * @author pzy
 *
 */
public class GetShiftManagementPersonalizedFormCmd extends AbstractCommonCommand<Map<String, Object>>{

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

	@Override
	public BizLogContext getLogContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
    Map<String, Object> retmap = new HashMap<String, Object>();
    List<Map<String, Object>> grouplist = new ArrayList<Map<String, Object>>();
    Map<String, Object> groupitem = null;
    List<Object> itemlist = null;
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();
    String sql = "";
    try {
      if(!HrmUserVarify.checkUserRight("KQClass:Management",user)) {
        retmap.put("status", "-1");
        retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
        return retmap;
      }
      ManageDetachComInfo ManageDetachComInfo = new ManageDetachComInfo();
      String detachable = ManageDetachComInfo.getDetachable();
      String _id = Util.null2String(params.get("id"));
      boolean isEdit = false;

      Map<String,Object> fieldOtherMap = new HashMap<>();
      Object[] fieldVal = new Object[8];
      Object[] fieldEnableVal = new Object[6];
      Map<String,Object> ruleMaps = new HashMap<>();
      Map<String,Object> ruleMap = new HashMap<>();
      Map<String,Object> groupRuleMaps = new HashMap<>();//分组用的map
      List<Object> ruleList = new ArrayList<>();
      if(_id.length() > 0){
        int personalizedRuleid = 0;
        String idSql = "select * from kq_ShiftPersonalizedRule where (isdelete is null or  isdelete <> '1') and serialid=? ";
        rs = new RecordSet();
        rs.executeQuery(idSql,_id);
        if(rs.next()) {
          isEdit = true;
          personalizedRuleid = rs.getInt("id");
          String permitlateminutes          = 	rs.getString("permitlateminutes");
          String seriouslateminutes         = 	rs.getString("seriouslateminutes");
          String lateabsentminutes          = 	rs.getString("lateabsentminutes");
          String permitleaveearlyminutes    = 	rs.getString("permitleaveearlyminutes");
          String seriousleaveearlyminutes   = 	rs.getString("seriousleaveearlyminutes");
          String leaveearlyabsentminutes    = 	rs.getString("leaveearlyabsentminutes");

          String permitlatestatus           = 	rs.getString("permitlatestatus");
          String seriouslatestatus          = 	rs.getString("seriouslatestatus");
          String lateabsentstatus           = 	rs.getString("lateabsentstatus");
          String permitleaveearlystatus     = 	rs.getString("permitleaveearlystatus");
          String seriousleaveearlystatus    = 	rs.getString("seriousleaveearlystatus");
          String leaveearlyabsentstatus     = 	rs.getString("leaveearlyabsentstatus");

          String enableexcludelate    = 	rs.getString("enableexcludelate");
          String enableexcludeleaveearly     = 	rs.getString("enableexcludeleaveearly");

          fieldVal[0] = permitlateminutes;
          fieldVal[1] = permitleaveearlyminutes;
          fieldVal[2] = seriouslateminutes;
          fieldVal[3] = seriousleaveearlyminutes;
          fieldVal[4] = lateabsentminutes;
          fieldVal[5] = leaveearlyabsentminutes;
          fieldVal[6] = Util.null2s(enableexcludelate,"0");
          fieldVal[7] = Util.null2s(enableexcludeleaveearly,"0");

          fieldEnableVal[0] = permitlatestatus;
          fieldEnableVal[1] = permitleaveearlystatus;
          fieldEnableVal[2] = seriouslatestatus;
          fieldEnableVal[3] = seriousleaveearlystatus;
          fieldEnableVal[4] = lateabsentstatus;
          fieldEnableVal[5] = leaveearlyabsentstatus;
        }
        if(personalizedRuleid > 0){

          String ruleSql = "select * from kq_ShiftPersonalizedRuleDetail where (isdelete is null or  isdelete <> '1') and personalizedruleid =?  order by ruledetailtype,record ";
          rs = new RecordSet();
          rs.executeQuery(ruleSql,personalizedRuleid);
          while(rs.next()){
            String enable          = 	rs.getString("enable");
            String enablesame          = 	rs.getString("enablesame");
            String advancetime         = 	rs.getString("advancetime");
            String postponetime          = 	rs.getString("postponetime");
            String ruledetailtype    = 	rs.getString("ruledetailtype");
            String record    = 	rs.getString("record");
            ruleMap = new HashMap<>();
            ruleMap.put("advancetime", advancetime);
            ruleMap.put("postponetime", postponetime);

            fieldOtherMap.put(ruledetailtype, enable);
            fieldOtherMap.put(ruledetailtype+"same", enablesame);

            if(!"lateoutlatein".equalsIgnoreCase(ruledetailtype)){
              ruleMaps.put(ruledetailtype, ruleMap);
            }else{
              ruleMap.put("record", record);
              if(groupRuleMaps.get(record) != null){
                List<Object> tmpRuleList = (List<Object>)groupRuleMaps.get(record);
                tmpRuleList.add(ruleMap);
              }else{
                ruleList.add(ruleMap);
                groupRuleMaps.put(record, ruleList);
              }
            }
          }
          if(!ruleList.isEmpty()){
            ruleMaps.put("lateoutlatein", ruleList);
          }
        }
      }
      //允许迟到分钟数 允许早退分钟数 严重迟到分钟数 严重早退分钟数 迟到*分钟算旷工 早退早*分钟算旷工 是否排除迟到分钟数 是否排除早退分钟数
      HrmFieldBean hrmFieldBean = null;
      String[] fieldrules = new String[]{"permitlateminutes,388578,1,2","permitleaveearlyminutes,388579,1,2","seriouslateminutes,388580,1,2",
          "seriousleaveearlyminutes,388581,1,2","lateabsentminutes,20081,1,2","leaveearlyabsentminutes,20082,1,2",
          "enableexcludelate,508515,4,1","enableexcludeleaveearly,508516,4,1"};
      //允许迟到是否开启 严重迟到是否开启 迟到视作旷工是否开启 允许早退是否开启 严重早退是否开启 早退视作旷工是否开启
//      "permitlatestatus,388578,4,1","seriouslatestatus,388579,4,1","lateabsentstatus,388582,4,1",
//          "permitleaveearlystatus,388579,4,1","seriousleaveearlystatus,388581,4,1","leaveearlyabsentstatus,388582,4,1"

      //允许早到早走 允许晚到晚走 允许晚走晚到
      String[] fieldothers = new String[]{"earlyinearlyout,388586,4,2","earlyinearlyoutsame,513620,4,1","lateinlateout,388589,4,2","lateinlateoutsame,513620,4,1","lateoutlatein,388590,4,2","lateoutlateinsame,513620,4,1"};

      HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
      SearchConditionItem searchConditionItem = null;
      List<SearchConditionOption> options = new ArrayList<SearchConditionOption>();

      groupitem = new HashMap<String, Object>();
      groupitem.put("title", SystemEnv.getHtmlLabelName(388576,weaver.general.Util.getIntValue(user.getLanguage())));
      groupitem.put("defaultshow", true);

      itemlist = new ArrayList<Object>();
      for (int j = 0; j < fieldrules.length; j++) {
        options = new ArrayList<SearchConditionOption>();
        String[] tmpField = Util.null2String(fieldrules[j]).split(",");
        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname(tmpField[0]);
        hrmFieldBean.setFieldlabel(tmpField[1]);
        hrmFieldBean.setFieldhtmltype(tmpField[2]);
        hrmFieldBean.setType(tmpField[3]);
        hrmFieldBean.setViewAttr(2);
        hrmFieldBean.setIsFormField(true);
        boolean is_exclude = false;
        if("enableexcludelate".equalsIgnoreCase(tmpField[0]) || "enableexcludeleaveearly".equalsIgnoreCase(tmpField[0])){
          is_exclude = true;
        }

        if(isEdit){
          hrmFieldBean.setFieldvalue(fieldVal[j]);
        }else{
          if(is_exclude){
            hrmFieldBean.setFieldvalue("0");
          }
        }
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        searchConditionItem.setColSpan(2);
        if(!is_exclude){
          searchConditionItem.setCheckbox(true);
        }
        Map<String,Object> otherParams = new HashMap<String,Object>();
        otherParams.put("min",0);
        searchConditionItem.setOtherParams(otherParams);

        if("lateabsentminutes".equalsIgnoreCase(tmpField[0]) || "leaveearlyabsentminutes".equalsIgnoreCase(tmpField[0])){
          searchConditionItem.setTip(SystemEnv.getHtmlLabelName(388582, user.getLanguage()));
        }

        if(isEdit){
          if(!is_exclude){
            searchConditionItem.setCheckboxValue("1".equals(fieldEnableVal[j]));
          }
        }else{
          searchConditionItem.setCheckboxValue(false);
        }
        searchConditionItem.setRules("required|integer");

        itemlist.add(searchConditionItem);
      }
      groupitem.put("items", itemlist);
      grouplist.add(groupitem);

      groupitem = new HashMap<String, Object>();
      groupitem.put("title", SystemEnv.getHtmlLabelName(10000837,weaver.general.Util.getIntValue(user.getLanguage())));
      groupitem.put("defaultshow", true);
      itemlist = new ArrayList<Object>();
      for (int j = 0; j < fieldothers.length; j++) {
        options = new ArrayList<SearchConditionOption>();
        String[] tmpField = Util.null2String(fieldothers[j]).split(",");
        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname(tmpField[0]);
        hrmFieldBean.setFieldlabel(tmpField[1]);
        hrmFieldBean.setFieldhtmltype(tmpField[2]);
        hrmFieldBean.setType(tmpField[3]);
        hrmFieldBean.setViewAttr(2);
        hrmFieldBean.setIsFormField(true);

        if(isEdit){
          if(!fieldOtherMap.isEmpty() && fieldOtherMap.get(tmpField[0]) != null){
            hrmFieldBean.setFieldvalue(fieldOtherMap.get(tmpField[0]));
          }
        }else{
          hrmFieldBean.setFieldvalue(0);
        }
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
//        searchConditionItem.setRules("required|string");

        itemlist.add(searchConditionItem);
      }
      groupitem.put("items", itemlist);
      grouplist.add(groupitem);

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

}
