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

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.iotrydailyreportdetail.vo.IotRyDailyReportDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotrydailyreportdetail.vo.IotRyDailyReportDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotrydailyreportdetail.IotRyDailyReportDetailDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotrydailyreportdetail.IotRyDailyReportDetailMapper;
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_DAILY_REPORT_DETAIL_NOT_EXISTS;

/**
 * 瑞鹰日报明细(生产动态拆分) Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotRyDailyReportDetailServiceImpl implements IotRyDailyReportDetailService {

    @Resource
    private IotRyDailyReportDetailMapper iotRyDailyReportDetailMapper;

    @Override
    public Long createIotRyDailyReportDetail(IotRyDailyReportDetailSaveReqVO createReqVO) {
        // 插入
        IotRyDailyReportDetailDO iotRyDailyReportDetail = BeanUtils.toBean(createReqVO, IotRyDailyReportDetailDO.class);
        iotRyDailyReportDetailMapper.insert(iotRyDailyReportDetail);
        // 返回
        return iotRyDailyReportDetail.getId();
    }

    @Override
    public void updateIotRyDailyReportDetail(IotRyDailyReportDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateIotRyDailyReportDetailExists(updateReqVO.getId());
        // 更新
        IotRyDailyReportDetailDO updateObj = BeanUtils.toBean(updateReqVO, IotRyDailyReportDetailDO.class);
        iotRyDailyReportDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotRyDailyReportDetail(Long id) {
        // 校验存在
        validateIotRyDailyReportDetailExists(id);
        // 删除
        iotRyDailyReportDetailMapper.deleteById(id);
    }

    private void validateIotRyDailyReportDetailExists(Long id) {
        if (iotRyDailyReportDetailMapper.selectById(id) == null) {
            throw exception(IOT_RY_DAILY_REPORT_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public IotRyDailyReportDetailDO getIotRyDailyReportDetail(Long id) {
        return iotRyDailyReportDetailMapper.selectById(id);
    }

    @Override
    public PageResult<IotRyDailyReportDetailDO> getIotRyDailyReportDetailPage(IotRyDailyReportDetailPageReqVO pageReqVO) {
        return iotRyDailyReportDetailMapper.selectPage(pageReqVO);
    }

}