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

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.iotoutbound.vo.IotOutboundPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotoutbound.vo.IotOutboundSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotoutbound.IotOutboundDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotoutbound.IotOutboundMapper;
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_OUTBOUND_NOT_EXISTS;

/**
 * PMS 出库 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotOutboundServiceImpl implements IotOutboundService {

    @Resource
    private IotOutboundMapper iotOutboundMapper;

    @Override
    public Long createIotOutbound(IotOutboundSaveReqVO createReqVO) {
        // 插入
        IotOutboundDO iotOutbound = BeanUtils.toBean(createReqVO, IotOutboundDO.class);
        iotOutboundMapper.insert(iotOutbound);
        // 返回
        return iotOutbound.getId();
    }

    @Override
    public void updateIotOutbound(IotOutboundSaveReqVO updateReqVO) {
        // 校验存在
        validateIotOutboundExists(updateReqVO.getId());
        // 更新
        IotOutboundDO updateObj = BeanUtils.toBean(updateReqVO, IotOutboundDO.class);
        iotOutboundMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotOutbound(Long id) {
        // 校验存在
        validateIotOutboundExists(id);
        // 删除
        iotOutboundMapper.deleteById(id);
    }

    private void validateIotOutboundExists(Long id) {
        if (iotOutboundMapper.selectById(id) == null) {
            throw exception(IOT_OUTBOUND_NOT_EXISTS);
        }
    }

    @Override
    public IotOutboundDO getIotOutbound(Long id) {
        return iotOutboundMapper.selectById(id);
    }

    @Override
    public PageResult<IotOutboundDO> getIotOutboundPage(IotOutboundPageReqVO pageReqVO) {
        return iotOutboundMapper.selectPage(pageReqVO);
    }

}