package cn.iocoder.yudao.module.pms.service.alarm;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotAlarmRecordPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotAlarmRecordSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.alarm.IotAlarmRecordDO;
import cn.iocoder.yudao.module.pms.dal.mysql.alarm.IotAlarmRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 运行监控告警记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotAlarmRecordServiceImpl implements IotAlarmRecordService {

    @Resource
    private IotAlarmRecordMapper iotAlarmRecordMapper;

    @Override
    public Long createIotAlarmRecord(IotAlarmRecordSaveReqVO createReqVO) {
        // 插入
        IotAlarmRecordDO iotAlarmRecord = BeanUtils.toBean(createReqVO, IotAlarmRecordDO.class);
        iotAlarmRecordMapper.insert(iotAlarmRecord);
        // 返回
        return iotAlarmRecord.getId();
    }

    @Override
    public void updateIotAlarmRecord(IotAlarmRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateIotAlarmRecordExists(updateReqVO.getId());
        // 更新
        IotAlarmRecordDO updateObj = BeanUtils.toBean(updateReqVO, IotAlarmRecordDO.class);
        iotAlarmRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotAlarmRecord(Long id) {
        // 校验存在
        validateIotAlarmRecordExists(id);
        // 删除
        iotAlarmRecordMapper.deleteById(id);
    }

    private void validateIotAlarmRecordExists(Long id) {
        if (iotAlarmRecordMapper.selectById(id) == null) {
            throw exception(new ErrorCode(1,"不存在"));
        }
    }

    @Override
    public IotAlarmRecordDO getIotAlarmRecord(Long id) {
        return iotAlarmRecordMapper.selectById(id);
    }

    @Override
    public PageResult<IotAlarmRecordDO> getIotAlarmRecordPage(IotAlarmRecordPageReqVO pageReqVO) {
        return iotAlarmRecordMapper.selectPage(pageReqVO);
    }

}