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

import org.jeecg.modules.projectManagement.entity.FdBioanalysisSampleMilestonesCompletedNumber;
import org.jeecg.modules.projectManagement.mapper.FdBioanalysisSampleMilestonesCompletedNumberMapper;
import org.jeecg.modules.projectManagement.service.IFdBioanalysisSampleMilestonesCompletedNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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

/**
 * @Description: 生物分析样本里程碑当前完成数量
 * @author： jeecg-boot
 * @date：   2019-06-04
 * @version： V1.0
 */
@Service
public class FdBioanalysisSampleMilestonesCompletedNumberServiceImpl extends ServiceImpl<FdBioanalysisSampleMilestonesCompletedNumberMapper, FdBioanalysisSampleMilestonesCompletedNumber> implements IFdBioanalysisSampleMilestonesCompletedNumberService {

    @Resource
    FdBioanalysisSampleMilestonesCompletedNumberMapper fdBioanalysisSampleMilestonesCompletedNumberMapper;

    /***
     * @Title:获取完成数量中填报月份与登记月份不符合的数据
            * @Description:TODO 获取里程碑编码合并行，化合物合并行
            * @param endDate, endDate]
            * @return java.util.List<org.jeecg.modules.projectManagement.entity.FdBioanalysisSampleMilestonesCompletedNumber>
            * @exception
            * @author 陈创
            * @date 2019/9/10 17:48
            * @updatehistory:
     *   2019/9/10 17:48 新增
            */
    @Override
    public List<FdBioanalysisSampleMilestonesCompletedNumber> getMonthlyDiscrepancyReport(String startDate, String endDate) {
        //填报月份与登记月份不符的完成数量数据
        List<FdBioanalysisSampleMilestonesCompletedNumber> numberList=fdBioanalysisSampleMilestonesCompletedNumberMapper.getMonthlyDiscrepancyReport(startDate,endDate);
        if(!numberList.isEmpty()&&numberList.size()>0){
            String milestonesCode="";
            String compound="";
            for(FdBioanalysisSampleMilestonesCompletedNumber number:numberList){
                if(number.getMilestoneCode()==null){
                    number.setMilestoneCodeMergeRow(1);
                }else{
                    if(!milestonesCode.equals(number.getMilestoneCode())){
                        milestonesCode=number.getMilestoneCode();
                        //找到不同里程碑重新对化合物合并
                        compound="";
                        //获取里程碑编码合并行数
                        milestoneCodeMergeRow(number,numberList);
                    }else{
                        number.setMilestoneCodeMergeRow(0);
                    }

                    if(!compound.equals(number.getCompoundId())){
                        compound=number.getCompoundId();
                        //获取化合物合并行数
                        compoundMergeRow(number,numberList);
                    }else{
                        number.setCompoundMergeRow(0);
                    }
                }
            }
        }

        return numberList;
    }


    /***
     * @Title:里程碑编码合并行
            * @Description:TODO
            * @param number, numberList]
            * @return void
            * @exception 
            * @author 陈创
            * @date 2019/9/10 17:37
            * @updatehistory:
     *   2019/9/10 17:37 新增
            */
    public void milestoneCodeMergeRow(FdBioanalysisSampleMilestonesCompletedNumber number,List<FdBioanalysisSampleMilestonesCompletedNumber> numberList){
        Integer milestoneCodeMergeRow=0;
        for(FdBioanalysisSampleMilestonesCompletedNumber number1:numberList){
            if(number.getMilestoneCode().equals(number1.getMilestoneCode())){
                milestoneCodeMergeRow++;
            }
        }
        number.setMilestoneCodeMergeRow(milestoneCodeMergeRow);
    }

    /***
     * @Title:化合物合并行处理
            * @Description:TODO
            * @param number, numberList]
            * @return void
            * @exception
            * @author 陈创
            * @date 2019/9/10 17:39
            * @updatehistory:
     *   2019/9/10 17:39 新增
            */
    public void compoundMergeRow(FdBioanalysisSampleMilestonesCompletedNumber number,List<FdBioanalysisSampleMilestonesCompletedNumber> numberList){
        Integer compoundMergeRow=0;
        String milestinesCode=number.getMilestoneCode();//里程碑编码
        String compoundId=number.getCompoundId();//化合物id
        for(FdBioanalysisSampleMilestonesCompletedNumber number1:numberList){
            //获取对应里程碑下面的化合物合并行数
            if(milestinesCode.equals(number1.getMilestoneCode())&&compoundId.equals(number1.getCompoundId())){
                compoundMergeRow++;
            }
        }
        number.setCompoundMergeRow(compoundMergeRow);
    }

    @Override
    public int updateCompletedNumberBatch(List<FdBioanalysisSampleMilestonesCompletedNumber> completedNumberList) {
        fdBioanalysisSampleMilestonesCompletedNumberMapper.updateCompletedNumberBatch(completedNumberList);
        return 0;
    }
}
