package com.starry.admin.modules.blindbox.service;

import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.blindbox.mapper.BlindBoxPoolMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class BlindBoxInventoryService {

    private final BlindBoxPoolMapper blindBoxPoolMapper;

    @Transactional(rollbackFor = Exception.class)
    public void reserveRewardStock(String tenantId, Long poolId, String rewardGiftId) {
        int affected = blindBoxPoolMapper.consumeRewardStock(tenantId, poolId, rewardGiftId);
        if (affected <= 0) {
            throw new CustomException("盲盒奖池库存不足，请稍后再试");
        }
    }
}
