package cn.timer.api.utils;

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

import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
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;

/**
 * 记录当前时间循环输出 遍历合同提醒关系表,每天固定时间(8点)查询是否提醒
 * 
 * @author Administrator
 *
 */
@Component
@Lazy(false)
@EnableScheduling
public class RemindUtil {

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

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

	/**
	 * 每天固定时间提醒
	 */
//	@Scheduled(cron = "0 0 8 * * ?") // 每天8点扫一下看有没要提醒的,有就发一个
	@Scheduled(cron = "0 8 15 * * ?") // 测试合同提醒 
	public static void 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 name = htgx.getName(); // 员工姓名
				String phone = htgx.getPhone(); // 员工手机
				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(betweenDay);
				if (sjc > 0) {
					if (betweenDay <= 1) {
						AliyunSMS.tx(name, htname, time, phone); // 少于1天短信提醒
					} else if (betweenDay == 3) {
						AliyunSMS.tx(name, htname, time, phone); // 少于3天短信提醒
					} else if (betweenDay == 7) {
						AliyunSMS.tx(name, htname, time, phone); // 少于7天短信提醒
					} else if (betweenDay == 30) {
						AliyunSMS.tx(name, htname, time, phone); // 少于30天短信提醒
					}

				} else {
					zzda.setTxkgType(1); // 关闭提醒
					zzda.updateById();
				}
			}
		}

	}

}