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

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.iotdevicepersonlog.vo.IotDevicePersonLogPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicepersonlog.vo.IotDevicePersonLogSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicepersonlog.IotDevicePersonLogDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotdevicepersonlog.IotDevicePersonLogMapper;
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_PERSON_LOG_NOT_EXISTS;

/**
 * 设备责任人日志 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotDevicePersonLogServiceImpl implements IotDevicePersonLogService {

    @Resource
    private IotDevicePersonLogMapper iotDevicePersonLogMapper;

    @Override
    public Long createIotDevicePersonLog(IotDevicePersonLogSaveReqVO createReqVO) {
        // 插入
        IotDevicePersonLogDO iotDevicePersonLog = BeanUtils.toBean(createReqVO, IotDevicePersonLogDO.class);
        iotDevicePersonLogMapper.insert(iotDevicePersonLog);
        // 返回
        return iotDevicePersonLog.getId();
    }

    @Override
    public void updateIotDevicePersonLog(IotDevicePersonLogSaveReqVO updateReqVO) {
        // 校验存在
        validateIotDevicePersonLogExists(updateReqVO.getId());
        // 更新
        IotDevicePersonLogDO updateObj = BeanUtils.toBean(updateReqVO, IotDevicePersonLogDO.class);
        iotDevicePersonLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotDevicePersonLog(Long id) {
        // 校验存在
        validateIotDevicePersonLogExists(id);
        // 删除
        iotDevicePersonLogMapper.deleteById(id);
    }

    private void validateIotDevicePersonLogExists(Long id) {
        if (iotDevicePersonLogMapper.selectById(id) == null) {
            throw exception(IOT_DEVICE_PERSON_LOG_NOT_EXISTS);
        }
    }

    @Override
    public IotDevicePersonLogDO getIotDevicePersonLog(Long id) {
        return iotDevicePersonLogMapper.selectById(id);
    }

    @Override
    public PageResult<IotDevicePersonLogDO> getIotDevicePersonLogPage(IotDevicePersonLogPageReqVO pageReqVO) {
        return iotDevicePersonLogMapper.selectPage(pageReqVO);
    }

}