package cn.iocoder.yudao.module.system.service.robot.chargelog;

import cn.iocoder.yudao.module.system.controller.admin.robot.chargelog.*;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotChargeLogDO;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotChargeLogMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.CHARGE_LOG_NOT_EXISTS;

/**
 * 车辆充电记录 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class RobotChargeLogServiceImpl implements RobotChargeLogService {

    @Resource
    private RobotChargeLogMapper chargeLogMapper;

    @Override
    public Long createChargeLog(RobotChargeLogSaveReqVO createReqVO) {
        // 插入
        RobotChargeLogDO chargeLog = BeanUtils.toBean(createReqVO, RobotChargeLogDO.class);
        chargeLogMapper.insert(chargeLog);
        // 返回
        return chargeLog.getId();
    }

    @Override
    public void updateChargeLog(RobotChargeLogSaveReqVO updateReqVO) {
        // 校验存在
        validateChargeLogExists(updateReqVO.getId());
        // 更新
        RobotChargeLogDO updateObj = BeanUtils.toBean(updateReqVO, RobotChargeLogDO.class);
        chargeLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteChargeLog(Long id) {
        // 校验存在
        validateChargeLogExists(id);
        // 删除
        chargeLogMapper.deleteById(id);
    }

    private void validateChargeLogExists(Long id) {
        if (chargeLogMapper.selectById(id) == null) {
            throw exception(CHARGE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public RobotChargeLogDO getChargeLog(Long id) {
        return chargeLogMapper.selectById(id);
    }

    @Override
    public PageResult<RobotChargeLogDO> getChargeLogPage(RobotChargeLogPageReqVO pageReqVO) {
        return chargeLogMapper.selectPage(pageReqVO);
    }

}