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.qualityControl.entity.*;
import org.jeecg.modules.qualityControl.mapper.FdQaBiologicalAnalysisVerificationMapper;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.jeecg.modules.qualityControl.mapper.FdQaBiometricSampleAnalysisMapper;
import org.jeecg.modules.qualityControl.service.IFdQaBiologicalAnalysisVerificationItemsService;
import org.jeecg.modules.qualityControl.service.IFdQaBiologicalAnalysisVerificationService;
import org.jeecg.modules.qualityControl.service.IFdQaQcPictureUploadService;
import org.jeecg.modules.qualityControl.service.IFdQaValidationService;
import org.springframework.stereotype.Service;

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

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 质量保证（生物分析验证）
 * @author： jeecg-boot
 * @date： 2019-06-11
 * @version： V1.0
 */
@Service
@Transactional
public class FdQaBiologicalAnalysisVerificationServiceImpl extends ServiceImpl<FdQaBiologicalAnalysisVerificationMapper, FdQaBiologicalAnalysisVerification> implements IFdQaBiologicalAnalysisVerificationService {

    @Resource
    IFdQaBiologicalAnalysisVerificationService fdQaBiologicalAnalysisVerificationService;

    @Resource
    IFdQaBiologicalAnalysisVerificationItemsService fdQaBiologicalAnalysisVerificationItemsService;

    @Resource
    IFdProjectFileService fdProjectFileService;

    @Resource
    FdQaBiometricSampleAnalysisMapper fdQaBiometricSampleAnalysisMapper;

    @Resource
    FdQaBiologicalAnalysisVerificationMapper fdQaBiologicalAnalysisVerificationMapper;

    @Resource
    IFdQaValidationService fdQaValidationService;

    @Resource
    IFdQaQcPictureUploadService iFdQaQcPictureUploadService;


    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加质量保证（生物分析验证） 同时添加明细 生物样本分析号表
     * @Date 14:31 2019/6/11
     * @Param [fdQaBiologicalAnalysisVerification]
     **/
    @Override
    public void addAll(FdQaBiologicalAnalysisVerification fdQaBiologicalAnalysisVerification) {
        if (fdQaBiologicalAnalysisVerification.getId() == null || fdQaBiologicalAnalysisVerification.getId() == "") {
            fdQaBiologicalAnalysisVerification.setDelFlag("0");
            save(fdQaBiologicalAnalysisVerification);
        } else {
            updateById(fdQaBiologicalAnalysisVerification);
            //删除明细
            LambdaQueryWrapper<FdQaBiologicalAnalysisVerificationItems> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdQaBiologicalAnalysisVerificationItems::getPkBiologicalAnalysisVerification, fdQaBiologicalAnalysisVerification.getId());
            fdQaBiologicalAnalysisVerificationItemsService.remove(queryWrapper1);
            //删除生物样本分析号
            LambdaQueryWrapper<FdQaValidation> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQaValidation::getPkQaValidation, fdQaBiologicalAnalysisVerification.getId());
            fdQaValidationService.remove(queryWrapper2);

        }
        if (fdQaBiologicalAnalysisVerification.getFdQaBiologicalAnalysisVerificationItemsList() != null) {
            int code = 1;
            int sort =1;
            for (FdQaBiologicalAnalysisVerificationItems fdQaBiologicalAnalysisVerificationItems : fdQaBiologicalAnalysisVerification.getFdQaBiologicalAnalysisVerificationItemsList()) {
                fdQaBiologicalAnalysisVerificationItems.setId("");
                fdQaBiologicalAnalysisVerificationItems.setCode(code+"");
                fdQaBiologicalAnalysisVerificationItems.setSort(sort);
                String approve = fdQaBiologicalAnalysisVerificationService.getApproveByDict(fdQaBiologicalAnalysisVerificationItems.getManagementApprove());
                String qaReview = fdQaBiologicalAnalysisVerificationService.getQAReviewByDict(fdQaBiologicalAnalysisVerificationItems.getQaReview());
                fdQaBiologicalAnalysisVerificationItems.setManagementApproveName(approve);
                fdQaBiologicalAnalysisVerificationItems.setQaReviewName(qaReview);
                // fdQaBiologicalAnalysisVerificationItems.setResponseDate(new Date());
                fdQaBiologicalAnalysisVerificationItems.setPkBiologicalAnalysisVerification(fdQaBiologicalAnalysisVerification.getId());
                code++;
                sort++;
            }
            fdQaBiologicalAnalysisVerificationItemsService.saveBatch(fdQaBiologicalAnalysisVerification.getFdQaBiologicalAnalysisVerificationItemsList());
            //保存明细对象对应图片集合数据
            pictureUploadSave(fdQaBiologicalAnalysisVerification.getFdQaBiologicalAnalysisVerificationItemsList());
        }
        if (fdQaBiologicalAnalysisVerification.getFdQaValidationList() != null) {
            int sort = 1;
            for (FdQaValidation fdQaValidation : fdQaBiologicalAnalysisVerification.getFdQaValidationList()) {
                fdQaValidation.setId("");
                fdQaValidation.setSort(sort + "");
                fdQaValidation.setPkQaValidation(fdQaBiologicalAnalysisVerification.getId());
                sort++;
            }
            fdQaValidationService.saveBatch(fdQaBiologicalAnalysisVerification.getFdQaValidationList());
        }


    }

    /**
     * @Author chenchuang
     * @Description //TODO 明细图片保存
     * @Date 2020/6/19 9:26
     * @Param [fdQcBiologicalAnalysisVerificationItemsList]
     * @return void
     */
    public void pictureUploadSave(List<FdQaBiologicalAnalysisVerificationItems> itemsList){
        //循环明细
        for(FdQaBiologicalAnalysisVerificationItems items:itemsList){
            //图片集合是否为空
            if(items.getFdQaQcPictureUploadList()!=null&&items.getFdQaQcPictureUploadList().size()>0){
                int sort=1;
                for(FdQaQcPictureUpload fdQaQcPictureUpload:items.getFdQaQcPictureUploadList()){
                    fdQaQcPictureUpload.setId(null);
                    fdQaQcPictureUpload.setProjectId(items.getId());//获取到对应明细id
                    fdQaQcPictureUpload.setSort(sort);
                    sort++;
                }
                iFdQaQcPictureUploadService.saveBatch(items.getFdQaQcPictureUploadList());
            }
        }
    }

    @Override
    public FdQaBiologicalAnalysisVerification queryQaBiologicalByConId(String contractId) {
        return fdQaBiologicalAnalysisVerificationMapper.queryQaBiologicalByConId(contractId);
    }

//    /**
//     * @return boolean
//     * @Author gurui
//     * @Description //TODO 删除 质量保证（生物分析验证）同时删除其明细
//     * @Date 13:52 2019/7/3
//     * @Param [id]
//     **/
//    @Override
//    public boolean deleteAll(String id) {
//        boolean result = true;
//        try {
//            LambdaQueryWrapper<FdQaBiologicalAnalysisVerificationItems> queryWrapper1 = new LambdaQueryWrapper<>();
//            queryWrapper1.eq(FdQaBiologicalAnalysisVerificationItems::getPkBiologicalAnalysisVerification, id);
//            fdQaBiologicalAnalysisVerificationItemsService.remove(queryWrapper1);
//            LambdaQueryWrapper<FdQaValidation> queryWrapper2 = new LambdaQueryWrapper<>();
//            queryWrapper2.eq(FdQaValidation::getPkQaValidation,id);
//            fdQaValidationService.remove(queryWrapper2);
//            fdQaBiologicalAnalysisVerificationService.removeById(id);
//        } catch (Exception e) {
//            result = false;
//            e.printStackTrace();
//        }
//        return result;
//    }

    /**
     * @return boolean
     * @Author gurui
     * @Description //TODO 删除 质量保证（生物分析验证）同时删除其明细 (逻辑删除)
     * @Date 13:52 2019/11/1
     * @Param [id]
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            LambdaQueryWrapper<FdQaBiologicalAnalysisVerificationItems> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdQaBiologicalAnalysisVerificationItems::getPkBiologicalAnalysisVerification, id);
            List<FdQaBiologicalAnalysisVerificationItems> list = fdQaBiologicalAnalysisVerificationItemsService.list(queryWrapper1);
            for (FdQaBiologicalAnalysisVerificationItems fdQaBiologicalAnalysisVerificationItems:list){
                fdQaBiologicalAnalysisVerificationItems.setDelFlag("1");
                fdQaBiologicalAnalysisVerificationItemsService.updateById(fdQaBiologicalAnalysisVerificationItems);
            }
            LambdaQueryWrapper<FdQaValidation> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQaValidation::getPkQaValidation,id);
            List<FdQaValidation> list1 = fdQaValidationService.list(queryWrapper2);
            for (FdQaValidation fdQaValidation:list1){
                fdQaValidation.setDelFlag("1");
                fdQaValidationService.updateById(fdQaValidation);
            }
            FdQaBiologicalAnalysisVerification fdQaBiologicalAnalysisVerification = fdQaBiologicalAnalysisVerificationService.getById(id);
            fdQaBiologicalAnalysisVerification.setDelFlag("1");
            fdQaBiologicalAnalysisVerificationService.updateById(fdQaBiologicalAnalysisVerification);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getByDict(String id) {
        return fdQaBiologicalAnalysisVerificationMapper.getByDict(id);
    }

    @Override
    public String getApproveByDict(String id) {
        return fdQaBiologicalAnalysisVerificationMapper.getApproveByDict(id);
    }

    @Override
    public String getQAReviewByDict(String id) {
        return fdQaBiologicalAnalysisVerificationMapper.getQAReviewByDict(id);
    }

    @Override
    public String getQCReviewByDict(String id) {
        return fdQaBiologicalAnalysisVerificationMapper.getQCReviewByDict(id);
    }

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

    @Override
    public FdQaBiologicalAnalysisVerification getByVerifiCodeAndType(FdQaBiologicalAnalysisVerification fdQaBiologicalAnalysisVerification) {
        return fdQaBiologicalAnalysisVerificationMapper.getByVerifiCodeAndType(fdQaBiologicalAnalysisVerification);
    }

    @Override
    public void getPictureUpload(List<FdQaBiologicalAnalysisVerificationItems> itemsList){
        if(itemsList!=null&&itemsList.size()>0){
            //获取明细id
            List<String> itemsId=new ArrayList<>();
            for(FdQaBiologicalAnalysisVerificationItems 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(FdQaBiologicalAnalysisVerificationItems 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);
                    }
                }
            }

        }
    }
}
