購入制限-同時

1737 ワード

  @TargetDataSource(name = "mysql2")
    @Transactional
    public void test() {
        Integer goodsNum = exampleMapper.selectGoodsNum();
        Integer hasOrderNum = exampleMapper.selectOrderNum();
        try {
            if (goodsNum > 0 && hasOrderNum < 3) {
                Integer reduce = exampleMapper.reduceGoods();
                logger.warn("===reduce num:" + reduce);
                if (reduce == 1) {
                    hasOrderNum = exampleMapper.selectOrderNumLock();
                    logger.warn("===order hasOrderNum:" + hasOrderNum);
                    if (hasOrderNum >= 3) {
                        throw new TokenException(Constants.OTHER_ERROR, " 3 ");
                    }
                    Integer order = exampleMapper.insertOrder();
                    logger.warn("===order order:" + order);
                }
            } else {
                System.out.println("======  :{}=== goodsNum:{}" + goodsNum + " hasOrderNum:" + hasOrderNum);
            }

        } catch (TokenException tok) {
            throw tok;
        } catch (Exception e) {
            System.out.println("=== ===" + JsonUtil.bean2Json(e));
            throw new RuntimeException(e);
        }
    }
 
public class TokenException extends RuntimeException {
    public String msg;
    public Integer code;

    public TokenException(Integer code, String msg) {
        this.msg = msg;
        this.code = code;
    }
}