|
@@ -1,5 +1,7 @@
|
|
|
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.ProductCouponDao;
|
|
|
import com.bosshand.virgo.api.workark.dao.ProductDao;
|
|
@@ -19,7 +21,9 @@ import java.time.Duration;
|
|
|
import java.time.Instant;
|
|
|
import java.time.ZoneId;
|
|
|
import java.time.ZonedDateTime;
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
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();
|
|
@@ -160,51 +163,80 @@ public class OrderInfoService {
|
|
|
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);
|
|
|
BigDecimal price = product.getPrice();
|
|
|
|
|
|
// 优惠券
|
|
|
if (productCouponList.size() > 0) {
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
|