package com.zehong.system.service.impl.track;

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

import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.SecurityUtils;
import com.zehong.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.track.TProductInspectMapper;
import com.zehong.system.domain.track.TProductInspect;
import com.zehong.system.service.track.ITProductInspectService;

/**
 * 生产执行Service业务层处理
 * 
 * @author zehong
 * @date 2024-08-26
 */
@Service
public class TProductInspectServiceImpl implements ITProductInspectService 
{
    @Autowired
    private TProductInspectMapper tProductInspectMapper;

    /**
     * 查询生产执行
     * 
     * @param id 生产执行ID
     * @return 生产执行
     */
    @Override
    public TProductInspect selectTProductInspectById(Long id)
    {
        return tProductInspectMapper.selectTProductInspectById(id);
    }

    /**
     * 查询生产执行列表
     * 
     * @param tProductInspect 生产执行
     * @return 生产执行
     */
    @Override
    public List<TProductInspect> selectTProductInspectList(TProductInspect tProductInspect)
    {
        return tProductInspectMapper.selectTProductInspectList(tProductInspect);
    }

    /**
     * 新增生产执行
     * 
     * @param tProductInspect 生产执行
     * @return 结果
     */
    @Override
    public int insertTProductInspect(TProductInspect tProductInspect)
    {
        try{
            tProductInspect.setCreateTime(DateUtils.getNowDate());
            tProductInspect.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
            return tProductInspectMapper.insertTProductInspect(tProductInspect);
        }catch (Exception e){
            Throwable cause = e.getCause();
            if (cause instanceof SQLIntegrityConstraintViolationException){
                String errMsg = cause.getMessage();
                if (StringUtils.isNotEmpty(errMsg) && errMsg.contains("index_unique_batch_no")){
                    throw new CustomException("批次号不能重复");
                }
            }
            throw e;
        }
    }

    /**
     * 修改生产执行
     * 
     * @param tProductInspect 生产执行
     * @return 结果
     */
    @Override
    public int updateTProductInspect(TProductInspect tProductInspect)
    {
        return tProductInspectMapper.updateTProductInspect(tProductInspect);
    }

    /**
     * 批量删除生产执行
     * 
     * @param ids 需要删除的生产执行ID
     * @return 结果
     */
    @Override
    public int deleteTProductInspectByIds(Long[] ids)
    {
        return tProductInspectMapper.deleteTProductInspectByIds(ids);
    }

    /**
     * 删除生产执行信息
     * 
     * @param id 生产执行ID
     * @return 结果
     */
    @Override
    public int deleteTProductInspectById(Long id)
    {
        return tProductInspectMapper.deleteTProductInspectById(id);
    }
}
