package cn.iocoder.yudao.module.system.service.biz;

import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.store.StoreManagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.store.StoreManageSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.StoreManageDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.StoreManageMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMMON_NOT_FOUND;

/**
 * 仓库管理 Service 实现类
 */
@Service
@Validated
public class StoreManageServiceImpl implements StoreManageService {

    @Resource
    private StoreManageMapper storeManageMapper;

    @Override
        public Long create(StoreManageSaveReqVO createReqVO) {
        // 插入
        StoreManageDO storeManage = BeanUtils.toBean(createReqVO, StoreManageDO.class);
        storeManageMapper.insert(storeManage);
        // 返回
        return storeManage.getId();
    }

    @Override
    public void update(StoreManageSaveReqVO updateReqVO) {
        // 校验存在
        validateExists(updateReqVO.getId());
        // 更新
        StoreManageDO updateObj = BeanUtils.toBean(updateReqVO, StoreManageDO.class);
        storeManageMapper.updateById(updateObj);
    }

    @Override
        public void delete(Long id) {
        // 校验存在
        validateExists(id);
        // 删除
        storeManageMapper.deleteById(id);
    }

    private void validateExists(Long id) {
        if (storeManageMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(COMMON_NOT_FOUND);
        }
    }

    @Override
    public StoreManageDO get(Long id) {
        return storeManageMapper.selectById(id);
    }

    @Override
    public List<StoreManageDO> list(Long[] tenantIds) {
        return storeManageMapper.selectList(new LambdaQueryWrapperX<StoreManageDO>().in(StoreManageDO::getTenantId,tenantIds));
    }

    @Override
    public PageResult<StoreManageDO> getPage(StoreManagePageReqVO pageReqVO) {
        return storeManageMapper.selectPage(pageReqVO);
    }

}
