package org.jeecg.modules.financeManage.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.financeManage.entity.FdIncome;
import org.jeecg.modules.financeManage.entity.FdIncomeItem;
import org.jeecg.modules.financeManage.service.IFdIncomeItemService;
import org.jeecg.modules.financeManage.service.IFdIncomeService;
import org.jeecg.modules.projectManagement.entity.FdBioanalysisSampleMilestonesItems;
import org.jeecg.modules.projectManagement.entity.FdProjectQuotationTemplateItem;
import org.jeecg.modules.projectManagement.mapper.FdBioanalysisSampleMilestonesItemsMapper;
import org.jeecg.modules.projectManagement.mapper.FdProjectQuotationTemplateItemMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.util.List;

public class FdIncomeItemJob implements Job {

    @Resource
    FdBioanalysisSampleMilestonesItemsMapper fdBioanalysisSampleMilestonesItemsMapper;
    @Resource
    IFdIncomeService iFdIncomeService;
    @Resource
    IFdIncomeItemService iFdIncomeItemService;
    @Resource
    FdProjectQuotationTemplateItemMapper fdProjectQuotationTemplateItemMapper;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //需要录入到收入确认的里程碑明细
        List<FdBioanalysisSampleMilestonesItems> fdBioanalysisSampleMilestonesItemsList=fdBioanalysisSampleMilestonesItemsMapper.getMilestonesItemsIsisInputIncome();
        //收入确认主表数据
        LambdaQueryWrapper<FdIncome> fdIncomeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        fdIncomeLambdaQueryWrapper.eq(FdIncome::getDelFlag,"0");
        List<FdIncome> fdIncomeList=iFdIncomeService.list(fdIncomeLambdaQueryWrapper);
        //收入确认子表数据
        LambdaQueryWrapper<FdIncomeItem> fdIncomeItemLambdaQueryWrapper=new LambdaQueryWrapper<>();
        fdIncomeItemLambdaQueryWrapper.eq(FdIncomeItem::getDelFlag,"0");
        fdIncomeItemLambdaQueryWrapper.orderByAsc(FdIncomeItem::getIncomeId);
        List<FdIncomeItem> fdIncomeItemList=iFdIncomeItemService.list(fdIncomeItemLambdaQueryWrapper);
        String fileId="";//项目档案id
        String month="";//归属月份
        for(FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems:fdBioanalysisSampleMilestonesItemsList) {
            fileId = fdBioanalysisSampleMilestonesItems.getPkProjectFile();//项目档案id
            month = fdBioanalysisSampleMilestonesItems.getMonthIncome();//归属月份
            for (FdIncome fdIncome : fdIncomeList) {//循环查找当前项目是否再收入确认中存在
                if (fileId.equals(fdIncome.getProjectFileId()) && month.equals(fdIncome.getIncomeDateMonth())) {
                    //生物分析的模板明细
                    List<FdProjectQuotationTemplateItem> fdProjectQuotationTemplateItemList = fdProjectQuotationTemplateItemMapper.getBioTempItemsBycontractId(fdIncome.getContractId());
                    String bioCompoundName = fdBioanalysisSampleMilestonesItems.getCompoundName().trim();//里程碑化合物名称
                    String bioMatrixTypeName = fdBioanalysisSampleMilestonesItems.getMatrixTypeName().trim();//里程碑明细基质名称
                    String bioName = fdBioanalysisSampleMilestonesItems.getQuotationBiologicalDetailsName().trim();//里程碑项目明细名称
                    String incomeId=fdIncome.getId();//收入确认主表id
                    boolean isIncomeItem=false;
                    //循环确认子表
                    for (FdIncomeItem fdIncomeItem : fdIncomeItemList) {
                        //查找当前交付物是否已经录入
                        if(incomeId.equals(fdIncomeItem.getIncomeId())&&bioCompoundName.equals(fdIncomeItem.getCompoundName())&&bioMatrixTypeName.equals(fdIncomeItem.getMatrixTypeName())&&bioName.equals(fdIncomeItem.getName())){
                            isIncomeItem=true;
                        }
                    }
                    //没有录入则进行录入
                    if(isIncomeItem==false){
                        for (FdProjectQuotationTemplateItem fdProjectQuotationTemplateItem : fdProjectQuotationTemplateItemList) {
                            String templateName = fdProjectQuotationTemplateItem.getQuotationTemplateName();//报价模板名称
                            String[] templateNames = templateName.split("-");
                            bioCompoundName = fdBioanalysisSampleMilestonesItems.getCompoundName().trim();//里程碑化合物名称
                            bioMatrixTypeName = fdBioanalysisSampleMilestonesItems.getMatrixTypeName().trim();//里程碑明细基质名称
                            bioName = fdBioanalysisSampleMilestonesItems.getQuotationBiologicalDetailsName().trim();//里程碑中关联的生物分析明细名称
                            String quoName = fdProjectQuotationTemplateItem.getName().trim();//报价生物分析交付物名称
                            //获取对应生物分析明细
                            if (bioCompoundName.equals(templateNames[1]) && bioMatrixTypeName.equals(templateNames[2]) && quoName.equals(bioName)) {
                                //产生收入确认子表对象
                                FdIncomeItem fdIncomeItem=new FdIncomeItem();
                                fdIncomeItem.setProjectQuotationTempItemId(fdProjectQuotationTemplateItem.getId());
                                fdIncomeItem.setName(fdProjectQuotationTemplateItem.getName());
                                fdIncomeItem.setDeliverables(fdProjectQuotationTemplateItem.getDeliverables());
                                fdIncomeItem.setCompletionTime(fdProjectQuotationTemplateItem.getTimeline());
                                fdIncomeItem.setFasting(fdProjectQuotationTemplateItem.getFeeCategory1Contract());
                                fdIncomeItem.setAfterMeal(fdProjectQuotationTemplateItem.getFeeCategory2Contract());
                                fdIncomeItem.setTempType(fdProjectQuotationTemplateItem.getType());
                                fdIncomeItem.setTempName(fdProjectQuotationTemplateItem.getQuotationTemplateName());
                                //为空则默认为字符空
                                if(StringUtils.isEmpty(fdIncomeItem.getApportionmentMethodOne())){
                                    fdIncomeItem.setApportionmentMethodOne("");
                                }
                                if(StringUtils.isEmpty(fdIncomeItem.getLastPeriodCompleted())){
                                    fdIncomeItem.setLastPeriodCompleted("");
                                }
                                if(StringUtils.isEmpty(fdIncomeItem.getCurrentIssueCompleted())){
                                    fdIncomeItem.setCurrentIssueCompleted("");
                                }
                                fdIncomeItem.setCompoundName(bioCompoundName);
                                fdIncomeItem.setMatrixTypeName(bioMatrixTypeName);
                                fdIncomeItem.setIncomeId(fdIncome.getId());
                                fdIncomeItem.setCreateBy("admin");
                                iFdIncomeItemService.save(fdIncomeItem);
                            }
                        }
                    }
                }
            }
        }
    }
}
