package com.sys.bus.sys.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Enhancer;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.cron4j.ITask;
import com.sys.bus.hr.model.Person;
import com.sys.bus.hr.service.HrService;
import com.sys.bus.jx.service.WflogService;
import com.sys.bus.model.Entry;
import com.sys.bus.model.WxPerson;
import com.sys.bus.sys.wx.WxUtil;
import com.sys.bus.syscode.service.SysCodeService;
import com.sys.core.utils.Tool;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.formula.functions.T;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AutoConfirmTask implements ITask {
	Log log = LogFactory.getLog(this.getClass());
	static boolean flag = false;//防重复调用
	static SysCodeService sysCodeService = new SysCodeService();
	static WflogService wflogService =  Enhancer.enhance(WflogService.class);
	public void run() {
		if(!flag){
			log.info("进入 AutoConfirmTask 。。。。");
			flag = true;
			SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try{
				String dayStr = sysCodeService.getSysCodeName("自动确认等待天数").getSyscodeValue();
				int day = 5;
				try{
				   day = Integer.parseInt(dayStr);
				   if(day<=0)return;
				}catch (Exception e){
				}
				Calendar c = Calendar.getInstance();
				c.add(Calendar.DAY_OF_YEAR,0-day);
				String date = dateTimeFormat.format(c.getTime());
				//
				String sql = "select * from  ct_jx_entry WHERE entry_apptype='9' and entry_confirmstatus is null and  entry_apptime <? and entry_rating in ('A','B','S')";
				List<Entry> list = Entry.dao.find(sql, date);
				for(Entry entry : list) {
					String sqlStr = "update  ct_jx_entry set entry_confirmstatus ='2' WHERE entry_id =?";
					Db.update(sqlStr,entry.getEntryId());
					wflogService.addWfLog(null, "超时自动确认指标结果","91",entry.getEntryCycleid(),entry.getEntryId(),entry.getEntryNo(),"1","");
				}

			} catch (Exception e) {
				log.error("自动确认指标结果任务异常",e);
			}finally{
				flag = false;
			}

		}else{
			log.error("AutoConfirmTask 任务进行中。。。。本次跳出任务");
		}
	}

	public void stop() {
		log.info("------AutoConfirmTask stop-------------");
	}

}
