package cn.iocoder.yudao.module.system.service.log;

import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotOperationLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotOperationLogSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.log.RobotOperationLogDO;
import cn.iocoder.yudao.module.system.dal.mysql.log.RobotOperationLogMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.OPERATION_LOG_NOT_EXISTS;

/**
 * 机器人动作记录 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class RobotOperationLogServiceImpl implements RobotOperationLogService {

    @Resource
    private RobotOperationLogMapper operationLogMapper;

    @Override
    public Long createOperationLog(RobotOperationLogSaveReqVO createReqVO) {
        // 插入
        RobotOperationLogDO operationLog = BeanUtils.toBean(createReqVO, RobotOperationLogDO.class);
        operationLogMapper.insert(operationLog);
        // 返回
        return operationLog.getId();
    }

    @Override
    public void updateOperationLog(RobotOperationLogSaveReqVO updateReqVO) {
        // 校验存在
        validateOperationLogExists(updateReqVO.getId());
        // 更新
        RobotOperationLogDO updateObj = BeanUtils.toBean(updateReqVO, RobotOperationLogDO.class);
        operationLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperationLog(Long id) {
        // 校验存在
        validateOperationLogExists(id);
        // 删除
        operationLogMapper.deleteById(id);
    }

    private void validateOperationLogExists(Long id) {
        if (operationLogMapper.selectById(id) == null) {
            throw exception(OPERATION_LOG_NOT_EXISTS);
        }
    }

    @Override
    public RobotOperationLogDO getOperationLog(Long id) {
        return operationLogMapper.selectById(id);
    }

    @Override
    public PageResult<RobotOperationLogDO> getOperationLogPage(RobotOperationLogPageReqVO pageReqVO) {
        return operationLogMapper.selectPage(pageReqVO);
    }

}