Search in sources :

Example 6 with Bucket

use of com.actionworks.flashsale.domain.model.Bucket in project flash-sale by ThoughtsBeta.

the class DefaultBucketsArrangementService method queryStockBucketsSummary.

@Override
public StockBucketSummaryDTO queryStockBucketsSummary(Long itemId) {
    if (itemId == null) {
        throw new StockBucketException("参数错误");
    }
    List<Bucket> buckets = bucketsDomainService.getBucketsByItem(itemId);
    int remainAvailableStocks = buckets.stream().mapToInt(Bucket::getAvailableStocksAmount).sum();
    Optional<Bucket> primaryBucketOptional = buckets.stream().filter(Bucket::isPrimaryBucket).findFirst();
    if (!primaryBucketOptional.isPresent()) {
        return new StockBucketSummaryDTO();
    }
    List<StockBucketDTO> subBuckets = buckets.stream().map(StockBucketBuilder::toStockBucketDTO).collect(toList());
    return new StockBucketSummaryDTO().setTotalStocksAmount(primaryBucketOptional.get().getTotalStocksAmount()).setAvailableStocksAmount(remainAvailableStocks).setBuckets(subBuckets);
}
Also used : Bucket(com.actionworks.flashsale.domain.model.Bucket) StockBucketSummaryDTO(com.actionworks.flashsale.app.model.dto.StockBucketSummaryDTO) StockBucketException(com.actionworks.flashsale.app.exception.StockBucketException) StockBucketDTO(com.actionworks.flashsale.app.model.dto.StockBucketDTO)

Example 7 with Bucket

use of com.actionworks.flashsale.domain.model.Bucket in project flash-sale by ThoughtsBeta.

the class DefaultBucketsArrangementService method initStockBuckets.

private void initStockBuckets(Long itemId, Integer totalStockAmount, Integer bucketsQuantity) {
    Bucket primaryBucket = new Bucket().initPrimary().setTotalStocksAmount(totalStockAmount).setItemId(itemId);
    List<Bucket> presentBuckets = buildBuckets(itemId, totalStockAmount, bucketsQuantity, primaryBucket);
    submitBucketsToArrange(itemId, presentBuckets);
}
Also used : Bucket(com.actionworks.flashsale.domain.model.Bucket)

Example 8 with Bucket

use of com.actionworks.flashsale.domain.model.Bucket in project flash-sale by ThoughtsBeta.

the class DefaultBucketsArrangementService method arrangeStockBucketsBasedTotalMode.

/**
 * 根据总量库存重新分桶
 */
private void arrangeStockBucketsBasedTotalMode(Long itemId, Integer totalStockAmount, Integer bucketsAmount, List<Bucket> existingBuckets) {
    // 重新分桶
    int remainAvailableStocks = existingBuckets.stream().filter(Bucket::isSubBucket).mapToInt(Bucket::getAvailableStocksAmount).sum();
    Optional<Bucket> primaryBucketOptional = existingBuckets.stream().filter(Bucket::isPrimaryBucket).findFirst();
    if (!primaryBucketOptional.isPresent()) {
        return;
    }
    // 回收分桶库存到主桶
    Bucket primaryBucket = primaryBucketOptional.get();
    primaryBucket.addAvailableStocks(remainAvailableStocks);
    int soldStocksAmount = primaryBucket.getTotalStocksAmount() - primaryBucket.getAvailableStocksAmount();
    if (soldStocksAmount > totalStockAmount) {
        throw new StockBucketException("已售库存大于当期所设库存总量!");
    }
    primaryBucket.setTotalStocksAmount(totalStockAmount);
    List<Bucket> presentBuckets = buildBuckets(itemId, totalStockAmount, bucketsAmount, primaryBucket);
    submitBucketsToArrange(itemId, presentBuckets);
}
Also used : Bucket(com.actionworks.flashsale.domain.model.Bucket) StockBucketException(com.actionworks.flashsale.app.exception.StockBucketException)

Example 9 with Bucket

use of com.actionworks.flashsale.domain.model.Bucket in project flash-sale by ThoughtsBeta.

the class BucketBuilder method toDomainObject.

public static Bucket toDomainObject(BucketDO bucketDO) {
    Bucket bucket = new Bucket();
    BeanUtils.copyProperties(bucketDO, bucket);
    return bucket;
}
Also used : Bucket(com.actionworks.flashsale.domain.model.Bucket)

Aggregations

Bucket (com.actionworks.flashsale.domain.model.Bucket)9 StockBucketException (com.actionworks.flashsale.app.exception.StockBucketException)5 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)2 StockBucketDTO (com.actionworks.flashsale.app.model.dto.StockBucketDTO)1 StockBucketSummaryDTO (com.actionworks.flashsale.app.model.dto.StockBucketSummaryDTO)1 StockBucketEvent (com.actionworks.flashsale.domain.event.StockBucketEvent)1 DomainException (com.actionworks.flashsale.domain.exception.DomainException)1 ArrayList (java.util.ArrayList)1 DefaultRedisScript (org.springframework.data.redis.core.script.DefaultRedisScript)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 Transactional (org.springframework.transaction.annotation.Transactional)1