package org.jeecg.modules.projectManagement.job;

import org.jeecg.modules.financeManage.service.IFdProjectReceivablesService;
import org.jeecg.modules.projectManagement.entity.FdBioanalysisSampleMilestonesItems;
import org.jeecg.modules.projectManagement.mapper.FdBioanalysisSampleMilestonesItemsMapper;
import org.jeecg.modules.projectManagement.mapper.FdBioanalysisSampleMilestonesMapper;
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:2020/4/26 16:00
 */
public class BioanalysisSampleMilestonesItemsJob implements Job {

    @Resource
    FdBioanalysisSampleMilestonesItemsMapper fdBioanalysisSampleMilestonesItemsMapper;

    @Resource
    IFdProjectReceivablesService iFdProjectReceivablesService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("执行");
        List<FdBioanalysisSampleMilestonesItems> fdBioanalysisSampleMilestonesItemsList = fdBioanalysisSampleMilestonesItemsMapper.getMilestonesItemsOvertime();
        if (fdBioanalysisSampleMilestonesItemsList != null && fdBioanalysisSampleMilestonesItemsList.size() > 0) {
            Date nowDate = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String pkBioanalysisSampleMilestones = "";//里程碑项目id
            String contractNo = "";
            String projectFileNo = "";
            String bioPi = "";
            String description = "";
            for (FdBioanalysisSampleMilestonesItems milestonesItems : fdBioanalysisSampleMilestonesItemsList) {
                Date d1 = null;//当前日
                Date d2 = null;//预计完成时间
                try {
                    d1 = df.parse(formatter.format(nowDate));
                    d2 = df.parse(formatter.format(milestonesItems.getEstimateEndTime()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                    Long diff = d1.getTime() - d2.getTime();//当前日减去预计完成时间
                    Long days = diff / (1000 * 60 * 60 * 24);//相差天
                    if (pkBioanalysisSampleMilestones.equals("")) {//第一次
                        //超过预计一天、两天、三天、每超过三天的明细
                        if (days.intValue() == 1 || days.intValue() == 2 || days.intValue() == 3 || (days.intValue() > 3 && days.intValue() % 3 == 0)) {
                            pkBioanalysisSampleMilestones = milestonesItems.getPkBioanalysisSampleMilestones();
                            contractNo = milestonesItems.getContractCode();
                            projectFileNo = milestonesItems.getProjectFileNo();
                            bioPi = milestonesItems.getInvestigatorId();
                            description += milestonesItems.getCompoundName() + "--" + milestonesItems.getMatrixTypeName() + "--" + milestonesItems.getDescription() + "\n";
                        }
                    }
                    // else if () {//属于当前里程碑明细
                    //     //超过预计一天、两天、三天、每超过三天的明细
                    //     if (days.intValue() == 1 || days.intValue() == 2 || days.intValue() == 3 || (days.intValue() > 3 && days.intValue() % 3 == 0)) {
                    //         contractNo = milestonesItems.getContractCode();
                    //         projectFileNo = milestonesItems.getProjectFileNo();
                    //         bioPi = milestonesItems.getInvestigatorId();
                    //         description += milestonesItems.getCompoundName() + "--" + milestonesItems.getMatrixTypeName() + "--" + milestonesItems.getDescription() + "\n";
                    //     }
                    // }
                    else {//查询到子表属于另一个里程碑的明细
                        if(!pkBioanalysisSampleMilestones.equals(milestonesItems.getPkBioanalysisSampleMilestones())){
                            if (!description.equals("")) {
                                String msg = "里程碑" +
                                        "\n合同号：" + contractNo +
                                        "\n项目号：" + projectFileNo +
                                        "\n\n" + description +
                                        "已经超过预计结束时间，请确认项目进度！";
                                iFdProjectReceivablesService.noticeFinance("e643ccb260d640cf85d5d2e492389e8b", "里程碑项目超时提醒", msg);
                                description="";
                            }
                        }
                        //超过预计一天、两天、三天、每超过三天的明细
                        if (days.intValue() == 1 || days.intValue() == 2 || days.intValue() == 3 || (days.intValue() > 3 && days.intValue() % 3 == 0)) {
                            contractNo = milestonesItems.getContractCode();
                            projectFileNo = milestonesItems.getProjectFileNo();
                            bioPi = milestonesItems.getInvestigatorId();
                            description += milestonesItems.getCompoundName() + "--" + milestonesItems.getMatrixTypeName() + "--" + milestonesItems.getDescription() + "\n";
                        }
                        pkBioanalysisSampleMilestones = milestonesItems.getPkBioanalysisSampleMilestones();
                    }
            }
            //最后一次执行
            if (!description.equals("")) {
                String msg = "里程碑：" +
                        "\n合同号：" + contractNo +
                        "\n项目号：" + projectFileNo +
                        "\n\n" + description +
                        "已经超过预计结束时间，请确认项目进度！";
                iFdProjectReceivablesService.noticeFinance("e643ccb260d640cf85d5d2e492389e8b", "里程碑项目超时提醒", msg);
            }
        }
    }

}
