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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicemaxrunlog.vo.IotDeviceMaxRunLogPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicemaxrunlog.vo.IotDeviceMaxRunLogSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicemaxrunlog.IotDeviceMaxRunLogDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotdevicemaxrunlog.IotDeviceMaxRunLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_DEVICE_MAX_RUN_LOG_NOT_EXISTS;

/**
 * 设备运行数据记录最大值 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotDeviceMaxRunLogServiceImpl implements IotDeviceMaxRunLogService {

    @Resource
    private IotDeviceMaxRunLogMapper iotDeviceMaxRunLogMapper;

    @Override
    public Long createIotDeviceMaxRunLog(IotDeviceMaxRunLogSaveReqVO createReqVO) {
        // 插入
        IotDeviceMaxRunLogDO iotDeviceMaxRunLog = BeanUtils.toBean(createReqVO, IotDeviceMaxRunLogDO.class);
        iotDeviceMaxRunLogMapper.insert(iotDeviceMaxRunLog);
        // 返回
        return iotDeviceMaxRunLog.getId();
    }

    @Override
    public void updateIotDeviceMaxRunLog(IotDeviceMaxRunLogSaveReqVO updateReqVO) {
        // 校验存在
        validateIotDeviceMaxRunLogExists(updateReqVO.getId());
        // 更新
        IotDeviceMaxRunLogDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceMaxRunLogDO.class);
        iotDeviceMaxRunLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotDeviceMaxRunLog(Long id) {
        // 校验存在
        validateIotDeviceMaxRunLogExists(id);
        // 删除
        iotDeviceMaxRunLogMapper.deleteById(id);
    }

    private void validateIotDeviceMaxRunLogExists(Long id) {
        if (iotDeviceMaxRunLogMapper.selectById(id) == null) {
            throw exception(IOT_DEVICE_MAX_RUN_LOG_NOT_EXISTS);
        }
    }

    @Override
    public IotDeviceMaxRunLogDO getIotDeviceMaxRunLog(Long id) {
        return iotDeviceMaxRunLogMapper.selectById(id);
    }

    @Override
    public PageResult<IotDeviceMaxRunLogDO> getIotDeviceMaxRunLogPage(IotDeviceMaxRunLogPageReqVO pageReqVO) {
        return iotDeviceMaxRunLogMapper.selectPage(pageReqVO);
    }

}