package cn.iocoder.yudao.module.product.service.deviceoperatelog;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.deviceoperatelog.vo.DeviceOperateLogCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.deviceoperatelog.vo.DeviceOperateLogPageReqVO;
import cn.iocoder.yudao.module.product.convert.deviceoperatelog.DeviceOperateLogConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.deviceoperatelog.DeviceOperateLogDO;
import cn.iocoder.yudao.module.product.dal.mysql.deviceoperatelog.DeviceOperateLogMapper;
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.product.enums.ErrorCodeConstants.DEVICE_OPERATE_LOG_NOT_EXISTS;

/**
 * 设备操作日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DeviceOperateLogServiceImpl implements DeviceOperateLogService {

    @Resource
    private DeviceOperateLogMapper deviceOperateLogMapper;

    @Override
    public Long createDeviceOperateLog(DeviceOperateLogCreateReqVO createReqVO) {
        // 插入
        DeviceOperateLogDO deviceOperateLog = DeviceOperateLogConvert.INSTANCE.convert(createReqVO);
        deviceOperateLogMapper.insert(deviceOperateLog);
        // 返回
        return deviceOperateLog.getId();
    }

    @Override
    public void deleteDeviceOperateLog(Long id) {
        // 校验存在
        validateDeviceOperateLogExists(id);
        // 删除
        deviceOperateLogMapper.deleteById(id);
    }

    private void validateDeviceOperateLogExists(Long id) {
        if (deviceOperateLogMapper.selectById(id) == null) {
            throw exception(DEVICE_OPERATE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public DeviceOperateLogDO getDeviceOperateLog(Long id) {
        return deviceOperateLogMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceOperateLogDO> getDeviceOperateLogPage(DeviceOperateLogPageReqVO pageReqVO) {
        return deviceOperateLogMapper.selectPage(pageReqVO);
    }

}
