package com.hw.dms.service.impl;

import java.util.List;

import com.hw.common.core.exception.ServiceException;
import com.hw.common.core.utils.DateUtils;
import com.hw.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsBaseDeviceLedgerMapper;
import com.hw.dms.domain.DmsBaseDeviceLedger;
import com.hw.dms.service.IDmsBaseDeviceLedgerService;

/**
 * 设备台账信息Service业务层处理
 *
 * @author xins
 * @date 2023-12-28
 */
@Service
public class DmsBaseDeviceLedgerServiceImpl implements IDmsBaseDeviceLedgerService {
    @Autowired
    private DmsBaseDeviceLedgerMapper dmsBaseDeviceLedgerMapper;

    /**
     * 查询设备台账信息
     *
     * @param deviceId 设备台账信息主键
     * @return 设备台账信息
     */
    @Override
    public DmsBaseDeviceLedger selectDmsBaseDeviceLedgerByDeviceId(Long deviceId) {
        return dmsBaseDeviceLedgerMapper.selectDmsBaseDeviceLedgerByDeviceId(deviceId);
    }

    /**
     * 查询设备台账信息列表
     *
     * @param dmsBaseDeviceLedger 设备台账信息
     * @return 设备台账信息
     */
    @Override
    public List<DmsBaseDeviceLedger> selectDmsBaseDeviceLedgerList(DmsBaseDeviceLedger dmsBaseDeviceLedger) {
        return dmsBaseDeviceLedgerMapper.selectDmsBaseDeviceLedgerList(dmsBaseDeviceLedger);
    }

    /**
     * 新增设备台账信息
     *
     * @param dmsBaseDeviceLedger 设备台账信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseDeviceLedger(DmsBaseDeviceLedger dmsBaseDeviceLedger) {
        dmsBaseDeviceLedger.setIsFlag(1l);
        LoginUser loginUser = new LoginUser();
        dmsBaseDeviceLedger.setCreateBy(loginUser.getUsername());
        dmsBaseDeviceLedger.setCreateTime(DateUtils.getNowDate());
        return dmsBaseDeviceLedgerMapper.insertDmsBaseDeviceLedger(dmsBaseDeviceLedger);
    }

    /**
     * 修改设备台账信息
     *
     * @param dmsBaseDeviceLedger 设备台账信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseDeviceLedger(DmsBaseDeviceLedger dmsBaseDeviceLedger) {
        LoginUser loginUser = new LoginUser();
        dmsBaseDeviceLedger.setUpdateBy(loginUser.getUsername());
        dmsBaseDeviceLedger.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseDeviceLedgerMapper.updateDmsBaseDeviceLedger(dmsBaseDeviceLedger);
    }

    /**
     * 批量删除设备台账信息
     *
     * @param deviceIds 需要删除的设备台账信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceLedgerByDeviceIds(Long[] deviceIds) {
        return dmsBaseDeviceLedgerMapper.deleteDmsBaseDeviceLedgerByDeviceIds(deviceIds);
    }

    /**
     * 删除设备台账信息信息
     *
     * @param deviceId 设备台账信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceLedgerByDeviceId(Long deviceId) {
        return dmsBaseDeviceLedgerMapper.deleteDmsBaseDeviceLedgerByDeviceId(deviceId);
    }


    /**
     * 查询设备台账信息
     *
     * @param deviceCode 设备Code
     * @return 设备台账信息
     */
    @Override
    public DmsBaseDeviceLedger selectDmsBaseDeviceLedgerByDeviceCode(String deviceCode) {
        DmsBaseDeviceLedger dmsBaseDeviceLedger = dmsBaseDeviceLedgerMapper.selectDmsBaseDeviceLedgerByDeviceCode(deviceCode);
        if (dmsBaseDeviceLedger == null) {
            throw new ServiceException("无此设备");
        }
        return dmsBaseDeviceLedger;
    }

}
