package cn.iocoder.yudao.module.mes.service.energyrecord;

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.module.mes.controller.admin.energyrecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.energyrecord.EnergyRecordDO;
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 cn.iocoder.yudao.module.mes.dal.mysql.energyrecord.EnergyRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;

/**
 * 能源设备历史记录 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class EnergyRecordServiceImpl implements EnergyRecordService {

    @Resource
    private EnergyRecordMapper energyRecordMapper;

    @Override
    public Long createEnergyRecord(EnergyRecordSaveReqVO createReqVO) {
        // 插入
        EnergyRecordDO energyRecord = BeanUtils.toBean(createReqVO, EnergyRecordDO.class);
        energyRecordMapper.insert(energyRecord);
        // 返回
        return energyRecord.getId();
    }

    @Override
    public void updateEnergyRecord(EnergyRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateEnergyRecordExists(updateReqVO.getId());
        // 更新
        EnergyRecordDO updateObj = BeanUtils.toBean(updateReqVO, EnergyRecordDO.class);
        energyRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteEnergyRecord(Long id) {
        // 校验存在
        validateEnergyRecordExists(id);
        // 删除
        energyRecordMapper.deleteById(id);
    }

    private void validateEnergyRecordExists(Long id) {
        if (energyRecordMapper.selectById(id) == null) {
            throw exception(ENERGY_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public EnergyRecordDO getEnergyRecord(Long id) {
        return energyRecordMapper.selectById(id);
    }

    @Override
    public PageResult<EnergyRecordDO> getEnergyRecordPage(EnergyRecordPageReqVO pageReqVO) {
        return energyRecordMapper.selectPage(pageReqVO);
    }

}