Search in sources :

Example 1 with SeckillCloseException

use of com.goodskill.common.exception.SeckillCloseException in project goodsKill by techa03.

the class SeckillServiceImpl method reduceNumberInner.

@Transactional(rollbackFor = Exception.class)
@Override
public int reduceNumberInner(SuccessKilled successKilled) {
    successKilledMapper.insert(successKilled);
    Seckill wrapper = new Seckill();
    wrapper.setSeckillId(successKilled.getSeckillId());
    UpdateWrapper<Seckill> updateWrapper = new UpdateWrapper(wrapper);
    updateWrapper.gt("end_time", successKilled.getCreateTime());
    updateWrapper.lt("start_time", successKilled.getCreateTime());
    updateWrapper.gt("number", 0);
    updateWrapper.setSql("number = number - 1");
    int update = baseMapper.update(null, updateWrapper);
    if (update <= 0) {
        throw new SeckillCloseException("seckill is closed");
    } else {
        taskExecutor.execute(() -> streamBridge.send(DEFAULT_BINDING_NAME_MONGO_SAVE, MessageBuilder.withPayload(SeckillMockSaveVo.builder().seckillId(successKilled.getSeckillId()).userPhone(successKilled.getUserPhone()).note(REDIS_MONGO_REACTIVE.getName()).build()).build()));
        log.info("已发送");
        return update;
    }
}
Also used : SeckillCloseException(com.goodskill.common.exception.SeckillCloseException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) Seckill(com.goodskill.entity.Seckill) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SeckillCloseException

use of com.goodskill.common.exception.SeckillCloseException in project goodsKill by techa03.

the class SeckillServiceImpl method executeSeckill.

@Transactional(rollbackFor = Exception.class)
@Override
public SeckillExecutionDTO executeSeckill(long seckillId, String userPhone, String md5) {
    if (md5 == null || !md5.equals(MD5Util.getMD5(seckillId))) {
        throw new SeckillException("seckill data rewrite");
    }
    Date nowTime = DateUtil.getNowTime();
    try {
        int updateCount = baseMapper.reduceNumber(seckillId, nowTime);
        if (updateCount <= 0) {
            throw new SeckillCloseException("seckill is closed");
        } else {
            SuccessKilled successKilled = new SuccessKilled();
            successKilled.setSeckillId(seckillId);
            successKilled.setUserPhone(userPhone);
            int insertCount = successKilledMapper.insert(successKilled);
            String qrfilepath = alipayRunner.tradePrecreate(seckillId);
            if (insertCount <= 0) {
                throw new RepeatKillException("seckill repeated");
            } else {
                SuccessKilled key = new SuccessKilled();
                key.setSeckillId(seckillId);
                key.setUserPhone(userPhone);
                return new SeckillExecutionDTO(seckillId, SeckillStatEnum.SUCCESS.getStateInfo(), successKilledMapper.selectOne(new QueryWrapper<>(key)), qrfilepath);
            }
        }
    } catch (SeckillCloseException | RepeatKillException e1) {
        log.info(e1.getMessage(), e1);
        throw e1;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SeckillException("seckill inner error:" + e.getMessage());
    }
}
Also used : SeckillCloseException(com.goodskill.common.exception.SeckillCloseException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) SeckillException(com.goodskill.common.exception.SeckillException) SuccessKilled(com.goodskill.entity.SuccessKilled) RepeatKillException(com.goodskill.common.exception.RepeatKillException) Date(java.util.Date) RepeatKillException(com.goodskill.common.exception.RepeatKillException) IOException(java.io.IOException) SeckillCloseException(com.goodskill.common.exception.SeckillCloseException) SeckillException(com.goodskill.common.exception.SeckillException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with SeckillCloseException

use of com.goodskill.common.exception.SeckillCloseException in project goodsKill by techa03.

the class SeckillController method execute.

@PostMapping(value = "/{seckillId}/{md5}/execution", produces = { "application/json;charset=UTF-8" })
@ResponseBody
public Result<SeckillExecutionDTO> execute(@PathVariable("seckillId") Long seckillId, @PathVariable("md5") String md5, @CookieValue(value = "killPhone", required = false) String phone) {
    Result<SeckillExecutionDTO> result;
    if (phone == null) {
        result = Result.fail("未注册");
        return result;
    }
    try {
        SeckillExecutionDTO execution = seckillService.executeSeckill(seckillId, phone, md5);
        result = Result.ok(execution);
    } catch (RepeatKillException e) {
        logger.info(e.getMessage(), e);
        SeckillExecutionDTO execution = new SeckillExecutionDTO(seckillId, SeckillStatEnum.REPEAT_KILL.getStateInfo());
        result = Result.fail(execution, null);
    } catch (SeckillCloseException e) {
        logger.info(e.getMessage(), e);
        SeckillExecutionDTO execution = new SeckillExecutionDTO(seckillId, SeckillStatEnum.END.getStateInfo());
        result = Result.fail(execution, null);
    } catch (Exception e) {
        logger.info(e.getMessage(), e);
        SeckillExecutionDTO execution = new SeckillExecutionDTO(seckillId, SeckillStatEnum.INNER_ERROR.getStateInfo());
        result = Result.fail(execution, null);
    }
    return result;
}
Also used : SeckillCloseException(com.goodskill.common.exception.SeckillCloseException) RepeatKillException(com.goodskill.common.exception.RepeatKillException) RepeatKillException(com.goodskill.common.exception.RepeatKillException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SeckillCloseException(com.goodskill.common.exception.SeckillCloseException) SeckillExecutionDTO(com.goodskill.api.dto.SeckillExecutionDTO)

Aggregations

SeckillCloseException (com.goodskill.common.exception.SeckillCloseException)3 RepeatKillException (com.goodskill.common.exception.RepeatKillException)2 IOException (java.io.IOException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)1 SeckillExecutionDTO (com.goodskill.api.dto.SeckillExecutionDTO)1 SeckillException (com.goodskill.common.exception.SeckillException)1 Seckill (com.goodskill.entity.Seckill)1 SuccessKilled (com.goodskill.entity.SuccessKilled)1 FileNotFoundException (java.io.FileNotFoundException)1 Date (java.util.Date)1