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

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.iotdevicestatuslog.vo.IotDeviceStatusLogPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicestatuslog.vo.IotDeviceStatusLogSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicestatuslog.IotDeviceStatusLogDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotdevicestatuslog.IotDeviceStatusLogMapper;
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_STATUS_LOG_NOT_EXISTS;

/**
 * 设备状态调整日志 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotDeviceStatusLogServiceImpl implements IotDeviceStatusLogService {

    @Resource
    private IotDeviceStatusLogMapper iotDeviceStatusLogMapper;

    @Override
    public Long createIotDeviceStatusLog(IotDeviceStatusLogSaveReqVO createReqVO) {
        // 插入
        IotDeviceStatusLogDO iotDeviceStatusLog = BeanUtils.toBean(createReqVO, IotDeviceStatusLogDO.class);
        iotDeviceStatusLogMapper.insert(iotDeviceStatusLog);
        // 返回
        return iotDeviceStatusLog.getId();
    }

    @Override
    public void updateIotDeviceStatusLog(IotDeviceStatusLogSaveReqVO updateReqVO) {
        // 校验存在
        validateIotDeviceStatusLogExists(updateReqVO.getId());
        // 更新
        IotDeviceStatusLogDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceStatusLogDO.class);
        iotDeviceStatusLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotDeviceStatusLog(Long id) {
        // 校验存在
        validateIotDeviceStatusLogExists(id);
        // 删除
        iotDeviceStatusLogMapper.deleteById(id);
    }

    private void validateIotDeviceStatusLogExists(Long id) {
        if (iotDeviceStatusLogMapper.selectById(id) == null) {
            throw exception(IOT_DEVICE_STATUS_LOG_NOT_EXISTS);
        }
    }

    @Override
    public IotDeviceStatusLogDO getIotDeviceStatusLog(Long id) {
        return iotDeviceStatusLogMapper.selectById(id);
    }

    @Override
    public PageResult<IotDeviceStatusLogDO> getIotDeviceStatusLogPage(IotDeviceStatusLogPageReqVO pageReqVO) {
        return iotDeviceStatusLogMapper.selectPage(pageReqVO);
    }

}