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

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.iotcargolocation.vo.IotCargoLocationPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotcargolocation.vo.IotCargoLocationSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcargolocation.IotCargoLocationDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotcargolocation.IotCargoLocationMapper;
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_CARGO_LOCATION_NOT_EXISTS;

/**
 * PMS 货位 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotCargoLocationServiceImpl implements IotCargoLocationService {

    @Resource
    private IotCargoLocationMapper iotCargoLocationMapper;

    @Override
    public Long createIotCargoLocation(IotCargoLocationSaveReqVO createReqVO) {
        // 插入
        IotCargoLocationDO iotCargoLocation = BeanUtils.toBean(createReqVO, IotCargoLocationDO.class);
        iotCargoLocationMapper.insert(iotCargoLocation);
        // 返回
        return iotCargoLocation.getId();
    }

    @Override
    public void updateIotCargoLocation(IotCargoLocationSaveReqVO updateReqVO) {
        // 校验存在
        validateIotCargoLocationExists(updateReqVO.getId());
        // 更新
        IotCargoLocationDO updateObj = BeanUtils.toBean(updateReqVO, IotCargoLocationDO.class);
        iotCargoLocationMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotCargoLocation(Long id) {
        // 校验存在
        validateIotCargoLocationExists(id);
        // 删除
        iotCargoLocationMapper.deleteById(id);
    }

    private void validateIotCargoLocationExists(Long id) {
        if (iotCargoLocationMapper.selectById(id) == null) {
            throw exception(IOT_CARGO_LOCATION_NOT_EXISTS);
        }
    }

    @Override
    public IotCargoLocationDO getIotCargoLocation(Long id) {
        return iotCargoLocationMapper.selectById(id);
    }

    @Override
    public PageResult<IotCargoLocationDO> getIotCargoLocationPage(IotCargoLocationPageReqVO pageReqVO) {
        return iotCargoLocationMapper.selectPage(pageReqVO);
    }

}