package cn.timer.api.utils.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.timer.api.bean.htzz.HtzzAdminZzda;
import cn.timer.api.bean.htzz.HtzzAssoHtgx;
import cn.timer.api.utils.aliyun.AliyunSMS;

/**
 * 记录当前时间循环输出 遍历合同提醒关系表,每天固定时间(8点)查询是否提醒
 * 
 * @author Administrator
 *
 */
@Component
@Lazy
public class RemindUtil implements SchedulingConfigurer {
	
	@Autowired
	private AliyunSMS sms;
	
	@Value("${config-8timer.remind.one}")
	private int one;
	
	@Value("${config-8timer.remind.two}")
	private int two;
	
	@Value("${config-8timer.remind.three}")
	private int three;

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	static String className = new Exception().getStackTrace()[0].getClassName();
	static String methodName = null;

	// 数据库动态更改定时配置
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.addTriggerTask(() -> {
			// 任务逻辑
			methodName = reportCurrentTime();
		}, triggerContext -> {
			CronTrigger cronTrigger = new CronTrigger(CronUtil.getCron(className, methodName)); // cron配置
			return cronTrigger.nextExecutionTime(triggerContext); // 下次执行任务的时间
		});
	}

	/**
	 * 间隔时间提醒 30min/次
	 */
	@Scheduled(fixedRate = 30 * 60 * 1000)
	private void now() {
		System.err.println("当前时间:" + dateFormat.format(new Date()));
	}

	/**
	 * 每天固定时间提醒
	 */
//	@Scheduled(cron = "0 0 8 * * ?") // 每天8点扫一下看有没要提醒的,有就发一个
	public  String reportCurrentTime() {

		List<HtzzAssoHtgx> htgxs = HtzzAssoHtgx.builder().build().selectAll();
		for (HtzzAssoHtgx htgx : htgxs) {
			QueryWrapper<HtzzAdminZzda> q = new QueryWrapper<HtzzAdminZzda>();
			q.select("id", "zjmc", "txkg_type", "yxdqr").eq("id", htgx.getHtid()).eq("is_delete", 0).eq("txkg_type", 0);
			HtzzAdminZzda zzda = HtzzAdminZzda.builder().build().selectOne(q);
			if (zzda != null) {
				String phone = htgx.getPhone(); // 员工手机
				String name = htgx.getName(); // 员工姓名
				String htname = zzda.getZjmc(); // 合同名称
				Date now = new Date(); // 当前时间
				Date dqsj = zzda.getYxdqr(); // 到期时间
				String time = DateUtil.formatDate(dqsj); // 到期時間格式
				Long betweenDay = DateUtil.between(dqsj, now, DateUnit.DAY); // 时间差天数
				Long sjc = dqsj.getTime() - now.getTime(); // 时间差数值
				System.err.println(name + " 的 " + htname + "还有: " + betweenDay + " 天到期");
				if (sjc > 0) {
					if (betweenDay <= 1) {
						sms.remind(name, htname, time, phone); // 少于1天短信提醒
					} else if (betweenDay == one) {
						sms.remind(name, htname, time, phone); // 短信提醒1
					} else if (betweenDay == two) {
						sms.remind(name, htname, time, phone); // 短信提醒2
					} else if (betweenDay == three) {
						sms.remind(name, htname, time, phone); // 短信提醒3
					}
				} else {
					zzda.setTxkgType(1); // 关闭提醒
					zzda.updateById();
				}
			}
		}
		return new Exception().getStackTrace()[0].getMethodName();
	}

}