package com.engine.kq.biz;

import com.engine.kq.log.KQLog;
import com.weaver.util.threadPool.ThreadPoolUtil;
import com.weaver.util.threadPool.constant.ModulePoolEnum;
import weaver.common.DateUtil;
import weaver.conn.ConnStatement;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.WArrayList;

import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 考勤数据格式化定时任务
 */
public class KQFormatJob extends TimerTask {
  private static KQFormatJob instance = new KQFormatJob();
  public static WArrayList kqformatIds = new WArrayList();
  private static boolean flag = false;
  private KQLog kqLog = new KQLog();

  public static KQFormatJob getInstance(){
    return instance;
  }

  public void run() {
    KQFormatThread kqFormatThread = null;
    ConnStatement cs = new ConnStatement();
    String sql = "";
    try{
      if(flag) return;
      flag = true;
      if(kqformatIds.size()>0){
        flag = false;
        return;
      }
      sql = " select id, kqdate, resourceid from kq_format_pool where status = 0 order by kqdate, resourceid asc";
      boolean kqformatAccurate = KQSettingsBiz.getKqformatAccurate();
      if (kqformatAccurate){
        String currentFullTime = DateUtil.getFullDate();
        RecordSet rs = new RecordSet();
        if(rs.getDBType().equals("oracle")){
          sql = " select id, kqdate, resourceid,exectime from kq_format_pool where status = 0 and exectime is not null and exectime < to_date('"+currentFullTime+"','yyyy-mm-dd hh24:mi:ss') order by kqdate, resourceid, exectime asc";
        }
        else if(rs.getDBType().equals("postgresql")){
          sql = " select id, kqdate, resourceid,exectime from kq_format_pool where status = 0 and exectime is not null and exectime < to_date('"+currentFullTime+"','yyyy-mm-dd hh24:mi:ss') order by kqdate, resourceid, exectime asc";
        }
        else if (rs.getDBType().equals("mysql")) {
          sql = " select id, kqdate, resourceid,exectime from kq_format_pool where status = 0 and exectime is not null and exectime<STR_TO_DATE('"+currentFullTime+"', '%Y-%m-%d %H:%i:%s') order by kqdate, resourceid, exectime asc";
        }else{
          sql = " select id, kqdate, resourceid,exectime from kq_format_pool where status = 0 and exectime is not null and exectime<CONVERT(datetime,'"+currentFullTime+"',120) order by kqdate, resourceid, exectime asc";
        }
      }
      //rs.executeQuery(sql);
      cs.setStatementSql(sql);
      cs.executeQuery();
      while (cs.next()) {
        String key = cs.getString("kqdate") +"|"+cs.getString("resourceid");
        if(kqformatIds.contains(key))continue;
        kqformatIds.add(key);
        kqFormatThread = new KQFormatThread();
        kqFormatThread.setId(cs.getString("id"));
        kqFormatThread.setKqdate(cs.getString("kqdate"));
        kqFormatThread.setResourceid(cs.getString("resourceid"));

        if(kqformatAccurate) {
          kqLog.info(Thread.currentThread().getId() + "===KQFormatJob in>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + cs.getString("id") + "==" + cs.getString("kqdate") + "==" + cs.getString("resourceid") + "==" + cs.getString("exectime"));
        }else{
          kqLog.info(Thread.currentThread().getId() + "===KQFormatJob in>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + cs.getString("id") + "==" + cs.getString("kqdate") + "==" + cs.getString("resourceid"));
        }
        //kqformatPool.execute(kqFormatThread);
        ThreadPoolUtil.fixedPoolExecute(ModulePoolEnum.HRM, "KQFormatJob", kqFormatThread);
      }

      //初始化当天考勤数据、格式化上一天考勤数据
      //已经新增了计划任务KQYesterdayFormatJob，所以下面的屏蔽掉
//      new KQReportBiz().initKQReportData();
      //System.out.println("执行格式化考勤定时任务:"+ DateUtil.getFullDate());
      flag = false;
    }catch (Exception e){
      flag = false;
      new BaseBean().writeLog(e);
      kqLog.info("考勤数据格式化定时任务错误：",e);
    }finally {
      try {
        cs.close();
      }catch(Exception ex) {new BaseBean().writeLog("关闭考勤数据格式化定时任务数据库链接错误");}
    }
  }
}