Search in sources :

Example 1 with DomainException

use of com.actionworks.flashsale.domain.exception.DomainException 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 DomainException

use of com.actionworks.flashsale.domain.exception.DomainException 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 DomainException

use of com.actionworks.flashsale.domain.exception.DomainException in project flash-sale by ThoughtsBeta.

the class FlashActivityDomainServiceImpl method offlineActivity.

@Override
public void offlineActivity(Long userId, Long activityId) {
    logger.info("activityOffline|下线秒杀活动|{},{}", userId, activityId);
    if (StringUtils.isEmpty(userId) || activityId == null) {
        throw new DomainException(PARAMS_INVALID);
    }
    Optional<FlashActivity> flashActivityOptional = flashActivityRepository.findById(activityId);
    if (!flashActivityOptional.isPresent()) {
        throw new DomainException(FLASH_ACTIVITY_DOES_NOT_EXIST);
    }
    FlashActivity flashActivity = flashActivityOptional.get();
    if (FlashActivityStatus.isOffline(flashActivity.getStatus())) {
        return;
    }
    if (!FlashActivityStatus.isOnline(flashActivity.getStatus())) {
        throw new DomainException(FLASH_ACTIVITY_NOT_ONLINE);
    }
    flashActivity.setStatus(FlashActivityStatus.OFFLINE.getCode());
    flashActivityRepository.save(flashActivity);
    logger.info("activityOffline|活动已下线|{},{}", userId, flashActivity.getId());
    FlashActivityEvent flashActivityEvent = new FlashActivityEvent();
    flashActivityEvent.setEventType(FlashActivityEventType.OFFLINE);
    flashActivityEvent.setFlashActivity(flashActivity);
    domainEventPublisher.publish(flashActivityEvent);
    logger.info("activityOffline|活动下线事件已发布|{}", JSON.toJSON(flashActivityEvent));
}
Also used : FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlashActivityEvent(com.actionworks.flashsale.domain.event.FlashActivityEvent)

Example 4 with DomainException

use of com.actionworks.flashsale.domain.exception.DomainException in project flash-sale by ThoughtsBeta.

the class FlashActivityDomainServiceImpl method publishActivity.

@Override
public void publishActivity(Long userId, FlashActivity flashActivity) {
    logger.info("activityPublish|发布秒杀活动|{},{}", userId, JSON.toJSONString(flashActivity));
    if (flashActivity == null || !flashActivity.validateParamsForCreateOrUpdate()) {
        throw new DomainException(ONLINE_FLASH_ACTIVITY_PARAMS_INVALID);
    }
    flashActivity.setStatus(FlashActivityStatus.PUBLISHED.getCode());
    flashActivityRepository.save(flashActivity);
    logger.info("activityPublish|活动已发布|{},{}", userId, flashActivity.getId());
    FlashActivityEvent flashActivityEvent = new FlashActivityEvent();
    flashActivityEvent.setEventType(FlashActivityEventType.PUBLISHED);
    flashActivityEvent.setFlashActivity(flashActivity);
    domainEventPublisher.publish(flashActivityEvent);
    logger.info("activityPublish|活动发布事件已发布|{}", JSON.toJSON(flashActivityEvent));
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlashActivityEvent(com.actionworks.flashsale.domain.event.FlashActivityEvent)

Example 5 with DomainException

use of com.actionworks.flashsale.domain.exception.DomainException in project flash-sale by ThoughtsBeta.

the class FlashItemDomainServiceImpl method publishFlashItem.

@Override
public void publishFlashItem(FlashItem flashItem) {
    logger.info("itemPublish|发布秒杀品|{}", JSON.toJSON(flashItem));
    if (flashItem == null || !flashItem.validateParamsForCreate()) {
        throw new DomainException(ONLINE_FLASH_ITEM_PARAMS_INVALID);
    }
    flashItem.setStatus(FlashItemStatus.PUBLISHED.getCode());
    flashItemRepository.save(flashItem);
    logger.info("itemPublish|秒杀品已发布|{}", flashItem.getId());
    FlashItemEvent flashItemEvent = new FlashItemEvent();
    flashItemEvent.setEventType(FlashItemEventType.PUBLISHED);
    flashItemEvent.setFlashItem(flashItem);
    domainEventPublisher.publish(flashItemEvent);
    logger.info("itemPublish|秒杀品发布事件已发布|{}", flashItem.getId());
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlashItemEvent(com.actionworks.flashsale.domain.event.FlashItemEvent)

Aggregations

DomainException (com.actionworks.flashsale.domain.exception.DomainException)13 FlashActivityEvent (com.actionworks.flashsale.domain.event.FlashActivityEvent)4 FlashItemEvent (com.actionworks.flashsale.domain.event.FlashItemEvent)3 StockBucketEvent (com.actionworks.flashsale.domain.event.StockBucketEvent)3 FlashOrderEvent (com.actionworks.flashsale.domain.event.FlashOrderEvent)2 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)2 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)2 BizException (com.actionworks.flashsale.app.exception.BizException)1 Bucket (com.actionworks.flashsale.domain.model.Bucket)1 FlashOrder (com.actionworks.flashsale.domain.model.entity.FlashOrder)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)1