package org.jeecg.modules.sampleAndConsumableManagement.job;

import org.jeecg.modules.financeManage.service.IFdProjectReceivablesService;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdSampleStorage;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdSampleStorageService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

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

/**
 * 描述
 *
 * @author:陈创
 * @date:2019/8/21 19:02
 */
public class FdSampleStorageJob implements Job {

    @Resource
    IFdSampleStorageService iFdSampleStorageService;
    @Resource
    IFdProjectReceivablesService fdProjectReceivablesService;

    /***
     * @Title:样本储存邮件定时通知生物分析PM
     * @Description:TODO
     * @param jobExecutionContext
     * @return void
     * @exception
     * @author 陈创
     * @date 2019/8/21 19:07
     * @updatehistory:
     *   2019/8/21 19:07 查询
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("999999999999999999");
        //样本储存数据
        List<FdSampleStorage> fdSampleStorageList = iFdSampleStorageService.list();
        //判断是否存在样本储存数据
        if (fdSampleStorageList != null && fdSampleStorageList.size() > 0) {
            //获取当前日期
            Date nowDate = new Date();
            //循环样本储存数据
            for (FdSampleStorage fdSampleStorage : iFdSampleStorageService.list()) {
                //摧毁日期和返回日期为空时
                if (fdSampleStorage.getDestructionDate() == null && fdSampleStorage.getReturnDate() == null) {
                    timed(fdSampleStorage, nowDate);
                }
            }
        }


    }

    /***
     * @Title:定时逻辑
     * @Description:TODO
     * @param fdSampleStorage,nowDate
     * @return void
     * @exception
     * @author 陈创
     * @date 2019/8/22 9:27
     * @updatehistory:
     *   2019/8/22 9:27 查询
     */
    public void timed(FdSampleStorage fdSampleStorage, Date nowDate) {
        //样本储存到期日
        Date sampleStorageExpireDate = fdSampleStorage.getSampleStorageChargeExpireDate();
        //到期日不为空则
        if (sampleStorageExpireDate != null) {
            try {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date d1 = df.parse(formatter.format(nowDate));//当前日
                Date d2 = df.parse(formatter.format(sampleStorageExpireDate));//到期日
                Long diff = d2.getTime() - d1.getTime();
                Long days = diff / (1000 * 60 * 60 * 24);//相差天
                System.out.println("相差天：" + days);
                if (days.longValue() == 30) {
                    //提前30天
                    emailReminder(fdSampleStorage,days);
                } else if (days.longValue() == 15) {
                    //提前15天
                    emailReminder(fdSampleStorage,days);
                } else if (days.longValue() == 0) {
                    //当天
                    emailReminder(fdSampleStorage,days);
                } else if (days.longValue() == -15) {
                    //过期15天
                    emailReminder(fdSampleStorage,days);
                } else if (days.longValue() == -30) {
                    //过期30天
                    emailReminder(fdSampleStorage,days);
                } else if (days.longValue()<-30){
                    //持续提醒、直到摧毁日期或返回日期填写
                    emailReminder(fdSampleStorage,days);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    /***
     * @Title:邮件通知
            * @Description:TODO
            * @param fdSampleStorage, days]
            * @return void
            * @exception 
            * @author 陈创
            * @date 2019/8/22 10:29
            * @updatehistory:
     *   2019/8/22 10:29 查询
            */
    public void emailReminder(FdSampleStorage fdSampleStorage,Long days){
        String dayString="已过期!";
        if(days>0){
            //即将到期
            dayString="还有"+days+"天到期!";
        }else if(days<0){
            //已经过期
            dayString="已过期"+(days*-1)+"天!";
        }
        String msg="样本储存单号:'"+fdSampleStorage.getTrackingCode()+"'"+dayString+"请完善摧毁日期与返回日期。";
        //发送给生物分析项目经理（字段名字命名有误）
        fdProjectReceivablesService.noticeFinance(fdSampleStorage.getAiologicalAnalysis(),"样本储存到期提醒",msg);
    }

}
