package cn.timer.api.controller.htzz.atttimer;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

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

import cn.timer.api.bean.htzz.HtzzAdminZzda;
import cn.timer.api.bean.htzz.HtzzAssoHtgx;
import cn.timer.api.bean.htzz.HtzzAssoZztx;
import cn.timer.api.controller.kqgl.ClockInTool;

/**
 * 消息定时器
 *
 */
@Configuration // 1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class NewsTaskTiming {
	
	@Autowired
	private AlicloudSMS sms;
	
	@Scheduled(cron = "0 0 8 * * ?")//早上八点
	public void MessageAlert() throws ParseException {
		//当前时间
		String nowdate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();//
		
		List<HtzzAdminZzda> htzzs = HtzzAdminZzda.builder().build().selectList(new QueryWrapper<HtzzAdminZzda>().lambda().eq(HtzzAdminZzda::getTxkgType, 1)
				.ge(HtzzAdminZzda::getYxdqr, nowdate));
		for(HtzzAdminZzda zz : htzzs) {
			String dqrq = ClockInTool.SunNovCSTYMD(zz.getYxdqr());//有效期到期日
			
			//合同证照提醒表设置
			HtzzAssoZztx zzsz = HtzzAssoZztx.builder().build().selectOne(new QueryWrapper<HtzzAssoZztx>().lambda().eq(HtzzAssoZztx::getZzdaId, zz.getId()));
			if(zzsz != null) {
				int txsj = zzsz.getTxsj();//提醒时间周期 0-到期提醒 1-提前一天  2-提前二天  ...
				String tqsj = ClockInTool.requires_extra_times(dqrq,-txsj,3,1);
				
				if(nowdate.equals(tqsj)) {
					List<HtzzAssoHtgx> txrs = HtzzAssoHtgx.builder().build().selectList(new QueryWrapper<HtzzAssoHtgx>().lambda().eq(HtzzAssoHtgx::getHtid, zz.getId()));
					for(HtzzAssoHtgx rt : txrs) {
						//发送短信
						List<Object> list = sms.remind(zz, rt.getPhone(),rt.getName(),rt.getTzrid());
					}
				}
				
			}
			
		}
		
	}
}