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

import cn.hutool.core.collection.CollUtil;
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.iotshelves.vo.IotShelvesPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotshelves.vo.IotShelvesSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotshelves.IotShelvesDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotshelves.IotShelvesMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_SHELVES_NOT_EXISTS;

/**
 * PMS 货架 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotShelvesServiceImpl implements IotShelvesService {

    @Resource
    private IotShelvesMapper iotShelvesMapper;

    @Override
    public Long createIotShelves(IotShelvesSaveReqVO createReqVO) {
        // 插入
        IotShelvesDO iotShelves = BeanUtils.toBean(createReqVO, IotShelvesDO.class);
        iotShelvesMapper.insert(iotShelves);
        // 返回
        return iotShelves.getId();
    }

    @Override
    public void updateIotShelves(IotShelvesSaveReqVO updateReqVO) {
        // 校验存在
        validateIotShelvesExists(updateReqVO.getId());
        // 更新
        IotShelvesDO updateObj = BeanUtils.toBean(updateReqVO, IotShelvesDO.class);
        iotShelvesMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotShelves(Long id) {
        // 校验存在
        validateIotShelvesExists(id);
        // 删除
        iotShelvesMapper.deleteById(id);
    }

    private void validateIotShelvesExists(Long id) {
        if (iotShelvesMapper.selectById(id) == null) {
            throw exception(IOT_SHELVES_NOT_EXISTS);
        }
    }

    @Override
    public IotShelvesDO getIotShelves(Long id) {
        return iotShelvesMapper.selectById(id);
    }

    @Override
    public PageResult<IotShelvesDO> getIotShelvesPage(IotShelvesPageReqVO pageReqVO) {
        return iotShelvesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IotShelvesDO> getShelvesList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return iotShelvesMapper.selectBatchIds(ids);
    }

    @Override
    public List<IotShelvesDO> getShelvesListByStatus(Collection<Integer> statuses) {
        return iotShelvesMapper.selectListByStatus(statuses);
    }

}