package org.jeecg.modules.qualityControl.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.jeecg.modules.basedata.entity.FdPersonnel;
import org.jeecg.modules.basedata.service.IFdPersonnelService;
import org.jeecg.modules.projectManagement.entity.FdContractClause;
import org.jeecg.modules.projectManagement.service.IFdContractClauseService;
import org.jeecg.modules.qualityControl.entity.*;
import org.jeecg.modules.qualityControl.mapper.FdQcBiologicalAnalysisVerificationMapper;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.jeecg.modules.qualityControl.service.*;
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.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 质量控制（生物分析验证）
 * @author： jeecg-boot
 * @date： 2019-06-12
 * @version： V1.0
 */
@Service
@Transactional
public class FdQcBiologicalAnalysisVerificationServiceImpl extends ServiceImpl<FdQcBiologicalAnalysisVerificationMapper, FdQcBiologicalAnalysisVerification> implements IFdQcBiologicalAnalysisVerificationService {

    @Resource
    IFdQcBiologicalAnalysisVerificationService fdQcBiologicalAnalysisVerificationService;

    @Resource
    IFdQcBiologicalAnalysisVerificationItemsService fdQcBiologicalAnalysisVerificationItemsService;

    @Resource
    IFdQcValidationService fdQcValidationService;

    @Resource
    FdQcBiologicalAnalysisVerificationMapper fdQcBiologicalAnalysisVerificationMapper;

    @Resource
    IFdQaBiologicalAnalysisVerificationService fdQaBiologicalAnalysisVerificationService;

    @Resource
    IFdQaQcPictureUploadService iFdQaQcPictureUploadService;


    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加质量控制（生物分析验证） 同时添加 明细
     * @Date 9:59 2019/6/13
     * @Param [fdQcBiologicalAnalysisVerification]
     **/
    @Override
    public void addAll(FdQcBiologicalAnalysisVerification fdQcBiologicalAnalysisVerification) {
        if (fdQcBiologicalAnalysisVerification.getId() == null || fdQcBiologicalAnalysisVerification.getId() == "") {
            fdQcBiologicalAnalysisVerification.setDelFlag("0");
            save(fdQcBiologicalAnalysisVerification);
        } else {
            updateById(fdQcBiologicalAnalysisVerification);
            //删除明细
            LambdaQueryWrapper<FdQcBiologicalAnalysisVerificationItems> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdQcBiologicalAnalysisVerificationItems::getPkBiologicalAnalysisVerification, fdQcBiologicalAnalysisVerification.getId());
            fdQcBiologicalAnalysisVerificationItemsService.remove(queryWrapper1);
            //删除生物分析验证号
            LambdaQueryWrapper<FdQcValidation> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQcValidation::getPkQcValidation, fdQcBiologicalAnalysisVerification.getId());
            fdQcValidationService.remove(queryWrapper2);
        }

        if (fdQcBiologicalAnalysisVerification.getFdQcBiologicalAnalysisVerificationItemsList() != null) {
            int sort=1;
            for (FdQcBiologicalAnalysisVerificationItems fdQcBiologicalAnalysisVerificationItems : fdQcBiologicalAnalysisVerification.getFdQcBiologicalAnalysisVerificationItemsList()) {
                fdQcBiologicalAnalysisVerificationItems.setId("");
                fdQcBiologicalAnalysisVerificationItems.setSort(sort);
                String approve = fdQaBiologicalAnalysisVerificationService.getApproveByDict(fdQcBiologicalAnalysisVerificationItems.getManagementApprove());
                String qcReview = fdQaBiologicalAnalysisVerificationService.getQCReviewByDict(fdQcBiologicalAnalysisVerificationItems.getQcReview());
                fdQcBiologicalAnalysisVerificationItems.setManagementApproveName(approve);
                fdQcBiologicalAnalysisVerificationItems.setQcReviewName(qcReview);
                // fdQcBiologicalAnalysisVerificationItems.setResponseDate(new Date());
                fdQcBiologicalAnalysisVerificationItems.setPkBiologicalAnalysisVerification(fdQcBiologicalAnalysisVerification.getId());
                sort++;
            }
            fdQcBiologicalAnalysisVerificationItemsService.saveBatch(fdQcBiologicalAnalysisVerification.getFdQcBiologicalAnalysisVerificationItemsList());
            //保存图片上传文件
            pictureUploadSave(fdQcBiologicalAnalysisVerification.getFdQcBiologicalAnalysisVerificationItemsList());
        }


        if (fdQcBiologicalAnalysisVerification.getFdQcValidationList() != null) {
            int sort = 1;
            for (FdQcValidation fdQcValidation : fdQcBiologicalAnalysisVerification.getFdQcValidationList()) {
                fdQcValidation.setId("");
                fdQcValidation.setSort(sort + "");
                fdQcValidation.setPkQcValidation(fdQcBiologicalAnalysisVerification.getId());
                sort++;
            }
            fdQcValidationService.saveBatch(fdQcBiologicalAnalysisVerification.getFdQcValidationList());
        }


    }

    /**
    * @Author chenchuang
    * @Description //TODO 明细图片保存
    * @Date 2020/6/19 9:26
    * @Param [fdQcBiologicalAnalysisVerificationItemsList]
    * @return void
    */
    public void pictureUploadSave(List<FdQcBiologicalAnalysisVerificationItems> fdQcBiologicalAnalysisVerificationItemsList){
        //循环明细
        for(FdQcBiologicalAnalysisVerificationItems fdQcBiologicalAnalysisVerificationItems:fdQcBiologicalAnalysisVerificationItemsList){
            //图片集合是否为空
            if(fdQcBiologicalAnalysisVerificationItems.getFdQaQcPictureUploadList()!=null&&fdQcBiologicalAnalysisVerificationItems.getFdQaQcPictureUploadList().size()>0){

                int sort=1;
                for(FdQaQcPictureUpload fdQaQcPictureUpload:fdQcBiologicalAnalysisVerificationItems.getFdQaQcPictureUploadList()){
                    fdQaQcPictureUpload.setId(null);
                    fdQaQcPictureUpload.setProjectId(fdQcBiologicalAnalysisVerificationItems.getId());//获取到对应明细id
                    fdQaQcPictureUpload.setSort(sort);
                    sort++;
                }
                iFdQaQcPictureUploadService.saveBatch(fdQcBiologicalAnalysisVerificationItems.getFdQaQcPictureUploadList());
            }
        }


    }

    @Resource
    private IFdPersonnelService fdPersonnelService;

    @Resource
    private IFdContractClauseService fdContractClauseService;

    @Override
    public FdQcBiologicalAnalysisVerification queryQcBiologicalByConId(String pkProjectFile) {
        FdQcBiologicalAnalysisVerification fdQcBiologicalAnalysisVerification = fdQcBiologicalAnalysisVerificationMapper.queryQcBiologicalByConId(pkProjectFile);
        FdPersonnel personnel = fdPersonnelService.getByIdPersonnel(fdQcBiologicalAnalysisVerification.getBd());
        if(personnel!=null) {
            fdQcBiologicalAnalysisVerification.setBd(personnel.getName());
        }
        FdPersonnel personnels = fdPersonnelService.getByIdPersonnel(fdQcBiologicalAnalysisVerification.getInvestigator());
        if(personnels!=null) {
            fdQcBiologicalAnalysisVerification.setInvestigator(personnels.getName());
        }
        FdPersonnel personneles = fdPersonnelService.getByIdPersonnel(fdQcBiologicalAnalysisVerification.getClinicalPm());
        if(personneles!=null) {
            fdQcBiologicalAnalysisVerification.setClinicalPm(personneles.getName());
        }

        //  查询收款节点
        QueryWrapper<FdContractClause> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contract_id", fdQcBiologicalAnalysisVerification.getPkContract());
        List<FdContractClause> list = fdContractClauseService.list(queryWrapper);
        fdQcBiologicalAnalysisVerification.setFdContractClauseList(list);

        return fdQcBiologicalAnalysisVerification;
    }

//    //删除全部(物理)
//    @Override
//    public boolean deleteAll(String id) {
//        boolean result = true;
//        try {
//            LambdaQueryWrapper<FdQcBiologicalAnalysisVerificationItems> queryWrapper2 = new LambdaQueryWrapper<>();
//            queryWrapper2.eq(FdQcBiologicalAnalysisVerificationItems::getPkBiologicalAnalysisVerification, id);
//            fdQcBiologicalAnalysisVerificationItemsService.remove(queryWrapper2);
//            LambdaQueryWrapper<FdQcValidation> queryWrapper1 = new LambdaQueryWrapper<>();
//            queryWrapper1.eq(FdQcValidation::getPkQcValidation,id);
//            fdQcValidationService.remove(queryWrapper1);
//            fdQcBiologicalAnalysisVerificationService.removeById(id);
//        } catch (Exception e) {
//            result = false;
//            e.printStackTrace();
//        }
//        return result;
//    }


    /**
     * @Author gurui
     * @Description //TODO 逻辑删除
     * @Date 下午 1:31 1/11/2019
     * @Param [id]
     * @return boolean
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            LambdaQueryWrapper<FdQcBiologicalAnalysisVerificationItems> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQcBiologicalAnalysisVerificationItems::getPkBiologicalAnalysisVerification, id);
            List<FdQcBiologicalAnalysisVerificationItems> list = fdQcBiologicalAnalysisVerificationItemsService.list(queryWrapper2);
            for (FdQcBiologicalAnalysisVerificationItems fdQcBiologicalAnalysisVerificationItems:list){
                fdQcBiologicalAnalysisVerificationItems.setDelFlag("1");
                fdQcBiologicalAnalysisVerificationItemsService.updateById(fdQcBiologicalAnalysisVerificationItems);
            }
            LambdaQueryWrapper<FdQcValidation> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdQcValidation::getPkQcValidation,id);
            List<FdQcValidation> list1 = fdQcValidationService.list(queryWrapper1);
            for (FdQcValidation fdQcValidation :list1){
                fdQcValidation.setDelFlag("1");
                fdQcValidationService.updateById(fdQcValidation);
            }
            FdQcBiologicalAnalysisVerification fdQcBiologicalAnalysisVerification = fdQcBiologicalAnalysisVerificationService.getById(id);
            fdQcBiologicalAnalysisVerification.setDelFlag("1");
            fdQcBiologicalAnalysisVerificationService.updateById(fdQcBiologicalAnalysisVerification);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

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

    @Override
    public FdQcBiologicalAnalysisVerification getByVerifiCodeAndType(FdQcBiologicalAnalysisVerification fdQcBiologicalAnalysisVerification) {
        return fdQcBiologicalAnalysisVerificationMapper.getByVerifiCodeAndType(fdQcBiologicalAnalysisVerification);
    }

    @Override
    public void getPictureUpload(List<FdQcBiologicalAnalysisVerificationItems> itemsList){
        if(itemsList!=null&&itemsList.size()>0){
            //获取明细id
            List<String> itemsId=new ArrayList<>();
            for(FdQcBiologicalAnalysisVerificationItems items:itemsList){
                itemsId.add(items.getId());
            }
            //根据明细id查询图片文件
            LambdaQueryWrapper<FdQaQcPictureUpload> uploadLambdaQueryWrapper=new LambdaQueryWrapper<>();
            uploadLambdaQueryWrapper.eq(FdQaQcPictureUpload::getDelFlag,"0");
            uploadLambdaQueryWrapper.in(FdQaQcPictureUpload::getProjectId,itemsId);
            uploadLambdaQueryWrapper.orderByAsc(FdQaQcPictureUpload::getSort);
            List<FdQaQcPictureUpload> fdQaQcPictureUploadList=iFdQaQcPictureUploadService.list(uploadLambdaQueryWrapper);

            //把对应的图片文件查询到明细对象中
            if(fdQaQcPictureUploadList!=null&&fdQaQcPictureUploadList.size()>0){
                for(FdQcBiologicalAnalysisVerificationItems items:itemsList){
                    //获取对应图片对象
                    List<FdQaQcPictureUpload> fdQaQcPictureUploadListGet=new ArrayList<>();
                    for(FdQaQcPictureUpload fdQaQcPictureUpload:fdQaQcPictureUploadList){
                        if(items.getId().equals(fdQaQcPictureUpload.getProjectId())){
                            fdQaQcPictureUploadListGet.add(fdQaQcPictureUpload);
                        }
                    }
                    if(fdQaQcPictureUploadListGet.size()>0){
                        items.setFdQaQcPictureUploadList(fdQaQcPictureUploadListGet);
                    }
                }
            }

        }
    }

}
