package org.jeecg.modules.projectManagement.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.financeManage.entity.FdClinicalIncome;
import org.jeecg.modules.financeManage.entity.FdIncome;
import org.jeecg.modules.financeManage.service.IFdClinicalIncomeService;
import org.jeecg.modules.financeManage.service.IFdIncomeService;
import org.jeecg.modules.projectManagement.entity.*;
import org.jeecg.modules.projectManagement.mapper.FdContractMapper;
import org.jeecg.modules.projectManagement.service.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import static org.jeecg.common.util.oConvertUtils.isEmpty;

/**
 * 功能描述：
 *
 * @Author: chenchuang
 * @Date: 2020/8/21 11:15
 */
public class FdBioanalysisSampleMilestonesIdJob implements Job {

    @Resource
    IFdContractService iFdContractService;

    @Resource
    IFdBioanalysisSampleMilestonesService iFdBioanalysisSampleMilestonesService;

    @Resource
    IFdClinicalProjectMilestonesService iFdClinicalProjectMilestonesService;

    @Resource
    IFdIncomeService iFdIncomeService;

    @Resource
    IFdClinicalIncomeService iFdClinicalIncomeService;

    @Resource
    IFdHistoricalProgressService iFdHistoricalProgressService;

    @Resource
    IFdBioanalysisProgressService iFdBioanalysisProgressService;

    @Resource
    FdContractMapper fdContractMapper;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("9999999999999999999999999999");
        show();
    }

    //获取生物分析 、临床、临床医院、生物分析收入确认、临床收入确认历史进度
    public void show(){
        Calendar now = Calendar.getInstance();
        int month=(now.get(Calendar.MONTH) + 1);
        //查询全部合同数据
        LambdaQueryWrapper<FdContract> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FdContract::getDelFlag,"0");
        lambdaQueryWrapper.orderByDesc(FdContract::getCreateTime);
//        List<FdContract> fdContractList=iFdContractService.list(lambdaQueryWrapper);
        List<FdContract> fdContractList=fdContractMapper.getContractAndFild();
        if(fdContractList!=null&&fdContractList.size()>0){
            //生物分析里程碑数据
            LambdaQueryWrapper<FdBioanalysisSampleMilestones> sampleMilestonesLambdaQueryWrapper=new LambdaQueryWrapper<>();
            sampleMilestonesLambdaQueryWrapper.eq(FdBioanalysisSampleMilestones::getDelFlag,"0");
            sampleMilestonesLambdaQueryWrapper.orderByDesc(FdBioanalysisSampleMilestones::getCreateTime);
            List<FdBioanalysisSampleMilestones> fdBioanalysisSampleMilestonesList=iFdBioanalysisSampleMilestonesService.list(sampleMilestonesLambdaQueryWrapper);
            //临床里程碑数据
            LambdaQueryWrapper<FdClinicalProjectMilestones> clinicalProjectMilestonesLambdaQueryWrapper=new LambdaQueryWrapper<>();
            clinicalProjectMilestonesLambdaQueryWrapper.eq(FdClinicalProjectMilestones::getDelFlag,"0");
            clinicalProjectMilestonesLambdaQueryWrapper.orderByDesc(FdClinicalProjectMilestones::getCreateTime);
            List<FdClinicalProjectMilestones> clinicalProjectMilestonesList=iFdClinicalProjectMilestonesService.list(clinicalProjectMilestonesLambdaQueryWrapper);
            //项目收入确认数据
            LambdaQueryWrapper<FdIncome> incomeLambdaQueryWrapper=new LambdaQueryWrapper<>();
            incomeLambdaQueryWrapper.eq(FdIncome::getDelFlag,"0");
            incomeLambdaQueryWrapper.orderByDesc(FdIncome::getCreateTime);
            List<FdIncome> fdIncomeList=iFdIncomeService.list(incomeLambdaQueryWrapper);
            //临床收入确认数据
            LambdaQueryWrapper<FdClinicalIncome> clinicalIncomeLambdaQueryWrapper=new LambdaQueryWrapper<>();
            clinicalIncomeLambdaQueryWrapper.eq(FdClinicalIncome::getDelFlag,"0");
            clinicalIncomeLambdaQueryWrapper.orderByDesc(FdClinicalIncome::getCreateTime);
            List<FdClinicalIncome> fdClinicalIncomeList=iFdClinicalIncomeService.list(clinicalIncomeLambdaQueryWrapper);
            //循环合同档案
            for(FdContract fdContract:fdContractList){
                String contractId=fdContract.getId();//合同id
                ////////////////生物分析项目进度
                Double bioanalysisProgressAccumulation=0.00;
                //循环生物分析里程碑数据
                List<FdBioanalysisSampleMilestones> fdBioanalysisSampleMilestonesListSave=new ArrayList<>();
                if(fdBioanalysisSampleMilestonesList!=null){
                    for(FdBioanalysisSampleMilestones bioanalysisSampleMilestones:fdBioanalysisSampleMilestonesList){
                        //获取合同下对应的生物分析项目
                        if(bioanalysisSampleMilestones.getPkContract().equals(contractId)){
                            //获取项目进度
                            String overallProgress=bioanalysisSampleMilestones.getOverallProgress();
                            if(StringUtils.isNotBlank(overallProgress)&&isNumber(overallProgress)){
                                fdBioanalysisSampleMilestonesListSave.add(bioanalysisSampleMilestones);
                                bioanalysisProgressAccumulation=(getDecimal(bioanalysisProgressAccumulation).add(getDecimal(Double.parseDouble(overallProgress)))).doubleValue();
                            }
                        }
                    }
                }

                ////////////临床项目进度
                //临床里程碑进度
                Double clinicalProgress=0.00;
                //临床医院进度
                Double clinicalHospitalProgress=0.00;
                if(clinicalProjectMilestonesList!=null){
                    for(FdClinicalProjectMilestones clinicalProjectMilestones:clinicalProjectMilestonesList){
                        //找到对应合同号得临床项目
                        if(clinicalProjectMilestones.getPkContract().equals(contractId)){
                            //获取临床里程碑进度
                            String overallProgress=clinicalProjectMilestones.getOverallProgress();
                            if(StringUtils.isNotBlank(overallProgress)&&isNumber(overallProgress)){
                                clinicalProgress=Double.parseDouble(overallProgress);
                            }
                            //获取临床医院进度
                            String hospitalProgress=clinicalProjectMilestones.getHospitalProgress();
                            if(StringUtils.isNotBlank(hospitalProgress)&&isNumber(hospitalProgress)){
                                clinicalHospitalProgress=Double.parseDouble(hospitalProgress);
                            }
                        }
                    }
                }

                //////////////项目收入进度
                Double bioanalysisIncomeProgress=0.00;
                if(fdIncomeList!=null){
                    for(FdIncome fdIncome:fdIncomeList){
                        //找到对应合同号的确认项目
                        if(fdIncome.getContractId().equals(contractId)){
                            //获取完工百分比
                            bioanalysisIncomeProgress=fdIncome.getPercentageOfCompletion();
                        }
                    }
                }

                ///////////////临床项目进度获取
                Double clinicalIncomeProgress=0.00;
                if(fdClinicalIncomeList!=null){
                    for(FdClinicalIncome fdClinicalIncome:fdClinicalIncomeList){
                        //找到对应合同下的项目
                        if(fdClinicalIncome.getContractId().equals(contractId)){
                            //获取百分比
                            String percentageOfCompletion=fdClinicalIncome.getPercentageOfCompletion();
                            if(StringUtils.isNotBlank(percentageOfCompletion)&&isNumber(percentageOfCompletion)){
                                clinicalIncomeProgress=Double.parseDouble(fdClinicalIncome.getPercentageOfCompletion());
                            }
                        }
                    }
                }
                String bioanalysisProgress="0.00";


                //进度记录表
                FdHistoricalProgress fdHistoricalProgress=new FdHistoricalProgress();
                fdHistoricalProgress.setContractId(fdContract.getId());
                fdHistoricalProgress.setContractCode(fdContract.getCode());
                //生物分析存在多个进度则取平均
                if(fdBioanalysisSampleMilestonesListSave.size()>0){
                    bioanalysisProgress=formatDouble5(bioanalysisProgressAccumulation/fdBioanalysisSampleMilestonesListSave.size());
                }
                fdHistoricalProgress.setBioanalysisProgress(bioanalysisProgress);
                fdHistoricalProgress.setClinicalHospitalProgress(clinicalHospitalProgress+"");
                fdHistoricalProgress.setClinicalProgress(clinicalProgress+"");
                fdHistoricalProgress.setBioanalysisIncomeProgress(bioanalysisIncomeProgress+"");
                fdHistoricalProgress.setClinicalIncomeProgress(clinicalIncomeProgress+"");
                fdHistoricalProgress.setMonth(month+"");
                fdHistoricalProgress.setCreateBy("admin");
                if(bioanalysisIncomeProgress==0.00||clinicalIncomeProgress==0.00){
                    //只存在临床或者生物分析的保存
                    saveWhere(bioanalysisIncomeProgress,clinicalIncomeProgress,fdHistoricalProgress,fdBioanalysisSampleMilestonesListSave,contractId,100.0);
                }else{
                    //都存在的保存
                    saveWhere(bioanalysisIncomeProgress,clinicalIncomeProgress,fdHistoricalProgress,fdBioanalysisSampleMilestonesListSave,contractId,200.0);
                }
            }
        }
    }

    //保存时判断小于完成最终进度的持续保存，超过或等于的判断数据库是否已经记录 已经记录的则不需要保存
    public void saveWhere(Double bioanalysisIncomeProgress,Double clinicalIncomeProgress,FdHistoricalProgress fdHistoricalProgress,List<FdBioanalysisSampleMilestones> fdBioanalysisSampleMilestonesListSave,String contractId,Double progress){
        //如果收入确认的完工百分比小于100或200则持续记录
        if((getDecimal(bioanalysisIncomeProgress).add(getDecimal(clinicalIncomeProgress))).doubleValue()<progress){
            iFdHistoricalProgressService.save(fdHistoricalProgress);
            //保存生物分析历史进度
            saveFdBioanalysisProgress(fdHistoricalProgress,fdBioanalysisSampleMilestonesListSave);
        }else{
            //判断收入确认进度完成的是否记录到数据库
            LambdaQueryWrapper<FdHistoricalProgress> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(FdHistoricalProgress::getDelFlag,"0");
            lambdaQueryWrapper1.eq(FdHistoricalProgress::getContractId,contractId);
            lambdaQueryWrapper1.eq(FdHistoricalProgress::getBioanalysisIncomeProgress,bioanalysisIncomeProgress);
            lambdaQueryWrapper1.eq(FdHistoricalProgress::getClinicalIncomeProgress,clinicalIncomeProgress);
            List<FdHistoricalProgress> fdHistoricalProgressList=iFdHistoricalProgressService.list(lambdaQueryWrapper1);
            //判断是否数据库存在
            if(fdHistoricalProgressList==null||fdHistoricalProgressList.size()<1){
                iFdHistoricalProgressService.save(fdHistoricalProgress);
                //保存生物分析历史进度
                saveFdBioanalysisProgress(fdHistoricalProgress,fdBioanalysisSampleMilestonesListSave);
            }
        }
    }

    //保存生物分析历史进度
    public void saveFdBioanalysisProgress(FdHistoricalProgress fdHistoricalProgress,List<FdBioanalysisSampleMilestones> fdBioanalysisSampleMilestonesList){
        List<FdBioanalysisProgress> fdBioanalysisProgressList=new ArrayList<>();
        Integer sort=1;
        //循环获取生物分析里程碑进度以及相关数据
        for(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones:fdBioanalysisSampleMilestonesList){
            FdBioanalysisProgress fdBioanalysisProgress=new FdBioanalysisProgress();
            fdBioanalysisProgress.setFdHistoricalProgressId(fdHistoricalProgress.getId());
            fdBioanalysisProgress.setFdBioanalysisSampleMilestonesId(fdBioanalysisSampleMilestones.getId());
            fdBioanalysisProgress.setBioanalysisProgress(fdBioanalysisSampleMilestones.getOverallProgress());
            fdBioanalysisProgress.setSort(sort);
            fdBioanalysisProgress.setCreateBy("admin");
            fdBioanalysisProgressList.add(fdBioanalysisProgress);
            sort++;
        }
        iFdBioanalysisProgressService.saveBatch(fdBioanalysisProgressList);
    }


    public static String formatDouble5(double d) {
        return String.format("%.2f", d);
    }

    private boolean isNumber(String str){
        if(isEmpty(str)){
            return false;
        }
        String reg = "\\d+(\\.\\d+)?";
        return str.matches(reg);

    }

    public BigDecimal getDecimal(Double d){
        BigDecimal decimal= new BigDecimal(d);
        return decimal;
    }
}
