package com.aucma.dms.service.impl;

//import com.hw.common.core.exception.ServiceException;
//import com.hw.common.core.utils.DateUtils;
//import com.hw.common.security.utils.SecurityUtils;
//import com.aucma.dms.domain.DmsBaseDeviceInstall;
//import com.aucma.dms.mapper.DmsBaseDeviceInstallMapper;
//import com.aucma.dms.service.IDmsBaseDeviceInstallService;
//import com.hw.system.api.model.LoginUser;

import com.aucma.common.core.domain.model.LoginUser;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsBaseDeviceInstall;
import com.aucma.dms.mapper.DmsBaseDeviceInstallMapper;
import com.aucma.dms.service.IDmsBaseDeviceInstallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备安装信息Service业务层处理
 *
 * @author xins
 * @date 2024-01-02
 */
@Service
public class DmsBaseDeviceInstallServiceImpl implements IDmsBaseDeviceInstallService
{
    @Autowired
    private DmsBaseDeviceInstallMapper dmsBaseDeviceInstallMapper;

    /**
     * 查询设备安装信息
     *
     * @param deviceInstallId 设备安装信息主键
     * @return 设备安装信息
     */
    @Override
    public DmsBaseDeviceInstall selectDmsBaseDeviceInstallByDeviceInstallId(Long deviceInstallId)
    {
        return dmsBaseDeviceInstallMapper.selectDmsBaseDeviceInstallByDeviceInstallId(deviceInstallId);
    }

    /**
     * 查询设备安装信息列表
     *
     * @param dmsBaseDeviceInstall 设备安装信息
     * @return 设备安装信息
     */
    @Override
    public List<DmsBaseDeviceInstall> selectDmsBaseDeviceInstallList(DmsBaseDeviceInstall dmsBaseDeviceInstall)
    {
        return dmsBaseDeviceInstallMapper.selectDmsBaseDeviceInstallList(dmsBaseDeviceInstall);
    }

    /**
     * 新增设备安装信息
     *
     * @param dmsBaseDeviceInstall 设备安装信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseDeviceInstall(DmsBaseDeviceInstall dmsBaseDeviceInstall)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        DmsBaseDeviceInstall DeviceInstall = new DmsBaseDeviceInstall();
        DeviceInstall.setDeviceId(dmsBaseDeviceInstall.getDeviceId());
        List<DmsBaseDeviceInstall> dmsBaseDeviceInstallList = dmsBaseDeviceInstallMapper
                .selectDmsBaseDeviceInstallList(DeviceInstall);
        if (dmsBaseDeviceInstallList.size()>0){
            throw  new ServiceException("该设备已安装，只能有一条安装记录");
        }
        dmsBaseDeviceInstall.setCreateBy(loginUser.getUserId());
        dmsBaseDeviceInstall.setCreateTime(DateUtils.getNowDate());
        return dmsBaseDeviceInstallMapper.insertDmsBaseDeviceInstall(dmsBaseDeviceInstall);
    }

    /**
     * 修改设备安装信息
     *
     * @param dmsBaseDeviceInstall 设备安装信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseDeviceInstall(DmsBaseDeviceInstall dmsBaseDeviceInstall)
    {  DmsBaseDeviceInstall DeviceInstall = new DmsBaseDeviceInstall();
        DeviceInstall.setDeviceId(dmsBaseDeviceInstall.getDeviceId());
        List<DmsBaseDeviceInstall> dmsBaseDeviceInstallList = dmsBaseDeviceInstallMapper
                .selectDmsBaseDeviceInstallList(DeviceInstall);
        if (dmsBaseDeviceInstallList.size()>0&&!dmsBaseDeviceInstallList.get(0).getDeviceInstallId()
                .equals(dmsBaseDeviceInstall.getDeviceInstallId())){
            throw  new ServiceException("该设备已安装，只能有一条安装记录");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        dmsBaseDeviceInstall.setUpdateBy(loginUser.getUserId());
        dmsBaseDeviceInstall.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseDeviceInstallMapper.updateDmsBaseDeviceInstall(dmsBaseDeviceInstall);
    }

    /**
     * 批量删除设备安装信息
     *
     * @param deviceInstallIds 需要删除的设备安装信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceInstallByDeviceInstallIds(Long[] deviceInstallIds)
    {
        return dmsBaseDeviceInstallMapper.deleteDmsBaseDeviceInstallByDeviceInstallIds(deviceInstallIds);
    }

    /**
     * 删除设备安装信息信息
     *
     * @param deviceInstallId 设备安装信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceInstallByDeviceInstallId(Long deviceInstallId)
    {
        return dmsBaseDeviceInstallMapper.deleteDmsBaseDeviceInstallByDeviceInstallId(deviceInstallId);
    }
}
