use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.
the class DefaultFlashItemAppService method publishFlashItem.
@Override
public AppResult publishFlashItem(Long userId, Long activityId, FlashItemPublishCommand itemPublishCommand) {
logger.info("itemPublish|发布秒杀品|{},{},{}", userId, activityId, JSON.toJSON(itemPublishCommand));
if (userId == null || activityId == null || itemPublishCommand == null || !itemPublishCommand.validate()) {
throw new BizException(INVALID_PARAMS);
}
AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_CREATE);
if (!authResult.isSuccess()) {
throw new AuthException(UNAUTHORIZED_ACCESS);
}
DistributedLock itemCreateLock = lockFactoryService.getDistributedLock(getItemCreateLockKey(userId));
try {
boolean isLockSuccess = itemCreateLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
if (!isLockSuccess) {
throw new BizException(FREQUENTLY_ERROR);
}
FlashActivity flashActivity = flashActivityDomainService.getFlashActivity(activityId);
if (flashActivity == null) {
throw new BizException(ACTIVITY_NOT_FOUND);
}
FlashItem flashItem = toDomain(itemPublishCommand);
flashItem.setActivityId(activityId);
flashItem.setStockWarmUp(0);
flashItemDomainService.publishFlashItem(flashItem);
logger.info("itemPublish|秒杀品已发布");
return AppResult.buildSuccess();
} catch (Exception e) {
logger.error("itemPublish|秒杀品发布失败|{}", userId, e);
throw new BizException("秒杀品发布失败");
} finally {
itemCreateLock.unlock();
}
}
use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.
the class FlashActivitiesCacheService method tryToUpdateActivitiesCacheByLock.
public FlashActivitiesCache tryToUpdateActivitiesCacheByLock(Integer pageNumber) {
logger.info("activitiesCache|更新远程缓存|{}", pageNumber);
DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ACTIVITIES_CACHE_LOCK_KEY);
try {
boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
if (!isLockSuccess) {
return new FlashActivitiesCache().tryLater();
}
PagesQueryCondition pagesQueryCondition = new PagesQueryCondition();
PageResult<FlashActivity> flashActivityPageResult = flashActivityDomainService.getFlashActivities(pagesQueryCondition);
FlashActivitiesCache flashActivitiesCache;
if (flashActivityPageResult == null) {
flashActivitiesCache = new FlashActivitiesCache().notExist();
} else {
flashActivitiesCache = new FlashActivitiesCache().setTotal(flashActivityPageResult.getTotal()).setFlashActivities(flashActivityPageResult.getData()).setVersion(System.currentTimeMillis());
}
distributedCacheService.put(buildActivityCacheKey(pageNumber), JSON.toJSONString(flashActivitiesCache), FIVE_MINUTES);
logger.info("activitiesCache|远程缓存已更新|{}", pageNumber);
return flashActivitiesCache;
} catch (InterruptedException e) {
logger.error("activitiesCache|远程缓存更新失败", e);
return new FlashActivitiesCache().tryLater();
} finally {
lock.unlock();
}
}
use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.
the class FlashActivityCacheService method tryToUpdateActivityCacheByLock.
public FlashActivityCache tryToUpdateActivityCacheByLock(Long activityId) {
logger.info("activityCache|更新远程缓存|{}", activityId);
DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ACTIVITY_CACHE_LOCK_KEY + activityId);
try {
boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
if (!isLockSuccess) {
return new FlashActivityCache().tryLater();
}
FlashActivity flashActivity = flashActivityDomainService.getFlashActivity(activityId);
FlashActivityCache flashActivityCache;
if (flashActivity == null) {
flashActivityCache = new FlashActivityCache().notExist();
} else {
flashActivityCache = new FlashActivityCache().with(flashActivity).withVersion(System.currentTimeMillis());
}
distributedCacheService.put(buildActivityCacheKey(activityId), JSON.toJSONString(flashActivityCache), FIVE_MINUTES);
logger.info("activityCache|远程缓存已更新|{}", activityId);
return flashActivityCache;
} catch (InterruptedException e) {
logger.error("activityCache|远程缓存更新失败|{}", activityId);
return new FlashActivityCache().tryLater();
} finally {
lock.unlock();
}
}
use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.
the class FlashActivityDomainServiceImpl method getFlashActivities.
@Override
public PageResult<FlashActivity> getFlashActivities(PagesQueryCondition pagesQueryCondition) {
if (pagesQueryCondition == null) {
pagesQueryCondition = new PagesQueryCondition();
}
List<FlashActivity> flashActivities = flashActivityRepository.findFlashActivitiesByCondition(pagesQueryCondition.buildParams());
Integer total = flashActivityRepository.countFlashActivitiesByCondition(pagesQueryCondition);
return PageResult.with(flashActivities, total);
}
use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.
the class FlashActivityBuilder method toDomainObject.
public static FlashActivity toDomainObject(FlashActivityDO flashActivityDO) {
FlashActivity flashActivity = new FlashActivity();
BeanUtils.copyProperties(flashActivityDO, flashActivity);
return flashActivity;
}
Aggregations