package com.engine.kq.biz;

import com.alibaba.fastjson.JSON;
import com.engine.kq.log.KQLog;
import weaver.cache.*;
import weaver.conn.RecordSet;
import weaver.general.Util;

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

/**
 * 班次一天内上下班次数缓存类
 */
public class KQShiftOnOffWorkSectionComInfo 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<String> serials = new ArrayList<>();
      List<Object> workSectionList = new ArrayList<>();
      List<Object> sectionList = new ArrayList<>();
      Map<String,Object> serialMaps = new HashMap<>();//分组用的map
      Map<String,Object> groupSectionMaps = new HashMap<>();//分组用的map
      Map<String,Object> sectionMap = new HashMap<>();
      String serialidold = "";

      String getStartWorkSections = "select * from kq_ShiftOnOffWorkSections where 1=1 order by serialid,record ";
      rs.executeQuery(getStartWorkSections);
      while (rs.next()) {
        String id = Util.null2String(rs.getString("id"));
        String serialid = Util.null2String(rs.getString("serialid"));
        String across = Util.null2String(rs.getString("across"));
        String times = Util.null2String(rs.getString("times"));
        String mins = Util.null2String(rs.getString("mins"));
        //增加了打卡规则，每一个班次的时间点都有允许打卡开始时间和结束时间，这个_next就是和mins相对应的打卡开始和结束时间
        String mins_next = Util.null2s(rs.getString("mins_next"),"-1");
        String record = Util.null2String(rs.getString("record"));
        String onoffworktype = Util.null2String(rs.getString("onoffworktype"));

        sectionMap = new HashMap<>();
        sectionMap.put("across", across);
        sectionMap.put("times", times);
        sectionMap.put("mins", mins);
        sectionMap.put("mins_next", mins_next);

        if(serialMaps.get(serialid) != null){
          Map<String,Object> tmpgroupSectionMaps = (Map<String,Object>)serialMaps.get(serialid);
          if(tmpgroupSectionMaps.get(record) != null){
            List<Object> tmpWorkSection = (List<Object>) tmpgroupSectionMaps.get(record);
            List<Object> tmpSection = ((List<Object>)tmpWorkSection.get(tmpWorkSection.size()-1));
            if("start".equalsIgnoreCase(onoffworktype)){
              tmpSection.set(0, sectionMap);
            }else if("end".equalsIgnoreCase(onoffworktype)){
              tmpSection.set(1, sectionMap);
            }
          }else{
            workSectionList = new ArrayList<>();
            sectionList = new ArrayList<>();
            sectionList.add("");
            sectionList.add("");
            if("start".equalsIgnoreCase(onoffworktype)){
              sectionList.set(0, sectionMap);
            }else if("end".equalsIgnoreCase(onoffworktype)){
              sectionList.set(1, sectionMap);
            }
            workSectionList.add(sectionList);
            tmpgroupSectionMaps.put(record, workSectionList);
          }
        }else{
          groupSectionMaps = new HashMap<>();
          workSectionList = new ArrayList<>();
          serialMaps.put(serialid, groupSectionMaps);
          if(groupSectionMaps.get(record) != null){
            List<Object> tmpWorkSection = (List<Object>) groupSectionMaps.get(record);
            List<Object> tmpSection = ((List<Object>)tmpWorkSection.get(tmpWorkSection.size()-1));
            if("start".equalsIgnoreCase(onoffworktype)){
              tmpSection.set(0, sectionMap);
            }else if("end".equalsIgnoreCase(onoffworktype)){
              tmpSection.set(1, sectionMap);
            }
          }else{
            sectionList = new ArrayList<>();
            sectionList.add("");
            sectionList.add("");
            if("start".equalsIgnoreCase(onoffworktype)){
              sectionList.set(0, sectionMap);
            }else if("end".equalsIgnoreCase(onoffworktype)){
              sectionList.set(1, sectionMap);
            }
            workSectionList.add(sectionList);
            groupSectionMaps.put(record, workSectionList);
          }
        }
      }

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

  /**
   * 根据班次id获取工作时段
   * @param serialid
   * @return
   */
  public List<Object> getWorkSectionTimes(String serialid){
    //return (List<Object>) getObjValue(serial, serialid);
    RecordSet rs = new RecordSet();
    kqLog = new KQLog();
    List<Object> valList = new ArrayList<>();
    List<Object> workSectionList = new ArrayList<>();
    try {
      List<String> serials = new ArrayList<>();
      List<Object> sectionList = new ArrayList<>();
      Map<String,Object> serialMaps = new HashMap<>();//分组用的map
      Map<String,Object> groupSectionMaps = new HashMap<>();//分组用的map
      Map<String,Object> sectionMap = new HashMap<>();
      String serialidold = "";

      String getStartWorkSections = "select * from kq_ShiftOnOffWorkSections where serialid=? order by serialid,record ";
      rs.executeQuery(getStartWorkSections,serialid);
      while (rs.next()) {
        String id = Util.null2String(rs.getString("id"));
        //String serialid = Util.null2String(rs.getString("serialid"));
        String across = Util.null2String(rs.getString("across"));
        String times = Util.null2String(rs.getString("times"));
        String mins = Util.null2String(rs.getString("mins"));
        //增加了打卡规则，每一个班次的时间点都有允许打卡开始时间和结束时间，这个_next就是和mins相对应的打卡开始和结束时间
        String mins_next = Util.null2s(rs.getString("mins_next"),"-1");
        String record = Util.null2String(rs.getString("record"));
        String onoffworktype = Util.null2String(rs.getString("onoffworktype"));

        sectionMap = new HashMap<>();
        sectionMap.put("across", across);
        sectionMap.put("times", times);
        sectionMap.put("mins", mins);
        sectionMap.put("mins_next", mins_next);

        if(serialMaps.get(serialid) != null){
          Map<String,Object> tmpgroupSectionMaps = (Map<String,Object>)serialMaps.get(serialid);
          if(tmpgroupSectionMaps.get(record) != null){
            List<Object> tmpWorkSection = (List<Object>) tmpgroupSectionMaps.get(record);
            List<Object> tmpSection = ((List<Object>)tmpWorkSection.get(tmpWorkSection.size()-1));
            if("start".equalsIgnoreCase(onoffworktype)){
              tmpSection.set(0, sectionMap);
            }else if("end".equalsIgnoreCase(onoffworktype)){
              tmpSection.set(1, sectionMap);
            }
          }else{
            workSectionList = new ArrayList<>();
            sectionList = new ArrayList<>();
            sectionList.add("");
            sectionList.add("");
            if("start".equalsIgnoreCase(onoffworktype)){
              sectionList.set(0, sectionMap);
            }else if("end".equalsIgnoreCase(onoffworktype)){
              sectionList.set(1, sectionMap);
            }
            workSectionList.add(sectionList);
            tmpgroupSectionMaps.put(record, workSectionList);
          }
        }else{
          groupSectionMaps = new HashMap<>();
          workSectionList = new ArrayList<>();
          serialMaps.put(serialid, groupSectionMaps);
          if(groupSectionMaps.get(record) != null){
            List<Object> tmpWorkSection = (List<Object>) groupSectionMaps.get(record);
            List<Object> tmpSection = ((List<Object>)tmpWorkSection.get(tmpWorkSection.size()-1));
            if("start".equalsIgnoreCase(onoffworktype)){
              tmpSection.set(0, sectionMap);
            }else if("end".equalsIgnoreCase(onoffworktype)){
              tmpSection.set(1, sectionMap);
            }
          }else{
            sectionList = new ArrayList<>();
            sectionList.add("");
            sectionList.add("");
            if("start".equalsIgnoreCase(onoffworktype)){
              sectionList.set(0, sectionMap);
            }else if("end".equalsIgnoreCase(onoffworktype)){
              sectionList.set(1, sectionMap);
            }
            workSectionList.add(sectionList);
            groupSectionMaps.put(record, workSectionList);
          }
        }
      }

      for(Map.Entry<String,Object> me : serialMaps.entrySet()){
        valList = new ArrayList<>();
        String id = me.getKey();
        Map<String,Object> val = (Map<String,Object>)me.getValue();
        for(Map.Entry<String,Object> mee : val.entrySet()){
          valList.addAll((List<Object>)mee.getValue());
        }
      }

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

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