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.FdQaProcessFacility;
import org.jeecg.modules.qualityControl.entity.FdQaProcessFacilityItems;
import org.jeecg.modules.qualityControl.entity.FdQaQcPictureUpload;
import org.jeecg.modules.qualityControl.mapper.FdQaProcessFacilityMapper;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.jeecg.modules.qualityControl.service.IFdQaBiologicalAnalysisVerificationService;
import org.jeecg.modules.qualityControl.service.IFdQaProcessFacilityItemsService;
import org.jeecg.modules.qualityControl.service.IFdQaProcessFacilityService;
import org.jeecg.modules.qualityControl.service.IFdQaQcPictureUploadService;
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
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.List;

/**
 * @Description: 质量保证（工艺与设备）
 * @author： jeecg-boot
 * @date： 2019-06-06
 * @version： V1.0
 */
@Service
@Transactional
public class FdQaProcessFacilityServiceImpl extends ServiceImpl<FdQaProcessFacilityMapper, FdQaProcessFacility> implements IFdQaProcessFacilityService {

    @Resource
    FdQaProcessFacilityMapper fdQaProcessFacilityMapper;

    @Resource
    IFdQaProcessFacilityService fdQaProcessFacilityService;

    @Resource
    IFdProjectFileService fdProjectFileService;

    @Resource
    IFdQaProcessFacilityItemsService fdQaProcessFacilityItemsService;

    @Resource
    IFdQaBiologicalAnalysisVerificationService fdQaBiologicalAnalysisVerificationService;

    @Resource
    IFdQaQcPictureUploadService iFdQaQcPictureUploadService;

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

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加 质量保证（工艺与设备） 同时添加 质量保证明细
     * @Date 10:57 2019/6/10
     * @Param [fdQaProcessFacility]
     **/
    @Override
    public void addAll(FdQaProcessFacility fdQaProcessFacility) {
        if (fdQaProcessFacility.getId() == null || fdQaProcessFacility.getId() == "") {
            fdQaProcessFacility.setDelFlag("0");
            save(fdQaProcessFacility);
        } else {
            updateById(fdQaProcessFacility);
            //删除质量保证明细
            LambdaQueryWrapper<FdQaProcessFacilityItems> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQaProcessFacilityItems::getPkQaProcessFacility,fdQaProcessFacility.getId());
            fdQaProcessFacilityItemsService.remove(queryWrapper2);
        }
        if (fdQaProcessFacility.getFdQaProcessFacilityItemsList()!=null){
            int sort=1;
            for (FdQaProcessFacilityItems fdQaProcessFacilityItems:fdQaProcessFacility.getFdQaProcessFacilityItemsList()){
                fdQaProcessFacilityItems.setId("");
                fdQaProcessFacilityItems.setSort(sort);
                String approve = fdQaBiologicalAnalysisVerificationService.getApproveByDict(fdQaProcessFacilityItems.getManagementApprove());
                String qaReview = fdQaBiologicalAnalysisVerificationService.getQAReviewByDict(fdQaProcessFacilityItems.getQaReview());
                fdQaProcessFacilityItems.setManagementApproveName(approve);
                fdQaProcessFacilityItems.setQaReviewName(qaReview);
                fdQaProcessFacilityItems.setPkQaProcessFacility(fdQaProcessFacility.getId());
                sort++;
            }
            //批量保存
            fdQaProcessFacilityItemsService.saveBatch(fdQaProcessFacility.getFdQaProcessFacilityItemsList());
            //保存明细对象对应的图片数据集合
            pictureUploadSave(fdQaProcessFacility.getFdQaProcessFacilityItemsList());
        }


    }

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

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

//    /**
//     * @Author gurui
//     * @Description //TODO 根据 id 删除 质量保证（工艺与设备） 同时删除其明细表
//     * @Date 10:36 2019/7/3
//     * @Param [id]
//     * @return boolean
//     **/
//    @Override
//    public boolean deleteAll(String id) {
//        boolean result = true;
//        try {
//            LambdaQueryWrapper<FdQaProcessFacilityItems> queryWrapper2 = new LambdaQueryWrapper<>();
//            queryWrapper2.eq(FdQaProcessFacilityItems::getPkQaProcessFacility,id);
//            fdQaProcessFacilityItemsService.remove(queryWrapper2);
//            fdQaProcessFacilityService.removeById(id);
//        }catch (Exception e){
//            result = false;
//            e.printStackTrace();
//        }
//        return result;
//    }

    /**
     * @Author gurui
     * @Description //TODO 根据 id 删除 质量保证（工艺与设备） 同时删除其明细表 (逻辑删除)
     * @Date 10:36 2019/11/1
     * @Param [id]
     * @return boolean
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            LambdaQueryWrapper<FdQaProcessFacilityItems> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdQaProcessFacilityItems::getPkQaProcessFacility,id);
            List<FdQaProcessFacilityItems> list = fdQaProcessFacilityItemsService.list(queryWrapper2);
            for (FdQaProcessFacilityItems fdQaProcessFacilityItems:list){
                fdQaProcessFacilityItems.setDelFlag("1");
                fdQaProcessFacilityItemsService.updateById(fdQaProcessFacilityItems);
            }
            FdQaProcessFacility fdQaProcessFacility = fdQaProcessFacilityService.getById(id);
            fdQaProcessFacility.setDelFlag("1");
            fdQaProcessFacilityService.updateById(fdQaProcessFacility);
        }catch (Exception e){
            result = false;
            e.printStackTrace();
        }
        return result;
    }

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

        }
    }
}
