Search in sources :

Example 1 with Seckill

use of com.whoiszxl.entity.Seckill in project shopzz by whoiszxl.

the class SeckillAdminController method update.

@SaCheckLogin
@PutMapping
@ApiOperation(value = "更新秒杀活动", notes = "更新秒杀活动", response = ResponseResult.class)
public ResponseResult<Boolean> update(@RequestBody SeckillUpdateCommand bannerSaveCommand) {
    Seckill seckill = dozerUtils.map(bannerSaveCommand, Seckill.class);
    boolean updateFlag = seckillService.updateById(seckill);
    return ResponseResult.buildByFlag(updateFlag);
}
Also used : Seckill(com.whoiszxl.entity.Seckill) SaCheckLogin(cn.dev33.satoken.annotation.SaCheckLogin) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with Seckill

use of com.whoiszxl.entity.Seckill in project shopzz by whoiszxl.

the class QueuedPlaceOrderServiceImpl method checkSeckill.

private boolean checkSeckill(Long seckillId) {
    // 1. 从缓存中拿到活动信息
    SeckillCache seckillCache = seckillCachedService.getCachedSeckill(seckillId, null);
    // 2. 校验缓存是否需要延迟加载
    if (seckillCache.isLater()) {
        log.info("从缓存中获取秒杀活动信息需要延迟加载, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 3. 校验活动是否存在
    if (!seckillCache.isExist() || seckillCache.getSeckill() == null) {
        log.info("活动不存在, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 4. 校验活动是否上线
    Seckill seckill = seckillCache.getSeckill();
    if (!seckill.getStatus().equals(StatusEnum.OPEN.getCode())) {
        log.info("活动未开启, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 5. 校验活动是否在进行中
    LocalDateTime now = LocalDateTime.now();
    if (!seckill.getStartTime().isBefore(now) || !seckill.getEndTime().isAfter(now)) {
        log.info("活动未在开启时间内, 秒杀活动ID: {}", seckillId);
        return false;
    }
    return true;
}
Also used : LocalDateTime(java.time.LocalDateTime) SeckillCache(com.whoiszxl.cqrs.cache.SeckillCache) Seckill(com.whoiszxl.entity.Seckill)

Example 3 with Seckill

use of com.whoiszxl.entity.Seckill in project shopzz by whoiszxl.

the class DefaultPlaceOrderServiceImpl method checkSeckill.

private boolean checkSeckill(Long seckillId) {
    // 1. 从缓存中拿到活动信息
    SeckillCache seckillCache = seckillCachedService.getCachedSeckill(seckillId, null);
    // 2. 校验缓存是否需要延迟加载
    if (!seckillCache.isLater()) {
        log.info("从缓存中获取秒杀活动信息需要延迟加载, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 3. 校验活动是否存在
    if (!seckillCache.isExist() || seckillCache.getSeckill() == null) {
        log.info("活动不存在, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 4. 校验活动是否上线
    Seckill seckill = seckillCache.getSeckill();
    if (!seckill.getStatus().equals(StatusEnum.OPEN.getCode())) {
        log.info("活动未开启, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 5. 校验活动是否在进行中
    LocalDateTime now = LocalDateTime.now();
    if (!seckill.getStartTime().isBefore(now) || !seckill.getEndTime().isAfter(now)) {
        log.info("活动未在开启时间内, 秒杀活动ID: {}", seckillId);
        return false;
    }
    return true;
}
Also used : LocalDateTime(java.time.LocalDateTime) SeckillCache(com.whoiszxl.cqrs.cache.SeckillCache) Seckill(com.whoiszxl.entity.Seckill)

Example 4 with Seckill

use of com.whoiszxl.entity.Seckill in project shopzz by whoiszxl.

the class SeckillCachedServiceImpl method loadDb.

private SeckillCache loadDb(Long seckillId) {
    DistributedLock lock = distributedLockFactory.getDistributedLock(RedisKeyPrefixConstants.LOCK_GET_SECKILL_FROM_DB + seckillId);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new SeckillCache().tryLater();
        }
        Seckill seckill = seckillService.getById(seckillId);
        SeckillCache seckillCache;
        if (seckill == null) {
            seckillCache = new SeckillCache().notExist();
        } else {
            seckillCache = new SeckillCache().with(seckill);
        }
        redisUtils.setEx(RedisKeyPrefixConstants.CACHE_SECKILL + seckillId, JsonUtil.toJson(seckillCache), 3, TimeUnit.MINUTES);
        log.info("从数据库加载数据,并保存到redis中:{}", seckillCache);
        return seckillCache;
    } catch (Exception e) {
        log.error("从数据库加载数据失败", e);
        return new SeckillCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.whoiszxl.DistributedLock) SeckillCache(com.whoiszxl.cqrs.cache.SeckillCache) Seckill(com.whoiszxl.entity.Seckill)

Example 5 with Seckill

use of com.whoiszxl.entity.Seckill in project shopzz by whoiszxl.

the class SeckillAdminController method save.

@SaCheckLogin
@PostMapping
@ApiOperation(value = "新增秒杀活动", notes = "新增秒杀活动", response = ResponseResult.class)
public ResponseResult<Boolean> save(@RequestBody SeckillSaveCommand bannerSaveCommand) {
    Seckill seckill = dozerUtils.map(bannerSaveCommand, Seckill.class);
    boolean saveFlag = seckillService.save(seckill);
    return ResponseResult.buildByFlag(saveFlag);
}
Also used : Seckill(com.whoiszxl.entity.Seckill) SaCheckLogin(cn.dev33.satoken.annotation.SaCheckLogin) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

Seckill (com.whoiszxl.entity.Seckill)5 SeckillCache (com.whoiszxl.cqrs.cache.SeckillCache)3 SaCheckLogin (cn.dev33.satoken.annotation.SaCheckLogin)2 ApiOperation (io.swagger.annotations.ApiOperation)2 LocalDateTime (java.time.LocalDateTime)2 DistributedLock (com.whoiszxl.DistributedLock)1