dcs 3 月之前
父节点
当前提交
2010b80c7f

+ 1 - 1
virgo.api/src/main/java/com/bosshand/virgo/api/workark/controller/OrderInfoController.java

@@ -62,7 +62,7 @@ public class OrderInfoController {
                 }
                 }
             }
             }
         }
         }
-        return Response.ok(orderInfoService.calculatePrice(productId, productCouponList));
+        return Response.ok(orderInfoService.calculateDetails(productId, productCouponList));
     }
     }
 
 
     @ApiOperation("查询")
     @ApiOperation("查询")

+ 1 - 1
virgo.api/src/main/java/com/bosshand/virgo/api/workark/dao/ProductCouponDao.java

@@ -25,5 +25,5 @@ public interface ProductCouponDao {
 
 
     List<ProductCoupon> getProductCouponModelId(long userId);
     List<ProductCoupon> getProductCouponModelId(long userId);
 
 
-    ProductCoupon getUserId(long productCouponId, long userId);
+    ProductCoupon getUserId(long id, long userId);
 }
 }

+ 4 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/model/OrderInfo.java

@@ -1,5 +1,7 @@
 package com.bosshand.virgo.api.workark.model;
 package com.bosshand.virgo.api.workark.model;
 
 
+import com.fasterxml.jackson.annotation.JsonFormat;
+
 import java.util.Date;
 import java.util.Date;
 
 
 /**
 /**
@@ -17,11 +19,13 @@ public class OrderInfo {
     /**
     /**
      * 创建时间
      * 创建时间
      */
      */
+    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     private Date createTime;
     private Date createTime;
 
 
     /**
     /**
      * 更新时间
      * 更新时间
      */
      */
+    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     private Date updateTime;
     private Date updateTime;
 
 
     /**
     /**

+ 70 - 38
virgo.api/src/main/java/com/bosshand/virgo/api/workark/service/OrderInfoService.java

@@ -1,5 +1,7 @@
 package com.bosshand.virgo.api.workark.service;
 package com.bosshand.virgo.api.workark.service;
 
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.bosshand.virgo.api.workark.dao.OrderInfoDao;
 import com.bosshand.virgo.api.workark.dao.OrderInfoDao;
 import com.bosshand.virgo.api.workark.dao.ProductCouponDao;
 import com.bosshand.virgo.api.workark.dao.ProductCouponDao;
 import com.bosshand.virgo.api.workark.dao.ProductDao;
 import com.bosshand.virgo.api.workark.dao.ProductDao;
@@ -19,7 +21,9 @@ import java.time.Duration;
 import java.time.Instant;
 import java.time.Instant;
 import java.time.ZoneId;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
 import java.time.ZonedDateTime;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
 
 
 @Service
 @Service
 public class OrderInfoService {
 public class OrderInfoService {
@@ -73,8 +77,7 @@ public class OrderInfoService {
         }
         }
 
 
         //计算价格
         //计算价格
-        Map<Long, BigDecimal> longBigDecimalMap = calculatePrice(productId, productCouponList);
-        BigDecimal price = longBigDecimalMap.get(-1L);
+        BigDecimal price = calculatePrice(productId, productCouponList);
 
 
         //生成订单
         //生成订单
         orderInfo = new OrderInfo();
         orderInfo = new OrderInfo();
@@ -160,51 +163,80 @@ public class OrderInfoService {
         return orderInfoList;
         return orderInfoList;
     }
     }
 
 
-    public Map<Long, BigDecimal> calculatePrice(long productId, List<ProductCoupon> productCouponList) {
-
-        Map<Long, BigDecimal> map = new HashMap<>();
+    public JSONObject calculateDetails(long productId, List<ProductCoupon> productCouponList) {
+        // 获取商品信息
+        Product product = productDao.get(productId);
+        BigDecimal price = product.getPrice();
+        JSONObject json = new JSONObject();
+        if (productCouponList.size() > 0) {
+            JSONArray ja = new JSONArray();
+            for (ProductCoupon productCoupon : productCouponList) {
+                JSONObject js = new JSONObject();
+                js.put("id", productCoupon.getId());
+                js.put("title", productCoupon.getTitle());
+                js.put("minus", calculatePriceSingle(price, productCoupon));
+                ja.add(js);
+            }
+            json.put("productCoupon", ja);
+        }
+        json.put("totalFee", calculatePrice(productId, productCouponList));
+        return json;
+    }
 
 
-        //获取商品信息
+    /**
+     * 计算最终价格
+     * @param productId
+     * @param productCouponList
+     * @return
+     */
+    public BigDecimal calculatePrice(long productId, List<ProductCoupon> productCouponList) {
+        // 获取商品信息
         Product product = productDao.get(productId);
         Product product = productDao.get(productId);
         BigDecimal price = product.getPrice();
         BigDecimal price = product.getPrice();
 
 
         // 优惠券
         // 优惠券
         if (productCouponList.size() > 0) {
         if (productCouponList.size() > 0) {
             for (ProductCoupon productCoupon : productCouponList) {
             for (ProductCoupon productCoupon : productCouponList) {
-                if (productCoupon.getType() == 1) {
-                    // 门槛
-                    BigDecimal threshold = productCoupon.getThreshold();
-                    if (price.compareTo(threshold) > 0) {
-                        //优惠金额
-                        BigDecimal couponAmount = productCoupon.getCouponAmount();
-                        price = price.subtract(couponAmount);
-                        map.put(productCoupon.getId(), couponAmount);
-                    }
-                }
-                if (productCoupon.getType() == 2) {
-                    // 打折
-                    BigDecimal discount = productCoupon.getDiscount();
-                    // 门槛
-                    BigDecimal threshold = productCoupon.getThreshold();
-                    // 最高优惠
-                    BigDecimal mostConstraint = productCoupon.getMostConstraint();
-                    if (price.compareTo(threshold) > 0) {
-                        // 四舍五入 保留两位小数
-                        BigDecimal multiply = price.multiply(discount).setScale(2, BigDecimal.ROUND_HALF_UP);
-                        // 大于1000,按1000算
-                        if (multiply.compareTo(mostConstraint) > 0) {
-                            price = price.subtract(mostConstraint);
-                            map.put(productCoupon.getId(), mostConstraint);
-                        } else {
-                            price = price.subtract(multiply);
-                            map.put(productCoupon.getId(), multiply);
-                        }
-                    }
+                price = price.subtract(calculatePriceSingle(price, productCoupon));
+            }
+        }
+        return price;
+    }
+
+    /**
+     * 计算单个优惠券减多少
+     * @param productCoupon
+     * @param price
+     * @return
+     */
+    public BigDecimal calculatePriceSingle(BigDecimal price, ProductCoupon productCoupon) {
+        if (productCoupon.getType() == 1) {
+            // 门槛
+            BigDecimal threshold = productCoupon.getThreshold();
+            if (price.compareTo(threshold) > 0) {
+                // 优惠金额
+                return productCoupon.getCouponAmount();
+            }
+        }
+        if (productCoupon.getType() == 2) {
+            // 打折
+            BigDecimal discount = productCoupon.getDiscount();
+            // 门槛
+            BigDecimal threshold = productCoupon.getThreshold();
+            // 最高优惠
+            BigDecimal mostConstraint = productCoupon.getMostConstraint();
+            if (price.compareTo(threshold) > 0) {
+                // 四舍五入 保留两位小数
+                BigDecimal multiply = price.multiply(discount).setScale(2, BigDecimal.ROUND_HALF_UP);
+                // 大于1000,按1000算
+                if (multiply.compareTo(mostConstraint) > 0) {
+                    return mostConstraint;
+                } else {
+                    return price.subtract(multiply);
                 }
                 }
             }
             }
         }
         }
-        map.put(-1L, price);
-        return map;
+        return new BigDecimal(0);
     }
     }
 
 
 
 

+ 1 - 1
virgo.api/src/main/resources/mapper/OrderInfoMapper.xml

@@ -47,7 +47,7 @@
 
 
     <insert id="save" useGeneratedKeys="true" keyProperty="id">
     <insert id="save" useGeneratedKeys="true" keyProperty="id">
         INSERT INTO workark_orderInfo (createTime, title, orderNo, userId, productId, totalFee, codeUrl, orderStatus, paymentType, productCouponIds) VALUES
         INSERT INTO workark_orderInfo (createTime, title, orderNo, userId, productId, totalFee, codeUrl, orderStatus, paymentType, productCouponIds) VALUES
-                            (now(), #{paymentType}, #{orderNo}, #{userId}, #{productId}, #{totalFee}, #{codeUrl}, #{orderStatus}, #{paymentType}, #{productCouponIds})
+                            (now(), #{title}, #{orderNo}, #{userId}, #{productId}, #{totalFee}, #{codeUrl}, #{orderStatus}, #{paymentType}, #{productCouponIds})
     </insert>
     </insert>
 
 
     <update id="update" parameterType="com.bosshand.virgo.api.workark.model.OrderInfo">
     <update id="update" parameterType="com.bosshand.virgo.api.workark.model.OrderInfo">