package com.fjhx.sale.service.quality.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.common.enums.CodingRuleEnum;
import com.fjhx.common.enums.FlowStatusEnum;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.file.enums.FileTypeEnums;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.sale.entity.quality.po.*;
import com.fjhx.sale.mapper.quality.EhsdQualityMapper;
import com.fjhx.sale.service.quality.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.annotation.LogicIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.sale.entity.quality.vo.EhsdQualityVo;
import com.fjhx.sale.entity.quality.dto.EhsdQualitySelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.sale.entity.quality.dto.EhsdQualityDto;


/**
 * <p>
 * 质检基础信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-05-22
 */
@Service
public class EhsdQualityServiceImpl extends ServiceImpl<EhsdQualityMapper, EhsdQuality> implements EhsdQualityService {

    @Autowired
    private CodingRuleService codingRuleService;

    @Autowired
    private EhsdQualityAqlService ehsdQualityAqlService;

    @Autowired
    private EhsdQualityMarkingDetailService ehsdQualityMarkingDetailService;

    @Autowired
    private EhsdQualityProblemService ehsdQualityProblemService;

    @Autowired
    private EhsdQualityProductService ehsdQualityProductService;

    @Autowired
    private EhsdQualityProductColorService ehsdQualityProductColorService;

    @Autowired
    private EhsdQualityWorkshipService ehsdQualityWorkshipService;

    @Autowired
    private EhsdQualityCartonsService ehsdQualityCartonsService;

    @Autowired
    private EhsdQualityPackService ehsdQualityPackService;

    @Autowired
    private EhsdQualitySiteTestService ehsdQualitySiteTestService;

    @Override
    public Page<EhsdQualityVo> getPage(EhsdQualitySelectDto dto) {
        IWrapper<EhsdQuality> wrapper = getWrapper();
        wrapper.orderByDesc("eq", EhsdQuality::getId);
        Page<EhsdQualityVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        return page;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public EhsdQuality detail(Long id) {
        EhsdQuality quality = this.getOne(Wrappers.<EhsdQuality>query().lambda().eq(EhsdQuality::getBusinessId,id));
        if(ObjectUtils.isEmpty(quality)){
            throw new ServiceException("质检单不存在");
        }
        quality.setEhsdQualityAqlList(ehsdQualityAqlService.list(Wrappers.<EhsdQualityAql>query().lambda().eq(EhsdQualityAql::getQualityId,quality.getId())));
        quality.setEhsdQualityMarkingDetails(ehsdQualityMarkingDetailService.list(Wrappers.<EhsdQualityMarkingDetail>query().lambda().eq(EhsdQualityMarkingDetail::getQualityId,quality.getId())));
        quality.setEhsdQualityProblemList(ehsdQualityProblemService.list(Wrappers.<EhsdQualityProblem>query().lambda().eq(EhsdQualityProblem::getQualityId,quality.getId())));
        quality.setEhsdQualitySiteTestList(ehsdQualitySiteTestService.list(Wrappers.<EhsdQualitySiteTest>query().lambda().eq(EhsdQualitySiteTest::getQualityId,quality.getId())));
        quality.setEhsdQualityPackList(ehsdQualityPackService.list(Wrappers.<EhsdQualityPack>query().lambda().eq(EhsdQualityPack::getQualityId,quality.getId())));
        quality.setEhsdQualityCartonsList(ehsdQualityCartonsService.list(Wrappers.<EhsdQualityCartons>query().lambda().eq(EhsdQualityCartons::getQualityId,quality.getId())));
        quality.setEhsdQualityWorkshipList(ehsdQualityWorkshipService.list(Wrappers.<EhsdQualityWorkship>query().lambda().eq(EhsdQualityWorkship::getQualityId,quality.getId())));
        quality.setEhsdQualityProductColorList(ehsdQualityProductColorService.list(Wrappers.<EhsdQualityProductColor>query().lambda().eq(EhsdQualityProductColor::getQualityId,quality.getId())));
        quality.setEhsdQualityProductList(ehsdQualityProductService.list(Wrappers.<EhsdQualityProduct>query().lambda().eq(EhsdQualityProduct::getQualityId,quality.getId())));
        return quality;
    }

    /**
     * 提交
     * @param ehsdQualityDto
     */
    @Override
    @LogicIgnore(tableName = {""},alias = {""})
    @DSTransactional
    public void submit(EhsdQualityDto ehsdQualityDto) {
        if(ObjectUtils.isEmpty(ehsdQualityDto.getId())){//添加
//            ehsdQualityDto.setCode(codingRuleService.createCode(CodingRuleEnum.CONTRACT.getKey(),null));
            EhsdQuality quality = this.getOne(Wrappers.<EhsdQuality>query().lambda().eq(EhsdQuality::getBusinessId,ehsdQualityDto.getBusinessId()));
            if(ObjectUtils.isNotEmpty(quality)){
                throw new ServiceException("当前合同已有质检单，请勿重新添加");
            }
            this.save(ehsdQualityDto);
        }else{//修改
            this.updateById(ehsdQualityDto);
            //清空数据
            ehsdQualityAqlService.remove(Wrappers.<EhsdQualityAql>query().lambda().eq(EhsdQualityAql::getQualityId,ehsdQualityDto.getId()));
            ehsdQualityMarkingDetailService.remove(Wrappers.<EhsdQualityMarkingDetail>query().lambda().eq(EhsdQualityMarkingDetail::getQualityId,ehsdQualityDto.getId()));
            ehsdQualityProductService.remove(Wrappers.<EhsdQualityProduct>query().lambda().eq(EhsdQualityProduct::getQualityId,ehsdQualityDto.getId()));
            ehsdQualityWorkshipService.remove(Wrappers.<EhsdQualityWorkship>query().lambda().eq(EhsdQualityWorkship::getQualityId,ehsdQualityDto.getId()));
            ehsdQualityCartonsService.remove(Wrappers.<EhsdQualityCartons>query().lambda().eq(EhsdQualityCartons::getQualityId,ehsdQualityDto.getId()));
            ehsdQualityPackService.remove(Wrappers.<EhsdQualityPack>query().lambda().eq(EhsdQualityPack::getQualityId,ehsdQualityDto.getId()));
            ehsdQualityProductColorService.remove(Wrappers.<EhsdQualityProductColor>query().lambda().eq(EhsdQualityProductColor::getQualityId,ehsdQualityDto.getId()));
            ehsdQualityProblemService.remove(Wrappers.<EhsdQualityProblem>query().lambda().eq(EhsdQualityProblem::getQualityId,ehsdQualityDto.getId()));
            ehsdQualitySiteTestService.remove(Wrappers.<EhsdQualitySiteTest>query().lambda().eq(EhsdQualitySiteTest::getQualityId,ehsdQualityDto.getId()));
//            //清空文件
//            ObsFileUtil.removeFile(ehsdQualityDto.getId());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualityAqlList())){
            ehsdQualityDto.getEhsdQualityAqlList().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualityAqlService.saveBatch(ehsdQualityDto.getEhsdQualityAqlList());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualityProblemList())){
            ehsdQualityDto.getEhsdQualityProblemList().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualityProblemService.saveBatch(ehsdQualityDto.getEhsdQualityProblemList());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualityPackList())){
            ehsdQualityDto.getEhsdQualityPackList().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualityPackService.saveBatch(ehsdQualityDto.getEhsdQualityPackList());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualityProductList())){
            ehsdQualityDto.getEhsdQualityProductList().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualityProductService.saveBatch(ehsdQualityDto.getEhsdQualityProductList());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualityProductColorList())){
            ehsdQualityDto.getEhsdQualityProductColorList().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualityProductColorService.saveBatch(ehsdQualityDto.getEhsdQualityProductColorList());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualityWorkshipList())){
            ehsdQualityDto.getEhsdQualityWorkshipList().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualityWorkshipService.saveBatch(ehsdQualityDto.getEhsdQualityWorkshipList());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualityCartonsList())){
            ehsdQualityDto.getEhsdQualityCartonsList().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualityCartonsService.saveBatch(ehsdQualityDto.getEhsdQualityCartonsList());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualitySiteTestList())){
            ehsdQualityDto.getEhsdQualitySiteTestList().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualitySiteTestService.saveBatch(ehsdQualityDto.getEhsdQualitySiteTestList());
        }
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getEhsdQualityMarkingDetails())){
            ehsdQualityDto.getEhsdQualityMarkingDetails().forEach(object -> object.setQualityId(ehsdQualityDto.getId()));
            ehsdQualityMarkingDetailService.saveBatch(ehsdQualityDto.getEhsdQualityMarkingDetails());
        }
        //样品采集记录文件列表
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getSampleCollectionRecordFileList())){
            ObsFileUtil.saveFile(ehsdQualityDto.getSampleCollectionRecordFileList(), ehsdQualityDto.getId(),FileTypeEnums.FILE_TYPE_10.getKey());
        }
        //工序照片文件列表列表
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getWorkshipPhotoFileList())){
            ObsFileUtil.saveFile(ehsdQualityDto.getWorkshipPhotoFileList(), ehsdQualityDto.getId(),FileTypeEnums.FILE_TYPE_20.getKey());
        }
        //物品缺陷照片文件列表
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getDefectPhotosOfItemsFileList())){
            ObsFileUtil.saveFile(ehsdQualityDto.getDefectPhotosOfItemsFileList(), ehsdQualityDto.getId(),FileTypeEnums.FILE_TYPE_30.getKey());
        }
        //支持文档视频文件列表
        if(CollectionUtils.isNotEmpty(ehsdQualityDto.getSupportDocumentationFileList())){
            ObsFileUtil.saveFile(ehsdQualityDto.getSupportDocumentationFileList(), ehsdQualityDto.getId(),FileTypeEnums.FILE_TYPE_40.getKey());
        }
    }

    @Override
    public void edit(EhsdQualityDto ehsdQualityDto) {
        this.updateById(ehsdQualityDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
