package com.aucma.base.service.impl;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

import com.aucma.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.base.mapper.StationDefectRelationMapper;
import com.aucma.base.domain.StationDefectRelation;
import com.aucma.base.service.IStationDefectRelationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 质检工位/缺陷维护Service业务层处理
 *
 * @author Yinq
 * @date 2023-11-27
 */
@Service
public class StationDefectRelationServiceImpl implements IStationDefectRelationService {
    @Autowired
    private StationDefectRelationMapper stationDefectRelationMapper;

    /**
     * 查询质检工位/缺陷维护
     *
     * @param objId 质检工位/缺陷维护主键
     * @return 质检工位/缺陷维护
     */
    @Override
    public StationDefectRelation selectStationDefectRelationByObjId(Long objId) {
        return stationDefectRelationMapper.selectStationDefectRelationByObjId(objId);
    }

    /**
     * 查询质检工位/缺陷维护列表
     *
     * @param stationDefectRelation 质检工位/缺陷维护
     * @return 质检工位/缺陷维护
     */
    @Override
    public List<StationDefectRelation> selectStationDefectRelationList(StationDefectRelation stationDefectRelation) {
        return stationDefectRelationMapper.selectStationDefectRelationList(stationDefectRelation);
    }

    /**
     * 新增质检工位/缺陷维护
     *
     * @param stationDefectRelation 质检工位/缺陷维护
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertStationDefectRelation(StationDefectRelation stationDefectRelation) {
        int i = 1;
        if (!stationDefectRelation.getDefectCodeList().isEmpty()) {
            List<String> defectCodeList = stationDefectRelation.getDefectCodeList();
            for (String defectCode : defectCodeList) {
                try {
                    stationDefectRelation.setDefectCode(defectCode);
                    stationDefectRelationMapper.insertStationDefectRelation(stationDefectRelation);
                } catch (Exception e) {
                    throw new ServiceException("该工位已绑定" + stationDefectRelation.getDefectCode() + "缺陷编号！");

                }
            }
        }
        return i;
    }

    /**
     * 修改质检工位/缺陷维护
     *
     * @param stationDefectRelation 质检工位/缺陷维护
     * @return 结果
     */
    @Override
    public int updateStationDefectRelation(StationDefectRelation stationDefectRelation) {
        int i = 0;
        try {
            i = stationDefectRelationMapper.updateStationDefectRelation(stationDefectRelation);
        } catch (Exception e) {
            throw new ServiceException("该工位已绑定此缺陷！");
        }
        return i;
    }

    /**
     * 批量删除质检工位/缺陷维护
     *
     * @param objIds 需要删除的质检工位/缺陷维护主键
     * @return 结果
     */
    @Override
    public int deleteStationDefectRelationByObjIds(Long[] objIds) {
        return stationDefectRelationMapper.deleteStationDefectRelationByObjIds(objIds);
    }

    /**
     * 删除质检工位/缺陷维护信息
     *
     * @param objId 质检工位/缺陷维护主键
     * @return 结果
     */
    @Override
    public int deleteStationDefectRelationByObjId(Long objId) {
        return stationDefectRelationMapper.deleteStationDefectRelationByObjId(objId);
    }
}
