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

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.iotprojectdailyreport.vo.IotProjectDailyReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojectdailyreport.vo.IotProjectDailyReportSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojectdailyreport.IotProjectDailyReportDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotprojectdailyreport.IotProjectDailyReportMapper;
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_PROJECT_DAILY_REPORT_NOT_EXISTS;

/**
 * 项目日报 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotProjectDailyReportServiceImpl implements IotProjectDailyReportService {

    @Resource
    private IotProjectDailyReportMapper iotProjectDailyReportMapper;

    @Override
    public Long createIotProjectDailyReport(IotProjectDailyReportSaveReqVO createReqVO) {
        // 插入
        IotProjectDailyReportDO iotProjectDailyReport = BeanUtils.toBean(createReqVO, IotProjectDailyReportDO.class);
        iotProjectDailyReportMapper.insert(iotProjectDailyReport);
        // 返回
        return iotProjectDailyReport.getId();
    }

    @Override
    public void updateIotProjectDailyReport(IotProjectDailyReportSaveReqVO updateReqVO) {
        // 校验存在
        validateIotProjectDailyReportExists(updateReqVO.getId());
        // 更新
        IotProjectDailyReportDO updateObj = BeanUtils.toBean(updateReqVO, IotProjectDailyReportDO.class);
        iotProjectDailyReportMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotProjectDailyReport(Long id) {
        // 校验存在
        validateIotProjectDailyReportExists(id);
        // 删除
        iotProjectDailyReportMapper.deleteById(id);
    }

    private void validateIotProjectDailyReportExists(Long id) {
        if (iotProjectDailyReportMapper.selectById(id) == null) {
            throw exception(IOT_PROJECT_DAILY_REPORT_NOT_EXISTS);
        }
    }

    @Override
    public IotProjectDailyReportDO getIotProjectDailyReport(Long id) {
        return iotProjectDailyReportMapper.selectById(id);
    }

    @Override
    public PageResult<IotProjectDailyReportDO> getIotProjectDailyReportPage(IotProjectDailyReportPageReqVO pageReqVO) {
        return iotProjectDailyReportMapper.selectPage(pageReqVO);
    }

}