浏览代码

修复订单确认页优惠券问题

beaver383 6 年之前
父节点
当前提交
898942fa82

+ 13 - 3
litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java

@@ -25,10 +25,20 @@ public class CouponVerifyService {
      * @param checkedGoodsPrice
      * @return
      */
-    public LitemallCoupon checkCoupon(Integer userId, Integer couponId, BigDecimal checkedGoodsPrice) {
+    public LitemallCoupon checkCoupon(Integer userId, Integer couponId, Integer userCouponId, BigDecimal checkedGoodsPrice) {
         LitemallCoupon coupon = couponService.findById(couponId);
-        LitemallCouponUser couponUser = couponUserService.queryOne(userId, couponId);
-        if (coupon == null || couponUser == null) {
+        if (coupon == null) {
+            return null;
+        }
+
+        LitemallCouponUser couponUser = couponUserService.findById(userCouponId);
+        if (couponUser == null) {
+            couponUser = couponUserService.queryOne(userId, couponId);
+        } else if (!couponId.equals(couponUser.getCouponId())) {
+            return null;
+        }
+
+        if (couponUser == null) {
             return null;
         }
 

+ 18 - 15
litemall-vue/src/views/order/checkout.vue

@@ -105,7 +105,7 @@ export default {
 
   methods: {
     onSubmit() {     
-      const {AddressId, CartId, CouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId');
+      const {AddressId, CartId, CouponId, UserCouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId', 'UserCouponId');
 
       if (AddressId === null) {
         Toast.fail('请设置收货地址');
@@ -119,6 +119,7 @@ export default {
         addressId: AddressId,
         cartId: CartId,
         couponId: CouponId,
+        userCouponId: UserCouponId,
         grouponLinkId: 0,
         grouponRulesId: 0,
         message: this.message
@@ -155,15 +156,17 @@ export default {
     getCoupons() {
       const {AddressId, CartId, CouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId');
       couponSelectList({cartId: CartId, grouponRulesId: 0}).then(res => {
-        var cList = res.data.data
+        var cList = res.data.data.list;
         this.coupons = []
+        this.disabledCoupons = [];
         for(var i = 0; i < cList.length; i++){
           var c = cList[i]
 
           var coupon = {
             id: c.id,
+            cid: c.cid,
             name: c.name,
-            condition: c.min,
+            condition: '满' + c.min + '元可用',
             value: c.discount * 100,
             description: c.desc,
             startAt: new Date(c.startTime).getTime()/1000,
@@ -171,11 +174,10 @@ export default {
             valueDesc: c.discount,
             unitDesc: '元'            
           }
-          this.coupons.push(coupon)
-
-          if(c.id === this.couponId){
-            this.chosenCoupon = i;
-            break;
+          if (c.available) {
+            this.coupons.push(coupon);
+          } else {
+            this.disabledCoupons.push(coupon);
           }
         }
         
@@ -183,9 +185,9 @@ export default {
       })
     },
     init() {
-      const {AddressId, CartId, CouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId');
+      const {AddressId, CartId, CouponId, UserCouponId} = getLocalStorage('AddressId', 'CartId', 'CouponId', 'UserCouponId');
 
-      cartCheckout({cartId: CartId, addressId: AddressId, couponId: CouponId, grouponRulesId: 0}).then(res => {
+      cartCheckout({cartId: CartId, addressId: AddressId, couponId: CouponId, userCouponId: UserCouponId, grouponRulesId: 0}).then(res => {
           var data = res.data.data
 
           this.checkedGoodsList = data.checkedGoodsList;
@@ -198,7 +200,7 @@ export default {
           this.goodsTotalPrice= data.goodsTotalPrice;
           this.orderTotalPrice= data.orderTotalPrice;
 
-          setLocalStorage({AddressId: data.addressId, CartId: data.cartId, CouponId: data.couponId});
+          setLocalStorage({AddressId: data.addressId, CartId: data.cartId, CouponId: data.couponId, UserCouponId: data.userCouponId});
       });
 
     },
@@ -207,11 +209,12 @@ export default {
       this.chosenCoupon = index;
       
       if(index === -1 ){
-        setLocalStorage({CouponId: -1});
+        setLocalStorage({CouponId: -1, UserCouponId: -1});
       }
       else{
-        const couponId = this.coupons[index].id;
-        setLocalStorage({CouponId: couponId});  
+        const couponId = this.coupons[index].cid;
+        const userCouponId = this.coupons[index].id;
+        setLocalStorage({CouponId: couponId, UserCouponId: userCouponId});
       }
 
       this.init()
@@ -226,7 +229,7 @@ export default {
     [SubmitBar.name]: SubmitBar,
     [Card.name]: Card,
     [Field.name]: Field,
-    [Tag.name]: Field,
+    [Tag.name]: Tag,
     [CouponCell.name]: CouponCell,
     [CouponList.name]: CouponList,
     [Popup.name]: Popup

+ 3 - 2
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/service/WxOrderService.java

@@ -240,6 +240,7 @@ public class WxOrderService {
         Integer cartId = JacksonUtil.parseInteger(body, "cartId");
         Integer addressId = JacksonUtil.parseInteger(body, "addressId");
         Integer couponId = JacksonUtil.parseInteger(body, "couponId");
+        Integer userCouponId = JacksonUtil.parseInteger(body, "userCouponId");
         String message = JacksonUtil.parseString(body, "message");
         Integer grouponRulesId = JacksonUtil.parseInteger(body, "grouponRulesId");
         Integer grouponLinkId = JacksonUtil.parseInteger(body, "grouponLinkId");
@@ -301,7 +302,7 @@ public class WxOrderService {
         BigDecimal couponPrice = new BigDecimal(0.00);
         // 如果couponId=0则没有优惠券,couponId=-1则不使用优惠券
         if (couponId != 0 && couponId != -1) {
-            LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponId, checkedGoodsPrice);
+            LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponId, userCouponId, checkedGoodsPrice);
             if (coupon == null) {
                 return ResponseUtil.badArgumentValue();
             }
@@ -390,7 +391,7 @@ public class WxOrderService {
 
         // 如果使用了优惠券,设置优惠券使用状态
         if (couponId != 0 && couponId != -1) {
-            LitemallCouponUser couponUser = couponUserService.queryOne(userId, couponId);
+            LitemallCouponUser couponUser = couponUserService.findById(userCouponId);
             couponUser.setStatus(CouponUserConstant.STATUS_USED);
             couponUser.setUsedTime(LocalDateTime.now());
             couponUser.setOrderId(orderId);

+ 18 - 0
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/vo/CouponVo.java

@@ -4,6 +4,7 @@ import java.time.LocalDateTime;
 
 public class CouponVo {
     private Integer id;
+    private Integer cid;
     private String name;
     private String desc;
     private String tag;
@@ -11,6 +12,7 @@ public class CouponVo {
     private String discount;
     private LocalDateTime startTime;
     private LocalDateTime endTime;
+    private boolean available;
 
     public Integer getId() {
         return id;
@@ -20,6 +22,14 @@ public class CouponVo {
         this.id = id;
     }
 
+    public Integer getCid() {
+        return cid;
+    }
+
+    public void setCid(Integer cid) {
+        this.cid = cid;
+    }
+
     public String getName() {
         return name;
     }
@@ -75,4 +85,12 @@ public class CouponVo {
     public void setEndTime(LocalDateTime endTime) {
         this.endTime = endTime;
     }
+
+    public boolean isAvailable() {
+        return available;
+    }
+
+    public void setAvailable(boolean available) {
+        this.available = available;
+    }
 }

+ 9 - 3
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxCartController.java

@@ -386,7 +386,7 @@ public class WxCartController {
      * @return 购物车操作结果
      */
     @GetMapping("checkout")
-    public Object checkout(@LoginUser Integer userId, Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId) {
+    public Object checkout(@LoginUser Integer userId, Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId) {
         if (userId == null) {
             return ResponseUtil.unlogin();
         }
@@ -445,10 +445,12 @@ public class WxCartController {
         // 计算优惠券可用情况
         BigDecimal tmpCouponPrice = new BigDecimal(0.00);
         Integer tmpCouponId = 0;
+        Integer tmpUserCouponId = 0;
         int tmpCouponLength = 0;
         List<LitemallCouponUser> couponUserList = couponUserService.queryAll(userId);
         for(LitemallCouponUser couponUser : couponUserList){
-            LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponUser.getCouponId(), checkedGoodsPrice);
+            tmpUserCouponId = couponUser.getId();
+            LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponUser.getCouponId(), tmpUserCouponId, checkedGoodsPrice);
             if(coupon == null){
                 continue;
             }
@@ -468,17 +470,20 @@ public class WxCartController {
         // 3. 用户已选择优惠券,则测试优惠券是否合适
         if (couponId == null || couponId.equals(-1)){
             couponId = -1;
+            userCouponId = -1;
         }
         else if (couponId.equals(0)) {
             couponPrice = tmpCouponPrice;
             couponId = tmpCouponId;
+            userCouponId = tmpUserCouponId;
         }
         else {
-            LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponId, checkedGoodsPrice);
+            LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponId, userCouponId, checkedGoodsPrice);
             // 用户选择的优惠券有问题,则选择合适优惠券,否则使用用户选择的优惠券
             if(coupon == null){
                 couponPrice = tmpCouponPrice;
                 couponId = tmpCouponId;
+                userCouponId = tmpUserCouponId;
             }
             else {
                 couponPrice = coupon.getDiscount();
@@ -502,6 +507,7 @@ public class WxCartController {
         Map<String, Object> data = new HashMap<>();
         data.put("addressId", addressId);
         data.put("couponId", couponId);
+        data.put("userCouponId", userCouponId);
         data.put("cartId", cartId);
         data.put("grouponRulesId", grouponRulesId);
         data.put("grouponPrice", grouponPrice);

+ 6 - 10
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxCouponController.java

@@ -99,7 +99,8 @@ public class WxCouponController {
             Integer couponId = couponUser.getCouponId();
             LitemallCoupon coupon = couponService.findById(couponId);
             CouponVo couponVo = new CouponVo();
-            couponVo.setId(coupon.getId());
+            couponVo.setId(couponUser.getId());
+            couponVo.setCid(coupon.getId());
             couponVo.setName(coupon.getName());
             couponVo.setDesc(coupon.getDesc());
             couponVo.setTag(coupon.getTag());
@@ -160,17 +161,12 @@ public class WxCouponController {
 
         // 计算优惠券可用情况
         List<LitemallCouponUser> couponUserList = couponUserService.queryAll(userId);
-        List<LitemallCouponUser> availableCouponUserList = new ArrayList<>(couponUserList.size());
-        for (LitemallCouponUser couponUser : couponUserList) {
-            LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, couponUser.getCouponId(), checkedGoodsPrice);
-            if (coupon == null) {
-                continue;
-            }
-            availableCouponUserList.add(couponUser);
+        List<CouponVo> couponVoList = change(couponUserList);
+        for (CouponVo cv : couponVoList) {
+            LitemallCoupon coupon = couponVerifyService.checkCoupon(userId, cv.getCid(), cv.getId(), checkedGoodsPrice);
+            cv.setAvailable(coupon != null);
         }
 
-        List<CouponVo> couponVoList = change(availableCouponUserList);
-
         return ResponseUtil.okList(couponVoList);
     }