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

import com.baomidou.mybatisplus.annotation.TableField;
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.FdQaBiometricSampleAnalysisMapper;
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-11
 * @version： V1.0
 */
@Service
@Transactional
public class FdQaBiometricSampleAnalysisServiceImpl extends ServiceImpl<FdQaBiometricSampleAnalysisMapper, FdQaBiometricSampleAnalysis> implements IFdQaBiometricSampleAnalysisService {

    @Resource
    IFdQaBiometricSampleAnalysisService fdQaBiometricSampleAnalysisService;

    @Resource
    IFdQaBiometricSampleAnalysisItemsService fdQaBiometricSampleAnalysisItemsService;

    @Resource
    IFdQaSampleService fdQaSampleService;

    @Resource
    FdQaBiometricSampleAnalysisMapper fdQaBiometricSampleAnalysisMapper;

    @Resource
    IFdQaBiologicalAnalysisVerificationService fdQaBiologicalAnalysisVerificationService;

    @Resource
    IFdQaQcPictureUploadService iFdQaQcPictureUploadService;

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加 质量保证（生物样本分析） 同时添加 明细
     * @Date 14:32 2019/6/11
     * @Param [fdQaBiometricSampleAnalysis]
     **/
    @Override
    public void addAll(FdQaBiometricSampleAnalysis fdQaBiometricSampleAnalysis) {
        if (fdQaBiometricSampleAnalysis.getId() == null || fdQaBiometricSampleAnalysis.getId() == "") {
            fdQaBiometricSampleAnalysis.setDelFlag("0");
            save(fdQaBiometricSampleAnalysis);
        } else {
            updateById(fdQaBiometricSampleAnalysis);
            //删除明细
            LambdaQueryWrapper<FdQaBiometricSampleAnalysisItems> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdQaBiometricSampleAnalysisItems::getPkQaBiometricSampleAnalysis, fdQaBiometricSampleAnalysis.getId());
            fdQaBiometricSampleAnalysisItemsService.remove(queryWrapper1);
            //删除生物样本分析号
            LambdaQueryWrapper<FdQaSample> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQaSample::getPkQaSample, fdQaBiometricSampleAnalysis.getId());
            fdQaSampleService.remove(queryWrapper2);
        }
        if (fdQaBiometricSampleAnalysis.getFdQaBiometricSampleAnalysisItemsList() != null) {
            int sort=1;
            for (FdQaBiometricSampleAnalysisItems fdQaBiometricSampleAnalysisItems : fdQaBiometricSampleAnalysis.getFdQaBiometricSampleAnalysisItemsList()) {
                fdQaBiometricSampleAnalysisItems.setId("");
                fdQaBiometricSampleAnalysisItems.setSort(sort);
                String approve = fdQaBiologicalAnalysisVerificationService.getApproveByDict(fdQaBiometricSampleAnalysisItems.getManagementApprove());
                String qaReview = fdQaBiologicalAnalysisVerificationService.getQAReviewByDict(fdQaBiometricSampleAnalysisItems.getQaReview());
                fdQaBiometricSampleAnalysisItems.setManagementApproveName(approve);
                fdQaBiometricSampleAnalysisItems.setQaReviewName(qaReview);
                // fdQaBiometricSampleAnalysisItems.setResponseDate(new Date());
                fdQaBiometricSampleAnalysisItems.setPkQaBiometricSampleAnalysis(fdQaBiometricSampleAnalysis.getId());
                sort++;
            }
            fdQaBiometricSampleAnalysisItemsService.saveBatch(fdQaBiometricSampleAnalysis.getFdQaBiometricSampleAnalysisItemsList());
            //保存明细对象对应的图片集合数据
            pictureUploadSave(fdQaBiometricSampleAnalysis.getFdQaBiometricSampleAnalysisItemsList());
        }
        if (fdQaBiometricSampleAnalysis.getFdQaSampleList() != null) {
            int sort = 1;
            for (FdQaSample fdQaSample : fdQaBiometricSampleAnalysis.getFdQaSampleList()) {
                fdQaSample.setId("");
                fdQaSample.setSort(sort + "");
                fdQaSample.setPkQaSample(fdQaBiometricSampleAnalysis.getId());
                sort++;
            }
            fdQaSampleService.saveBatch(fdQaBiometricSampleAnalysis.getFdQaSampleList());
        }


    }

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

    /**
     * @return org.jeecg.modules.qualityControl.entity.FdQaBiometricSampleAnalysis
     * @Author gurui
     * @Description //TODO 根据合同id查询项目档案信息
     * @Date 13:27 2019/7/3
     * @Param [contractId]
     **/
    @Override
    public FdQaBiometricSampleAnalysis queryQaBiometricByConId(String contractId) {
        return fdQaBiometricSampleAnalysisMapper.queryQaBiometricByConId(contractId);
    }

//    /**
//     * @return boolean
//     * @Author gurui
//     * @Description //TODO 删除 质量保证（生物样本分析） 同时 删除其明细
//     * @Date 13:34 2019/7/3
//     * @Param [id]
//     **/
//    @Override
//    public boolean deleteAll(String id) {
//        boolean result = true;
//        try {
//            LambdaQueryWrapper<FdQaBiometricSampleAnalysisItems> queryWrapper1 = new LambdaQueryWrapper<>();
//            queryWrapper1.eq(FdQaBiometricSampleAnalysisItems::getPkQaBiometricSampleAnalysis, id);
//            fdQaBiometricSampleAnalysisItemsService.remove(queryWrapper1);
//            LambdaQueryWrapper<FdQaSample> queryWrapper2 = new LambdaQueryWrapper<>();
//            queryWrapper2.eq(FdQaSample::getPkQaSample,id);
//            fdQaSampleService.remove(queryWrapper2);
//            fdQaBiometricSampleAnalysisService.removeById(id);
//        } catch (Exception e) {
//            result = false;
//            e.printStackTrace();
//        }
//        return result;
//    }

    /**
     * @return boolean
     * @Author gurui
     * @Description //TODO 删除 质量保证（生物样本分析） 同时 删除其明细 (逻辑删除)
     * @Date 13:34 2019/11/1
     * @Param [id]
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            LambdaQueryWrapper<FdQaBiometricSampleAnalysisItems> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdQaBiometricSampleAnalysisItems::getPkQaBiometricSampleAnalysis, id);
            List<FdQaBiometricSampleAnalysisItems> list = fdQaBiometricSampleAnalysisItemsService.list(queryWrapper1);
            for (FdQaBiometricSampleAnalysisItems fdQaBiometricSampleAnalysisItems:list){
                fdQaBiometricSampleAnalysisItems.setDelFlag("1");
                fdQaBiometricSampleAnalysisItemsService.updateById(fdQaBiometricSampleAnalysisItems);
            }
            LambdaQueryWrapper<FdQaSample> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQaSample::getPkQaSample,id);
            List<FdQaSample> list1 = fdQaSampleService.list(queryWrapper2);
            for (FdQaSample fdQaSample:list1){
                fdQaSample.setDelFlag("1");
                fdQaSampleService.updateById(fdQaSample);
            }
            FdQaBiometricSampleAnalysis fdQaBiometricSampleAnalysis = fdQaBiometricSampleAnalysisService.getById(id);
            fdQaBiometricSampleAnalysis.setDelFlag("1");
            fdQaBiometricSampleAnalysisService.updateById(fdQaBiometricSampleAnalysis);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

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

    @Override
    public FdQaBiometricSampleAnalysis getBySampleCodeAndType(FdQaBiometricSampleAnalysis fdQaBiometricSampleAnalysis) {
        return fdQaBiometricSampleAnalysisMapper.getBySampleCodeAndType(fdQaBiometricSampleAnalysis);
    }

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

        }
    }
}
