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

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 com.zehong.system.domain.track.TProductDevice;
import com.zehong.system.mapper.track.TProductDeviceMapper;
import com.zehong.system.service.track.ITProductDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 设备Service业务层处理
 * 
 * @author zehong
 * @date 2024-08-28
 */
@Service
public class TProductDeviceServiceImpl implements ITProductDeviceService 
{
    @Autowired
    private TProductDeviceMapper tProductDeviceMapper;

    /**
     * 查询设备
     * 
     * @param id 设备ID
     * @return 设备
     */
    @Override
    public TProductDevice selectTProductDeviceById(Long id)
    {
        return tProductDeviceMapper.selectTProductDeviceById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param tProductDevice 设备
     * @return 设备
     */
    @Override
    public List<TProductDevice> selectTProductDeviceList(TProductDevice tProductDevice)
    {
        return tProductDeviceMapper.selectTProductDeviceList(tProductDevice);
    }

    /**
     * 新增设备
     * 
     * @param tProductDevice 设备
     * @return 结果
     */
    @Override
    public int insertTProductDevice(TProductDevice tProductDevice)
    {
        try{
            tProductDevice.setCreateTime(DateUtils.getNowDate());
            tProductDevice.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
            return tProductDeviceMapper.insertTProductDevice(tProductDevice);
        }catch (Exception e){
            Throwable cause = e.getCause();
            if (cause instanceof SQLIntegrityConstraintViolationException){
                String errMsg = cause.getMessage();
                if (StringUtils.isNotEmpty(errMsg) && errMsg.contains("index_unique_device_no")){
                    throw new CustomException("设备编号不能重复");
                }
            }
            throw e;
        }
    }

    /**
     * 修改设备
     * 
     * @param tProductDevice 设备
     * @return 结果
     */
    @Override
    public int updateTProductDevice(TProductDevice tProductDevice)
    {
        return tProductDeviceMapper.updateTProductDevice(tProductDevice);
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备ID
     * @return 结果
     */
    @Override
    public int deleteTProductDeviceByIds(Long[] ids)
    {
        return tProductDeviceMapper.deleteTProductDeviceByIds(ids);
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备ID
     * @return 结果
     */
    @Override
    public int deleteTProductDeviceById(Long id)
    {
        return tProductDeviceMapper.deleteTProductDeviceById(id);
    }
}
