package com.engine.kq.biz;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.engine.kq.log.KQLog;
import weaver.cache.*;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.StaticObj;
import weaver.general.Util;

/**
 * 人性化班次设置其他规则缓存类
 */
public class KQShiftPersonalizedRuleDetailComInfo extends CacheBase implements Serializable {
  /*
       * 需要关注 : 如果完全在 initCache 自己定义数据初始化， 这个字段可以不定义
       */
  protected static String TABLE_NAME = "";

  /*
   * 需要关注 : 如果完全在 initCache 自己定义数据初始化，或者不需要指定固定的条件， 这个字段可以不定义
   * sql中的where信息，不要以where开始
   */
  protected static String TABLE_WHERE = null;

  /*
   * 需要关注 : 如果完全在 initCache 自己定义数据初始化，或者不需要指定顺序， 这个字段可以不定义 sql中的order
   * by信息，不要以order by开始
   *
   */
  protected static String TABLE_ORDER = null;

  @PKColumn(type = CacheColumnType.NUMBER)
  protected static String PK_NAME = "id";

  @CacheColumn
  protected static int serial;

  private KQLog kqLog = new KQLog();

  @Override
  protected boolean autoInitIfNotFound() {
    return false;
  }

  @Override
  public CacheMap initCache() {
    CacheMap localData = createCacheMap();
    RecordSet rs = new RecordSet();
    kqLog = new KQLog();
    try {
      List<Object> workSectionList = new ArrayList<>();
      Map<String,Object> serialMaps = new HashMap<>();//分组用的map
      Map<String,Object> groupSectionMaps = new HashMap<>();//分组用的map
      Map<String,Object> sectionMap = new HashMap<>();

      //给考勤报表提供，排序方式如下
      String ruleSql = "";
      ruleSql = "select * from kq_ShiftPersonalizedRuleDetail where ruledetailtype='lateinlateout' order by personalizedruleid,postponetime desc ";
      rs.executeQuery(ruleSql);
      while (rs.next()) {
        String id = Util.null2String(rs.getString("id"));
        String personalizedruleid = Util.null2String(rs.getString("personalizedruleid"));
        String enable = Util.null2String(rs.getString("enable"));
        String enablesame = Util.null2String(rs.getString("enablesame"));
        if (!"1".equalsIgnoreCase(enable)) {
          //只取启用的
          continue;
        }
        //第一天下班晚走
        String advancetime = Util.null2String(rs.getString("advancetime"));
        //第二天上班可以晚到
        String postponetime = Util.null2String(rs.getString("postponetime"));
        String ruledetailtype = Util.null2String(rs.getString("ruledetailtype"));
        String record = Util.null2String(rs.getString("record"));

        sectionMap = new HashMap<>();
        sectionMap.put("enable", enable);
        sectionMap.put("enablesame", enablesame);
        sectionMap.put("advancetime", advancetime);
        sectionMap.put("postponetime", postponetime);
        sectionMap.put("record", record);
        if(serialMaps.get(personalizedruleid) != null){
          Map<String,Object> groupSectionMapsTmp = (Map<String,Object>)serialMaps.get(personalizedruleid);
          if(groupSectionMapsTmp.get(ruledetailtype) != null){
            List<Object> workSectionListTmp = (List<Object>)groupSectionMapsTmp.get(ruledetailtype);
            workSectionListTmp.add(sectionMap);
          }else{
            workSectionList = new ArrayList<>();
            workSectionList.add(sectionMap);
            groupSectionMapsTmp.put(ruledetailtype, workSectionList);
          }
        }else{
          groupSectionMaps = new HashMap<>();
          serialMaps.put(personalizedruleid, groupSectionMaps);
          if(groupSectionMaps.get(ruledetailtype) != null){
            List<Object> workSectionListTmp = (List<Object>)groupSectionMaps.get(ruledetailtype);
            workSectionListTmp.add(sectionMap);
          }else{
            workSectionList = new ArrayList<>();
            workSectionList.add(sectionMap);
            groupSectionMaps.put(ruledetailtype, workSectionList);
          }

        }
      }

      ruleSql = "select * from kq_ShiftPersonalizedRuleDetail where ruledetailtype in ('earlyinearlyout','lateoutlatein') order by personalizedruleid,advancetime desc ";
      rs.executeQuery(ruleSql);
      while (rs.next()) {
        String id = Util.null2String(rs.getString("id"));
        String personalizedruleid = Util.null2String(rs.getString("personalizedruleid"));
        String enable = Util.null2String(rs.getString("enable"));
        String enablesame = Util.null2String(rs.getString("enablesame"));
        if (!"1".equalsIgnoreCase(enable)) {
          //只取启用的
          continue;
        }
        //第一天下班晚走
        String advancetime = Util.null2String(rs.getString("advancetime"));
        //第二天上班可以晚到
        String postponetime = Util.null2String(rs.getString("postponetime"));
        String ruledetailtype = Util.null2String(rs.getString("ruledetailtype"));
        String record = Util.null2String(rs.getString("record"));

        sectionMap = new HashMap<>();
        sectionMap.put("enable", enable);
        sectionMap.put("enablesame", enablesame);
        sectionMap.put("advancetime", advancetime);
        sectionMap.put("postponetime", postponetime);
        sectionMap.put("record", record);
        if(serialMaps.get(personalizedruleid) != null){
          Map<String,Object> groupSectionMapsTmp = (Map<String,Object>)serialMaps.get(personalizedruleid);
          if(groupSectionMapsTmp.get(ruledetailtype) != null){
            List<Object> workSectionListTmp = (List<Object>)groupSectionMapsTmp.get(ruledetailtype);
            workSectionListTmp.add(sectionMap);
          }else{
            workSectionList = new ArrayList<>();
            workSectionList.add(sectionMap);
            groupSectionMapsTmp.put(ruledetailtype, workSectionList);
          }
        }else{
          groupSectionMaps = new HashMap<>();
          serialMaps.put(personalizedruleid, groupSectionMaps);
          if(groupSectionMaps.get(ruledetailtype) != null){
            List<Object> workSectionListTmp = (List<Object>)groupSectionMaps.get(ruledetailtype);
            workSectionListTmp.add(sectionMap);
          }else{
            workSectionList = new ArrayList<>();
            workSectionList.add(sectionMap);
            groupSectionMaps.put(ruledetailtype, workSectionList);
          }

        }
      }

      for(Map.Entry<String,Object> me : serialMaps.entrySet()){
        String id = me.getKey();
        Map<String,Object> val = (Map<String,Object>)me.getValue();
        CacheItem cacheItem = createCacheItem();
        cacheItem.set(PK_INDEX, id);
        cacheItem.set(serial, val);
        modifyCacheItem(id, cacheItem);
        localData.put(id, cacheItem);
      }
    } catch (Exception e) {
      writeLog(e);
    }
    return localData;
  }

  /**
   * 根据班次个性化设置id获取详细信息
   * @param personalizedruleid
   * @return
   */
  public Map<String,Object> getPersonalizedRuleDetail(String personalizedruleid){
    return (Map<String,Object>) getObjValue(serial, personalizedruleid);
  }

  public Map<String,Object> getPersonalizedRuleDetail_sql(String personalizedruleid){
    Map<String,Object> groupSectionMaps = new HashMap<>();//分组用的map
    CacheMap localData = createCacheMap();
    RecordSet rs = new RecordSet();
    kqLog = new KQLog();
    try {
      List<Object> workSectionList = new ArrayList<>();
      Map<String,Object> serialMaps = new HashMap<>();//分组用的map
      Map<String,Object> sectionMap = new HashMap<>();

      //给考勤报表提供，排序方式如下
      String ruleSql = "";
      ruleSql = "select * from kq_ShiftPersonalizedRuleDetail where personalizedruleid=? and ruledetailtype='lateinlateout' order by personalizedruleid,postponetime desc ";
      rs.executeQuery(ruleSql,personalizedruleid);
      while (rs.next()) {
        String id = Util.null2String(rs.getString("id"));
        String enable = Util.null2String(rs.getString("enable"));
        String enablesame = Util.null2String(rs.getString("enablesame"));
        if (!"1".equalsIgnoreCase(enable)) {
          //只取启用的
          continue;
        }
        //第一天下班晚走
        String advancetime = Util.null2String(rs.getString("advancetime"));
        //第二天上班可以晚到
        String postponetime = Util.null2String(rs.getString("postponetime"));
        String ruledetailtype = Util.null2String(rs.getString("ruledetailtype"));
        String record = Util.null2String(rs.getString("record"));

        sectionMap = new HashMap<>();
        sectionMap.put("enable", enable);
        sectionMap.put("enablesame", enablesame);
        sectionMap.put("advancetime", advancetime);
        sectionMap.put("postponetime", postponetime);
        sectionMap.put("record", record);
        if(serialMaps.get(personalizedruleid) != null){
          Map<String,Object> groupSectionMapsTmp = (Map<String,Object>)serialMaps.get(personalizedruleid);
          if(groupSectionMapsTmp.get(ruledetailtype) != null){
            List<Object> workSectionListTmp = (List<Object>)groupSectionMapsTmp.get(ruledetailtype);
            workSectionListTmp.add(sectionMap);
          }else{
            workSectionList = new ArrayList<>();
            workSectionList.add(sectionMap);
            groupSectionMapsTmp.put(ruledetailtype, workSectionList);
          }
        }else{
          groupSectionMaps = new HashMap<>();
          serialMaps.put(personalizedruleid, groupSectionMaps);
          if(groupSectionMaps.get(ruledetailtype) != null){
            List<Object> workSectionListTmp = (List<Object>)groupSectionMaps.get(ruledetailtype);
            workSectionListTmp.add(sectionMap);
          }else{
            workSectionList = new ArrayList<>();
            workSectionList.add(sectionMap);
            groupSectionMaps.put(ruledetailtype, workSectionList);
          }

        }
      }

      ruleSql = "select * from kq_ShiftPersonalizedRuleDetail where personalizedruleid=? and ruledetailtype in ('earlyinearlyout','lateoutlatein') order by personalizedruleid,advancetime desc ";
      rs.executeQuery(ruleSql,personalizedruleid);
      while (rs.next()) {
        String id = Util.null2String(rs.getString("id"));
        String enable = Util.null2String(rs.getString("enable"));
        String enablesame = Util.null2String(rs.getString("enablesame"));
        if (!"1".equalsIgnoreCase(enable)) {
          //只取启用的
          continue;
        }
        //第一天下班晚走
        String advancetime = Util.null2String(rs.getString("advancetime"));
        //第二天上班可以晚到
        String postponetime = Util.null2String(rs.getString("postponetime"));
        String ruledetailtype = Util.null2String(rs.getString("ruledetailtype"));
        String record = Util.null2String(rs.getString("record"));

        sectionMap = new HashMap<>();
        sectionMap.put("enable", enable);
        sectionMap.put("enablesame", enablesame);
        sectionMap.put("advancetime", advancetime);
        sectionMap.put("postponetime", postponetime);
        sectionMap.put("record", record);
        if(serialMaps.get(personalizedruleid) != null){
          Map<String,Object> groupSectionMapsTmp = (Map<String,Object>)serialMaps.get(personalizedruleid);
          if(groupSectionMapsTmp.get(ruledetailtype) != null){
            List<Object> workSectionListTmp = (List<Object>)groupSectionMapsTmp.get(ruledetailtype);
            workSectionListTmp.add(sectionMap);
          }else{
            workSectionList = new ArrayList<>();
            workSectionList.add(sectionMap);
            groupSectionMapsTmp.put(ruledetailtype, workSectionList);
          }
        }else{
          groupSectionMaps = new HashMap<>();
          serialMaps.put(personalizedruleid, groupSectionMaps);
          if(groupSectionMaps.get(ruledetailtype) != null){
            List<Object> workSectionListTmp = (List<Object>)groupSectionMaps.get(ruledetailtype);
            workSectionListTmp.add(sectionMap);
          }else{
            workSectionList = new ArrayList<>();
            workSectionList.add(sectionMap);
            groupSectionMaps.put(ruledetailtype, workSectionList);
          }

        }
      }

    } catch (Exception e) {
      writeLog(e);
    }
    return groupSectionMaps;
  }

  /**
   * 删除缓存信息
   */
  public void removePersonalizedRuleDetailCache() {
    removeCache();
  }

}
