Search in sources :

Example 1 with FlashActivity

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

the class FlashActivityAppBuilder method toDomain.

public static FlashActivity toDomain(FlashActivityPublishCommand flashActivityPublishCommand) {
    if (flashActivityPublishCommand == null) {
        return null;
    }
    FlashActivity flashActivity = new FlashActivity();
    BeanUtils.copyProperties(flashActivityPublishCommand, flashActivity);
    return flashActivity;
}
Also used : FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity)

Example 2 with FlashActivity

use of com.actionworks.flashsale.domain.model.entity.FlashActivity 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 3 with FlashActivity

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

the class DefaultActivityAppService method modifyFlashActivity.

@Override
public AppResult modifyFlashActivity(Long userId, Long activityId, FlashActivityPublishCommand flashActivityPublishCommand) {
    logger.info("activityModification|秒杀活动修改|{},{},{}", userId, activityId, JSON.toJSONString(flashActivityPublishCommand));
    if (userId == null || flashActivityPublishCommand == null || !flashActivityPublishCommand.validate()) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ACTIVITY_MODIFICATION);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock activityModificationLock = lockFactoryService.getDistributedLock(getActivityModificationLockKey(activityId));
    try {
        boolean isLockSuccess = activityModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(FREQUENTLY_ERROR);
        }
        FlashActivity flashActivity = toDomain(flashActivityPublishCommand);
        flashActivity.setId(activityId);
        flashActivityDomainService.modifyActivity(userId, flashActivity);
        logger.info("activityModification|活动已修改");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("activityModification|活动修改失败|{},{}", userId, activityId, e);
        throw new BizException("活动修改失败");
    } finally {
        activityModificationLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 4 with FlashActivity

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

the class DefaultActivityAppService method getFlashActivities.

@Override
public AppMultiResult<FlashActivityDTO> getFlashActivities(Long userId, FlashActivitiesQuery flashActivitiesQuery) {
    List<FlashActivity> activities;
    Integer total;
    if (flashActivitiesQuery.isFirstPureQuery()) {
        FlashActivitiesCache flashActivitiesCache = flashActivitiesCacheService.getCachedActivities(flashActivitiesQuery.getPageNumber(), flashActivitiesQuery.getVersion());
        if (flashActivitiesCache.isLater()) {
            return AppMultiResult.tryLater();
        }
        activities = flashActivitiesCache.getFlashActivities();
        total = flashActivitiesCache.getTotal();
    } else {
        PageResult<FlashActivity> flashActivityPageResult = flashActivityDomainService.getFlashActivities(toFlashActivitiesQuery(flashActivitiesQuery));
        activities = flashActivityPageResult.getData();
        total = flashActivityPageResult.getTotal();
    }
    List<FlashActivityDTO> flashActivityDTOList = activities.stream().map(FlashActivityAppBuilder::toFlashActivityDTO).collect(Collectors.toList());
    return AppMultiResult.of(flashActivityDTOList, total);
}
Also used : FlashActivityDTO(com.actionworks.flashsale.app.model.dto.FlashActivityDTO) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivitiesCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)

Example 5 with FlashActivity

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

the class DefaultActivityAppService method isAllowPlaceOrderOrNot.

@Override
public boolean isAllowPlaceOrderOrNot(Long activityId) {
    FlashActivityCache flashActivityCache = flashActivityCacheService.getCachedActivity(activityId, null);
    if (flashActivityCache.isLater()) {
        logger.info("isAllowPlaceOrderOrNot|稍后再试|{}", activityId);
        return false;
    }
    if (!flashActivityCache.isExist() || flashActivityCache.getFlashActivity() == null) {
        logger.info("isAllowPlaceOrderOrNot|活动不存在|{}", activityId);
        return false;
    }
    FlashActivity flashActivity = flashActivityCache.getFlashActivity();
    if (!flashActivity.isOnline()) {
        logger.info("isAllowPlaceOrderOrNot|活动尚未上线|{}", activityId);
        return false;
    }
    if (!flashActivity.isInProgress()) {
        logger.info("isAllowPlaceOrderOrNot|活动非秒杀时段|{}", activityId);
        return false;
    }
    // 可在此处丰富其他校验规则
    return true;
}
Also used : FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivityCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)

Aggregations

FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)12 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)4 AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)2 BizException (com.actionworks.flashsale.app.exception.BizException)2 FlashActivitiesCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)2 FlashActivityCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)2 AuthException (com.actionworks.flashsale.controller.exception.AuthException)2 FlashActivityEvent (com.actionworks.flashsale.domain.event.FlashActivityEvent)2 DomainException (com.actionworks.flashsale.domain.exception.DomainException)2 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)2 FlashActivityDTO (com.actionworks.flashsale.app.model.dto.FlashActivityDTO)1 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)1 FlashActivityDO (com.actionworks.flashsale.persistence.model.FlashActivityDO)1