Commit 79a27917 authored by Junling Bu's avatar Junling Bu
Browse files

chore[litemall-wx-api]: 微调

parent fbc7dc6a
...@@ -24,22 +24,14 @@ import org.linlinjava.litemall.db.service.*; ...@@ -24,22 +24,14 @@ import org.linlinjava.litemall.db.service.*;
import org.linlinjava.litemall.db.util.CouponUserConstant; import org.linlinjava.litemall.db.util.CouponUserConstant;
import org.linlinjava.litemall.db.util.OrderHandleOption; import org.linlinjava.litemall.db.util.OrderHandleOption;
import org.linlinjava.litemall.db.util.OrderUtil; import org.linlinjava.litemall.db.util.OrderUtil;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.util.IpUtil; import org.linlinjava.litemall.wx.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -320,9 +312,9 @@ public class WxOrderService { ...@@ -320,9 +312,9 @@ public class WxOrderService {
// 使用优惠券减免的金额 // 使用优惠券减免的金额
BigDecimal couponPrice = new BigDecimal(0.00); BigDecimal couponPrice = new BigDecimal(0.00);
// 如果couponId=0则没有优惠券,couponId=-1则不使用优惠券 // 如果couponId=0则没有优惠券,couponId=-1则不使用优惠券
if(couponId != 0 && couponId != -1){ if (couponId != 0 && couponId != -1) {
LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponId, checkedGoodsPrice); LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponId, checkedGoodsPrice);
if(coupon == null){ if (coupon == null) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
couponPrice = coupon.getDiscount(); couponPrice = coupon.getDiscount();
...@@ -345,101 +337,101 @@ public class WxOrderService { ...@@ -345,101 +337,101 @@ public class WxOrderService {
Integer orderId = null; Integer orderId = null;
LitemallOrder order = null; LitemallOrder order = null;
// 订单 // 订单
order = new LitemallOrder(); order = new LitemallOrder();
order.setUserId(userId); order.setUserId(userId);
order.setOrderSn(orderService.generateOrderSn(userId)); order.setOrderSn(orderService.generateOrderSn(userId));
order.setOrderStatus(OrderUtil.STATUS_CREATE); order.setOrderStatus(OrderUtil.STATUS_CREATE);
order.setConsignee(checkedAddress.getName()); order.setConsignee(checkedAddress.getName());
order.setMobile(checkedAddress.getMobile()); order.setMobile(checkedAddress.getMobile());
order.setMessage(message); order.setMessage(message);
String detailedAddress = detailedAddress(checkedAddress); String detailedAddress = detailedAddress(checkedAddress);
order.setAddress(detailedAddress); order.setAddress(detailedAddress);
order.setGoodsPrice(checkedGoodsPrice); order.setGoodsPrice(checkedGoodsPrice);
order.setFreightPrice(freightPrice); order.setFreightPrice(freightPrice);
order.setCouponPrice(couponPrice); order.setCouponPrice(couponPrice);
order.setIntegralPrice(integralPrice); order.setIntegralPrice(integralPrice);
order.setOrderPrice(orderTotalPrice); order.setOrderPrice(orderTotalPrice);
order.setActualPrice(actualPrice); order.setActualPrice(actualPrice);
// 有团购活动 // 有团购活动
if (grouponRules != null) { if (grouponRules != null) {
order.setGrouponPrice(grouponPrice); // 团购价格 order.setGrouponPrice(grouponPrice); // 团购价格
} else { } else {
order.setGrouponPrice(new BigDecimal(0.00)); // 团购价格 order.setGrouponPrice(new BigDecimal(0.00)); // 团购价格
} }
// 添加订单表项 // 添加订单表项
orderService.add(order); orderService.add(order);
orderId = order.getId(); orderId = order.getId();
// 添加订单商品表项
for (LitemallCart cartGoods : checkedGoodsList) {
// 订单商品
LitemallOrderGoods orderGoods = new LitemallOrderGoods();
orderGoods.setOrderId(order.getId());
orderGoods.setGoodsId(cartGoods.getGoodsId());
orderGoods.setGoodsSn(cartGoods.getGoodsSn());
orderGoods.setProductId(cartGoods.getProductId());
orderGoods.setGoodsName(cartGoods.getGoodsName());
orderGoods.setPicUrl(cartGoods.getPicUrl());
orderGoods.setPrice(cartGoods.getPrice());
orderGoods.setNumber(cartGoods.getNumber());
orderGoods.setSpecifications(cartGoods.getSpecifications());
orderGoods.setAddTime(LocalDateTime.now());
orderGoodsService.add(orderGoods);
}
// 删除购物车里面的商品信息 // 添加订单商品表项
cartService.clearGoods(userId); for (LitemallCart cartGoods : checkedGoodsList) {
// 订单商品
// 商品货品数量减少 LitemallOrderGoods orderGoods = new LitemallOrderGoods();
for (LitemallCart checkGoods : checkedGoodsList) { orderGoods.setOrderId(order.getId());
Integer productId = checkGoods.getProductId(); orderGoods.setGoodsId(cartGoods.getGoodsId());
LitemallGoodsProduct product = productService.findById(productId); orderGoods.setGoodsSn(cartGoods.getGoodsSn());
orderGoods.setProductId(cartGoods.getProductId());
Integer remainNumber = product.getNumber() - checkGoods.getNumber(); orderGoods.setGoodsName(cartGoods.getGoodsName());
if (remainNumber < 0) { orderGoods.setPicUrl(cartGoods.getPicUrl());
throw new RuntimeException("下单的商品货品数量大于库存量"); orderGoods.setPrice(cartGoods.getPrice());
} orderGoods.setNumber(cartGoods.getNumber());
if (productService.reduceStock(productId, checkGoods.getNumber()) == 0) { orderGoods.setSpecifications(cartGoods.getSpecifications());
throw new RuntimeException("商品货品库存减少失败"); orderGoods.setAddTime(LocalDateTime.now());
}
}
// 如果使用了优惠券,设置优惠券使用状态 orderGoodsService.add(orderGoods);
if(couponId != 0 && couponId != -1){ }
LitemallCouponUser couponUser = couponUserService.queryOne(userId, couponId);
couponUser.setStatus(CouponUserConstant.STATUS_USED); // 删除购物车里面的商品信息
couponUser.setUsedTime(LocalDateTime.now()); cartService.clearGoods(userId);
couponUser.setOrderId(orderId);
couponUserService.update(couponUser); // 商品货品数量减少
for (LitemallCart checkGoods : checkedGoodsList) {
Integer productId = checkGoods.getProductId();
LitemallGoodsProduct product = productService.findById(productId);
Integer remainNumber = product.getNumber() - checkGoods.getNumber();
if (remainNumber < 0) {
throw new RuntimeException("下单的商品货品数量大于库存量");
}
if (productService.reduceStock(productId, checkGoods.getNumber()) == 0) {
throw new RuntimeException("商品货品库存减少失败");
} }
}
// 如果使用了优惠券,设置优惠券使用状态
if (couponId != 0 && couponId != -1) {
LitemallCouponUser couponUser = couponUserService.queryOne(userId, couponId);
couponUser.setStatus(CouponUserConstant.STATUS_USED);
couponUser.setUsedTime(LocalDateTime.now());
couponUser.setOrderId(orderId);
couponUserService.update(couponUser);
}
//如果是团购项目,添加团购信息 //如果是团购项目,添加团购信息
if (grouponRulesId != null && grouponRulesId > 0) { if (grouponRulesId != null && grouponRulesId > 0) {
LitemallGroupon groupon = new LitemallGroupon(); LitemallGroupon groupon = new LitemallGroupon();
groupon.setOrderId(orderId); groupon.setOrderId(orderId);
groupon.setPayed(false); groupon.setPayed(false);
groupon.setUserId(userId); groupon.setUserId(userId);
groupon.setRulesId(grouponRulesId); groupon.setRulesId(grouponRulesId);
//参与者 //参与者
if (grouponLinkId != null && grouponLinkId > 0) { if (grouponLinkId != null && grouponLinkId > 0) {
//参与的团购记录 //参与的团购记录
LitemallGroupon baseGroupon = grouponService.queryById(grouponLinkId); LitemallGroupon baseGroupon = grouponService.queryById(grouponLinkId);
groupon.setCreatorUserId(baseGroupon.getCreatorUserId()); groupon.setCreatorUserId(baseGroupon.getCreatorUserId());
groupon.setGrouponId(grouponLinkId); groupon.setGrouponId(grouponLinkId);
groupon.setShareUrl(baseGroupon.getShareUrl()); groupon.setShareUrl(baseGroupon.getShareUrl());
} else { } else {
groupon.setCreatorUserId(userId); groupon.setCreatorUserId(userId);
groupon.setGrouponId(0); groupon.setGrouponId(0);
}
grouponService.createGroupon(groupon);
} }
grouponService.createGroupon(groupon);
}
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("orderId", orderId); data.put("orderId", orderId);
return ResponseUtil.ok(data); return ResponseUtil.ok(data);
...@@ -484,22 +476,22 @@ public class WxOrderService { ...@@ -484,22 +476,22 @@ public class WxOrderService {
return ResponseUtil.fail(ORDER_INVALID_OPERATION, "订单不能取消"); return ResponseUtil.fail(ORDER_INVALID_OPERATION, "订单不能取消");
} }
// 设置订单已取消状态 // 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_CANCEL); order.setOrderStatus(OrderUtil.STATUS_CANCEL);
order.setEndTime(LocalDateTime.now()); order.setEndTime(LocalDateTime.now());
if (orderService.updateWithOptimisticLocker(order) == 0) { if (orderService.updateWithOptimisticLocker(order) == 0) {
throw new RuntimeException("更新数据已失效"); throw new RuntimeException("更新数据已失效");
} }
// 商品货品数量增加 // 商品货品数量增加
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId); List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
for (LitemallOrderGoods orderGoods : orderGoodsList) { for (LitemallOrderGoods orderGoods : orderGoodsList) {
Integer productId = orderGoods.getProductId(); Integer productId = orderGoods.getProductId();
Short number = orderGoods.getNumber(); Short number = orderGoods.getNumber();
if (productService.addStock(productId, number) == 0) { if (productService.addStock(productId, number) == 0) {
throw new RuntimeException("商品货品库存增加失败"); throw new RuntimeException("商品货品库存增加失败");
}
} }
}
return ResponseUtil.ok(); return ResponseUtil.ok();
} }
...@@ -588,7 +580,7 @@ public class WxOrderService { ...@@ -588,7 +580,7 @@ public class WxOrderService {
* 2. 设置订单付款成功状态相关信息; * 2. 设置订单付款成功状态相关信息;
* 3. 响应微信商户平台. * 3. 响应微信商户平台.
* *
* @param request 请求内容 * @param request 请求内容
* @param response 响应内容 * @param response 响应内容
* @return 操作结果 * @return 操作结果
*/ */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment