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

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.iotsapstocklog.vo.IotSapStockLogPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotsapstocklog.vo.IotSapStockLogSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotsapstocklog.IotSapStockLogDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotsapstocklog.IotSapStockLogMapper;
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_SAP_STOCK_LOG_NOT_EXISTS;

/**
 * PMS SAP 库存(sap同步的原始数据日志) Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotSapStockLogServiceImpl implements IotSapStockLogService {

    @Resource
    private IotSapStockLogMapper iotSapStockLogMapper;

    @Override
    public Long createIotSapStockLog(IotSapStockLogSaveReqVO createReqVO) {
        // 插入
        IotSapStockLogDO iotSapStockLog = BeanUtils.toBean(createReqVO, IotSapStockLogDO.class);
        iotSapStockLogMapper.insert(iotSapStockLog);
        // 返回
        return iotSapStockLog.getId();
    }

    @Override
    public void updateIotSapStockLog(IotSapStockLogSaveReqVO updateReqVO) {
        // 校验存在
        validateIotSapStockLogExists(updateReqVO.getId());
        // 更新
        IotSapStockLogDO updateObj = BeanUtils.toBean(updateReqVO, IotSapStockLogDO.class);
        iotSapStockLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotSapStockLog(Long id) {
        // 校验存在
        validateIotSapStockLogExists(id);
        // 删除
        iotSapStockLogMapper.deleteById(id);
    }

    private void validateIotSapStockLogExists(Long id) {
        if (iotSapStockLogMapper.selectById(id) == null) {
            throw exception(IOT_SAP_STOCK_LOG_NOT_EXISTS);
        }
    }

    @Override
    public IotSapStockLogDO getIotSapStockLog(Long id) {
        return iotSapStockLogMapper.selectById(id);
    }

    @Override
    public PageResult<IotSapStockLogDO> getIotSapStockLogPage(IotSapStockLogPageReqVO pageReqVO) {
        return iotSapStockLogMapper.selectPage(pageReqVO);
    }

}