package com.engine.kq.biz;

import com.api.hrm.bean.SelectOption;
import com.engine.kq.biz.chain.cominfo.ShiftInfoCominfoBean;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.StaticObj;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 考勤流程设置缓存类
 * 1、给流程上半天和天下拉框对应关系用的
 * 2、给根据人员id不同带出不同的请假类型用的
 */
public class KQAttFlowSetComInfo extends BaseBean {
  private ArrayList ids = null;

  private StaticObj staticobj = null;

  public KQAttFlowSetComInfo(User user) {
    staticobj = StaticObj.getInstance();
    if(user != null){
      setKQAttFlowSetComInfo(user);
    }
  }

  public KQAttFlowSetComInfo() {
    staticobj = StaticObj.getInstance();
    getKQAttFlowSetComInfo() ;
  }

  private void getKQAttFlowSetComInfo(User user) {
    if(staticobj.getObject("KQAttFlowSetComInfo&KQShift") == null || ((Hashtable)staticobj.getObject("KQAttFlowSetComInfo&KQShift")).size()==0) {
      setKQAttFlowSetComInfo(user);
    }
    ids = (ArrayList)(staticobj.getRecordFromObj("KQAttFlowSetComInfo&KQShift", "ids"));
    if(ids==null){
      setKQAttFlowSetComInfo(user);
    }
  }

  private void getKQAttFlowSetComInfo() {
    if(staticobj.getObject("KQAttFlowSetComInfo&KQShift") == null || ((Hashtable)staticobj.getObject("KQAttFlowSetComInfo&KQShift")).size()==0) {
      setKQAttFlowSetComInfo();
    }
    ids = (ArrayList)(staticobj.getRecordFromObj("KQAttFlowSetComInfo&KQShift", "ids"));
    if(ids==null){
      setKQAttFlowSetComInfo();
    }
  }
  private void setKQAttFlowSetComInfo() {
    setKQAttFlowSetComInfo(null);
  }

  private void setKQAttFlowSetComInfo(User user) {
    if(ids != null){
      ids.clear();
    }else{
      ids = new ArrayList();
    }

    Map<String,Object> attFLowlinkMap = new HashMap<>();
    Map<String,Object> attFLowDependCfgMap = new HashMap<>();
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();
    //现在只有请假流程有特殊处理
    String getAttFLowInfo  = "select * from kq_att_proc_set where field005 = 1 and field006 in(0,6) ";
    try {
      rs.execute(getAttFLowInfo);
      while(rs.next()){
        String prefix = "";
        int type = 0;
        String id = rs.getString("id");
        String workflowid = rs.getString("field001");
        String attrType = rs.getString("field006");
        String setId = rs.getString("id");
        String usedetail = rs.getString("usedetail");
        if(usedetail.equalsIgnoreCase("1")){
          prefix = "detail_";
          type = 1;
        }

        //请假类型存储的list
        Map<String,Object> linkageCfg = new HashMap<String,Object>();//联动
        Map<String,Object> dependCfg = new HashMap<String,Object>();//请假类型数据过滤

        Map<String,String> map0 = new HashMap<>();
        String fieldSql = "select a.*,b.field002 fieldname from kq_att_proc_relation a left join kq_att_proc_fields b on a.field002 = b.id where b.field001=180 and a.field001="+setId+" and b.field002 in('"+prefix+"resourceId','"+prefix+"newLeaveType','"+prefix+"fromTime','"+prefix+"toTime') ";
        if("6".equalsIgnoreCase(attrType)){
          fieldSql = "select a.*,b.field002 fieldname from kq_att_proc_relation a left join kq_att_proc_fields b on a.field002 = b.id where b.field001=6 and a.field001="+setId+" and b.field002 in('resourceId','detail_newLeaveType','detail_leavefromTime','detail_leavetoTime','detail_fromTime','detail_toTime') ";
        }
        rs1.execute(fieldSql);
        while(rs1.next()){
          String fieldid = rs1.getString("field003");
          String fieldName = rs1.getString("fieldname");
          if("6".equalsIgnoreCase(attrType)){
            if(("resourceId").equalsIgnoreCase(fieldName)){
              map0.put("resourceId", fieldid);
            }
            if(("detail_newLeaveType").equalsIgnoreCase(fieldName)){
              map0.put("newLeaveType", fieldid);
            }
            if(("detail_leavefromTime").equalsIgnoreCase(fieldName)){
              map0.put("fromTime", fieldid);
            }
            if(("detail_leavetoTime").equalsIgnoreCase(fieldName)){
              map0.put("toTime", fieldid);
            }
            if(("detail_fromTime").equalsIgnoreCase(fieldName)){
              map0.put("back_fromTime", fieldid);
            }
            if(("detail_toTime").equalsIgnoreCase(fieldName)){
              map0.put("back_toTime", fieldid);
            }
          }else{
            if((prefix+"resourceId").equalsIgnoreCase(fieldName)){
              map0.put("resourceId", fieldid);
            }
            if((prefix+"newLeaveType").equalsIgnoreCase(fieldName)){
              map0.put("newLeaveType", fieldid);
            }
            if((prefix+"fromTime").equalsIgnoreCase(fieldName)){
              map0.put("fromTime", fieldid);
            }
            if((prefix+"toTime").equalsIgnoreCase(fieldName)){
              map0.put("toTime", fieldid);
            }
          }
        }

        if(!map0.isEmpty()){
          String resourceId_f = Util.null2String(map0.get("resourceId"));
          String newLeaveType_f = Util.null2String(map0.get("newLeaveType"));
          String fromTime_f = Util.null2String(map0.get("fromTime"));
          String toTime_f = Util.null2String(map0.get("toTime"));
          String back_fromTime_f = Util.null2String(map0.get("back_fromTime"));
          String back_toTime_f = Util.null2String(map0.get("back_toTime"));
          //   根据人员id联动请假类型
          resIdLinkLeaveType(resourceId_f,newLeaveType_f, dependCfg, type);
          //请假类型联动出时间或者上午下午下拉框
          leaveTypeLinkTime(newLeaveType_f, fromTime_f, toTime_f, linkageCfg, type, null,user,back_fromTime_f,back_toTime_f);
          attFLowlinkMap.put(workflowid, linkageCfg);
          attFLowDependCfgMap.put(workflowid, dependCfg);
        }
        ids.add(id);
      }

    } catch (Exception e) {
      writeLog(e) ;
      throw e ;
    }
    staticobj.putRecordToObj("KQAttFlowSetComInfo&KQShift", "ids", ids);
    staticobj.putRecordToObj("KQAttFlowSetComInfo&KQShift", "linkageCfg", attFLowlinkMap);
    staticobj.putRecordToObj("KQAttFlowSetComInfo&KQShift", "dependCfg", attFLowDependCfgMap);
  }

  /**
   * 根据人员id联动请假类型
   * {field83333_1:[field8555_1,field866639_1]}
   * 接口说明：
   * 请假类型选择的值field83333_1(表示主表明细表):[人力资源浏览按钮field8555_1(用数组表示如果以后要增加多个)]
   * @param resourceId_f
   * @param newLeaveType_f
   * @param linkageCfg
   * @param type
   */
  private void resIdLinkLeaveType(String resourceId_f, String newLeaveType_f, Map<String, Object> linkageCfg, int type) {
    List<String> linkageCfg1 = new ArrayList<String>();
    linkageCfg1.add(resourceId_f+"_"+type);
    linkageCfg.put(newLeaveType_f, linkageCfg1);
  }

  /**
   * 请假类型fieldid_主表还是明细表（0,1）{
   *     {
   *     请假类型选择的值:{
   *         changeType:"5-1"/"3-19" 选择了类型之后变成时间还是下拉框
   *       fieldid:"112_1", 时间浏览按钮对应的fieldid_主表还是明细表
   *         option:[    变成下拉框之后下拉框里面的值
   *   {key:"09:00",showname:"上午"},
   *   {key:"13:00",showname:"下午"},
   *   {key:"23:00",showname:"整天"}
   *             ]
   *     }
   *     }
   * }
   * 请假类型选择框联动时间控件，下拉框
   * @param leaveTypeFieldId  请假类型fieldid
   * @param fromtimefieldId  时间浏览框fieldid
   * @param totimefieldId  时间浏览框fieldid
   * @param linkageCfg
   * @param type  0:主表; 1：明细表1; 2:明细表2; 3:明细表3
   * @param deafultValue
   * @param user
   * @param back_fromTime_f  销假时间浏览框fieldid
   * @param back_toTime_f  销假时间浏览框fieldid
   */
  private void leaveTypeLinkTime(String leaveTypeFieldId, String fromtimefieldId,
      String totimefieldId, Map<String, Object> linkageCfg, int type, String[] deafultValue,User user,
      String back_fromTime_f,String back_toTime_f){
    int lan = user != null ? user.getLanguage() : 7;
    String allday = SystemEnv.getHtmlLabelName(390728, lan);
    KQLeaveRulesComInfo kqLeaveRulesComInfo = new KQLeaveRulesComInfo();
    KQTimeSelectionComInfo kqTimeSelectionComInfo = new KQTimeSelectionComInfo();

    Map<String,Object> selectChangeMapLinkage = new HashMap<String,Object>();

    kqLeaveRulesComInfo.setTofirstRow();
    while(kqLeaveRulesComInfo.next()){
      String am = SystemEnv.getHtmlLabelName(16689, lan);
      String pm = SystemEnv.getHtmlLabelName(16690, lan);
      String ruleid = kqLeaveRulesComInfo.getId();
      String changeType = kqLeaveRulesComInfo.getMinimumUnit();
      String timeselection = kqLeaveRulesComInfo.getTimeSelection();
      String selectiontype = ""+KqSplitFlowTypeEnum.LEAVE.getFlowtype();
      Map<String,String> map = kqTimeSelectionComInfo.getTimeselections(selectiontype,ruleid,changeType);
      if(!map.isEmpty()){
        am = Util.null2String(map.get("half_on"));
        pm = Util.null2String(map.get("half_off"));
        if(am.length() == 0){
          am = SystemEnv.getHtmlLabelName(16689, lan);
        }
        if(pm.length() == 0){
          pm = SystemEnv.getHtmlLabelName(16690, lan);
        }
      }
      boolean isHalf = "2".equals(changeType);
      boolean isWhole = "4".equals(changeType);
      changeType = (isHalf||isWhole)?"5-1":"3-19";
      if(isHalf && "2".equalsIgnoreCase(timeselection)){
        //如果不是下拉框形式，还是按照时间浏览按钮显示
        changeType = "3-19";
      }

      List<Object> fromHalfOption = new ArrayList<>();
      fromHalfOption.add(new SelectOption("08:00", am));
      fromHalfOption.add(new SelectOption("13:00", pm));

      List<Object> toHalfOption = new ArrayList<>();
      toHalfOption.add(new SelectOption("13:00", am));
      toHalfOption.add(new SelectOption("18:00", pm));

      List<Object> fromWholeOption = new ArrayList<>();
      fromWholeOption.add(new SelectOption("08:00", allday));

      List<Object> toWholeOption = new ArrayList<>();
      toWholeOption.add(new SelectOption("18:00", allday));

      List<Map<String,Object>> linkageCfg1 = new ArrayList<Map<String,Object>>();
      selectChangeMapLinkage.put(ruleid, linkageCfg1);
      Map<String,Object> cfg1 = new HashMap<String,Object>();
      cfg1.put("fieldid", fromtimefieldId+"_"+type);	//改变的浏览框ID(_0为主字段，_1为明细字段)
      cfg1.put("changeType", changeType);
      List<Object> _new = new ArrayList<>();
      if(isHalf){
        List<Object> _new_half = new ArrayList<>();
        _new_half.addAll(fromHalfOption);
        _new.addAll(_new_half);
      }
      if(isWhole){
        List<Object> _new_half = new ArrayList<>();
        _new_half.addAll(fromWholeOption);
        _new.addAll(_new_half);
      }
      cfg1.put("selectOption", _new);	//下拉框里的值对应关系
      linkageCfg1.add(cfg1);
      cfg1 = new HashMap<String,Object>();
      cfg1.put("fieldid", totimefieldId+"_"+type);	//改变的浏览框ID(_0为主字段，_1为明细字段)
      cfg1.put("changeType", changeType);
      List<Object> _new1 = new ArrayList<>();
      if(isHalf){
        List<Object> _new_whole = new ArrayList<>();
        _new_whole.addAll(toHalfOption);
        _new1.addAll(_new_whole);
      }
      if(isWhole){
        List<Object> _new_whole = new ArrayList<>();
        _new_whole.addAll(toWholeOption);
        _new1.addAll(_new_whole);
      }
      cfg1.put("selectOption", _new1);	//下拉框里的值对应关系
      linkageCfg1.add(cfg1);

      if(back_fromTime_f.length() > 0 && back_toTime_f.length() > 0){
        cfg1 = new HashMap<String,Object>();
        cfg1.put("fieldid", back_fromTime_f+"_"+type);	//改变的浏览框ID(_0为主字段，_1为明细字段)
        cfg1.put("changeType", changeType);
        List<Object> _new2 = new ArrayList<>();
        if(isHalf){
          List<Object> _new_whole = new ArrayList<>();
          _new_whole.addAll(fromHalfOption);
          _new2.addAll(_new_whole);
        }
        if(isWhole){
          List<Object> _new_whole = new ArrayList<>();
          _new_whole.addAll(fromWholeOption);
          _new2.addAll(_new_whole);
        }
        cfg1.put("selectOption", _new2);	//下拉框里的值对应关系
        linkageCfg1.add(cfg1);

        cfg1 = new HashMap<String,Object>();
        cfg1.put("fieldid", back_toTime_f+"_"+type);	//改变的浏览框ID(_0为主字段，_1为明细字段)
        cfg1.put("changeType", changeType);
        List<Object> _new3 = new ArrayList<>();
        if(isHalf){
          List<Object> _new_whole = new ArrayList<>();
          _new_whole.addAll(toHalfOption);
          _new3.addAll(_new_whole);
        }
        if(isWhole){
          List<Object> _new_whole = new ArrayList<>();
          _new_whole.addAll(toWholeOption);
          _new3.addAll(_new_whole);
        }
        cfg1.put("selectOption", _new3);	//下拉框里的值对应关系
        linkageCfg1.add(cfg1);
      }

    }

    linkageCfg.put(leaveTypeFieldId+"_"+type, selectChangeMapLinkage);

  }

  /**
   * 和流程联调的根据请假类型不同带出不同的时间选择框
   * @param workflowid
   * @return
   */
  public Map<String,Object> getLinkageCfgByWfId(String workflowid){
    Map<String,Object> attFLowlinkMap = (Map<String,Object>)staticobj.getRecordFromObj("KQAttFlowSetComInfo&KQShift", "linkageCfg");
    if(attFLowlinkMap != null && attFLowlinkMap.get(workflowid) != null){
      return (Map<String, Object>) attFLowlinkMap.get(workflowid);
    }else{
      return null;
    }
  }

  /**
   * com/api/hrm/service/HrmTimeSelWorkflowService.java
   * 这个地方会调用，控制流程的半天和整天下拉框的
   * 和流程联调的根据不同的人员带出不同的请假类型
   * @param workflowid
   * @return
   */
  public Map<String,Object> getDependCfgByWfId(String workflowid){
    Map<String,Object> attFLowDependCfgMap = (Map<String,Object>)staticobj.getRecordFromObj("KQAttFlowSetComInfo&KQShift", "dependCfg");
    if(attFLowDependCfgMap != null && attFLowDependCfgMap.get(workflowid) != null){
      return (Map<String, Object>) attFLowDependCfgMap.get(workflowid);
    }else{
      return null;
    }
  }

  public void removeKQAttFlowSetComInfoCache(){
    staticobj.removeObject("KQAttFlowSetComInfo&KQShift");
    KQAttProcSetComInfo kqAttProcSetComInfo = new KQAttProcSetComInfo();
    kqAttProcSetComInfo.removeCache();
  }

}
