package com.aucma.report.service.impl;

import java.util.List;

import com.aucma.common.utils.StringUtils;
import com.aucma.report.domain.ReportQualityInspection;
import com.aucma.report.service.IReportQualityInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.report.mapper.MaterialCompletionMapper;
import com.aucma.report.domain.MaterialCompletion;
import com.aucma.report.service.IMaterialCompletionService;

/**
 * 物料过点记录Service业务层处理
 *
 * @author Yinq
 * @date 2024-03-14
 */
@Service
public class MaterialCompletionServiceImpl implements IMaterialCompletionService {
    @Autowired
    private MaterialCompletionMapper materialCompletionMapper;

    @Autowired
    private IReportQualityInspectionService qualityInspectionService;

    /**
     * 查询物料过点记录
     *
     * @param objId 物料过点记录主键
     * @return 物料过点记录
     */
    @Override
    public MaterialCompletion selectMaterialCompletionByObjId(Long objId) {
        return materialCompletionMapper.selectMaterialCompletionByObjId(objId);
    }

    /**
     * 查询物料过点记录列表
     *
     * @param materialCompletion 物料过点记录
     * @return 物料过点记录
     */
    @Override
    public List<MaterialCompletion> selectMaterialCompletionList(MaterialCompletion materialCompletion) {
        return materialCompletionMapper.selectMaterialCompletionList(materialCompletion);
    }

    /**
     * 新增物料过点记录
     *
     * @param materialCompletion 物料过点记录
     * @return 结果
     */
    @Override
    public int insertMaterialCompletion(MaterialCompletion materialCompletion) {
        return materialCompletionMapper.insertMaterialCompletion(materialCompletion);
    }

    /**
     * 修改物料过点记录
     *
     * @param materialCompletion 物料过点记录
     * @return 结果
     */
    @Override
    public int updateMaterialCompletion(MaterialCompletion materialCompletion) {
        return materialCompletionMapper.updateMaterialCompletion(materialCompletion);
    }

    /**
     * 批量删除物料过点记录
     *
     * @param objIds 需要删除的物料过点记录主键
     * @return 结果
     */
    @Override
    public int deleteMaterialCompletionByObjIds(Long[] objIds) {
        return materialCompletionMapper.deleteMaterialCompletionByObjIds(objIds);
    }

    /**
     * 删除物料过点记录信息
     *
     * @param objId 物料过点记录主键
     * @return 结果
     */
    @Override
    public int deleteMaterialCompletionByObjId(Long objId) {
        return materialCompletionMapper.deleteMaterialCompletionByObjId(objId);
    }

    /**
     * 二线质检存入过点数据
     * 质检合格->存质检数据
     * @param inspection
     */
    @Override
    public void inspectionProcessing(ReportQualityInspection inspection) {
        MaterialCompletion materialCompletion = new MaterialCompletion();
        //质检转过点数据
        if (StringUtils.isNotEmpty(inspection.getBarCode())) {
            ReportQualityInspection qualityInspection = qualityInspectionService.selectReportQualityInspectionByBarcode(inspection.getBarCode(), "CX_02");
            if (StringUtils.isNotEmpty(qualityInspection.getOrderCode())){
                materialCompletion.setOrderCode(qualityInspection.getOrderCode());
            }
        }
        materialCompletion.setMaterialBarcode(inspection.getBarCode());
        materialCompletion.setMaterialCode(inspection.getBarCode().substring(7, 17));
        materialCompletion.setMaterialName(inspection.getMaterialName());
        materialCompletion.setStationName(inspection.getStationCode());
        materialCompletion.setCompleteDate(inspection.getInspectorTime());
        materialCompletion.setProductLineCode(inspection.getProductLineName());
        materialCompletionMapper.insertMaterialCompletion(materialCompletion);
    }

}
