|
@@ -1,13 +1,10 @@
|
|
|
package cn.reghao.tnb.content.app.mall.db.repository;
|
|
package cn.reghao.tnb.content.app.mall.db.repository;
|
|
|
|
|
|
|
|
-import cn.reghao.tnb.content.app.mall.db.mapper.OrderMapper;
|
|
|
|
|
|
|
+import cn.reghao.tnb.content.api.dto.ItemSnapshot;
|
|
|
import cn.reghao.tnb.content.app.mall.db.mapper.ProductMapper;
|
|
import cn.reghao.tnb.content.app.mall.db.mapper.ProductMapper;
|
|
|
import cn.reghao.tnb.content.app.mall.db.mapper.ProductSnapshotMapper;
|
|
import cn.reghao.tnb.content.app.mall.db.mapper.ProductSnapshotMapper;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.constant.OrderStatus;
|
|
|
|
|
-import cn.reghao.tnb.content.app.mall.model.po.Order;
|
|
|
|
|
import cn.reghao.tnb.content.app.mall.model.po.Product;
|
|
import cn.reghao.tnb.content.app.mall.model.po.Product;
|
|
|
import cn.reghao.tnb.content.app.mall.model.po.ProductSnapshot;
|
|
import cn.reghao.tnb.content.app.mall.model.po.ProductSnapshot;
|
|
|
-import cn.reghao.tnb.content.app.mall.model.vo.OrderDetail;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.stereotype.Repository;
|
|
import org.springframework.stereotype.Repository;
|
|
@@ -22,41 +19,25 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
public class MallRepository {
|
|
public class MallRepository {
|
|
|
private final ProductMapper productMapper;
|
|
private final ProductMapper productMapper;
|
|
|
private final ProductSnapshotMapper productSnapshotMapper;
|
|
private final ProductSnapshotMapper productSnapshotMapper;
|
|
|
- private final OrderMapper orderMapper;
|
|
|
|
|
|
|
|
|
|
- public MallRepository(ProductMapper productMapper, ProductSnapshotMapper productSnapshotMapper,
|
|
|
|
|
- OrderMapper orderMapper) {
|
|
|
|
|
|
|
+ public MallRepository(ProductMapper productMapper, ProductSnapshotMapper productSnapshotMapper) {
|
|
|
this.productMapper = productMapper;
|
|
this.productMapper = productMapper;
|
|
|
this.productSnapshotMapper = productSnapshotMapper;
|
|
this.productSnapshotMapper = productSnapshotMapper;
|
|
|
- this.orderMapper = orderMapper;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// TODO 需要锁来保证库存变化的准确性
|
|
// TODO 需要锁来保证库存变化的准确性
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void saveOrder(Order order, ProductSnapshot productSnapshot) {
|
|
|
|
|
- long productId = order.getProductId();
|
|
|
|
|
- int amount = order.getAmount();
|
|
|
|
|
|
|
+ public void updateProduct(int amount, ProductSnapshot productSnapshot) {
|
|
|
|
|
+ long productId = productSnapshot.getItemId();
|
|
|
// 减商品库存
|
|
// 减商品库存
|
|
|
productMapper.updateStockMinus(productId, amount);
|
|
productMapper.updateStockMinus(productId, amount);
|
|
|
-
|
|
|
|
|
- orderMapper.save(order);
|
|
|
|
|
productSnapshotMapper.save(productSnapshot);
|
|
productSnapshotMapper.save(productSnapshot);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void updateCancelOrder(Order order) {
|
|
|
|
|
- long productId = order.getProductId();
|
|
|
|
|
- int amount = order.getAmount();
|
|
|
|
|
|
|
+ public void updateCancelOrder(long productId, int amount) {
|
|
|
// 恢复商品库存
|
|
// 恢复商品库存
|
|
|
productMapper.updateStockMinus(productId, amount);
|
|
productMapper.updateStockMinus(productId, amount);
|
|
|
-
|
|
|
|
|
- long orderId = order.getOrderId();
|
|
|
|
|
- orderMapper.updateOrderStatus(orderId, OrderStatus.cancelled.getCode());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void updatePayOrder(long orderId) {
|
|
|
|
|
- int status = OrderStatus.toConfirm.getCode();
|
|
|
|
|
- orderMapper.updateOrderStatus(orderId, status);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Cacheable(cacheNames = "tnb:mall:product", key = "#itemId", unless = "#result == null")
|
|
@Cacheable(cacheNames = "tnb:mall:product", key = "#itemId", unless = "#result == null")
|
|
@@ -65,15 +46,7 @@ public class MallRepository {
|
|
|
return productMapper.findByItemId(itemId);
|
|
return productMapper.findByItemId(itemId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public OrderDetail getOrderDetail(long orderId) {
|
|
|
|
|
- Order order = orderMapper.findByOrderId(orderId);
|
|
|
|
|
- ProductSnapshot productSnapshot = productSnapshotMapper.findByOrderId(orderId);
|
|
|
|
|
- OrderDetail orderDetail = new OrderDetail(order, productSnapshot);
|
|
|
|
|
- return orderDetail;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public Order getOrder(long orderId) {
|
|
|
|
|
- Order order = orderMapper.findByOrderId(orderId);
|
|
|
|
|
- return order;
|
|
|
|
|
|
|
+ public ItemSnapshot getItemSnapshot(long orderId) {
|
|
|
|
|
+ return productSnapshotMapper.findItemSnapshotByOrderId(orderId);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|