package com.aucma.report.service.impl;

import java.util.List;

import com.aucma.base.domain.BaseOrderInfo;
import com.aucma.base.mapper.BaseOrderInfoMapper;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.exception.base.BaseException;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.report.mapper.RecordQualityMaterialMapper;
import com.aucma.report.domain.RecordQualityMaterial;
import com.aucma.report.service.IRecordQualityMaterialService;

/**
 * 质检材料信息Service业务层处理
 *
 * @author Yinq
 * @date 2024-01-07
 */
@Service
public class RecordQualityMaterialServiceImpl implements IRecordQualityMaterialService {
    @Autowired
    private RecordQualityMaterialMapper recordQualityMaterialMapper;

    @Autowired
    private BaseOrderInfoMapper orderInfoMapper;

    /**
     * 查询质检材料信息
     *
     * @param objId 质检材料信息主键
     * @return 质检材料信息
     */
    @Override
    public RecordQualityMaterial selectRecordQualityMaterialByObjId(Long objId) {
        return recordQualityMaterialMapper.selectRecordQualityMaterialByObjId(objId);
    }

    /**
     * 查询质检材料信息列表
     *
     * @param recordQualityMaterial 质检材料信息
     * @return 质检材料信息
     */
    @Override
    public List<RecordQualityMaterial> selectRecordQualityMaterialList(RecordQualityMaterial recordQualityMaterial) {
        return recordQualityMaterialMapper.selectRecordQualityMaterialList(recordQualityMaterial);
    }

    /**
     * 新增质检材料信息
     *
     * @param recordQualityMaterial 质检材料信息
     * @return 结果
     */
    @Override
    public int insertRecordQualityMaterial(RecordQualityMaterial recordQualityMaterial) {
        if (StringUtils.isNotEmpty(recordQualityMaterial.getOrderCode())) {
            BaseOrderInfo baseOrderInfo = new BaseOrderInfo();
            baseOrderInfo.setOrderCode(recordQualityMaterial.getOrderCode());
            List<BaseOrderInfo> baseOrderInfos = orderInfoMapper.selectBaseOrderInfoList(baseOrderInfo);
            if (baseOrderInfos.size() == 0) {
                throw new ServiceException("此SAP计划未维护!");
            }
            RecordQualityMaterial material = new RecordQualityMaterial();
            material.setOrderCode(baseOrderInfos.get(0).getOrderCode());
            material.setInfoType(recordQualityMaterial.getInfoType());
            List<RecordQualityMaterial> qualityMaterials = recordQualityMaterialMapper.selectRecordQualityMaterialList(material);
            if (qualityMaterials.size() > 0) {
                throw new ServiceException("此SAP计划已维护!");
            }
        }
        return recordQualityMaterialMapper.insertRecordQualityMaterial(recordQualityMaterial);
    }

    /**
     * 修改质检材料信息
     *
     * @param recordQualityMaterial 质检材料信息
     * @return 结果
     */
    @Override
    public int updateRecordQualityMaterial(RecordQualityMaterial recordQualityMaterial) {
        return recordQualityMaterialMapper.updateRecordQualityMaterial(recordQualityMaterial);
    }

    /**
     * 批量删除质检材料信息
     *
     * @param objIds 需要删除的质检材料信息主键
     * @return 结果
     */
    @Override
    public int deleteRecordQualityMaterialByObjIds(Long[] objIds) {
        return recordQualityMaterialMapper.deleteRecordQualityMaterialByObjIds(objIds);
    }

    /**
     * 删除质检材料信息信息
     *
     * @param objId 质检材料信息主键
     * @return 结果
     */
    @Override
    public int deleteRecordQualityMaterialByObjId(Long objId) {
        return recordQualityMaterialMapper.deleteRecordQualityMaterialByObjId(objId);
    }
}
