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

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.iotryhidailyreport.vo.IotRyHiDailyReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryhidailyreport.vo.IotRyHiDailyReportSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotryhidailyreport.IotRyHiDailyReportDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotryhidailyreport.IotRyHiDailyReportMapper;
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.pms.enums.ErrorCodeConstant.IOT_RY_HI_DAILY_REPORT_NOT_EXISTS;


/**
 * 瑞鹰历史日报 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotRyHiDailyReportServiceImpl implements IotRyHiDailyReportService {

    @Resource
    private IotRyHiDailyReportMapper iotRyHiDailyReportMapper;

    @Override
    public Long createIotRyHiDailyReport(IotRyHiDailyReportSaveReqVO createReqVO) {
        // 插入
        IotRyHiDailyReportDO iotRyHiDailyReport = BeanUtils.toBean(createReqVO, IotRyHiDailyReportDO.class);
        iotRyHiDailyReportMapper.insert(iotRyHiDailyReport);
        // 返回
        return iotRyHiDailyReport.getId();
    }

    @Override
    public void updateIotRyHiDailyReport(IotRyHiDailyReportSaveReqVO updateReqVO) {
        // 校验存在
        validateIotRyHiDailyReportExists(updateReqVO.getId());
        // 更新
        IotRyHiDailyReportDO updateObj = BeanUtils.toBean(updateReqVO, IotRyHiDailyReportDO.class);
        iotRyHiDailyReportMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotRyHiDailyReport(Long id) {
        // 校验存在
        validateIotRyHiDailyReportExists(id);
        // 删除
        iotRyHiDailyReportMapper.deleteById(id);
    }

    private void validateIotRyHiDailyReportExists(Long id) {
        if (iotRyHiDailyReportMapper.selectById(id) == null) {
            throw exception(IOT_RY_HI_DAILY_REPORT_NOT_EXISTS);
        }
    }

    @Override
    public IotRyHiDailyReportDO getIotRyHiDailyReport(Long id) {
        return iotRyHiDailyReportMapper.selectById(id);
    }

    @Override
    public PageResult<IotRyHiDailyReportDO> getIotRyHiDailyReportPage(IotRyHiDailyReportPageReqVO pageReqVO) {
        return iotRyHiDailyReportMapper.selectPage(pageReqVO);
    }

}