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

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.iotryimprovedailyreportdetail.vo.IotRyImproveDailyReportDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryimprovedailyreportdetail.vo.IotRyImproveDailyReportDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotryimprovedailyreportdetail.IotRyImproveDailyReportDetailDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotryimprovedailyreportdetail.IotRyImproveDailyReportDetailMapper;
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_IMPROVE_DAILY_REPORT_DETAIL_NOT_EXISTS;


/**
 * 瑞鹰设备整改日报明细(生产动态拆分) Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotRyImproveDailyReportDetailServiceImpl implements IotRyImproveDailyReportDetailService {

    @Resource
    private IotRyImproveDailyReportDetailMapper iotRyImproveDailyReportDetailMapper;

    @Override
    public Long createIotRyImproveDailyReportDetail(IotRyImproveDailyReportDetailSaveReqVO createReqVO) {
        // 插入
        IotRyImproveDailyReportDetailDO iotRyImproveDailyReportDetail = BeanUtils.toBean(createReqVO, IotRyImproveDailyReportDetailDO.class);
        iotRyImproveDailyReportDetailMapper.insert(iotRyImproveDailyReportDetail);
        // 返回
        return iotRyImproveDailyReportDetail.getId();
    }

    @Override
    public void updateIotRyImproveDailyReportDetail(IotRyImproveDailyReportDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateIotRyImproveDailyReportDetailExists(updateReqVO.getId());
        // 更新
        IotRyImproveDailyReportDetailDO updateObj = BeanUtils.toBean(updateReqVO, IotRyImproveDailyReportDetailDO.class);
        iotRyImproveDailyReportDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotRyImproveDailyReportDetail(Long id) {
        // 校验存在
        validateIotRyImproveDailyReportDetailExists(id);
        // 删除
        iotRyImproveDailyReportDetailMapper.deleteById(id);
    }

    private void validateIotRyImproveDailyReportDetailExists(Long id) {
        if (iotRyImproveDailyReportDetailMapper.selectById(id) == null) {
            throw exception(IOT_RY_IMPROVE_DAILY_REPORT_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public IotRyImproveDailyReportDetailDO getIotRyImproveDailyReportDetail(Long id) {
        return iotRyImproveDailyReportDetailMapper.selectById(id);
    }

    @Override
    public PageResult<IotRyImproveDailyReportDetailDO> getIotRyImproveDailyReportDetailPage(IotRyImproveDailyReportDetailPageReqVO pageReqVO) {
        return iotRyImproveDailyReportDetailMapper.selectPage(pageReqVO);
    }

}