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

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.iotstoragearea.vo.IotStorageAreaPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotstoragearea.vo.IotStorageAreaSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotstoragearea.IotStorageAreaDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotstoragearea.IotStorageAreaMapper;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
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_STORAGE_AREA_NOT_EXISTS;

/**
 * PMS 库区 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotStorageAreaServiceImpl implements IotStorageAreaService {

    @Resource
    private IotStorageAreaMapper iotStorageAreaMapper;

    @Override
    public Long createIotStorageArea(IotStorageAreaSaveReqVO createReqVO) {
        // 插入
        IotStorageAreaDO iotStorageArea = BeanUtils.toBean(createReqVO, IotStorageAreaDO.class);
        iotStorageAreaMapper.insert(iotStorageArea);
        // 返回
        return iotStorageArea.getId();
    }

    @Override
    public void updateIotStorageArea(IotStorageAreaSaveReqVO updateReqVO) {
        // 校验存在
        validateIotStorageAreaExists(updateReqVO.getId());
        // 更新
        IotStorageAreaDO updateObj = BeanUtils.toBean(updateReqVO, IotStorageAreaDO.class);
        iotStorageAreaMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotStorageArea(Long id) {
        // 校验存在
        validateIotStorageAreaExists(id);
        // 删除
        iotStorageAreaMapper.deleteById(id);
    }

    private void validateIotStorageAreaExists(Long id) {
        if (iotStorageAreaMapper.selectById(id) == null) {
            throw exception(IOT_STORAGE_AREA_NOT_EXISTS);
        }
    }

    @Override
    public IotStorageAreaDO getIotStorageArea(Long id) {
        return iotStorageAreaMapper.selectById(id);
    }

    @Override
    public PageResult<IotStorageAreaDO> getIotStorageAreaPage(IotStorageAreaPageReqVO pageReqVO) {
        return iotStorageAreaMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IotStorageAreaDO> getStorageAreaList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return iotStorageAreaMapper.selectBatchIds(ids);
    }

    @Override
    public List<IotStorageAreaDO> getStorageAreaListByStatus(Collection<Integer> statuses) {
        return iotStorageAreaMapper.selectListByStatus(statuses);
    }

}