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.FdQcBiometricSampleAnalysisMapper;
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 FdQcBiometricSampleAnalysisServiceImpl extends ServiceImpl<FdQcBiometricSampleAnalysisMapper, FdQcBiometricSampleAnalysis> implements IFdQcBiometricSampleAnalysisService {

    @Resource
    IFdQcBiometricSampleAnalysisService fdQcBiometricSampleAnalysisService;

    @Resource
    IFdQcBiometricSampleAnalysisItemsService fdQcBiometricSampleAnalysisItemsService;

    @Resource
    IFdQcSampleService fdQcSampleService;

    @Resource
    FdQcBiometricSampleAnalysisMapper fdQcBiometricSampleAnalysisMapper;

    @Resource
    IFdQaBiologicalAnalysisVerificationService fdQaBiologicalAnalysisVerificationService;

    @Resource
    IFdQaQcPictureUploadService iFdQaQcPictureUploadService;

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加 质量控制（生物样本分析） 同时添加  明细
     * @Date 9:08 2019/6/13
     * @Param [fdQcBiometricSampleAnalysis]
     **/
    @Override
    public void addAll(FdQcBiometricSampleAnalysis fdQcBiometricSampleAnalysis) {
        if (fdQcBiometricSampleAnalysis.getId() == "" || fdQcBiometricSampleAnalysis.getId() == null){
            fdQcBiometricSampleAnalysis.setDelFlag("0");
            fdQcBiometricSampleAnalysisService.save(fdQcBiometricSampleAnalysis);
        }else {
            fdQcBiometricSampleAnalysisService.updateById(fdQcBiometricSampleAnalysis);
            //删除明细
            LambdaQueryWrapper<FdQcBiometricSampleAnalysisItems> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdQcBiometricSampleAnalysisItems::getPkQcBiometricSampleAnalysis,fdQcBiometricSampleAnalysis.getId());
            fdQcBiometricSampleAnalysisItemsService.remove(queryWrapper1);
            //删除生物样本分析号
            LambdaQueryWrapper<FdQcSample> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQcSample::getPkQcSample, fdQcBiometricSampleAnalysis.getId());
            fdQcSampleService.remove(queryWrapper2);
        }
        if (fdQcBiometricSampleAnalysis.getFdQcBiometricSampleAnalysisItemsList()!=null){
           int sort=1;
            for (FdQcBiometricSampleAnalysisItems fdQcBiometricSampleAnalysisItems:fdQcBiometricSampleAnalysis.getFdQcBiometricSampleAnalysisItemsList()){
                fdQcBiometricSampleAnalysisItems.setId("");
                fdQcBiometricSampleAnalysisItems.setSort(sort);
                String approve = fdQaBiologicalAnalysisVerificationService.getApproveByDict(fdQcBiometricSampleAnalysisItems.getManagementApprove());
                String qcReview = fdQaBiologicalAnalysisVerificationService.getQCReviewByDict(fdQcBiometricSampleAnalysisItems.getQcReview());
                fdQcBiometricSampleAnalysisItems.setManagementApproveName(approve);
                fdQcBiometricSampleAnalysisItems.setQcReviewName(qcReview);
                // fdQcBiometricSampleAnalysisItems.setResponseDate(new Date());
                fdQcBiometricSampleAnalysisItems.setPkQcBiometricSampleAnalysis(fdQcBiometricSampleAnalysis.getId());
                sort++;
            }
            fdQcBiometricSampleAnalysisItemsService.saveBatch(fdQcBiometricSampleAnalysis.getFdQcBiometricSampleAnalysisItemsList());
            //获取明细对应图片集合
            pictureUploadSave(fdQcBiometricSampleAnalysis.getFdQcBiometricSampleAnalysisItemsList());
        }
        if (fdQcBiometricSampleAnalysis.getFdQcSampleList()!=null){
            int sort = 1;
            for (FdQcSample fdQcSample:fdQcBiometricSampleAnalysis.getFdQcSampleList()){
                fdQcSample.setId("");
                fdQcSample.setSort(""+sort);
                fdQcSample.setPkQcSample(fdQcBiometricSampleAnalysis.getId());
                sort++;
            }
            fdQcSampleService.saveBatch(fdQcBiometricSampleAnalysis.getFdQcSampleList());
        }




    }

    /**
     * @Author chenchuang
     * @Description //TODO 明细图片保存
     * @Date 2020/6/19 9:26
     * @Param [fdQcBiologicalAnalysisVerificationItemsList]
     * @return void
     */
    public void pictureUploadSave(List<FdQcBiometricSampleAnalysisItems> itemsList){
        //循环明细
        for(FdQcBiometricSampleAnalysisItems 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 FdQcBiometricSampleAnalysis queryQcBiometricByConId(String pkProjectFile) {
        return fdQcBiometricSampleAnalysisMapper.queryQcBiometricByConId(pkProjectFile);
    }

//    /**
//     * @Author gurui
//     * @Description //TODO 删除 质量控制（生物样本分析） 同时删除 明细
//     * @Date 14:29 2019/7/3
//     * @Param [id]
//     * @return boolean
//     **/
//    @Override
//    public boolean deleteAll(String id) {
//        boolean result = true;
//        try {
//            LambdaQueryWrapper<FdQcBiometricSampleAnalysisItems> queryWrapper2 = new LambdaQueryWrapper<>();
//            queryWrapper2.eq(FdQcBiometricSampleAnalysisItems::getPkQcBiometricSampleAnalysis,id);
//            fdQcBiometricSampleAnalysisItemsService.remove(queryWrapper2);
//            LambdaQueryWrapper<FdQcSample> queryWrapper1 = new LambdaQueryWrapper<>();
//            queryWrapper1.eq(FdQcSample::getPkQcSample,id);
//            fdQcSampleService.remove(queryWrapper1);
//            fdQcBiometricSampleAnalysisService.removeById(id);
//        }catch (Exception e){
//            e.printStackTrace();
//            result = false;
//        }
//        return result;
//    }

    /**
     * @Author gurui
     * @Description //TODO 删除 质量控制（生物样本分析） 同时删除 明细  (逻辑删除)
     * @Date 14:29 2019/7/3
     * @Param [id]
     * @return boolean
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            LambdaQueryWrapper<FdQcBiometricSampleAnalysisItems> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQcBiometricSampleAnalysisItems::getPkQcBiometricSampleAnalysis,id);
            List<FdQcBiometricSampleAnalysisItems> list = fdQcBiometricSampleAnalysisItemsService.list(queryWrapper2);
            for (FdQcBiometricSampleAnalysisItems fdQcBiometricSampleAnalysisItems:list){
                fdQcBiometricSampleAnalysisItems.setDelFlag("1");
                fdQcBiometricSampleAnalysisItemsService.updateById(fdQcBiometricSampleAnalysisItems);
            }
            LambdaQueryWrapper<FdQcSample> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdQcSample::getPkQcSample,id);
            List<FdQcSample> list1 = fdQcSampleService.list(queryWrapper1);
            for (FdQcSample fdQcSample:list1){
                fdQcSample.setDelFlag("1");
                fdQcSampleService.updateById(fdQcSample);
            }
            FdQcBiometricSampleAnalysis fdQcBiometricSampleAnalysis = fdQcBiometricSampleAnalysisService.getById(id);
            fdQcBiometricSampleAnalysis.setDelFlag("1");
            fdQcBiometricSampleAnalysisService.updateById(fdQcBiometricSampleAnalysis);
        }catch (Exception e){
            e.printStackTrace();
            result = false;
        }
        return result;
    }

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

    @Override
    public FdQcBiometricSampleAnalysis getBySampleCodeAndType(FdQcBiometricSampleAnalysis fdQcBiometricSampleAnalysis) {
        return fdQcBiometricSampleAnalysisMapper.getBySampleCodeAndType(fdQcBiometricSampleAnalysis);
    }

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

        }
    }
}
