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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.projectManagement.entity.FdBioanalysisSampleMilestonesCompletedNumber;
import org.jeecg.modules.projectManagement.entity.FdBioanalysisTemplate;
import org.jeecg.modules.projectManagement.mapper.FdBioanalysisTemplateMapper;
import org.jeecg.modules.projectManagement.service.IFdBioanalysisSampleMilestonesCompletedNumberService;
import org.jeecg.modules.projectManagement.service.IFdBioanalysisTemplateService;
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.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 生物分析里程碑模板
 * @author： jeecg-boot
 * @date：   2019-08-05
 * @version： V1.0
 */
@Transactional
@Service
public class FdBioanalysisTemplateServiceImpl extends ServiceImpl<FdBioanalysisTemplateMapper, FdBioanalysisTemplate> implements IFdBioanalysisTemplateService {

    @Resource
    FdBioanalysisTemplateMapper fdBioanalysisTemplateMapper;
    @Resource
    IFdBioanalysisTemplateService fdBioanalysisTemplateService;
    @Resource
    IFdBioanalysisSampleMilestonesCompletedNumberService iFdBioanalysisSampleMilestonesCompletedNumberService;

    /***
     * @Title:根据项目id获取模板数据
            * @Description:TODO
            * @param id
            * @return java.util.List<org.jeecg.modules.projectManagement.entity.FdBioanalysisTemplate>
            * @exception
            * @author 陈创
            * @date 2019/8/5 16:41
            * @updatehistory:
     *   2019/8/5 16:41 新增
            */
    @Override
    public List<FdBioanalysisTemplate> getTempletas(String id){
        if(id!=null&&!id.equals("")){
            LambdaQueryWrapper<FdBioanalysisTemplate> fdBioanalysisTemplateLambdaQueryWrapper=new LambdaQueryWrapper<>();
            //项目id
            fdBioanalysisTemplateLambdaQueryWrapper.eq(FdBioanalysisTemplate::getProjectId,id);
            fdBioanalysisTemplateLambdaQueryWrapper.eq(FdBioanalysisTemplate::getDelFlag,"0");
            fdBioanalysisTemplateLambdaQueryWrapper.orderByAsc(FdBioanalysisTemplate::getSort);
            List<FdBioanalysisTemplate> fdBioanalysisTemplateList=fdBioanalysisTemplateService.list(fdBioanalysisTemplateLambdaQueryWrapper);
            return fdBioanalysisTemplateList;
        }else {
            return null;
        }
    }

    /***
     * @Title:查询里程碑模板tree类型
            * @Description:TODO
            * @param fdBioanalysisTemplateList
            * @return java.util.List<org.jeecg.modules.projectManagement.entity.FdBioanalysisTemplate>
            * @exception 
            * @author 陈创
            * @date 2019/8/6 10:35
            * @updatehistory:
     *   2019/8/6 10:35 新增
            */
    @Override
    public List<FdBioanalysisTemplate> getTempletaTree(List<FdBioanalysisTemplate> fdBioanalysisTemplateList) {
        Map<Object, Object> map=new HashMap<>();
        List<FdBioanalysisTemplate> contract=new ArrayList<>();
        for (FdBioanalysisTemplate fdBioanalysisTemplate:fdBioanalysisTemplateList){
            if(fdBioanalysisTemplate.getParentId()==null||fdBioanalysisTemplate.getParentId().equals("")){
                map.put("keyId",fdBioanalysisTemplate.getKeyId());
                contract.add(fdBioanalysisTemplate);
            }
        }
        //循环合同模板类型找到子集化合物模板类型
        for(FdBioanalysisTemplate fdBioanalysisTemplate:contract){
            List<FdBioanalysisTemplate> compoundList=new ArrayList<>();
            //添加化合物类型
            for(FdBioanalysisTemplate fdBioanalysisTemplate1:fdBioanalysisTemplateList){
                if(fdBioanalysisTemplate.getKeyId().equals(fdBioanalysisTemplate1.getParentId())){
                    compoundList.add(fdBioanalysisTemplate1);

                }
            }
            fdBioanalysisTemplate.setFdBioanalysisTemplateList(compoundList);
        }
        for(FdBioanalysisTemplate fdBioanalysisTemplate:contract){
            //循环化合物找到子集基质
            for(FdBioanalysisTemplate fdBioanalysisTemplate1:fdBioanalysisTemplate.getFdBioanalysisTemplateList()){
                //添加基质类型
                List<FdBioanalysisTemplate> maxList=new ArrayList<>();
                for(FdBioanalysisTemplate fdBioanalysisTemplate2:fdBioanalysisTemplateList){
                    if(fdBioanalysisTemplate1.getKeyId().equals(fdBioanalysisTemplate2.getParentId())){
                        maxList.add(fdBioanalysisTemplate2);
                    }
                }
                fdBioanalysisTemplate1.setFdBioanalysisTemplateList(maxList);
            }
        }
        return contract;
    }

    @Override
    public int updateTemplateBatch(List<FdBioanalysisTemplate> templateList) {
        LambdaQueryWrapper<FdBioanalysisSampleMilestonesCompletedNumber> numberLambdaQueryWrapper=new LambdaQueryWrapper<>();
        numberLambdaQueryWrapper.eq(FdBioanalysisSampleMilestonesCompletedNumber::getPkBioanalysisSampleMilestones,templateList.get(0).getProjectId());
        numberLambdaQueryWrapper.eq(FdBioanalysisSampleMilestonesCompletedNumber::getDelFlag,"0");
        List<FdBioanalysisSampleMilestonesCompletedNumber> numberList=iFdBioanalysisSampleMilestonesCompletedNumberService.list(numberLambdaQueryWrapper);

        for(FdBioanalysisSampleMilestonesCompletedNumber number:numberList){
            //基质id
            String matrixId=number.getMatrixTypeId();
            for(FdBioanalysisTemplate fdBioanalysisTemplate:templateList){
                //模板类型中的基质id
                String matrixIdTemp=fdBioanalysisTemplate.getKeyId();
                if(matrixId.equals(matrixIdTemp)){
                    //获取更新的比例
                    number.setMatrixProportion(Double.valueOf(fdBioanalysisTemplate.getProportion()));
                    //获取更新的完成数量
                    number.setTotalNumber(fdBioanalysisTemplate.getSamplesTotal());
                }
            }
        }
        //计算进度
        updateNumberProportion(numberList);

        //批量修改选择的模板类型的比例与完成数量
        fdBioanalysisTemplateMapper.updateTemplateBatch(templateList);
        if(numberList!=null&&numberList.size()>0){
            //批量修改完成数量的当月进度数据
            iFdBioanalysisSampleMilestonesCompletedNumberService.updateCompletedNumberBatch(numberList);
        }

        return 0;
    }

    /***
     * @Title:更新完成数量与比例后计算单月进度与占项目进度
            * @Description:TODO
            * @param [fdBioanalysisSampleMilestonesCompletedNumberList]
            * @return void
            * @exception
            * @author 陈创
            * @date 2019/11/26 11:20
            * @updatehistory:
     *   2019/11/26 11:20 新增
            */
    public void updateNumberProportion(List<FdBioanalysisSampleMilestonesCompletedNumber> fdBioanalysisSampleMilestonesCompletedNumberList){
        for(FdBioanalysisSampleMilestonesCompletedNumber number:fdBioanalysisSampleMilestonesCompletedNumberList){
            Double currentNumber=0.00;
            if(StringUtils.isNotBlank(number.getCurrentNumber())){
                currentNumber=Double.parseDouble(number.getCurrentNumber());//单月完成数量
            }
            Double totalNumber=0.00;
            if(StringUtils.isNotBlank(number.getTotalNumber())){
                totalNumber=Double.parseDouble(number.getTotalNumber());//总完成数量
            }
            // Double monthlyPercentage=0.00;//当月完成百分比
            DecimalFormat df = new DecimalFormat("0.00");
            //获取当月百分比
            number.setMonthlyPercentage(df.format((currentNumber/totalNumber)*100));
            Double monthlyPercentage=Double.parseDouble(number.getMonthlyPercentage());//单月百分比
            Double matrixProportion=number.getMatrixProportion();//基质比例
            Double compoundProportion=number.getCompoundProportion();//化合物比例
            //获取占项目比
            number.setAccumulatedPercentage(df.format((monthlyPercentage*matrixProportion*compoundProportion)/10000));
        }
    }
}
