From 4ecd497e0092acd5ca960d9014d0b51c5816a859 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?=
<2291200076@qq.com>
Date: Sat, 5 Mar 2022 10:27:50 +0800
Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E9=94=80=E9=87=8F=E4=B8=AD?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
frontend/getitem.html | 1 +
.../com/cxyxiaomo/flashsale/dao/ItemDOMapper.java | 3 +++
.../com/cxyxiaomo/flashsale/service/ItemService.java | 4 ++++
.../flashsale/service/impl/ItemServiceImpl.java | 6 ++++++
.../flashsale/service/impl/OrderServiceImpl.java | 5 ++++-
src/main/resources/mapping/ItemDOMapper.xml | 11 ++++++++---
6 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/frontend/getitem.html b/frontend/getitem.html
index 45397bd..9a3dc19 100644
--- a/frontend/getitem.html
+++ b/frontend/getitem.html
@@ -100,6 +100,7 @@
success: function (data) {
if (data.status == "success") {
alert("下单成功");
+ window.location.reload();
} else {
alert("下单失败,原因为" + data.data.errMsg);
if (data.data.errCode == "20003") {
diff --git a/src/main/java/com/cxyxiaomo/flashsale/dao/ItemDOMapper.java b/src/main/java/com/cxyxiaomo/flashsale/dao/ItemDOMapper.java
index e221331..34b36ae 100644
--- a/src/main/java/com/cxyxiaomo/flashsale/dao/ItemDOMapper.java
+++ b/src/main/java/com/cxyxiaomo/flashsale/dao/ItemDOMapper.java
@@ -1,6 +1,7 @@
package com.cxyxiaomo.flashsale.dao;
import com.cxyxiaomo.flashsale.dataobject.ItemDO;
+import org.apache.ibatis.annotations.Param;
import java.util.List;
@@ -54,4 +55,6 @@ public interface ItemDOMapper {
* @mbg.generated Thu Mar 03 13:12:13 CST 2022
*/
int updateByPrimaryKey(ItemDO record);
+
+ int increaseSales(@Param("id") Integer id, @Param("amount") Integer amount);
}
\ No newline at end of file
diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/ItemService.java b/src/main/java/com/cxyxiaomo/flashsale/service/ItemService.java
index c8e55d7..b952765 100644
--- a/src/main/java/com/cxyxiaomo/flashsale/service/ItemService.java
+++ b/src/main/java/com/cxyxiaomo/flashsale/service/ItemService.java
@@ -15,5 +15,9 @@ public interface ItemService {
// 商品详情浏览
ItemModel getItemById(Integer id);
+ // 库存扣减
boolean decreaseStock(Integer itemId, Integer amount) throws BusinessException;
+
+ // 商品销量增加
+ void increaseSales(Integer itemId, Integer amount);
}
diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/impl/ItemServiceImpl.java b/src/main/java/com/cxyxiaomo/flashsale/service/impl/ItemServiceImpl.java
index e3dbeb2..f6c81fc 100644
--- a/src/main/java/com/cxyxiaomo/flashsale/service/impl/ItemServiceImpl.java
+++ b/src/main/java/com/cxyxiaomo/flashsale/service/impl/ItemServiceImpl.java
@@ -116,6 +116,12 @@ public class ItemServiceImpl implements ItemService {
}
}
+ @Override
+ @Transactional
+ public void increaseSales(Integer itemId, Integer amount) {
+ itemDOMapper.increaseSales(itemId, amount);
+ }
+
private ItemModel convertModelFromDataObject(ItemDO itemDO, ItemStockDO itemStockDO) {
ItemModel itemModel = new ItemModel();
BeanUtils.copyProperties(itemDO, itemModel);
diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/impl/OrderServiceImpl.java b/src/main/java/com/cxyxiaomo/flashsale/service/impl/OrderServiceImpl.java
index 73b6627..9c0fe18 100644
--- a/src/main/java/com/cxyxiaomo/flashsale/service/impl/OrderServiceImpl.java
+++ b/src/main/java/com/cxyxiaomo/flashsale/service/impl/OrderServiceImpl.java
@@ -70,12 +70,15 @@ public class OrderServiceImpl implements OrderService {
orderModel.setItemPrice(itemModel.getPrice());
orderModel.setOrderPrice(itemModel.getPrice().multiply(new BigDecimal(amount)));
- // 生成交易流水号,订单号
+ // 生成交易流水号(订单号)
orderModel.setId(generateOrderNo());
OrderDO orderDO = convertFromOrderModel(orderModel);
orderDOMapper.insertSelective(orderDO);
+ // 商品销量增加
+ itemService.increaseSales(itemId, amount);
+
// 4. 返回前端
return orderModel;
}
diff --git a/src/main/resources/mapping/ItemDOMapper.xml b/src/main/resources/mapping/ItemDOMapper.xml
index daf1f1c..343b5e6 100644
--- a/src/main/resources/mapping/ItemDOMapper.xml
+++ b/src/main/resources/mapping/ItemDOMapper.xml
@@ -28,7 +28,7 @@
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Mar 03 13:12:13 CST 2022.
-->
- select
+ select
from item_info
where id = #{id,jdbcType=INTEGER}
@@ -48,10 +48,10 @@
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Mar 03 13:12:13 CST 2022.
-->
- insert into item_info (id, title, price,
+ insert into item_info (id, title, price,
description, sales, img_url
)
- values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL},
+ values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL},
#{description,jdbcType=VARCHAR}, #{sales,jdbcType=INTEGER}, #{imgUrl,jdbcType=VARCHAR}
)
@@ -149,4 +149,9 @@
from item_info
order by sales DESC
+
+ update item_info
+ set sales = sales + #{amount}
+ where id = #{id,jdbcType=INTEGER}
+
\ No newline at end of file