Search in sources :

Example 1 with StockBucketEvent

use of com.actionworks.flashsale.domain.event.StockBucketEvent in project flash-sale by ThoughtsBeta.

the class BucketsDomainServiceImpl method arrangeBuckets.

@Override
public boolean arrangeBuckets(Long itemId, List<Bucket> buckets) {
    logger.info("arrangeBuckets|编排库存分桶|{},{}", itemId, JSON.toJSONString(buckets));
    if (itemId == null || itemId <= 0 || CollectionUtils.isEmpty(buckets)) {
        logger.info("arrangeBuckets|库存分桶参数错误|{}", itemId);
        throw new DomainException(PARAMS_INVALID);
    }
    Optional<Bucket> primaryBucketOptional = buckets.stream().filter(Bucket::isPrimaryBucket).findFirst();
    if (!primaryBucketOptional.isPresent()) {
        throw new DomainException(PRIMARY_BUCKET_IS_MISSING);
    }
    if (buckets.stream().filter(Bucket::isPrimaryBucket).count() > 1) {
        throw new DomainException(MULTI_PRIMARY_BUCKETS_FOUND_BUT_EXPECT_ONE);
    }
    buckets.forEach(stockBucket -> {
        if (stockBucket.getTotalStocksAmount() == null || stockBucket.getTotalStocksAmount() < 0) {
            throw new DomainException(TOTAL_STOCKS_AMOUNT_INVALID);
        }
        if (stockBucket.getAvailableStocksAmount() == null || stockBucket.getAvailableStocksAmount() <= 0) {
            throw new DomainException(AVAILABLE_STOCKS_AMOUNT_INVALID);
        }
        if (!stockBucket.getAvailableStocksAmount().equals(stockBucket.getTotalStocksAmount()) && stockBucket.isSubBucket()) {
            throw new DomainException(AVAILABLE_STOCKS_AMOUNT_NOT_EQUALS_TO_TOTAL_STOCKS_AMOUNT);
        }
        if (!itemId.equals(stockBucket.getItemId())) {
            throw new DomainException(STOCK_BUCKET_ITEM_INVALID);
        }
    });
    boolean success = bucketsRepository.submitBuckets(itemId, buckets);
    if (!success) {
        return false;
    }
    StockBucketEvent stockBucketEvent = new StockBucketEvent();
    stockBucketEvent.setEventType(StockBucketEventType.ARRANGED);
    stockBucketEvent.setItemId(itemId);
    domainEventPublisher.publish(stockBucketEvent);
    logger.info("arrangeBuckets|编排库存分桶已完成|{}", itemId);
    return true;
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) Bucket(com.actionworks.flashsale.domain.model.Bucket) StockBucketEvent(com.actionworks.flashsale.domain.event.StockBucketEvent)

Example 2 with StockBucketEvent

use of com.actionworks.flashsale.domain.event.StockBucketEvent in project flash-sale by ThoughtsBeta.

the class BucketsDomainServiceImpl method suspendBuckets.

@Override
public boolean suspendBuckets(Long itemId) {
    logger.info("suspendBuckets|禁用库存分桶|{}", itemId);
    if (itemId == null || itemId <= 0) {
        throw new DomainException(PARAMS_INVALID);
    }
    boolean success = bucketsRepository.suspendBuckets(itemId);
    if (!success) {
        return false;
    }
    StockBucketEvent stockBucketEvent = new StockBucketEvent();
    stockBucketEvent.setEventType(StockBucketEventType.DISABLED);
    stockBucketEvent.setItemId(itemId);
    domainEventPublisher.publish(stockBucketEvent);
    logger.info("suspendBuckets|库存分桶已禁用|{}", itemId);
    return true;
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) StockBucketEvent(com.actionworks.flashsale.domain.event.StockBucketEvent)

Example 3 with StockBucketEvent

use of com.actionworks.flashsale.domain.event.StockBucketEvent in project flash-sale by ThoughtsBeta.

the class BucketsDomainServiceImpl method resumeBuckets.

@Override
public boolean resumeBuckets(Long itemId) {
    logger.info("resumeBuckets|启用库存分桶|{}", itemId);
    if (itemId == null || itemId <= 0) {
        throw new DomainException(PARAMS_INVALID);
    }
    boolean success = bucketsRepository.resumeStockBuckets(itemId);
    if (!success) {
        return false;
    }
    StockBucketEvent stockBucketEvent = new StockBucketEvent();
    stockBucketEvent.setEventType(StockBucketEventType.ENABLED);
    stockBucketEvent.setItemId(itemId);
    domainEventPublisher.publish(stockBucketEvent);
    logger.info("suspendBuckets|库存桶已禁用|{}", itemId);
    return true;
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) StockBucketEvent(com.actionworks.flashsale.domain.event.StockBucketEvent)

Aggregations

StockBucketEvent (com.actionworks.flashsale.domain.event.StockBucketEvent)3 DomainException (com.actionworks.flashsale.domain.exception.DomainException)3 Bucket (com.actionworks.flashsale.domain.model.Bucket)1