package com.aucma.report.service.impl;

import java.util.List;

import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.report.mapper.RecordExceptionProcessMapper;
import com.aucma.report.domain.RecordExceptionProcess;
import com.aucma.report.service.IRecordExceptionProcessService;

import static com.aucma.common.utils.SecurityUtils.getUsername;

/**
 * 不合格品流程管理Service业务层处理
 * 
 * @author Yinq
 * @date 2024-04-24
 */
@Service
public class RecordExceptionProcessServiceImpl implements IRecordExceptionProcessService 
{
    @Autowired
    private RecordExceptionProcessMapper recordExceptionProcessMapper;

    /**
     * 查询不合格品流程管理
     * 
     * @param objId 不合格品流程管理主键
     * @return 不合格品流程管理
     */
    @Override
    public RecordExceptionProcess selectRecordExceptionProcessByObjId(Long objId)
    {
        return recordExceptionProcessMapper.selectRecordExceptionProcessByObjId(objId);
    }

    /**
     * 查询不合格品流程管理列表
     * 
     * @param recordExceptionProcess 不合格品流程管理
     * @return 不合格品流程管理
     */
    @Override
    public List<RecordExceptionProcess> selectRecordExceptionProcessList(RecordExceptionProcess recordExceptionProcess)
    {
        return recordExceptionProcessMapper.selectRecordExceptionProcessList(recordExceptionProcess);
    }

    /**
     * 新增不合格品流程管理
     * 
     * @param recordExceptionProcess 不合格品流程管理
     * @return 结果
     */
    @Override
    public int insertRecordExceptionProcess(RecordExceptionProcess recordExceptionProcess)
    {
        if (StringUtils.isNotNull(recordExceptionProcess.getBoxBarcode())){
            String boxBarcode = recordExceptionProcessMapper.checkBoxBarcode(recordExceptionProcess.getBoxBarcode());
            if (StringUtils.isEmpty(boxBarcode)){
              throw new ServiceException("箱体码输入错误，请重新输入！");
            }
        }
        RecordExceptionProcess exceptionProcess = new RecordExceptionProcess();
        exceptionProcess.setBoxBarcode(recordExceptionProcess.getBoxBarcode());
        exceptionProcess.setProductionUserFlag("1");
        List<RecordExceptionProcess> processList = recordExceptionProcessMapper.selectRecordExceptionProcessList(exceptionProcess);
        if (processList.size() > 0){
            throw new ServiceException("该箱体码正在流转审批,请勿重复提交流程！");
        }
        recordExceptionProcess.setCreatedBy(getUsername());
        recordExceptionProcess.setCreatedTime(DateUtils.getNowDate());
        recordExceptionProcess.setApplyTime(DateUtils.getNowDate());
        return recordExceptionProcessMapper.insertRecordExceptionProcess(recordExceptionProcess);
    }

    /**
     * 修改不合格品流程管理
     * 
     * @param recordExceptionProcess 不合格品流程管理
     * @return 结果
     */
    @Override
    public int updateRecordExceptionProcess(RecordExceptionProcess recordExceptionProcess)
    {
        return recordExceptionProcessMapper.updateRecordExceptionProcess(recordExceptionProcess);
    }

    /**
     * 批量删除不合格品流程管理
     * 
     * @param objIds 需要删除的不合格品流程管理主键
     * @return 结果
     */
    @Override
    public int deleteRecordExceptionProcessByObjIds(Long[] objIds)
    {
        return recordExceptionProcessMapper.deleteRecordExceptionProcessByObjIds(objIds);
    }

    /**
     * 删除不合格品流程管理信息
     * 
     * @param objId 不合格品流程管理主键
     * @return 结果
     */
    @Override
    public int deleteRecordExceptionProcessByObjId(Long objId)
    {
        return recordExceptionProcessMapper.deleteRecordExceptionProcessByObjId(objId);
    }
}
