Search in sources :

Example 1 with Product

use of com.baomidou.samples.localtx.entity.Product in project dynamic-datasource-samples by dynamic-datasource.

the class ProductService method reduceStock.

@DS("product")
public Double reduceStock(Long productId, Integer amount) {
    log.info("=============PRODUCT START=================");
    log.info("当前 XID: {}", TransactionContext.getXID());
    // 检查库存
    Product product = productMapper.selectById(productId);
    Assert.notNull(product, "商品不存在");
    Integer stock = product.getStock();
    log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, amount);
    if (stock < amount) {
        log.warn("商品编号为{} 库存不足,当前库存:{}", productId, stock);
        throw new RuntimeException("库存不足");
    }
    log.info("开始扣减商品编号为 {} 库存,单价商品价格为{}", productId, product.getPrice());
    // 扣减库存
    int currentStock = stock - amount;
    product.setStock(currentStock);
    productMapper.updateById(product);
    double totalPrice = product.getPrice() * amount;
    log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, amount, totalPrice);
    log.info("=============PRODUCT END=================");
    return totalPrice;
}
Also used : Product(com.baomidou.samples.localtx.entity.Product) DS(com.baomidou.dynamic.datasource.annotation.DS)

Aggregations

DS (com.baomidou.dynamic.datasource.annotation.DS)1 Product (com.baomidou.samples.localtx.entity.Product)1