package com.engine.kq.wfset.job;

import com.engine.kq.biz.KQShiftManagementComInfo;
import com.engine.kq.biz.KQTimesArrayComInfo;
import com.engine.kq.wfset.util.KQ122Util;
import com.engine.kq.wfset.util.KQAttFlowCheckUtil;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import weaver.conn.RecordSet;

/**
 * E9 考勤 时间(小时:分钟)和数组下标对应表
 */
public class HrmTimesArrayJob {

	/**
	 * 脚本更新后运行
	 */
	public static void goJob() {
	  boolean hasData = checkHasData();
	  if(!hasData){
      tableDataSync();
    }
    //在启动的时候初始化一下
    KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();
    KQShiftManagementComInfo kqShiftManagementComInfo = new KQShiftManagementComInfo();
    KQAttFlowCheckUtil kqAttFlowCheckUtil = new KQAttFlowCheckUtil();
    kqAttFlowCheckUtil.initLeaveUnit();
    //122考勤非标是否开启 开启过就永远开启了
    new KQ122Util().is122Open();
	}

  /**
   * 检查是否已经有表数据了
   * @return
   */
  private static boolean checkHasData() {
    String checkSql = "select count(1) from kq_timesarray";
    RecordSet rs = new RecordSet();
    int count = 0;
    rs.execute(checkSql);
    if(rs.next()){
      count  = rs.getInt(1);
    }
    boolean hasdata = (count == 2881);
    if(!hasdata){
      rs.execute("delete from kq_timesarray");
    }

    return hasdata;
  }
  /**
   * 初始化表数据
   */
  private static void tableDataSync() {
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();

    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
    LocalTime localTime = LocalTime.parse("00:00",dateTimeFormatter);
    int size = 1440;
    String insertSql = "insert into kq_timesarray(times,arrayindex) values(?,?) ";
    List<List> insertParamsList= new ArrayList<List>();

    int index = 0;
    for(int i = 0 ; i < 2;i++){
      if(i == 0){
        for(int j = 0 ; j < size ;j ++){
          LocalTime localTime0 = localTime.plusMinutes(j);
          String time = localTime0.format(dateTimeFormatter);
          List<Object> paramList = new ArrayList();
          paramList.add(time);
          paramList.add(index);
          index++;
          insertParamsList.add(paramList);
        }
      }
      if(i == 1){
        for(int j = 0 ; j < size ;j ++){
          LocalTime localTime0 = localTime.plusMinutes(j);
          String strLocalTime = localTime0.format(dateTimeFormatter);
          String[] strLocalTimes = strLocalTime.split(":");
          String localTimeHead = ""+(Integer.valueOf(strLocalTimes[0])+24);
          String localTimeBody = strLocalTimes[1];
          String time = localTimeHead+":"+localTimeBody;

          List<Object> paramList = new ArrayList();
          paramList.add(time);
          paramList.add(index);
          index++;
          insertParamsList.add(paramList);
        }
      }
    }

    boolean isRight = rs.executeBatchSql(insertSql, insertParamsList);
    if(isRight){
      //把48:00初始化进去
      String sql_48 = "insert into kq_timesarray(times,arrayindex) values(?,?) ";
      rs1.executeUpdate(sql_48, "48:00","2880");
      System.out.println("初始化kq_timesarray成功");
    }else{
      System.out.println("初始化kq_timesarray失败");
    }

  }

}
