package com.bonus.boot.manager.task;

import com.bonus.boot.manager.career.entity.CareerBean;
import com.bonus.boot.manager.manager.utils.DateTimeHelper;
import com.bonus.boot.manager.manager.utils.SendPhoneMessageUtil;
import com.bonus.boot.manager.manager.utils.StringHelper;
import com.bonus.boot.manager.task.dao.PhysicalResultTaskDao;
import com.bonus.boot.manager.task.entity.TaskBean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 包名称：com.bonus.boot.manager.task
 * 类名称：PhysicalResultTask
 * 类描述：体检结果定时器
 * 创建人：@author tqzhang
 * 创建时间：2023年06月27日 15:29
 */

@Component
@EnableAsync
public class PhysicalResultTask {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private PhysicalResultTaskDao dao;


    @Async("myAsync")
    @Scheduled(fixedRate = 1000 * 60 * 30)
    public void testScheduleTask() {
        try {
            Thread.sleep(6000);
            TaskBean b = new TaskBean();
            String result = this.dao.getIsExistThisYear(DateTimeHelper.getNowYear());
            if (StringHelper.isEmpty(result)) {
                this.dao.insertData(b);
                this.dao.insertYear(DateTimeHelper.getNowYear());
                System.out.println("体检结果管理 -- 基础数据" + Thread.currentThread().getName() + "-" + sdf.format(new Date()));
            }else {
                System.out.println("本年次数据已更新");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Async("myAsync")
    @Scheduled(cron = "0 0 23 * * ?")
    public void updateAppointStatusTask() {
        try {
            Thread.sleep(6000);
            TaskBean b = new TaskBean();
            if (DateTimeHelper.isWeekend()){
                //当前日期为周日
                //获取所有未上传的数据
                List<CareerBean> list = dao.getNoUploadData();
                if (list.size() > 0){
                    for (int i = 0; i < list.size(); i++) {
                        dao.updateStatusTask(list.get(i));
                    }
                }else {
                    System.out.println("无作废数据");
                }
                 //修改状态为作废
                System.out.println("体检结果管理 -- 修改状态为作废" + Thread.currentThread().getName() + "-" + sdf.format(new Date()));
            }else {
                System.out.println("当前日期不是周日");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 体检时间前一天通知
     */
    @Async("myAsync")
    @Scheduled(cron = "0 0 17 * * ?")
    public void NotificationDayBeforeTask() {
        try {
            Thread.sleep(6000);
            TaskBean b = new TaskBean();
            //获取第二天哪所有需要提醒的人员信息及医院
            List<CareerBean> list = dao.getAllNoCheckPerson(getNextDay2());
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    SendPhoneMessageUtil.SendPhoneMsg(list.get(i).getPhone(),list.get(i).getPersonName()+"您于"+getNextDay()+"预约的"+list.get(i).getHospitalName()+"的体检,如无法前往，请登录平台取消预约，一周只可预约一次。");
                }
                System.out.println(getNextDay()+"信息发送完毕!");
            }else {
                System.out.println(getNextDay()+"没有预约人员");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 已预约未体检人员短信提醒    触发条件：预约时间15日之后提醒“已预约未体检”的人员
     */
//    @Async("myAsync")
//    @Scheduled(cron = "0 0 17 * * ?")
    public void noCheckForAppointTask() {
        try {
            Thread.sleep(6000);
            TaskBean b = new TaskBean();
            //获取十五天之前已预约未体检人员信息
            List<CareerBean> list = dao.getNoCheckForAppointList(obtainedFifteenDaysAgo(-14));
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    SendPhoneMessageUtil.SendPhoneMsg(list.get(i).getPhone(),list.get(i).getPersonName()+"您于"+obtainedFifteenDaysAgo(-14)+"预约的"+list.get(i).getHospitalName()+"的体检未及时参加体检,请尽快重新预约完成体检。");
                }
                System.out.println(getNextDay()+"信息发送完毕!");
            }else {
                System.out.println("无已预约未体检人员");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取明天
     * @return
     */
    public static String getNextDay(){
        Calendar cal=Calendar.getInstance();
        //这里改为1
        cal.add(Calendar.DATE,1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy年MM月dd日").format(time);
    }
    public static String getNextDay2(){
        Calendar cal=Calendar.getInstance();
        //这里改为1
        cal.add(Calendar.DATE,1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    public static String obtainedFifteenDaysAgo(int amount){
        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + amount);
        return new SimpleDateFormat("yyyy-MM-dd").format(now.getTime());
    }

}
