package com.sys.bus.sys.task;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.cron4j.ITask;
import com.sys.bus.sys.model.DtBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

public class ShrResultTask implements ITask {
	Log log = LogFactory.getLog(this.getClass());

	static boolean flag = false;//防重复调用
	public void run() {
		log.info("ShrResultTask任务进行中。。。。");
		if(flag){
			log.error("ShrResultTask任务进行中。。。。本次跳出任务");
			return;
		}


		flag = true;
		try{

			LocalDateTime dateTime = LocalDateTime.now();
			if(dateTime.getHour()==1 && dateTime.getMinute()<10){
				String delSql ="delete from  ct_mp_jxjgb ";
				String updateSql  ="update ct_jx_entry set entry_synshr=0 where entry_synshr =1";
				Db.update(delSql);
				Db.update(updateSql);
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			String sql =
					"select cycle_name,entry_score,ratingentry_value,entry_khpersonid,entry_synshr,entry_id,entryno_createtime from ct_jx_entry entry " +
							"left join ct_jx_entryno entryno on entry.entry_no = entryno.entryno_id " +
							"left join ct_jx_cycle cycle on cycle.cycle_id = entry.entry_cycleid " +
							"left join ct_jx_ratingentry ratingentry on ratingentry.ratingentry_id = entry.entry_id " +
							"where entryno.entryno_appnode=9 and (entry_synshr is null or entry_synshr =0) order by entryno.entryno_createtime  ";

			List<DtBean> dtBeans = new DtBean().dao().find(sql);
			String insertSql ="insert into ct_mp_jxjgb(fid,fpersonid,fcreatetime,cfjxzq,cfjxfs,cfjxdj)values(dbo.newbosid('EC77A4AD'),?,?,?,?,?)";
			String updateSql  ="update ct_jx_entry set entry_synshr=1,entry_syntime=? where entry_id =?";
			for (DtBean dtBean : dtBeans) {
				String entryno_createtime = dtBean.getStr("entryno_createtime");
				String entry_id = dtBean.getStr("entry_id");
				String cycle_name = dtBean.getStr("cycle_name");
				BigDecimal entry_score = dtBean.getBigDecimal("entry_score");
				String ratingentry_value = dtBean.getStr("ratingentry_value");
				String entry_khpersonid = dtBean.getStr("entry_khpersonid");

				Db.update(insertSql,entry_khpersonid,entryno_createtime,cycle_name,entry_score,ratingentry_value);
				Db.update(updateSql,sdf.format(new Date()),entry_id);
			}
		}catch (Exception e){
			log.error("ShrResultTask任务失败1！",e);
		}finally {
			flag = false;
		}



	}


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

}
