package com.engine.kq.biz;

import com.engine.kq.enums.KqSplitFlowTypeEnum;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import weaver.cache.CacheBase;
import weaver.cache.CacheColumn;
import weaver.cache.CacheColumnType;
import weaver.cache.CacheItem;
import weaver.cache.CacheMap;
import weaver.cache.PKColumn;
import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * 时间选择方式缓存类
 */
public class KQTimeSelectionComInfo extends CacheBase implements Serializable {

  protected static String TABLE_NAME = "kq_timeselection";
  /** sql中的where信息，不要以where开始 */
  protected static String TABLE_WHERE = "";//默认加载今天之前一个月数据
  /** sql中的order by信息，不要以order by开始 */
  protected static String TABLE_ORDER = "id";

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

  @CacheColumn(name = "timeselection")
  protected static int timeselection;

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

  @Override
  public CacheMap initCache() throws Exception {
    CacheMap localData = super.createCacheMap();
    RecordSet rs = new RecordSet();
    Map<String,Map<String,String>>  timeSelectionMap = new HashMap<>();

    String sql = "select * from KQ_TIMESELECTION ";
    rs.executeQuery(sql);
    while (rs.next()){
      String selectiontype = rs.getString("selectiontype");
      if(Util.getIntValue(selectiontype) < 0){
        continue;
      }
      String leaveruleid = rs.getString("leaveruleid");
      String minimumunit = rs.getString("minimumunit");
      String key = "";
      if(Util.getIntValue(selectiontype) == KqSplitFlowTypeEnum.LEAVE.getFlowtype()){
        key = selectiontype+"_"+leaveruleid+"_"+minimumunit;
      }else{
        key = selectiontype+"_0_"+minimumunit;
      }
      Map<String,String> map = new HashMap<>();
      String half_on = Util.null2String(rs.getString("half_on"));
      String half_off = Util.null2String(rs.getString("half_off"));
      String whole = Util.null2String(rs.getString("whole"));
      map.put("half_on", half_on);
      map.put("half_off", half_off);
      map.put("whole", whole);
      timeSelectionMap.put(key, map);
    }

    if(!timeSelectionMap.isEmpty()){
      for(Map.Entry me : timeSelectionMap.entrySet()){
        String id = Util.null2String(me.getKey());
        Object value = me.getValue();
        CacheItem cacheItem = createCacheItem();
        cacheItem.set(PK_INDEX, id);
        cacheItem.set(timeselection, value);
        modifyCacheItem(id, cacheItem);
        localData.put(id, cacheItem);
      }
    }
    return localData;
  }

  @Override
  public CacheItem initCache(String key) {
    if (key == null || "".equals(key.trim())) {
      return null;
    }
    Map<String,Map<String,String>>  timeSelectionMap = new HashMap<>();
    String[] keys = Util.splitString(key,"_");
    String selectiontype = keys[0];
    String leaveruleid = keys[1];
    String minimumunit = keys[2];
    RecordSet rs = new RecordSet();
    String sql = "";
    if(Util.getIntValue(selectiontype) == KqSplitFlowTypeEnum.LEAVE.getFlowtype()){
      sql = "select * from KQ_TIMESELECTION where selectiontype="+selectiontype+" and leaveruleid='"+leaveruleid+"' and minimumunit="+minimumunit;
    }else{
      sql = "select * from KQ_TIMESELECTION where selectiontype="+selectiontype+" and minimumunit="+minimumunit;
    }
    rs.executeQuery(sql);
    while (rs.next()){
      Map<String,String> map = new HashMap<>();
      String half_on = Util.null2String(rs.getString("half_on"));
      String half_off = Util.null2String(rs.getString("half_off"));
      String whole = Util.null2String(rs.getString("whole"));
      map.put("half_on", half_on);
      map.put("half_off", half_off);
      map.put("whole", whole);
      timeSelectionMap.put(key, map);
    }
    if(!timeSelectionMap.isEmpty()){
      CacheItem cacheItem = createCacheItem();
      for(Map.Entry me : timeSelectionMap.entrySet()){
        String id = Util.null2String(me.getKey());
        Object value = me.getValue();
        cacheItem.set(PK_INDEX, id);
        cacheItem.set(timeselection, value);
        modifyCacheItem(key, cacheItem);
      }
    }
    return null;
  }

  private String getId() {
    return (String) getRowValue(PK_INDEX);
  }

  private Map<String,String> getTimeselection() {
    return (Map<String,String>) getRowValue(timeselection);
  }

  private Map<String,String> getTimeselection(String key) {
    if(getObjValue(timeselection, key) != null){
      return (Map<String,String>) getObjValue(timeselection, key);
    }else{
      return new HashMap<>();
    }
  }

  /**
   *
   * @param selectiontype
   * @param leaveruleid 请假类型id，没有的话传0
   * @param minimumunit
   * @return
   */
  public Map<String,String> getTimeselections(String selectiontype, String leaveruleid, String minimumunit) {
    if(Util.null2String(selectiontype).length()==0||Util.null2String(minimumunit).length()==0){
      return new HashMap<>();
    }
    String key = Util.null2String(selectiontype)+"_"+Util.null2String(leaveruleid)+"_"+Util.null2String(minimumunit);
    if(Util.getIntValue(selectiontype) != KqSplitFlowTypeEnum.LEAVE.getFlowtype()){
      key = Util.null2String(selectiontype)+"_0_"+Util.null2String(minimumunit);
    }
    return this.getTimeselection(key);
  }

}
