package com.engine.kq.biz;

import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.chain.cominfo.HalfShiftComIndex;
import com.engine.kq.biz.chain.cominfo.RestShiftComIndex;
import com.engine.kq.biz.chain.cominfo.ShiftComIndex;
import com.engine.kq.biz.chain.cominfo.ShiftInfoCominfoBean;
import com.engine.kq.biz.chain.cominfo.WorkShiftComIndex;
import com.engine.kq.biz.chain.shiftinfo.ShiftInfoBean;
import com.engine.kq.cmd.shiftmanagement.toolkit.ShiftManagementToolKit;
import com.engine.kq.entity.TimeScopeEntity;
import com.engine.kq.log.KQLog;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.apache.commons.lang.math.NumberUtils;
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 KQShiftManagementComInfo 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 shiftbean;

  @CacheColumn
  protected static int color;

  @CacheColumn
  protected static int isoffdutyfreecheck;

  @CacheColumn
  protected static int shiftonoffworkcount;

  @CacheColumn
  protected static int serial;

  @CacheColumn
  protected static int isresttimeopen;

  @CacheColumn
  protected static int worktime;

  @CacheColumn
  protected static int punchsetting;

  @CacheColumn
  protected static int cardRemind;

  @CacheColumn
  protected static int cardRemOfSignIn;

  @CacheColumn
  protected static int minsBeforeSignIn;

  @CacheColumn
  protected static int cardRemOfSignOut;

  @CacheColumn
  protected static int minsAfterSignOut;

  @CacheColumn
  protected static int remindMode;

  @CacheColumn
  protected static int remindOnPC;

  @CacheColumn
  protected static int rest_shift;

  private KQLog kqLog = new KQLog();

  @Override
  public CacheItem initCache(String key) {
    if (key == null || "".equals(key.trim())) {
      return null;
    }
    CacheMap localData = createCacheMap();
    String sql = "";
    kqLog = new KQLog();
    ConcurrentHashMap<String, Object> resourceKQGroups = new ConcurrentHashMap<>();
    //考勤组优先级
    try {

      ConcurrentHashMap<String,Object> shiftInfoBeanMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> idMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> colorsMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> isoffdutyfreechecksMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> shiftonoffworkcountsMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> serialsMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> isresttimeopensMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> worktimesMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> punchsettingsMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> cardRemindMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> cardRemOfSignInMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> minsBeforeSignInMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> cardRemOfSignOutMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> minsAfterSignOutMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> remindModeMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> remindOnPCMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> restShiftMap = new ConcurrentHashMap<>();
      RecordSet rs = new RecordSet();
      String getShiftInfo  = "select * from kq_ShiftManagement where 1=1 and id="+key+" order by id ";
      rs.execute(getShiftInfo);
      while(rs.next()){
        String serialid = rs.getString("id");
        String isresttimeopen = rs.getString("isresttimeopen");
        String color = rs.getString("color");
        String shiftonoffworkcount = rs.getString("shiftonoffworkcount");
        String worktime = rs.getString("worktime");
        String serial = rs.getString("serial");
        String punchsetting = "1";
        String isoffdutyfreecheck = rs.getString("isoffdutyfreecheck");
        String cardRemind = rs.getString("cardRemind");
        String cardRemOfSignIn = rs.getString("cardRemOfSignIn");
        String minsBeforeSignIn = rs.getString("minsBeforeSignIn");
        String cardRemOfSignOut = rs.getString("cardRemOfSignOut");
        String minsAfterSignOut = rs.getString("minsAfterSignOut");
        String remindMode = rs.getString("remindMode");
        String remindOnPC = rs.getString("remindOnPC");
        String halfcalrule = rs.getString("halfcalrule");
        halfcalrule = Util.null2String(halfcalrule).length() == 0 ? "0" : halfcalrule;
        String halfcalpoint = rs.getString("halfcalpoint");
        String halfcalpoint2cross = rs.getString("halfcalpoint2cross");
        String restShift = rs.getString("rest_shift");

        getShiftInfoBean(serialid,isresttimeopen,worktime,punchsetting,shiftInfoBeanMap,halfcalrule,halfcalpoint,halfcalpoint2cross,restShift);

        idMap.put(serialid, serialid);
        colorsMap.put(serialid, color);
        isoffdutyfreechecksMap.put(serialid, isoffdutyfreecheck);
        shiftonoffworkcountsMap.put(serialid, shiftonoffworkcount);
        serialsMap.put(serialid, serial);
        isresttimeopensMap.put(serialid, isresttimeopen);
        worktimesMap.put(serialid, worktime);
        punchsettingsMap.put(serialid, punchsetting);
        cardRemindMap.put(serialid,cardRemind);
        cardRemOfSignInMap.put(serialid,cardRemOfSignIn);
        minsBeforeSignInMap.put(serialid,minsBeforeSignIn);
        cardRemOfSignOutMap.put(serialid,cardRemOfSignOut);
        minsAfterSignOutMap.put(serialid,minsAfterSignOut);
        remindModeMap.put(serialid,remindMode);
        remindOnPCMap.put(serialid,remindOnPC);
        restShiftMap.put(serialid,restShift);
      }


      if(shiftInfoBeanMap.size()>0){
        CacheItem cacheItem = createCacheItem();
        Iterator<Entry<String, Object>> iterator = shiftInfoBeanMap.entrySet().iterator();
        while (iterator.hasNext()) {
          Map.Entry<String, Object> entry = iterator.next();
          String id = entry.getKey();
          Object value = entry.getValue();
          cacheItem.set(PK_INDEX, id);
          cacheItem.set(shiftbean, value);
          cacheItem.set(isoffdutyfreecheck, isoffdutyfreechecksMap.get(id));
          cacheItem.set(shiftonoffworkcount, shiftonoffworkcountsMap.get(id));
          cacheItem.set(serial, serialsMap.get(id));
          cacheItem.set(isresttimeopen, isresttimeopensMap.get(id));
          cacheItem.set(worktime, worktimesMap.get(id));
          cacheItem.set(punchsetting, punchsettingsMap.get(id));
          cacheItem.set(cardRemind,cardRemindMap.get(id));
          cacheItem.set(cardRemOfSignIn,cardRemOfSignInMap.get(id));
          cacheItem.set(minsBeforeSignIn,minsBeforeSignInMap.get(id));
          cacheItem.set(cardRemOfSignOut,cardRemOfSignOutMap.get(id));
          cacheItem.set(minsAfterSignOut,minsAfterSignOutMap.get(id));
          cacheItem.set(remindMode,remindModeMap.get(id));
          cacheItem.set(remindOnPC,remindOnPCMap.get(id));
          cacheItem.set(rest_shift,restShiftMap.get(id));
          modifyCacheItem(id, cacheItem);
        }
        return cacheItem;
      }
    } catch (Exception e) {
      kqLog.info(e);
    }
    return null;
  }

  @Override
  public CacheMap initCache() {
    CacheMap localData = createCacheMap();
    String sql = "";
    kqLog = new KQLog();
    ConcurrentHashMap<String, Object> resourceKQGroups = new ConcurrentHashMap<>();
    //考勤组优先级
    try {

      ConcurrentHashMap<String,Object> shiftInfoBeanMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> idMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> colorsMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> isoffdutyfreechecksMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> shiftonoffworkcountsMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> serialsMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> isresttimeopensMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> worktimesMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> punchsettingsMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> cardRemindMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> cardRemOfSignInMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> minsBeforeSignInMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> cardRemOfSignOutMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> minsAfterSignOutMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> remindModeMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> remindOnPCMap = new ConcurrentHashMap<>();
      ConcurrentHashMap<String,Object> restShiftMap = new ConcurrentHashMap<>();
      RecordSet rs = new RecordSet();
      String getShiftInfo  = "select * from kq_ShiftManagement where 1=1 order by id ";
      rs.execute(getShiftInfo);
      while(rs.next()){
        String serialid = rs.getString("id");
        String isresttimeopen = rs.getString("isresttimeopen");
        String color = rs.getString("color");
        String shiftonoffworkcount = rs.getString("shiftonoffworkcount");
        String worktime = rs.getString("worktime");
        String serial = rs.getString("serial");
        String punchsetting = "1";
        String isoffdutyfreecheck = rs.getString("isoffdutyfreecheck");
        String cardRemind = rs.getString("cardRemind");
        String cardRemOfSignIn = rs.getString("cardRemOfSignIn");
        String minsBeforeSignIn = rs.getString("minsBeforeSignIn");
        String cardRemOfSignOut = rs.getString("cardRemOfSignOut");
        String minsAfterSignOut = rs.getString("minsAfterSignOut");
        String remindMode = rs.getString("remindMode");
        String remindOnPC = rs.getString("remindOnPC");
        String halfcalrule = rs.getString("halfcalrule");
        halfcalrule = Util.null2String(halfcalrule).length() == 0 ? "0" : halfcalrule;
        String halfcalpoint = rs.getString("halfcalpoint");
        String halfcalpoint2cross = rs.getString("halfcalpoint2cross");
        String restShift = rs.getString("rest_shift");

        getShiftInfoBean(serialid,isresttimeopen,worktime,punchsetting,shiftInfoBeanMap,halfcalrule,halfcalpoint,halfcalpoint2cross,restShift);

        idMap.put(serialid, serialid);
        colorsMap.put(serialid, color);
        isoffdutyfreechecksMap.put(serialid, isoffdutyfreecheck);
        shiftonoffworkcountsMap.put(serialid, shiftonoffworkcount);
        serialsMap.put(serialid, serial);
        isresttimeopensMap.put(serialid, isresttimeopen);
        worktimesMap.put(serialid, worktime);
        punchsettingsMap.put(serialid, punchsetting);
        cardRemindMap.put(serialid,cardRemind);
        cardRemOfSignInMap.put(serialid,cardRemOfSignIn);
        minsBeforeSignInMap.put(serialid,minsBeforeSignIn);
        cardRemOfSignOutMap.put(serialid,cardRemOfSignOut);
        minsAfterSignOutMap.put(serialid,minsAfterSignOut);
        remindModeMap.put(serialid,remindMode);
        remindOnPCMap.put(serialid,remindOnPC);
        restShiftMap.put(serialid,restShift);
      }


      if(shiftInfoBeanMap.size()>0){
        Iterator<Entry<String, Object>> iterator = shiftInfoBeanMap.entrySet().iterator();
        while (iterator.hasNext()) {
          Map.Entry<String, Object> entry = iterator.next();
          String id = entry.getKey();
          Object value = entry.getValue();
          CacheItem cacheItem = createCacheItem();
          cacheItem.set(PK_INDEX, id);
          cacheItem.set(shiftbean, value);
          cacheItem.set(isoffdutyfreecheck, isoffdutyfreechecksMap.get(id));
          cacheItem.set(shiftonoffworkcount, shiftonoffworkcountsMap.get(id));
          cacheItem.set(serial, serialsMap.get(id));
          cacheItem.set(isresttimeopen, isresttimeopensMap.get(id));
          cacheItem.set(worktime, worktimesMap.get(id));
          cacheItem.set(punchsetting, punchsettingsMap.get(id));
          cacheItem.set(cardRemind,cardRemindMap.get(id));
          cacheItem.set(cardRemOfSignIn,cardRemOfSignInMap.get(id));
          cacheItem.set(minsBeforeSignIn,minsBeforeSignInMap.get(id));
          cacheItem.set(cardRemOfSignOut,cardRemOfSignOutMap.get(id));
          cacheItem.set(minsAfterSignOut,minsAfterSignOutMap.get(id));
          cacheItem.set(remindMode,remindModeMap.get(id));
          cacheItem.set(remindOnPC,remindOnPCMap.get(id));
          cacheItem.set(rest_shift,restShiftMap.get(id));
          modifyCacheItem(id, cacheItem);
          localData.put(id, cacheItem);
        }
      }
    } catch (Exception e) {
      StringWriter errorsWriter = new StringWriter();
      e.printStackTrace(new PrintWriter(errorsWriter));
      kqLog.info(errorsWriter.toString());
    }
    return localData;
  }

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

  public String getColor() { return (String)getRowValue(color); }

  public String getColor(String key)
  {
    return (String)getValue(color,key);
  }

  public String getIsoffdutyfreecheck() { return (String)getRowValue(isoffdutyfreecheck); }

  public String getIsoffdutyfreecheck(String key)
  {
    return (String)getValue(isoffdutyfreecheck,key);
  }

  public String getShiftonoffworkcounts() { return (String)getRowValue(shiftonoffworkcount); }

  public String getShiftonoffworkcounts(String key)
  {
    return (String)getValue(shiftonoffworkcount,key);
  }

  public String getSerial() { return (String)getRowValue(serial); }

  public String getSerial(String key)
  {
    return (String)getValue(serial,key);
  }

  public String getIsresttimeopen() { return (String)getRowValue(isresttimeopen); }

  public String getIsresttimeopen(String key)
  {
    return (String)getValue(isresttimeopen,key);
  }

  public String getWorktime() { return (String)getRowValue(worktime); }

  public String getWorktime(String key)
  {
    return (String)getValue(worktime,key);
  }

  public String getPunchsetting() {
    return (String) getRowValue(punchsetting);
  }

  public String getPunchsetting(String key) {
    return (String) getValue(punchsetting, key);
  }

  public String getCardRemind() {
    return (String) getRowValue(cardRemind);
  }

  public String getCardRemind(String key) {
    return (String) getValue(cardRemind,key);
  }

  public String getCardRemOfSignIn() {
    return (String) getRowValue(cardRemOfSignIn);
  }

  public String getCardRemOfSignIn(String key) {
    return (String) getValue(cardRemOfSignIn, key);
  }

  public String getMinsBeforeSignIn() {
    return (String) getRowValue(minsBeforeSignIn);
  }

  public String getMinsBeforeSignIn(String key) {
    return (String) getValue(minsBeforeSignIn, key);
  }

  public String getCardRemOfSignOut() {
    return (String) getRowValue(cardRemOfSignOut);
  }

  public String getCardRemOfSignOut(String key) {
    return (String) getValue(cardRemOfSignOut, key);
  }

  public String getMinsAfterSignOut() {
    return (String) getRowValue(minsAfterSignOut);
  }

  public String getMinsAfterSignOut(String key) {
    return (String) getValue(minsAfterSignOut, key);
  }

  public String getRemindMode() {
    return (String) getRowValue(remindMode);
  }

  public String getRemindMode(String key) {
    return (String) getValue(remindMode, key);
  }

  public String getRemindOnPC() {
    return (String) getRowValue(remindOnPC);
  }

  public String getRemindOnPC(String key) {
    return (String) getValue(remindOnPC, key);
  }

	public String getRestShift() {
	    return (String) getRowValue(rest_shift);
	}

	public String getRestShift(String key) {
		return (String) getValue(rest_shift, key);
	}

  private void getShiftInfoBean(String serialid, String isresttimeopen, String worktime,
                                String punchsettings,
                                ConcurrentHashMap<String, Object> serialMap,
      String halfcalrule,String halfcalpoint,String halfcalpoint2cross, String restShift) throws Exception {

    Map<String,Object> workTimeMap = new HashMap<>();
    int workmins = 0;
    List<Object> workTimes = Collections.synchronizedList(new ArrayList<>());
    List<Object> restTimes = Collections.synchronizedList(new ArrayList<>());
    KQShiftOnOffWorkSectionComInfo kqShiftOnOffWorkSectionComInfo = new KQShiftOnOffWorkSectionComInfo();
    KQShiftRestTimeSectionComInfo kqShiftRestTimeSectionComInfo = new KQShiftRestTimeSectionComInfo();

    workTimes = kqShiftOnOffWorkSectionComInfo.getWorkSectionTimes(serialid);
    if(workTimes != null && !workTimes.isEmpty()){
      if("1".equalsIgnoreCase(isresttimeopen)) {
        //如果开启了才去判断
        restTimes = kqShiftRestTimeSectionComInfo.getRestSectionTimes(serialid);
      }
      if(NumberUtils.isNumber(worktime)){
        if(worktime.indexOf('.') == -1){
          workmins = Util.getIntValue(worktime,0);
        }else {
          worktime = worktime.substring(0,worktime.indexOf('.'));
          workmins = Util.getIntValue(worktime,0);
        }

      }else{
        workmins = 0;
        kqLog.info("班次有问题，serialid:"+serialid+"工作时长为:"+worktime);
      }
      workTimeMap.put("workTime", workTimes);
      workTimeMap.put("restTime", restTimes);
      workTimeMap.put("serialid", serialid);
      //工作时长分钟数
      workTimeMap.put("workmins", workmins+"");
      workTimeMap.put("punchsettings", punchsettings);
      workTimeMap.put("isresttimeopen", isresttimeopen);
      workTimeMap.put("halfcalrule", halfcalrule);
      workTimeMap.put("halfcalpoint", halfcalpoint);
      workTimeMap.put("halfcalpoint2cross", halfcalpoint2cross);
      workTimeMap.put("restShift", restShift.equals("1") ? "1" : "0");

      ShiftInfoCominfoBean shiftInfoCominfoBean = setShiftInfoBean(workTimeMap);
      serialMap.put(serialid, shiftInfoCominfoBean);
    }

  }

  private ShiftInfoCominfoBean setShiftInfoBean(Map<String,Object> workTimeMap) throws Exception {

    ShiftComIndex workComIndex = new WorkShiftComIndex(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005306,weaver.general.ThreadVarLanguage.getLang())+"",workTimeMap);
    ShiftComIndex restComIndex = new RestShiftComIndex(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005307,weaver.general.ThreadVarLanguage.getLang())+"",workTimeMap);
    ShiftComIndex halfComIndex = new HalfShiftComIndex(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005308,weaver.general.ThreadVarLanguage.getLang())+"",workTimeMap);
    //创建执行链
    //前一天的都执行完了，去获取当天工作时段，再执行半天的规则
    workComIndex.setDuration(restComIndex);
    //执行完了半天的规则，再最后判断休息的时段
    restComIndex.setDuration(halfComIndex);

    ShiftInfoCominfoBean shiftInfoCominfoBean = new ShiftInfoCominfoBean();

    workComIndex.handleDuration(shiftInfoCominfoBean);
    return shiftInfoCominfoBean;
  }

  /**
   * 考勤按钮
   * @param workdate
   * @param serialidInfo
   * @param containYesterday
   * @return
   */
  public Map<String,Object> getWorkButton(String workdate,Map<String,Object> serialidInfo, boolean containYesterday){
    Map<String,Object> shiftMap = new HashMap<>();

    //考勤按钮还是走缓存
    KQShiftManagementRedis kqShiftManagementRedis = new KQShiftManagementRedis();
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    if(serialidInfo != null && !serialidInfo.isEmpty()){
      LocalDate preLocalDate = LocalDate.parse(workdate);
      String preSplitDate = "";
      preLocalDate = preLocalDate.minusDays(1);
      preSplitDate = preLocalDate.format(dateFormatter);
      String serialid = Util.null2String(serialidInfo.get(workdate));
      String preSerialid = Util.null2String(serialidInfo.get(preSplitDate));
      if(containYesterday){
        if(Util.getIntValue(preSerialid) > 0){
          ShiftInfoCominfoBean pre_shiftInfoCominfoBean = kqShiftManagementRedis.getShiftInfoBean(preSerialid);
          List<Object> timelineList = pre_shiftInfoCominfoBean.getTimelineList();
          if(timelineList!=null && !timelineList.isEmpty()){
            shiftMap.put("pre_timelineList", timelineList);
          }
          shiftMap.put("pre_isAcross", pre_shiftInfoCominfoBean.getIsAcross());
          shiftMap.put("pre_allWorkTime", pre_shiftInfoCominfoBean.getAllWorkTime());
          shiftMap.put("pre_allAcrossWorkTime", pre_shiftInfoCominfoBean.getAllAcrossWorkTime());
          shiftMap.put("pre_signTime", pre_shiftInfoCominfoBean.getSignWorkTime());
          shiftMap.put("pre_restTime", pre_shiftInfoCominfoBean.getRestAcrossLongTime());
        }
      }
      if(Util.getIntValue(serialid) > 0){
        ShiftInfoCominfoBean cur_shiftInfoCominfoBean = kqShiftManagementRedis.getShiftInfoBean(serialid);
        Map<String,Object> shiftRuleMap = ShiftManagementToolKit.getShiftRuleInfo(serialid,false);
        List<Object> timelineList = cur_shiftInfoCominfoBean.getTimelineList();
        if(timelineList!=null && !timelineList.isEmpty()){
          shiftMap.put("timelineList", timelineList);
        }
        shiftMap.put("isAcross", cur_shiftInfoCominfoBean.getIsAcross());
        shiftMap.put("allWorkTime", cur_shiftInfoCominfoBean.getAllWorkTime());
        shiftMap.put("allAcrossWorkTime", cur_shiftInfoCominfoBean.getAllAcrossWorkTime());
        shiftMap.put("signTime", cur_shiftInfoCominfoBean.getSignWorkTime());
        shiftMap.put("restTime", cur_shiftInfoCominfoBean.getRestAcrossLongTime());
        if(shiftRuleMap != null && !shiftRuleMap.isEmpty()){
          shiftMap.put("shiftRuleMap", shiftRuleMap);
        }
      }
    }

    return shiftMap;
  }

  /**
   * 流程计算用的
   * @param workdate
   * @param serialidInfo
   * @param containYesterday
   * @return
   * shiftInfoBean 每一天内含有的班次都拆分出来,包含昨天班次在今天的时段0-24小时制的
   * shiftLongInfoBean 当天的班次数据，0-48小时制度的
   */
  public Map<String,Object> getWorkDuration(String workdate,Map<String,Object> serialidInfo,boolean containYesterday){

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    Map<String,Object> dateWorkTimeMap = new HashMap<>();
    LocalDate preLocalDate = LocalDate.parse(workdate);
    String preSplitDate = "";
    preLocalDate = preLocalDate.minusDays(1);
    preSplitDate = preLocalDate.format(dateFormatter);
    if(serialidInfo != null && !serialidInfo.isEmpty()){
      String preSerialid = Util.null2String(serialidInfo.get(preSplitDate));
      String serialid = Util.null2String(serialidInfo.get(workdate));
      ShiftInfoBean shiftInfoBean = new ShiftInfoBean();
      shiftInfoBean.setPreSplitDate(preSplitDate);
      shiftInfoBean.setSplitDate(workdate);
      shiftInfoBean.setD_Mins(0.0);
      shiftInfoBean.setPreSerialid(preSerialid);
      shiftInfoBean.setSerialid(serialid);
      if(containYesterday){
        if(Util.getIntValue(serialid) > 0 || Util.getIntValue(preSerialid) > 0){
          fillShiftInfoBean(shiftInfoBean, preSerialid, serialid);
          dateWorkTimeMap.put("shiftInfoBean", shiftInfoBean);
        }
      }else{
        if(Util.getIntValue(serialid) > 0){
          fillShiftInfoBean(shiftInfoBean, preSerialid, serialid);
          dateWorkTimeMap.put("shiftInfoBean", shiftInfoBean);
        }
      }
    }

    return dateWorkTimeMap;
  }

  /**
   * 根据班次id获取对应日期的上下班时间+休息时间
   * @param workdate
   * @param serialidInfo
   * @return
   */
  public Map<String,Object> getWorkTimeMap(String workdate,Map<String,Object> serialidInfo){

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    Map<String,Object> dateWorkTimeMap = new HashMap<>();
    LocalDate preLocalDate = LocalDate.parse(workdate);
    String preSplitDate = "";
    preLocalDate = preLocalDate.minusDays(1);
    preSplitDate = preLocalDate.format(dateFormatter);
    if(serialidInfo != null && !serialidInfo.isEmpty()){
      String preSerialid = Util.null2String(serialidInfo.get(preSplitDate));
      String serialid = Util.null2String(serialidInfo.get(workdate));
      getShiftInfoBeanMap(dateWorkTimeMap,preSerialid, serialid);
    }

    return dateWorkTimeMap;
  }

  /**
   * 根据考勤报表需要的 返回相应格式的数据
   * @param dateWorkTimeMap
   * @param preSerialid
   * @param serialid
   */
  private void getShiftInfoBeanMap(Map<String,Object> dateWorkTimeMap,String preSerialid, String serialid) {
    ShiftInfoCominfoBean cur_shiftInfoCominfoBean = getShiftInfoCominfoBean(serialid);

    if(cur_shiftInfoCominfoBean != null){
      dateWorkTimeMap.put("restTime",cur_shiftInfoCominfoBean.getRestAcrossLongTime());
      dateWorkTimeMap.put("workTime",cur_shiftInfoCominfoBean.getWorkAcrossLongTime());
      dateWorkTimeMap.put("workMins",cur_shiftInfoCominfoBean.getWorkmins());
      dateWorkTimeMap.put("isAcross",cur_shiftInfoCominfoBean.getIsAcross());
      dateWorkTimeMap.put("signTime", cur_shiftInfoCominfoBean.getSignWorkTime());
    }
  }

  /**
   * 每次获取的话都需要获取昨天和今天的班次数据
   * @param shiftInfoBean
   * @param preSerialid 昨天的班次
   * @param serialid 今天的班次
   */
  private void fillShiftInfoBean(ShiftInfoBean shiftInfoBean,String preSerialid,String serialid){
    ShiftInfoCominfoBean pre_shiftInfoCominfoBean = getShiftInfoCominfoBean(preSerialid);
    ShiftInfoCominfoBean cur_shiftInfoCominfoBean = getShiftInfoCominfoBean(serialid);
    Map<String,Object> pre_shiftRuleMap = ShiftManagementToolKit.getShiftRuleInfo(preSerialid,false);
    Map<String,Object> shiftRuleMap = ShiftManagementToolKit.getShiftRuleInfo(serialid,false);

    //把前一个班次的填充好
    if(pre_shiftInfoCominfoBean != null){
      shiftInfoBean.setPreWorkIndex(pre_shiftInfoCominfoBean.getPreWorkTimeIndex());
      shiftInfoBean.setPreWorkAcrossIndex(pre_shiftInfoCominfoBean.getWorkAcrossTimeIndex());
      shiftInfoBean.setPreWorkMinsAcrossIndex(pre_shiftInfoCominfoBean.getWorkPunchMins());
      shiftInfoBean.setPreRestIndex(pre_shiftInfoCominfoBean.getPreRestTimeIndex());
      shiftInfoBean.setPreHalfWorkIndex(pre_shiftInfoCominfoBean.getHalfWorkIndex());
      shiftInfoBean.setPreWorkmins(pre_shiftInfoCominfoBean.getWorkmins());
      shiftInfoBean.setPreAllWorkTime(pre_shiftInfoCominfoBean.getAllWorkTime());
      shiftInfoBean.setPreAllAcrossWorkTime(pre_shiftInfoCominfoBean.getAllAcrossWorkTime());
      shiftInfoBean.setPreAllRestTime(pre_shiftInfoCominfoBean.getAllRestTime());
      shiftInfoBean.setPreAllAcrossRestTime(pre_shiftInfoCominfoBean.getAllAcrossRestTime());
      shiftInfoBean.setPreSerialid(preSerialid);
      shiftInfoBean.setIsPreAcross(pre_shiftInfoCominfoBean.getIsAcross());
      shiftInfoBean.setShiftRuleMap(pre_shiftRuleMap);
    }

    //把当前班次的填充好
    if(cur_shiftInfoCominfoBean != null){
      shiftInfoBean.setWorkIndex(cur_shiftInfoCominfoBean.getWorkTimeIndex());
      shiftInfoBean.setWorkAcrossIndex(cur_shiftInfoCominfoBean.getWorkAcrossTimeIndex());
      shiftInfoBean.setWorkMinsAcrossIndex(cur_shiftInfoCominfoBean.getWorkPunchMins());
      shiftInfoBean.setRestIndex(cur_shiftInfoCominfoBean.getRestTimeIndex());
      shiftInfoBean.setHalfWorkIndex(cur_shiftInfoCominfoBean.getHalfWorkIndex());
      shiftInfoBean.setWorkmins(cur_shiftInfoCominfoBean.getWorkmins());
      shiftInfoBean.setAllWorkTime(cur_shiftInfoCominfoBean.getAllWorkTime());
      shiftInfoBean.setAllAcrossWorkTime(cur_shiftInfoCominfoBean.getAllAcrossWorkTime());
      shiftInfoBean.setAllRestTime(cur_shiftInfoCominfoBean.getAllRestTime());
      shiftInfoBean.setAllAcrossRestTime(cur_shiftInfoCominfoBean.getAllAcrossRestTime());
      shiftInfoBean.setAllLongWorkTime(cur_shiftInfoCominfoBean.getAllLongWorkTime());
      shiftInfoBean.setRestLongTimeIndex(cur_shiftInfoCominfoBean.getRestLongTimeIndex());
      shiftInfoBean.setWorkLongTimeIndex(cur_shiftInfoCominfoBean.getWorkLongTimeIndex());
      shiftInfoBean.setIsAcross(cur_shiftInfoCominfoBean.getIsAcross());
      shiftInfoBean.setSerialid(serialid);
      shiftInfoBean.setShiftRuleMap(shiftRuleMap);

      if(cur_shiftInfoCominfoBean.getWorkAcrossLongTime() != null){
        List<Object> workTime1 = cur_shiftInfoCominfoBean.getWorkAcrossLongTime().stream().collect(Collectors.toList());
        List<TimeScopeEntity> lsWorkTime = new KQWorkTime().formatTimeScope(workTime1, false);
        shiftInfoBean.setWorkTime(lsWorkTime);
      }
      if(cur_shiftInfoCominfoBean.getSignWorkTime() != null){
        List<Object> signTime1 = cur_shiftInfoCominfoBean.getSignWorkTime().stream().collect(Collectors.toList());
        List<TimeScopeEntity> lsSignTime = new KQWorkTime().formatTimeScope(signTime1, true);
        shiftInfoBean.setSignTime(lsSignTime);
      }
      shiftInfoBean.setHalfcalrule(cur_shiftInfoCominfoBean.getHalfcalrule());
      shiftInfoBean.setHalfcalpoint(cur_shiftInfoCominfoBean.getHalfcalpoint());
      shiftInfoBean.setHalfcalpoint2cross(cur_shiftInfoCominfoBean.getHalfcalpoint2cross());
      shiftInfoBean.setWorkAcrossTime(cur_shiftInfoCominfoBean.getWorkAcrossTime());
      shiftInfoBean.setEachWorkMins(cur_shiftInfoCominfoBean.getEachWorkMins());
    }
  }

  public void removeShiftManagementCache(){
    KQShiftRestTimeSectionComInfo kqShiftRestTimeSectionComInfo = new KQShiftRestTimeSectionComInfo();
    KQShiftOnOffWorkSectionComInfo kqShiftOnOffWorkSectionComInfo = new KQShiftOnOffWorkSectionComInfo();
    kqShiftRestTimeSectionComInfo.removeShiftRestTimeSectionCache();
    kqShiftOnOffWorkSectionComInfo.removeShiftWorkSectionCache();
    super.removeCache();
    //改用redis和staticobj的方式
    KQShiftManagementRedis kqShiftManagementRedis = new KQShiftManagementRedis();
    kqShiftManagementRedis.resetShiftValWithRedis();
  }

  /**
   * 根据班次获取shiftInfoCominfoBean
   * @param serialid
   * @return
   */
  public ShiftInfoCominfoBean getShiftInfoCominfoBean(String serialid){
//    Object object = getObjValue(shiftbean, serialid);
//    if(object != null){
//      return (ShiftInfoCominfoBean) object;
//    }else{
//      return null;
//    }
    //改用redis和staticobj的方式
    KQShiftManagementRedis kqShiftManagementRedis = new KQShiftManagementRedis();
    return kqShiftManagementRedis.getShiftInfoBeanMapBySql(serialid);
  }

  /**
   * 根据人员和日期获取shiftInfoCominfoBean
   * @param workdate
   * @param serialidInfo
   * @return
   */
  public ShiftInfoCominfoBean getShiftInfoCominfoBean(String workdate,Map<String,Object> serialidInfo){
    ShiftInfoCominfoBean shiftInfoCominfoBean = null;
    if(serialidInfo != null && !serialidInfo.isEmpty()){
      String serialid = Util.null2String(serialidInfo.get(workdate));
      if(Util.getIntValue(serialid) > 0){
        shiftInfoCominfoBean = getShiftInfoCominfoBean(serialid);
      }
    }
    return shiftInfoCominfoBean;
  }

}
