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

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.iotdailyreportfuel.vo.IotDailyReportFuelPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdailyreportfuel.vo.IotDailyReportFuelSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdailyreportfuel.IotDailyReportFuelDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotdailyreportfuel.IotDailyReportFuelMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_DAILY_REPORT_FUEL_NOT_EXISTS;

/**
 * 日报-油耗 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotDailyReportFuelServiceImpl implements IotDailyReportFuelService {

    @Resource
    private IotDailyReportFuelMapper iotDailyReportFuelMapper;

    @Override
    public Long createIotDailyReportFuel(IotDailyReportFuelSaveReqVO createReqVO) {
        // 插入
        IotDailyReportFuelDO iotDailyReportFuel = BeanUtils.toBean(createReqVO, IotDailyReportFuelDO.class);
        iotDailyReportFuelMapper.insert(iotDailyReportFuel);
        // 返回
        return iotDailyReportFuel.getId();
    }

    @Override
    public void updateIotDailyReportFuel(IotDailyReportFuelSaveReqVO updateReqVO) {
        // 校验存在
        validateIotDailyReportFuelExists(updateReqVO.getId());
        // 更新
        IotDailyReportFuelDO updateObj = BeanUtils.toBean(updateReqVO, IotDailyReportFuelDO.class);
        iotDailyReportFuelMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotDailyReportFuel(Long id) {
        // 校验存在
        validateIotDailyReportFuelExists(id);
        // 删除
        iotDailyReportFuelMapper.deleteById(id);
    }

    private void validateIotDailyReportFuelExists(Long id) {
        if (iotDailyReportFuelMapper.selectById(id) == null) {
            throw exception(IOT_DAILY_REPORT_FUEL_NOT_EXISTS);
        }
    }

    @Override
    public IotDailyReportFuelDO getIotDailyReportFuel(Long id) {
        return iotDailyReportFuelMapper.selectById(id);
    }

    @Override
    public PageResult<IotDailyReportFuelDO> getIotDailyReportFuelPage(IotDailyReportFuelPageReqVO pageReqVO) {
        return iotDailyReportFuelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IotDailyReportFuelDO> getIotDailyReportFuels(IotDailyReportFuelPageReqVO pageReqVO) {
        return iotDailyReportFuelMapper.selectList(pageReqVO);
    }

}