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.FdProjectFile;
import org.jeecg.modules.projectManagement.entity.FdProjectQuotationTemplateItem;
import org.jeecg.modules.projectManagement.mapper.FdBioanalysisSampleMilestonesItemsMapper;
import org.jeecg.modules.projectManagement.mapper.FdProjectQuotationTemplateItemMapper;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

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

public class FdIncomeJob implements Job {

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

    @Resource
    IFdProjectFileService fdProjectFileService;

    @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);
        String fileId="";//项目档案id
        String month="";//归属月份
        //循环里程碑明细
        for(FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems:fdBioanalysisSampleMilestonesItemsList) {
            if(fileId.equals("")&&month.equals("")){//第一次循环
                fileId = fdBioanalysisSampleMilestonesItems.getPkProjectFile();//项目档案id
                month = fdBioanalysisSampleMilestonesItems.getMonthIncome();//归属月份
                //获取收取确认主表信息
                addIncome(fileId,month,fdBioanalysisSampleMilestonesItems,fdIncomeList);
            }
            //下一次不相等则为新的确认信息
            if(!fileId.equals(fdBioanalysisSampleMilestonesItems.getPkProjectFile())&&!month.equals(fdBioanalysisSampleMilestonesItems.getMonthIncome())){
                fileId = fdBioanalysisSampleMilestonesItems.getPkProjectFile();//项目档案id
                month = fdBioanalysisSampleMilestonesItems.getMonthIncome();//归属月份
                //获取收取确认主表信息
                addIncome(fileId,month,fdBioanalysisSampleMilestonesItems,fdIncomeList);
            }
        }

    }

    public void addIncome(String fileId,String month,FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems,List<FdIncome> fdIncomeList){
        boolean isIncome=false;
        for(FdIncome fdIncome:fdIncomeList){
            if(fileId.equals(fdIncome.getProjectFileId())&&month.equals(fdIncome.getIncomeDateMonth())){
                isIncome=true;
            }
        }
        if(isIncome==false){
            FdProjectFile fdProjectFile= fdProjectFileService.selectContractById(fdBioanalysisSampleMilestonesItems.getPkProjectFile());
            FdIncome fdIncome=new FdIncome();
            fdIncome.setProjectFileId(fdProjectFile.getId());
            fdIncome.setContractId(fdProjectFile.getContractId());
            fdIncome.setContractName(fdProjectFile.getContractCode());
            fdIncome.setCurstomerId(fdProjectFile.getCustomerId());
            fdIncome.setCurstomerName(fdProjectFile.getCustomerName());
            fdIncome.setContractPartyId(fdProjectFile.getContractParty());
            fdIncome.setPfPersonId(fdProjectFile.getInvestigator());
            fdIncome.setPfPersonName(fdProjectFile.getInvestigatorName());
            fdIncome.setPfStatus(fdProjectFile.getStatus());
            fdIncome.setPfProjectName(fdProjectFile.getProjectName());
            fdIncome.setPfSignDate(fdProjectFile.getSignDate());
            fdIncome.setCompoundName(fdProjectFile.getCompound());
            fdIncome.setProjectFileNo(fdProjectFile.getProjectFileNo());
            fdIncome.setIncomeDateMonth(fdBioanalysisSampleMilestonesItems.getMonthIncome());
            fdIncome.setCreateBy("admin");
            iFdIncomeService.save(fdIncome);
        }
    }
}
