package org.jeecg.modules.projectManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.projectManagement.mapper.*;
import org.jeecg.modules.projectManagement.entity.*;
import org.jeecg.modules.projectManagement.service.*;
import org.jeecg.modules.system.entity.MouthConfiguration;
import org.jeecg.modules.system.mapper.MouthConfigurationMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 生物分析样本里程碑
 * @author： jeecg-boot
 * @date： 2019-06-04
 * @version： V1.0
 */
@Service
@Transactional
public class FdBioanalysisSampleMilestonesServiceImpl extends ServiceImpl<FdBioanalysisSampleMilestonesMapper, FdBioanalysisSampleMilestones> implements IFdBioanalysisSampleMilestonesService {

    @Resource
    FdBioanalysisSampleMilestonesMapper fdBioanalysisSampleMilestonesMapper;

    @Resource
    IFdBioanalysisSampleMilestonesItemsService fdBioanalysisSampleMilestonesItemsService;

    @Resource
    IFdBioanalysisSampleMilestonesCompletedNumberService fdBioanalysisSampleMilestonesCompletedNumberService;

    @Resource
    IFdBioanalysisSampleMilestonesSampleReceivedService fdBioanalysisSampleMilestonesSampleReceivedService;

    @Resource
    IFdBioanalysisSampleMilestonesService fdBioanalysisSampleMilestonesService;

    @Resource
    IFdProjectFileService fdProjectFileService;

    @Resource
    IFdBioanalysisSampleMilestonesMethodService fdBioanalysisSampleMilestonesMethodService;

    @Resource
    IFdBioanalysisSampleMilestonesConfirmService fdBioanalysisSampleMilestonesConfirmService;

    @Resource
    IFdBioanalysisSampleMilestonesValidationService fdBioanalysisSampleMilestonesValidationService;

    @Resource
    IFdBioanalysisSampleMilestonesSampleService fdBioanalysisSampleMilestonesSampleService;

    @Resource
    IFdProjectFileCompoundService fdProjectFileCompoundService;
    @Resource
    IFdBioanalysisTemplateService fdBioanalysisTemplateService;
    @Resource
    FdBioanalysisSampleMilestonesItemsMapper fdBioanalysisSampleMilestonesItemsMapper;

    @Resource
    FdClinicalProjectMilestonesMapper fdClinicalProjectMilestonesMapper;


    @Resource
    IFdContractProportionService iFdContractProportionService;
    @Resource
    IFdMilestonesProgressChangeService iFdMilestonesProgressChangeService;
    @Resource
    FdMilestonesProgressChangeMapper fdMilestonesProgressChangeMapper;

    @Resource
    IFdProjectFileMethodService iFdProjectFileMethodService;
    @Resource
    IFdProjectFileConfirmService iFdProjectFileConfirmService;
    @Resource
    IFdProjectFileValidationService iFdProjectFileValidationService;
    @Resource
    IFdProjectFileSampleService iFdProjectFileSampleService;

    @Resource
    IFdProjectQuotationTemplateItemService fdProjectQuotationTemplateItemService;
    @Resource
    FdProjectQuotationTemplateItemMapper fdProjectQuotationTemplateItemMapper;

    @Resource
    MouthConfigurationMapper mouthConfigurationMapper;

    @Override
    public IPage<FdBioanalysisSampleMilestones> findByPage(Page<FdBioanalysisSampleMilestones> page, QueryWrapper<FdBioanalysisSampleMilestones> queryWrapper) {
        return fdBioanalysisSampleMilestonesMapper.findByPage(page, queryWrapper);
    }

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加生物分析样本里程碑同时添加 生物分析样本里程碑明细 生物分析样本里程碑当前完成数量 生物分析样本接收日期 项目档案 生物分析方法号 生物分析方法确认项目号 生物分析方法验证号 生物分析样本分析号
     * @Date 20:48 2019/6/4
     * @Param [fdBioanalysisSampleMilestones]
     **/
    @Override
    public void addAll(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones) {
        fdBioanalysisSampleMilestones.setDelFlag("0");
        fdBioanalysisSampleMilestones.setStatus("0");
        // LambdaQueryWrapper<FdProjectFile> queryWrapper = new LambdaQueryWrapper<>();
        // queryWrapper.eq(FdProjectFile::getId, fdBioanalysisSampleMilestones.getPkProjectFile());
        // List<FdProjectFile> list = fdProjectFileService.list(queryWrapper);
        // for (FdProjectFile fdProjectFile : list) {
        //     if (fdProjectFile.getBioanalysisCode() != null && !fdProjectFile.getBioanalysisCode().equals("")) {
        //         fdBioanalysisSampleMilestones.setBioanalysisCode(fdProjectFile.getBioanalysisCode());
        //     }
        //     if (fdProjectFile.getBioanalysisValidationCode() != null && !fdProjectFile.getBioanalysisValidationCode().equals("")) {
        //         fdBioanalysisSampleMilestones.setBioanalysisValidationCode(fdProjectFile.getBioanalysisValidationCode());
        //     }
        //     if (fdProjectFile.getBioanalysisConfirmItemCode() != null && !fdProjectFile.getBioanalysisConfirmItemCode().equals("")) {
        //         fdBioanalysisSampleMilestones.setBioanalysisConfirmItemCode(fdProjectFile.getBioanalysisConfirmItemCode());
        //     }
        //     if (fdProjectFile.getBiologicalAnalysisSampleAnalysisCode() != null && !fdProjectFile.getBiologicalAnalysisSampleAnalysisCode().equals("")) {
        //         fdBioanalysisSampleMilestones.setBiologicalAnalysisSampleAnalysisCode(fdProjectFile.getBiologicalAnalysisSampleAnalysisCode());
        //     }
        // }

        //获取项目经理id、
        // FdProjectFile fdProjectFile = fdClinicalProjectMilestonesMapper.getProjectQuotationByContractId(fdBioanalysisSampleMilestones.getPkContract());
        FdProjectFile fdProjectFile=fdProjectFileService.getById(fdBioanalysisSampleMilestones.getPkProjectFile());
        fdBioanalysisSampleMilestones.setProjectManagerId(fdProjectFile.getProjectManagerId());

        fdBioanalysisSampleMilestonesService.save(fdBioanalysisSampleMilestones);
        //保存树形模板类型
        int sort=0;
        for(FdBioanalysisTemplate fdBioanalysisTemplate:fdBioanalysisSampleMilestones.getFdBioanalysisTemplateList()){
            fdBioanalysisTemplate.setId("");
            fdBioanalysisTemplate.setProjectId(fdBioanalysisSampleMilestones.getId());
            fdBioanalysisTemplate.setSort(sort);
            sort++;
        }
        //批量保存树形模板类型
        fdBioanalysisTemplateService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisTemplateList());

        //明细集合不等于空的则获取
        for(FdBioanalysisTemplate fdBioanalysisTemplate:fdBioanalysisSampleMilestones.getFdBioanalysisTemplateList()) {
            if (fdBioanalysisTemplate.getFdBioanalysisSampleMilestonesItemsList() != null && fdBioanalysisTemplate.getFdBioanalysisSampleMilestonesItemsList().size() > 0) {
                for (FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems : fdBioanalysisTemplate.getFdBioanalysisSampleMilestonesItemsList()) {
                    fdBioanalysisSampleMilestonesItems.setPkBioanalysisSampleMilestonesItemId(fdBioanalysisSampleMilestonesItems.getId());
                    fdBioanalysisSampleMilestonesItems.setId("");
                    fdBioanalysisSampleMilestonesItems.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
                    fdBioanalysisSampleMilestonesItems.setTemplateId(fdBioanalysisTemplate.getId());
                }
                List<FdBioanalysisSampleMilestonesItems> fdBioanalysisSampleMilestonesItemsList=fdBioanalysisTemplate.getFdBioanalysisSampleMilestonesItemsList();
                //保存明细模板项
                fdBioanalysisSampleMilestonesItemsService.saveBatch(fdBioanalysisSampleMilestonesItemsList);
                //修改parentId
                getIdUpdatePid(fdBioanalysisSampleMilestonesItemsList);
                //批量修改模板项parentId
                fdBioanalysisSampleMilestonesItemsMapper.parentIdBatch(fdBioanalysisSampleMilestonesItemsList);
            }
        }

        saveAll(fdBioanalysisSampleMilestones);
    }

    /***
     * @Title:保存树形模板类型，模板树形类型保存项目id,获取模板项明细集合
            * @Description:TODO
            * @param fdBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesItemsList]
            * @return void
            * @exception
            * @author 陈创
            * @date 2019/8/5 14:45
            * @updatehistory:
     *   2019/8/5 14:45 新增
            */
    public void saveTemplate(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){

    }

    //修改父极id
    public void getIdUpdatePid(List<FdBioanalysisSampleMilestonesItems> fdBioanalysisSampleMilestonesItemsList){
        for(FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems:fdBioanalysisSampleMilestonesItemsList){
            for(FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems2:fdBioanalysisSampleMilestonesItemsList){
                if(fdBioanalysisSampleMilestonesItems.getPkBioanalysisSampleMilestonesItemId().equals(fdBioanalysisSampleMilestonesItems2.getParentId())){
                    fdBioanalysisSampleMilestonesItems2.setParentId(fdBioanalysisSampleMilestonesItems.getId());
                }
            }

        }
    }


    /**
     * @return org.jeecg.modules.projectManagement.entity.FdBioanalysisSampleMilestones
     * @Author gurui
     * @Description //TODO 根据合同id查询相关数据
     * @Date 14:26 2019/6/28
     * @Param [contractId]
     **/
    @Override
    public FdBioanalysisSampleMilestones queryBioanalysisByConId(String projectFileId) {
        FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones= null;
        try {
            //根据合同号查询相关信息
            fdBioanalysisSampleMilestones = fdBioanalysisSampleMilestonesMapper.queryBioanalysisByConId(projectFileId);
            //查询项目档案化合物
            LambdaQueryWrapper<FdProjectFileCompound> fdProjectFileCompoundLambdaQueryWrapper=new LambdaQueryWrapper<>();
            fdProjectFileCompoundLambdaQueryWrapper.eq(FdProjectFileCompound::getProjectId,fdBioanalysisSampleMilestones.getPkProjectFile());
            fdProjectFileCompoundLambdaQueryWrapper.eq(FdProjectFileCompound::getDelFlag,"0");
            fdProjectFileCompoundLambdaQueryWrapper.orderByAsc(FdProjectFileCompound::getSort);
            List<FdProjectFileCompound> fdProjectFileCompoundList=fdProjectFileCompoundService.list(fdProjectFileCompoundLambdaQueryWrapper);
            fdBioanalysisSampleMilestones.setFdProjectFileCompoundList(fdProjectFileCompoundList);
        } catch (Exception e) {
            e.printStackTrace();
            fdBioanalysisSampleMilestones=null;
        }
        return fdBioanalysisSampleMilestones;
    };

    /**
     * @return boolean
     * @Author gurui
     * @Description //TODO 删除生物分析样本里程碑同时删除 生物分析样本里程碑明细 生物分析样本里程碑当前完成数量 生物分析样本接收日期 项目档案 生物分析方法号 生物分析方法确认项目号 生物分析方法验证号 生物分析样本分析号
     * @Date 14:49 2019/6/28
     * @Param [id]
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones = fdBioanalysisSampleMilestonesMapper.selectById(id);
        try {
            fdBioanalysisSampleMilestonesService.removeById(id);
            delete(fdBioanalysisSampleMilestones);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }

        return result;
    }

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 复制一条一样的数据
     * @Date 14:54 2019/6/28
     * @Param [id]
     **/
    @Override
    public void copy(String id) {
        //根据id获得里程碑
        FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones = fdBioanalysisSampleMilestonesMapper.selectById(id);
        fdBioanalysisSampleMilestones.setDelFlag("0");
        fdBioanalysisSampleMilestones.setCreateTime(null);
        fdBioanalysisSampleMilestones.setBioanalysisCode(fdBioanalysisSampleMilestones.getBioanalysisCode() + "(副本)");
        fdBioanalysisSampleMilestonesService.save(fdBioanalysisSampleMilestones);
        String fdBioanalysisSampleMilestonesId = fdBioanalysisSampleMilestones.getId();
        //生物分析里程碑明细
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesItems> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(FdBioanalysisSampleMilestonesItems::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesId);
        List<FdBioanalysisSampleMilestonesItems> list1 = fdBioanalysisSampleMilestonesItemsService.list(queryWrapper1);
        //生物分析样本里程碑当前完成数量
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesCompletedNumber> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(FdBioanalysisSampleMilestonesCompletedNumber::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesId);
        List<FdBioanalysisSampleMilestonesCompletedNumber> list2 = fdBioanalysisSampleMilestonesCompletedNumberService.list(queryWrapper2);
        //生物分析样本接收日期
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesSampleReceived> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(FdBioanalysisSampleMilestonesSampleReceived::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesId);
        List<FdBioanalysisSampleMilestonesSampleReceived> list3 = fdBioanalysisSampleMilestonesSampleReceivedService.list(queryWrapper3);
        //生物分析方法号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesMethod> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(FdBioanalysisSampleMilestonesMethod::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesId);
        List<FdBioanalysisSampleMilestonesMethod> list4 = fdBioanalysisSampleMilestonesMethodService.list(queryWrapper4);
        //生物分析方法确认项目号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesConfirm> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.eq(FdBioanalysisSampleMilestonesConfirm::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesId);
        List<FdBioanalysisSampleMilestonesConfirm> list5 = fdBioanalysisSampleMilestonesConfirmService.list(queryWrapper5);
        //生物分析方法验证号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesValidation> queryWrapper6 = new LambdaQueryWrapper<>();
        queryWrapper6.eq(FdBioanalysisSampleMilestonesValidation::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesId);
        List<FdBioanalysisSampleMilestonesValidation> list6 = fdBioanalysisSampleMilestonesValidationService.list(queryWrapper6);
        //生物分析样本分析号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesSample> queryWrapper7 = new LambdaQueryWrapper<>();
        queryWrapper7.eq(FdBioanalysisSampleMilestonesSample::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesId);
        List<FdBioanalysisSampleMilestonesSample> list7 = fdBioanalysisSampleMilestonesSampleService.list(queryWrapper7);
        fdBioanalysisSampleMilestones.setFdBioanalysisSampleMilestonesItemsList(list1);
        fdBioanalysisSampleMilestones.setFdBioanalysisSampleMilestonesCompletedNumberList(list2);
        fdBioanalysisSampleMilestones.setFdBioanalysisSampleMilestonesSampleReceivedList(list3);
        fdBioanalysisSampleMilestones.setFdBioanalysisSampleMilestonesMethodList(list4);
        fdBioanalysisSampleMilestones.setFdBioanalysisSampleMilestonesConfirmList(list5);
        fdBioanalysisSampleMilestones.setFdBioanalysisSampleMilestonesValidationList(list6);
        fdBioanalysisSampleMilestones.setFdBioanalysisSampleMilestonesSampleList(list7);
        saveAll(fdBioanalysisSampleMilestones);
    }


    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author gurui
     * @Description //TODO 批量弃审核数据 使其状态为0
     * @Date 16:05 2019/6/28
     * @Param [ids]
     **/
    @Override
    public Map<String, Object> auditBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unAudit = " ";//可审计
        String audit = " ";//已审计
        //根据多个id查询报价对象集合
        List<FdBioanalysisSampleMilestones> fdClinicalProjectMilestonesList = fdBioanalysisSampleMilestonesMapper.selectBatchIds(ids);
        //修改
        if (fdClinicalProjectMilestonesList == null || fdClinicalProjectMilestonesList.isEmpty()) {
            audit += "未找到修改数据</br>";
        } else {
            //判断哪些可关闭哪些不可关闭
            for (FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones : fdClinicalProjectMilestonesList) {
                if (fdBioanalysisSampleMilestones.getStatus().equals("0")) {
                    audit += fdBioanalysisSampleMilestones.getProjectName() + "，";
                } else {
                    unAudit += fdBioanalysisSampleMilestones.getProjectName() + "，";
                }
            }
            //可关闭存在则操作数据库修改
            if (!unAudit.equals("")) {
                fdBioanalysisSampleMilestonesMapper.auditBatch(ids);
            }
        }
        //去掉最后一个"，"号
        audit = audit.substring(0, audit.length() - 1);
        unAudit = unAudit.substring(0, unAudit.length() - 1);
        //加关键词
        if (!unAudit.equals("")) {
            unAudit = unAudit + "弃审核成功";
        }
        if (!audit.equals("")) {
            audit = "其中:" + audit + "已处于未审核状态。<br> ";
        }
        map.put("msg", unAudit + audit);
        return map;
    }

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author gurui
     * @Description //TODO 批量审核数据 使其状态为1
     * @Date 13:50 2019/6/28
     * @Param [list]
     **/
    @Override
    public Map<String, Object> reviewBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unReview = " "; //未审核
        String review = " "; //已审核
        //根据多个id查询报价对象集合
        List<FdBioanalysisSampleMilestones> fdClinicalProjectMilestonesList = fdBioanalysisSampleMilestonesMapper.selectBatchIds(ids);
        //修改
        if (fdClinicalProjectMilestonesList == null || fdClinicalProjectMilestonesList.isEmpty()) {
            review += "未找到修改数据</br>";
        } else {
            //判断哪些已审核哪些未审核
            for (FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones : fdClinicalProjectMilestonesList) {
                if (fdBioanalysisSampleMilestones.getStatus().equals("0")) {
                    unReview += fdBioanalysisSampleMilestones.getProjectName()+",";
                }
                if (fdBioanalysisSampleMilestones.getStatus().equals("1")) {
                    review += fdBioanalysisSampleMilestones.getProjectName()+",";
                }
                if (fdBioanalysisSampleMilestones.getStatus().equals("2")) {
                    throw new RuntimeException("已关闭！无法审核");
                }
                if (fdBioanalysisSampleMilestones.getStatus().equals("3")) {
                    throw new RuntimeException("已转合同！无法审核");
                }
            }
            //去掉最后一个","号
            unReview = unReview.substring(0, unReview.length() - 1);
            review = review.substring(0, review.length() - 1);
            //操作数据库将未审核的修改成已审核
            if (!unReview.equals("")) {
                fdBioanalysisSampleMilestonesMapper.reviewBatch(ids);
            }
        }
        //加关键词
        if (!unReview.equals("")) {
            unReview = unReview + "审核成功";
        }
        if (!review.equals("")) {
            review = "其中：" + review + "已审核。<br>";
        }
        map.put("msg", unReview + review);
        return map;
    }


    /**
     * @return void
     * @Author gurui
     * @Description //TODO 详情保存
     * @Date 9:47 2019/7/11
     * @Param [fdBioanalysisSampleMilestones]
     **/
    @Override
    public boolean viewUpdateMilestonesItems(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones) {
        // //删除生物分析里程碑明细
        // LambdaQueryWrapper<FdBioanalysisSampleMilestonesItems> queryWrapper1 = new LambdaQueryWrapper<>();
        // queryWrapper1.eq(FdBioanalysisSampleMilestonesItems::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        // fdBioanalysisSampleMilestonesItemsService.remove(queryWrapper1);
        // //生物分析里程碑明细
        // if (fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList()!=null){
        //     int i = 1;
        //     for (FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems : fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList()) {
        //         fdBioanalysisSampleMilestonesItems.setSort(i + "");
        //         fdBioanalysisSampleMilestonesItems.setId("");
        //         fdBioanalysisSampleMilestonesItems.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
        //         i++;
        //     }
        //     fdBioanalysisSampleMilestonesItemsService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList());
        // }
        // try {
            //修改主表数据
            FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones1=new FdBioanalysisSampleMilestones();
            fdBioanalysisSampleMilestones1.setId(fdBioanalysisSampleMilestones.getId());
            fdBioanalysisSampleMilestones1.setOverallProgress(fdBioanalysisSampleMilestones.getOverallProgress());
            fdBioanalysisSampleMilestones1.setTotalSuccNumber(fdBioanalysisSampleMilestones.getTotalSuccNumber());
            fdBioanalysisSampleMilestones1.setSampleAnalysisProgress(fdBioanalysisSampleMilestones.getSampleAnalysisProgress());
            fdBioanalysisSampleMilestones1.setBioanalysisCode(fdBioanalysisSampleMilestones.getBioanalysisCode());
            fdBioanalysisSampleMilestones1.setBioanalysisConfirmItemCode(fdBioanalysisSampleMilestones.getBioanalysisConfirmItemCode());
            fdBioanalysisSampleMilestones1.setBioanalysisValidationCode(fdBioanalysisSampleMilestones.getBioanalysisValidationCode());
            fdBioanalysisSampleMilestones1.setBiologicalAnalysisSampleAnalysisCode(fdBioanalysisSampleMilestones.getBiologicalAnalysisSampleAnalysisCode());
            method(fdBioanalysisSampleMilestones);
            saveAll(fdBioanalysisSampleMilestones);
            contractOverallProgress(fdBioanalysisSampleMilestones1);
            fdBioanalysisSampleMilestonesMapper.updateById(fdBioanalysisSampleMilestones1);
            //获取需要录入到收入确认的明细
            isInputIncome(fdBioanalysisSampleMilestones);
            //修改模板项
            fdBioanalysisSampleMilestonesItemsMapper.tempItemsBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList());
            return true;
        // } catch (Exception e) {
        //     e.printStackTrace();
        //     return false;
        // }
    }


    /***
     * @Title:
     * @Description:TODO 获取需要录入到收入确认的交付物
     * @param fdBioanalysisSampleMilestones
     * @return void
     * @exception
     * @author 陈创
     * @date 2020/4/10 16:11
     * @updatehistory:
     *   2020/4/10 16:11 新增
     */
    public void isInputIncomeHistory(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){
        //获取需要录入到收入确认的明细
        // List<FdProjectQuotationTemplateItem> quotationTemplateItemsIncom=new ArrayList<>();
        //生物分析的模板明细
        List<FdProjectQuotationTemplateItem> fdProjectQuotationTemplateItemList=fdProjectQuotationTemplateItemMapper.getBioTempItemsBycontractId(fdBioanalysisSampleMilestones.getPkContract());
        //里程碑的模板明细
        List<FdBioanalysisSampleMilestonesItems> fdBioanalysisSampleMilestonesItemsList=fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList();
        for(FdBioanalysisSampleMilestonesItems bioItems:fdBioanalysisSampleMilestonesItemsList){
            String compoundId="";//化合物id
            String tempId=bioItems.getTemplateId();//模板id
            //模板集合
            LambdaQueryWrapper<FdBioanalysisTemplate> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(FdBioanalysisTemplate::getDelFlag,'0');
            lambdaQueryWrapper.eq(FdBioanalysisTemplate::getProjectId,fdBioanalysisSampleMilestones.getId());
            List<FdBioanalysisTemplate> fdBioanalysisTemplateList=fdBioanalysisTemplateService.list(lambdaQueryWrapper);
            //获取基质名称化合物id
            for(FdBioanalysisTemplate template:fdBioanalysisTemplateList){
                if(tempId.equals(template.getId())){
                    bioItems.setMatrixTypeName(template.getName());
                    compoundId=template.getParentId();
                }
            }
            //获取化合物名称
            for(FdBioanalysisTemplate template:fdBioanalysisTemplateList){
                if(compoundId.equals(template.getKeyId())){
                    bioItems.setCompoundName(template.getName());
                }
            }
            //获取需要录入到收入确认的明细
            for(FdProjectQuotationTemplateItem quotationTemplateItem:fdProjectQuotationTemplateItemList){
                String templateName=quotationTemplateItem.getQuotationTemplateName();//报价模板名称
                String[] templateNames=templateName.split("-");
                String bioCompoundName=bioItems.getCompoundName().trim();//里程碑化合物名称
                String bioMatrixTypeName=bioItems.getMatrixTypeName().trim();//里程碑明细基质名称
                String bioName=bioItems.getDescription().trim();//里程碑项目明细名称
                String bioWeight=bioItems.getWeight().trim();//举重
                String bioProgress=bioItems.getProjectProgress().trim();//完成进度
                String quoName=quotationTemplateItem.getName().trim();//报价生物分析交付物名称
                //化合物名称相等、基质名称相等、明细名称相等、举重与完成进度相等、没有录入到收入确认的
                if(bioCompoundName.equals(templateNames[1])&&bioMatrixTypeName.equals(templateNames[2])&&quoName.equals(bioName)&&bioWeight.equals(bioProgress)&&bioItems.getIsInputIncome()==1){
                    bioItems.setIsInputIncome(2);
                    Date[] date=new Date[]{null};
                    //获取完成时间（项目有子集所以需要获取所有子集所完成的最后时间）
                    monthMax(bioItems,fdBioanalysisSampleMilestonesItemsList,date);
                    System.out.println("----------------------------------");
                    System.out.println(date[0]);
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String dateString = formatter.format(date[0]);
                    //获取归属月份
                     MouthConfiguration mouthConfiguration =mouthConfigurationMapper.getMouthConfigurationBetween(dateString);
                     bioItems.setMonthIncome(mouthConfiguration.getMonth());
//                     quotationTemplateItem.setMonth(mouthConfiguration.getMonth());
//                     quotationTemplateItemsIncom.add(quotationTemplateItem);
                }
            }
        }
    }


    /***
     * @Title:
     * @Description:TODO 获取需要录入到收入确认的交付物
     * @param fdBioanalysisSampleMilestones
     * @return void
     * @exception
     * @author 陈创
     * @date 2020/5/14 16:11
     * @updatehistory:
     *   2020/4/10 16:11 新增
     */
    public void isInputIncome(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){
        //里程碑的模板明细
        List<FdBioanalysisSampleMilestonesItems> fdBioanalysisSampleMilestonesItemsList=fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList();
        for(FdBioanalysisSampleMilestonesItems bioItems:fdBioanalysisSampleMilestonesItemsList){
            String bioWeight=bioItems.getWeight().trim();//举重
            String bioProgress=bioItems.getProjectProgress().trim();//完成进度
            String quotationBiologicalDetailsName=bioItems.getQuotationBiologicalDetailsName();//生物分析明细名称
            //生物分析明细名称不为空、举重与完成进度相等、没有录入到收入确认的
            if(StringUtils.isNotBlank(quotationBiologicalDetailsName)&&bioWeight.equals(bioProgress)&&bioItems.getIsInputIncome()==1&&bioItems.getActualEndTime()!=null){
                bioItems.setIsInputIncome(2);
                Date[] date=new Date[]{null};
                //获取完成时间（项目有子集所以需要获取所有子集所完成的最后时间）
                monthMax(bioItems,fdBioanalysisSampleMilestonesItemsList,date);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = formatter.format(date[0]);
                //获取归属月份
                MouthConfiguration mouthConfiguration =mouthConfigurationMapper.getMouthConfigurationBetween(dateString);
                bioItems.setMonthIncome(mouthConfiguration.getMonth());
//                     quotationTemplateItem.setMonth(mouthConfiguration.getMonth());
//                     quotationTemplateItemsIncom.add(quotationTemplateItem);
            }
        }
    }

    /***
     * @Title:
     * @Description:TODO 递归获取需要录入到收入确认的月份
     * @param fdBioanalysisSampleMilestonesItems, milesItems, date]
     * @return void
     * @exception
     * @author 陈创
     * @date 2020/4/10 14:47
     * @updatehistory:
     *   2020/4/10 14:47 新增
     */
    public void monthMax(FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems,List<FdBioanalysisSampleMilestonesItems> milesItems,Date[] date){
        Date maxActualEndTime=fdBioanalysisSampleMilestonesItems.getActualEndTime();//里程碑项目实际完成时间
        Boolean isChilod=false;//判断是否存在子集条目
        for(FdBioanalysisSampleMilestonesItems milestonesItems:milesItems){
            //判断是否是子集条目
            if(StringUtils.isNotBlank(milestonesItems.getParentId())&&milestonesItems.getParentId().equals(fdBioanalysisSampleMilestonesItems.getId())
                    &&milestonesItems.getActualEndTime()!=null){
                isChilod=true;
                //如果存在子集则递归
                monthMax(milestonesItems,milesItems,date);
            }
        }
        //没有子集则是末级
        if(isChilod==false){
            if(date[0]==null){//第一次获取最后时间
                date[0]=maxActualEndTime;
            }else{
                //如果获取的的时间比上一次大则覆盖完成时间
                if(maxActualEndTime.compareTo(date[0])==1){
                    date[0]=maxActualEndTime;
                }
            }
        }
    }


    /***
     * @Title:对生物分析里程碑相关方法号删除
            * @Description:TODO
            * @param fdBioanalysisSampleMilestones
            * @return void
            * @exception
            * @author 陈创
            * @date 2020/4/3 16:17
            * @updatehistory:
     *   2020/4/3 16:17 新增
            */
    public void method(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){
        //生物分析方法号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesMethod> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(FdBioanalysisSampleMilestonesMethod::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesMethodService.remove(queryWrapper4);
        //生物分析方法确认项目号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesConfirm> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.eq(FdBioanalysisSampleMilestonesConfirm::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesConfirmService.remove(queryWrapper5);
        //生物分析方法验证号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesValidation> queryWrapper6 = new LambdaQueryWrapper<>();
        queryWrapper6.eq(FdBioanalysisSampleMilestonesValidation::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesValidationService.remove(queryWrapper6);
        //生物分析样本分析号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesSample> queryWrapper7 = new LambdaQueryWrapper<>();
        queryWrapper7.eq(FdBioanalysisSampleMilestonesSample::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesSampleService.remove(queryWrapper7);
    }

    /**
     * @return java.util.List<org.jeecg.modules.projectManagement.entity.FdContract>
     * @Author gurui
     * @Description //TODO  查询已审核的项目报价的合同
     * @Date 10:04 2019/7/11
     * @Param []
     **/
    @Override
    public List<FdContract> getFileContractList() {
        return fdBioanalysisSampleMilestonesMapper.getFileContractList();
    }

    /**
     * @return boolean
     * @Author gurui
     * @Description //TODO 修改
     * @Date 17:07 2019/7/11
     * @Param [fdBioanalysisSampleMilestones]
     **/
    @Override
    public boolean editAll(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones) {
        boolean result = true;
        try {
            fdBioanalysisSampleMilestonesService.updateById(fdBioanalysisSampleMilestones);
            delete(fdBioanalysisSampleMilestones);
            saveAll(fdBioanalysisSampleMilestones);
            //保存模板明细
            List<FdBioanalysisSampleMilestonesItems> fdBioanalysisSampleMilestonesItemsList=fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList();
            for(FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems:fdBioanalysisSampleMilestonesItemsList){
                fdBioanalysisSampleMilestonesItems.setPkBioanalysisSampleMilestonesItemId(fdBioanalysisSampleMilestonesItems.getId());
                fdBioanalysisSampleMilestonesItems.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
                fdBioanalysisSampleMilestonesItems.setId("");
            }
            fdBioanalysisSampleMilestonesItemsService.saveBatch(fdBioanalysisSampleMilestonesItemsList);
            //修改parentId
            getIdUpdatePid(fdBioanalysisSampleMilestonesItemsList);
            //批量修改模板项parentId
            fdBioanalysisSampleMilestonesItemsMapper.parentIdBatch(fdBioanalysisSampleMilestonesItemsList);

        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }


    //删除所有的子表内容
    public void delete(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones) {
        //删除生物分析里程碑明细
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesItems> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(FdBioanalysisSampleMilestonesItems::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesItemsService.remove(queryWrapper1);
        //删除生物分析样本里程碑当前完成数量
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesCompletedNumber> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(FdBioanalysisSampleMilestonesCompletedNumber::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesCompletedNumberService.remove(queryWrapper2);
        //删除生物分析样本接收日期
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesSampleReceived> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(FdBioanalysisSampleMilestonesSampleReceived::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesSampleReceivedService.remove(queryWrapper3);
        //生物分析方法号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesMethod> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(FdBioanalysisSampleMilestonesMethod::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesMethodService.remove(queryWrapper4);
        //生物分析方法确认项目号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesConfirm> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.eq(FdBioanalysisSampleMilestonesConfirm::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesConfirmService.remove(queryWrapper5);
        //生物分析方法验证号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesValidation> queryWrapper6 = new LambdaQueryWrapper<>();
        queryWrapper6.eq(FdBioanalysisSampleMilestonesValidation::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesValidationService.remove(queryWrapper6);
        //生物分析样本分析号
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesSample> queryWrapper7 = new LambdaQueryWrapper<>();
        queryWrapper7.eq(FdBioanalysisSampleMilestonesSample::getPkBioanalysisSampleMilestones, fdBioanalysisSampleMilestones.getId());
        fdBioanalysisSampleMilestonesSampleService.remove(queryWrapper7);
    }
    //保存所有的子表内容
    public void saveAll(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones) {
        //生物分析里程碑明细
        // if (fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList()!=null){
        //     int i = 1;
        //     for (FdBioanalysisSampleMilestonesItems fdBioanalysisSampleMilestonesItems : fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList()) {
        //         fdBioanalysisSampleMilestonesItems.setSort(i + "");
        //         fdBioanalysisSampleMilestonesItems.setId("");
        //         fdBioanalysisSampleMilestonesItems.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
        //         i++;
        //     }
        //     fdBioanalysisSampleMilestonesItemsService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesItemsList());
        // }
        //生物分析样本里程碑当前完成数量
        // if(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesCompletedNumberList()!=null){
        //     for (FdBioanalysisSampleMilestonesCompletedNumber fdBioanalysisSampleMilestonesCompletedNumber : fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesCompletedNumberList()) {
        //         fdBioanalysisSampleMilestonesCompletedNumber.setId("");
        //         fdBioanalysisSampleMilestonesCompletedNumber.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
        //     }
        //     fdBioanalysisSampleMilestonesCompletedNumberService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesCompletedNumberList());
        // }
        //生物分析样本接收日期
        if (fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesSampleReceivedList()!=null){
            for (FdBioanalysisSampleMilestonesSampleReceived fdBioanalysisSampleMilestonesSampleReceived : fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesSampleReceivedList()) {
                fdBioanalysisSampleMilestonesSampleReceived.setId("");
                fdBioanalysisSampleMilestonesSampleReceived.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
            }
            fdBioanalysisSampleMilestonesSampleReceivedService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesSampleReceivedList());
        }
        //生物分析方法号
        if (fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesMethodList()!=null){
            for (FdBioanalysisSampleMilestonesMethod fdBioanalysisSampleMilestonesMethod : fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesMethodList()) {
                fdBioanalysisSampleMilestonesMethod.setId("");
                fdBioanalysisSampleMilestonesMethod.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
            }
            fdBioanalysisSampleMilestonesMethodService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesMethodList());
            //回写新增的方法号码到项目档案
            writeBackMethodFile(fdBioanalysisSampleMilestones);
        }
        //生物分析方法确认项目号
        if (fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesConfirmList()!=null){
            for (FdBioanalysisSampleMilestonesConfirm fdBioanalysisSampleMilestonesConfirm : fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesConfirmList()) {
                fdBioanalysisSampleMilestonesConfirm.setId("");
                fdBioanalysisSampleMilestonesConfirm.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
            }
            fdBioanalysisSampleMilestonesConfirmService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesConfirmList());
            //回写新增的方法确认项目号码到项目档案
            writeBackConfirmFile(fdBioanalysisSampleMilestones);
        }
        //生物分析方法验证号
        if (fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesValidationList()!=null){
            for (FdBioanalysisSampleMilestonesValidation fdBioanalysisSampleMilestonesValidation : fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesValidationList()) {
                fdBioanalysisSampleMilestonesValidation.setId("");
                fdBioanalysisSampleMilestonesValidation.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
            }
            fdBioanalysisSampleMilestonesValidationService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesValidationList());
            //回写新增的生物分析方法验证号到项目档案
            writeBackValidationFile(fdBioanalysisSampleMilestones);
        }
        //生物分析样本分析号
        if (fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesSampleList()!=null){
            for (FdBioanalysisSampleMilestonesSample fdBioanalysisSampleMilestonesSample : fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesSampleList()) {
                fdBioanalysisSampleMilestonesSample.setId("");
                fdBioanalysisSampleMilestonesSample.setPkBioanalysisSampleMilestones(fdBioanalysisSampleMilestones.getId());
            }
            fdBioanalysisSampleMilestonesSampleService.saveBatch(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesSampleList());
            // writeBackFile(fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesSampleList(),null);
            //回写新增的生物分析样本分析号到项目档案
            writeBackSampleFile(fdBioanalysisSampleMilestones);
        }
    }

    /***
     * @Title:
            * @Description:TODO 方法号回写到项目档案
            * @param fdBioanalysisSampleMilestones
            * @return void
            * @exception
            * @author 陈创
            * @date 2020/4/8 14:31
            * @updatehistory:
     *   2020/4/8 14:31 新增
            */
    public void writeBackMethodFile(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){
        //当前里程碑填写的号码集合
        List<FdBioanalysisSampleMilestonesMethod> fdBioanalysisSampleMilestonesMethodList=fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesMethodList();
        //查询项目档案中的号码
        LambdaQueryWrapper<FdProjectFileMethod> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FdProjectFileMethod::getProjectId,fdBioanalysisSampleMilestones.getPkProjectFile());
        lambdaQueryWrapper.eq(FdProjectFileMethod::getDelFlag,"0");
        List<FdProjectFileMethod> fdProjectFileMethodList=iFdProjectFileMethodService.list(lambdaQueryWrapper);
        //里程碑中的号码需要回写到项目档案中的集合
        List<FdBioanalysisSampleMilestonesMethod> methodListAdd=new ArrayList<>();
        String sort="1";
        if(!fdProjectFileMethodList.isEmpty()){//项目档案中的是否为空
            //里程碑方法号
            for(FdBioanalysisSampleMilestonesMethod method:fdBioanalysisSampleMilestonesMethodList){
                String code=method.getCode();
                Boolean isExistence=false;
                for(FdProjectFileMethod fileMethod:fdProjectFileMethodList){
                    //项目档案中是否存在
                    if(StringUtils.isNotBlank(code)&&code.equals(fileMethod.getCode())){
                        isExistence=true;
                    }
                }
                //不存在则加入回写集合
                if(isExistence==false){
                    methodListAdd.add(method);
                }
            }
            //获取最后一个号码的排序
            sort=fdProjectFileMethodList.get(fdProjectFileMethodList.size()-1).getSort();
        }else{
            methodListAdd=fdBioanalysisSampleMilestonesMethodList;
        }

        //回写号码集合
        List<FdProjectFileMethod> fileMethodListAdd=new ArrayList<>();
        for(FdBioanalysisSampleMilestonesMethod method:methodListAdd){
            FdProjectFileMethod fileMethod=new FdProjectFileMethod();
            sort=Integer.parseInt(sort)+1+"";
            fileMethod.setSort(sort);
            fileMethod.setProjectId(fdBioanalysisSampleMilestones.getPkProjectFile());
            fileMethod.setCode(method.getCode());
            fileMethod.setMemo(method.getMemo());
            fileMethodListAdd.add(fileMethod);
        }
        iFdProjectFileMethodService.saveBatch(fileMethodListAdd);
    }

    /***
     * @Title:
     * @Description:TODO 方法确认项目号回写到项目档案
     * @param fdBioanalysisSampleMilestones
     * @return void
     * @exception
     * @author 陈创
     * @date 2020/4/8 14:31
     * @updatehistory:
     *   2020/4/8 14:31 新增
     */
    public void writeBackConfirmFile(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){
        //当前里程碑填写的号码集合
        List<FdBioanalysisSampleMilestonesConfirm> fdBioanalysisSampleMilestonesConfirmList=fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesConfirmList();
        //查询项目档案中的号码
        LambdaQueryWrapper<FdProjectFileConfirm> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FdProjectFileConfirm::getProjectId,fdBioanalysisSampleMilestones.getPkProjectFile());
        lambdaQueryWrapper.eq(FdProjectFileConfirm::getDelFlag,"0");
        List<FdProjectFileConfirm> fdProjectFileConfirmList=iFdProjectFileConfirmService.list(lambdaQueryWrapper);
        //里程碑中的号码需要回写到项目档案中的集合
        List<FdBioanalysisSampleMilestonesConfirm> methodListAdd=new ArrayList<>();
        String sort="1";
        if(!fdProjectFileConfirmList.isEmpty()){//项目档案中的是否为空
            //里程碑方法号
            for(FdBioanalysisSampleMilestonesConfirm method:fdBioanalysisSampleMilestonesConfirmList){
                String code=method.getCode();
                Boolean isExistence=false;
                for(FdProjectFileConfirm fileMethod:fdProjectFileConfirmList){
                    //项目档案中是否存在
                    if(StringUtils.isNotBlank(code)&&code.equals(fileMethod.getCode())){
                        isExistence=true;
                    }
                }
                //不存在则加入回写集合
                if(isExistence==false){
                    methodListAdd.add(method);
                }
            }
            //获取最后一个号码的排序
            sort=fdProjectFileConfirmList.get(fdProjectFileConfirmList.size()-1).getSort();
        }else{
            methodListAdd=fdBioanalysisSampleMilestonesConfirmList;
        }

        //回写号码集合
        List<FdProjectFileConfirm> fileMethodListAdd=new ArrayList<>();
        for(FdBioanalysisSampleMilestonesConfirm method:methodListAdd){
            FdProjectFileConfirm fileMethod=new FdProjectFileConfirm();
            sort=Integer.parseInt(sort)+1+"";
            fileMethod.setSort(sort);
            fileMethod.setProjectId(fdBioanalysisSampleMilestones.getPkProjectFile());
            fileMethod.setCode(method.getCode());
            fileMethod.setMemo(method.getMemo());
            fileMethodListAdd.add(fileMethod);
        }
        iFdProjectFileConfirmService.saveBatch(fileMethodListAdd);
    }




    /***
     * @Title:
     * @Description:TODO 方法验证号回写到项目档案
     * @param fdBioanalysisSampleMilestones
     * @return void
     * @exception
     * @author 陈创
     * @date 2020/4/8 14:31
     * @updatehistory:
     *   2020/4/8 14:31 新增
     */
    public void writeBackValidationFile(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){
        //当前里程碑填写的号码集合
        List<FdBioanalysisSampleMilestonesValidation> fdBioanalysisSampleMilestonesValidationList=fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesValidationList();
        //查询项目档案中的号码
        LambdaQueryWrapper<FdProjectFileValidation> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FdProjectFileValidation::getProjectId,fdBioanalysisSampleMilestones.getPkProjectFile());
        lambdaQueryWrapper.eq(FdProjectFileValidation::getDelFlag,"0");
        List<FdProjectFileValidation> fdProjectFileValidationList=iFdProjectFileValidationService.list(lambdaQueryWrapper);
        //里程碑中的号码需要回写到项目档案中的集合
        List<FdBioanalysisSampleMilestonesValidation> methodListAdd=new ArrayList<>();
        String sort="0";
        if(!fdProjectFileValidationList.isEmpty()){//项目档案中的是否为空
            //里程碑方法号
            for(FdBioanalysisSampleMilestonesValidation method:fdBioanalysisSampleMilestonesValidationList){
                String code=method.getCode();
                Boolean isExistence=false;
                for(FdProjectFileValidation fileMethod:fdProjectFileValidationList){
                    //项目档案中是否存在
                    if(StringUtils.isNotBlank(code)&&code.equals(fileMethod.getCode())){
                        isExistence=true;
                    }
                }
                //不存在则加入回写集合
                if(isExistence==false){
                    methodListAdd.add(method);
                }
            }
            //获取最后一个号码的排序
            sort=fdProjectFileValidationList.get(fdProjectFileValidationList.size()-1).getSort();
        }else{
            methodListAdd=fdBioanalysisSampleMilestonesValidationList;
        }

        //回写号码集合
        List<FdProjectFileValidation> fileMethodListAdd=new ArrayList<>();
        for(FdBioanalysisSampleMilestonesValidation method:methodListAdd){
            FdProjectFileValidation fileMethod=new FdProjectFileValidation();
            sort=Integer.parseInt(sort)+1+"";
            fileMethod.setSort(sort);
            fileMethod.setProjectId(fdBioanalysisSampleMilestones.getPkProjectFile());
            fileMethod.setCode(method.getCode());
            fileMethod.setMemo(method.getMemo());
            fileMethodListAdd.add(fileMethod);
        }
        iFdProjectFileValidationService.saveBatch(fileMethodListAdd);
    }

    /***
     * @Title:
     * @Description:TODO 样本号回写到项目档案
     * @param fdBioanalysisSampleMilestones
     * @return void
     * @exception
     * @author 陈创
     * @date 2020/4/8 14:31
     * @updatehistory:
     *   2020/4/8 14:31 新增
     */
    public void writeBackSampleFile(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){
        //当前里程碑填写的号码集合
        List<FdBioanalysisSampleMilestonesSample> fdBioanalysisSampleMilestonesSampleList=fdBioanalysisSampleMilestones.getFdBioanalysisSampleMilestonesSampleList();
        //查询项目档案中的号码
        LambdaQueryWrapper<FdProjectFileSample> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FdProjectFileSample::getProjectId,fdBioanalysisSampleMilestones.getPkProjectFile());
        lambdaQueryWrapper.eq(FdProjectFileSample::getDelFlag,"0");
        List<FdProjectFileSample> fdProjectFileSampleList=iFdProjectFileSampleService.list(lambdaQueryWrapper);
        //里程碑中的号码需要回写到项目档案中的集合
        List<FdBioanalysisSampleMilestonesSample> methodListAdd=new ArrayList<>();
        String sort="1";
        if(!fdProjectFileSampleList.isEmpty()){//项目档案中的是否为空
            //里程碑方法号
            for(FdBioanalysisSampleMilestonesSample method:fdBioanalysisSampleMilestonesSampleList){
                String code=method.getCode();
                Boolean isExistence=false;
                for(FdProjectFileSample fileMethod:fdProjectFileSampleList){
                    //项目档案中是否存在
                    if(StringUtils.isNotBlank(code)&&code.equals(fileMethod.getCode())){
                        isExistence=true;
                    }
                }
                //不存在则加入回写集合
                if(isExistence==false){
                    methodListAdd.add(method);
                }
            }
            //获取最后一个号码的排序
            sort=fdProjectFileSampleList.get(fdProjectFileSampleList.size()-1).getSort();
        }else{
            methodListAdd=fdBioanalysisSampleMilestonesSampleList;
        }

        //回写号码集合
        List<FdProjectFileSample> fileMethodListAdd=new ArrayList<>();
        for(FdBioanalysisSampleMilestonesSample method:methodListAdd){
            FdProjectFileSample fileMethod=new FdProjectFileSample();
            sort=Integer.parseInt(sort)+1+"";
            fileMethod.setSort(sort);
            fileMethod.setProjectId(fdBioanalysisSampleMilestones.getPkProjectFile());
            fileMethod.setCode(method.getCode());
            fileMethod.setMemo(method.getMemo());
            fileMethodListAdd.add(fileMethod);
        }
        iFdProjectFileSampleService.saveBatch(fileMethodListAdd);
    }

    /***
     * @Title:根据合同比例计算合同里程碑总进度并查询里程碑项目进度是否有改变是否记录
     * @Description:TODO
     * @param fdBioanalysisSampleMilestones
     * @return void
     * @exception
     * @author 陈创
     * @date 2019/9/24 16:11
     * @updatehistory:
     *   2019/9/24 16:11 修改
     */
    public void contractOverallProgress(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones){
        //根据id查询里程碑对象
        FdBioanalysisSampleMilestones fdBioanalysisSampleMilestonesById=fdBioanalysisSampleMilestonesMapper.selectById(fdBioanalysisSampleMilestones.getId());
        //当前里程碑总进度
        double overallProgress=Double.parseDouble(fdBioanalysisSampleMilestones.getOverallProgress());
        //查询合同里程碑比例
        LambdaQueryWrapper<FdContractProportion> fdContractClauseLambdaQueryWrapper=new LambdaQueryWrapper<>();
        fdContractClauseLambdaQueryWrapper.eq(FdContractProportion::getContractId,fdBioanalysisSampleMilestonesById.getPkContract());
        //比例集合
        List<FdContractProportion> fdContractProportionList=iFdContractProportionService.list(fdContractClauseLambdaQueryWrapper);
        if(!fdContractProportionList.isEmpty()&&fdContractProportionList.size()>0){
            double bioPro=0.00;
            //循环比例集合
            for(FdContractProportion fdContractProportion:fdContractProportionList){
                //获取生物分析比例
                if(fdContractProportion.getProportionItem().equals("生物分析")){
                    String bioProS=fdContractProportion.getProportion().replace("%","");
                    //生物分析比例
                    bioPro=Double.parseDouble(bioProS);
                    //获取根据合同比例算出的总进度
                    fdBioanalysisSampleMilestones.setContractOverallProgress(String.format("%.2f",(bioPro*overallProgress)/100));
                }
            }
        }

        //记录项目进度有变化的里程碑项目
        saveMilestoneProgressChange(fdBioanalysisSampleMilestones,fdBioanalysisSampleMilestonesById);
    }

    /***
     * @Title:记录进度变化的里程碑
     * @Description:TODO
     * @param fdBioanalysisSampleMilestones, fdBioanalysisSampleMilestonesById]
     * @return void
     * @exception
     * @author 陈创
     * @date 2019/9/24 16:26
     * @updatehistory:
     *   2019/9/24 16:26 新增
     */
    public void saveMilestoneProgressChange(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones,FdBioanalysisSampleMilestones fdBioanalysisSampleMilestonesById){
        if(StringUtils.isNotBlank(fdBioanalysisSampleMilestones.getOverallProgress())){
            //判断现在的项目进度是否与之前进度不一样
            if(!fdBioanalysisSampleMilestonesById.getOverallProgress().equals(fdBioanalysisSampleMilestones.getOverallProgress())){
                FdMilestonesProgressChange fdMilestonesProgressChange=new FdMilestonesProgressChange();
                fdMilestonesProgressChange.setMilestonesId(fdBioanalysisSampleMilestones.getId());
                fdMilestonesProgressChange.setChangeTime(new Date());
                iFdMilestonesProgressChangeService.save(fdMilestonesProgressChange);
            }
        }

    }


    /***
     * @Title:生物分析里程碑进度变化报表
            * @Description:TODO
            * @param endDate, endDate]
            * @return java.util.List<org.jeecg.modules.projectManagement.entity.FdBioanalysisSampleMilestones>
            * @exception
            * @author 陈创
            * @date 2019/9/24 18:26
            * @updatehistory:
     *   2019/9/24 18:26 新增
            */
    @Override
    public List<FdBioanalysisSampleMilestones> progressReport(String startDate, String endDate) {
        //记录进度变动的数据
        List<FdMilestonesProgressChange> fdMilestonesProgressChangeList=fdMilestonesProgressChangeMapper.getByChangeTime(startDate,endDate);
        //全部里程碑进度
        LambdaQueryWrapper<FdBioanalysisSampleMilestones> queryWrapperBioMil=new LambdaQueryWrapper<>();
        queryWrapperBioMil.eq(FdBioanalysisSampleMilestones::getDelFlag,"0");
        List<FdBioanalysisSampleMilestones> fdBioanalysisSampleMilestonesList=fdBioanalysisSampleMilestonesService.list(queryWrapperBioMil);
        //获取报表数据
        List<FdBioanalysisSampleMilestones> fdBioanalysisSampleMilestonesListResult=new ArrayList<>();
        if(!fdMilestonesProgressChangeList.isEmpty()&&fdMilestonesProgressChangeList.size()>0&&!fdBioanalysisSampleMilestonesList.isEmpty()&&fdBioanalysisSampleMilestonesList.size()>0){
            bioProgressList(fdMilestonesProgressChangeList,fdBioanalysisSampleMilestonesList,fdBioanalysisSampleMilestonesListResult);
        }
        return fdBioanalysisSampleMilestonesListResult;
    }

    @Override
    public List<FdBioanalysisSampleMilestones> getMainExport() {
        return fdBioanalysisSampleMilestonesMapper.getMainExport();
    }

    /***
     * @Title:获取进度有变化的里程碑数据
            * @Description:TODO
            * @param fdMilestonesProgressChangeList, fdBioanalysisSampleMilestonesList, fdBioanalysisSampleMilestonesListResult]
            * @return void
            * @exception
            * @author 陈创
            * @date 2019/9/24 18:25
            * @updatehistory:
     *   2019/9/24 18:25 新增
            */
    public void bioProgressList(List<FdMilestonesProgressChange> fdMilestonesProgressChangeList,List<FdBioanalysisSampleMilestones> fdBioanalysisSampleMilestonesList,List<FdBioanalysisSampleMilestones> fdBioanalysisSampleMilestonesListResult){
        //循环进度有变化的记录数据
        for(FdMilestonesProgressChange fdMilestonesProgressChange:fdMilestonesProgressChangeList){
            //变动里程碑id
            String changeMilestonesId=fdMilestonesProgressChange.getMilestonesId();
            //循环全部生物分析里程碑
            for(FdBioanalysisSampleMilestones fdBioanalysisSampleMilestones:fdBioanalysisSampleMilestonesList){
                //生物分析里程碑id
                String bioMilestonesId=fdBioanalysisSampleMilestones.getId();
                //记录中存在则获取
                if(changeMilestonesId.equals(bioMilestonesId)){
                    fdBioanalysisSampleMilestonesListResult.add(fdBioanalysisSampleMilestones);
                }
            }
        }
    }




}
