|
|
@@ -1,6 +1,7 @@
|
|
|
package com.fs.live.service.impl;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.sql.Timestamp;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
@@ -71,6 +72,7 @@ import com.fs.store.service.*;
|
|
|
import com.fs.store.service.channel.PaymentHandler;
|
|
|
import com.fs.store.service.channel.PaymentHandlerHolder;
|
|
|
import com.fs.store.service.channel.param.PayProcessContext;
|
|
|
+import com.fs.store.vo.FsStoreProductActivityListVO;
|
|
|
import com.fs.system.config.SnowflakeUtils;
|
|
|
import com.fs.system.service.ISysConfigService;
|
|
|
import com.fs.wx.domain.FsWxExpressTask;
|
|
|
@@ -986,16 +988,6 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
order.setDeliveryType(null);
|
|
|
}
|
|
|
|
|
|
- // 商品合计 = totalPrice + discountMoney - payDelivery (全部转为Double再相加减,保留两位小数)
|
|
|
- Double totalPrice = order.getTotalPrice() != null ? Double.parseDouble(order.getTotalPrice().toString()) : 0d;
|
|
|
- Double discountMoney = order.getDiscountMoney() != null ? Double.parseDouble(order.getDiscountMoney().toString()) : 0d;
|
|
|
- double payDelivery = order.getPayDelivery() != null ? Double.parseDouble(order.getPayDelivery().toString()) : 0d;
|
|
|
- double goodsAmount = totalPrice + discountMoney - payDelivery;
|
|
|
- // 保留两位小数,进行四舍五入
|
|
|
- goodsAmount = new java.math.BigDecimal(goodsAmount).setScale(2, java.math.RoundingMode.HALF_UP).doubleValue();
|
|
|
-
|
|
|
- order.setPayPrice(order.getTotalPrice());
|
|
|
- order.setTotalPrice(BigDecimal.valueOf(goodsAmount));
|
|
|
|
|
|
LiveOrderPayment liveOrderPayment = paymentMap.get(String.valueOf(order.getOrderId()));
|
|
|
if (ObjectUtil.isNotNull(liveOrderPayment)) {
|
|
|
@@ -1141,12 +1133,20 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
liveOrder.setPayType("1");
|
|
|
liveOrder.setTotalPrice(totalPrice);
|
|
|
liveOrder.setPayMoney(liveOrder.getTotalPrice());
|
|
|
+ liveOrder.setPayPrice(liveOrder.getTotalPrice());
|
|
|
|
|
|
|
|
|
if (baseMapper.insertLiveOrder(liveOrder) > 0) {
|
|
|
for (LiveOrderItem liveOrderItem : liveOrderItemList) {
|
|
|
liveOrderItem.setOrderId(liveOrder.getOrderId());
|
|
|
}
|
|
|
+ //计算出库价(净价)
|
|
|
+ List<BigDecimal> netPrices = calcLiveItemNetPrices(liveOrder.getPayPrice(), liveOrderItemList);
|
|
|
+ for (int i = 0; i < liveOrderItemList.size(); i++) {
|
|
|
+ if (i < netPrices.size()) {
|
|
|
+ liveOrderItemList.get(i).setNetPrice(netPrices.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
liveOrderItemMapper.insertBatchList(liveOrderItemList);
|
|
|
// list Long 转为Long数组
|
|
|
|
|
|
@@ -1884,34 +1884,38 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- BigDecimal totalPrice = BigDecimal.ZERO;
|
|
|
- BigDecimal payPrice = fsStoreProduct.getPrice().multiply(new BigDecimal(param.getTotalNum()));
|
|
|
+ // totalPrice = 商品总价(不含邮费)
|
|
|
+ BigDecimal totalPrice = fsStoreProduct.getPrice().multiply(new BigDecimal(param.getTotalNum()));
|
|
|
if (fsStoreProductAttrValue != null) {
|
|
|
- payPrice = fsStoreProductAttrValue.getPrice().multiply(new BigDecimal(param.getTotalNum()));
|
|
|
+ totalPrice = fsStoreProductAttrValue.getPrice().multiply(new BigDecimal(param.getTotalNum()));
|
|
|
}
|
|
|
- totalPrice = totalPrice.add(payPrice);
|
|
|
- BigDecimal payDelivery = BigDecimal.ZERO;
|
|
|
- BigDecimal deductionPrice = BigDecimal.ZERO;
|
|
|
+
|
|
|
+ // payPostage = 应付邮费
|
|
|
+ BigDecimal payPostage = BigDecimal.ZERO;
|
|
|
if (param.getCityId() != null) {
|
|
|
- payDelivery = handleDeliveryMoney(param.getCityId(), fsStoreProduct, param.getTotalNum(), fsStoreProductAttrValue);
|
|
|
- payPrice = payPrice.add(payDelivery);
|
|
|
+ payPostage = handleDeliveryMoney(param.getCityId(), fsStoreProduct, param.getTotalNum(), fsStoreProductAttrValue);
|
|
|
}
|
|
|
|
|
|
+ // payPrice = 支付总价(商品总价 + 邮费 - 优惠券)
|
|
|
+ BigDecimal payPrice = totalPrice.add(payPostage);
|
|
|
+ BigDecimal deductionPrice = BigDecimal.ZERO;
|
|
|
if (param.getCouponUserId() != null) {
|
|
|
LiveCouponUser couponUser = liveCouponUserService.selectLiveCouponUserById(param.getCouponUserId());
|
|
|
if (couponUser != null && couponUser.getStatus() == 0) {
|
|
|
if (couponUser.getUseMinPrice().compareTo(payPrice) < 1) {
|
|
|
- payPrice = payPrice.subtract(couponUser.getCouponPrice());
|
|
|
deductionPrice = couponUser.getCouponPrice();
|
|
|
+ payPrice = payPrice.subtract(deductionPrice);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- return LiveOrderComputeDTO.builder().payPrice(payPrice)
|
|
|
- .payDelivery(payDelivery)
|
|
|
- .deductionPrice(deductionPrice)
|
|
|
+ return LiveOrderComputeDTO.builder()
|
|
|
.totalPrice(totalPrice)
|
|
|
+ .payPrice(payPrice)
|
|
|
+ .payPostage(payPostage)
|
|
|
+ .payDelivery(payPostage)
|
|
|
+ .deductionPrice(deductionPrice)
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
@@ -2713,6 +2717,7 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
liveOrder.setPayType("1");
|
|
|
liveOrder.setTotalPrice(totalPrice);
|
|
|
liveOrder.setPayMoney(BigDecimal.ZERO);
|
|
|
+ liveOrder.setPayPrice(totalPrice);
|
|
|
try {
|
|
|
if (baseMapper.insertLiveOrder(liveOrder) > 0) {
|
|
|
liveUserLotteryRecord.setOrderId(liveOrder.getOrderId());
|
|
|
@@ -2738,6 +2743,14 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
liveOrderItem.setProductId(liveOrder.getProductId());
|
|
|
liveOrderItem.setNum(Long.valueOf(liveOrder.getTotalNum()));
|
|
|
liveOrderItem.setJsonInfo(JSON.toJSONString(dto));
|
|
|
+ //计算出库价(净价)- 单个商品情况
|
|
|
+ BigDecimal productTotal = fsStoreProduct.getPrice().multiply(new BigDecimal(liveOrder.getTotalNum()));
|
|
|
+ BigDecimal netPrice = BigDecimal.ZERO;
|
|
|
+ if (productTotal.compareTo(BigDecimal.ZERO) > 0 && liveOrder.getPayPrice() != null) {
|
|
|
+ BigDecimal discountRate = liveOrder.getPayPrice().divide(productTotal, 10, RoundingMode.HALF_UP);
|
|
|
+ netPrice = fsStoreProduct.getPrice().multiply(discountRate).setScale(2, RoundingMode.HALF_UP);
|
|
|
+ }
|
|
|
+ liveOrderItem.setNetPrice(netPrice);
|
|
|
liveOrderItemMapper.insertLiveOrderItem(liveOrderItem);
|
|
|
redisCache.deleteObject("orderKey:" + liveOrder.getOrderKey());
|
|
|
return R.ok("下单成功").put("order", liveOrder);
|
|
|
@@ -3179,38 +3192,33 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 计算价格:如果有规格,使用规格价格;否则使用商品价格
|
|
|
- BigDecimal payPrice = fsStoreProduct.getPrice().multiply(new BigDecimal(liveOrder.getTotalNum()));
|
|
|
+ // 计算价格(仿照 FsStoreOrderServiceImpl):
|
|
|
+ // totalPrice = 商品总价(不含邮费)
|
|
|
+ // payPostage = 应付邮费
|
|
|
+ // payPrice = 应付价格(商品总价 + 邮费 - 优惠券)
|
|
|
+ // payMoney = 实付价格(下单时等于应付价格)
|
|
|
+ BigDecimal totalPrice = fsStoreProduct.getPrice().multiply(new BigDecimal(liveOrder.getTotalNum()));
|
|
|
if (fsStoreProductAttrValue != null) {
|
|
|
- payPrice = fsStoreProductAttrValue.getPrice().multiply(new BigDecimal(liveOrder.getTotalNum()));
|
|
|
+ totalPrice = fsStoreProductAttrValue.getPrice().multiply(new BigDecimal(liveOrder.getTotalNum()));
|
|
|
}
|
|
|
-
|
|
|
- // 直播不需要服务费 0915 1735 左
|
|
|
-// String config=configService.selectConfigByKey("store.config");
|
|
|
-// StoreConfig storeConfig= JSONUtil.toBean(config,StoreConfig.class);
|
|
|
-// BigDecimal serviceFee=new BigDecimal(0);
|
|
|
-// if(storeConfig.getServiceFee()!=null){
|
|
|
-// if(liveOrder.getCompanyUserId()==null||liveOrder.getCompanyUserId()==0){
|
|
|
-// serviceFee=storeConfig.getServiceFee();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// payPrice = payPrice.add(serviceFee);
|
|
|
- // 生成
|
|
|
- BigDecimal deliveryMoney = handleDeliveryMoney(liveOrder.getCityId(), fsStoreProduct, liveOrder.getTotalNum(), fsStoreProductAttrValue);
|
|
|
- payPrice = payPrice.add(deliveryMoney);
|
|
|
- liveOrder.setDiscountMoney(BigDecimal.ZERO);
|
|
|
+ liveOrder.setTotalPrice(totalPrice);
|
|
|
|
|
|
- //优惠券处理
|
|
|
+ BigDecimal payPostage = handleDeliveryMoney(liveOrder.getCityId(), fsStoreProduct, liveOrder.getTotalNum(), fsStoreProductAttrValue);
|
|
|
+ liveOrder.setPayPostage(payPostage);
|
|
|
+ liveOrder.setPayDelivery(payPostage);
|
|
|
+
|
|
|
+ liveOrder.setDiscountMoney(BigDecimal.ZERO);
|
|
|
+ // 优惠券处理
|
|
|
if (liveOrder.getCouponUserId() != null) {
|
|
|
LiveCouponUser couponUser = liveCouponUserService.selectLiveCouponUserById(liveOrder.getCouponUserId());
|
|
|
if (couponUser != null && couponUser.getStatus() == 0) {
|
|
|
if (!couponUser.getUserId().toString().equals(liveOrder.getUserId())) {
|
|
|
return R.error("非法操作");
|
|
|
}
|
|
|
- if (couponUser.getUseMinPrice().compareTo(payPrice) < 1) {
|
|
|
+ BigDecimal payPriceBeforeCoupon = totalPrice.add(payPostage);
|
|
|
+ if (couponUser.getUseMinPrice().compareTo(payPriceBeforeCoupon) < 1) {
|
|
|
liveOrder.setUserCouponId(couponUser.getId());
|
|
|
liveOrder.setDiscountMoney(couponUser.getCouponPrice());
|
|
|
- //更新优惠券状态
|
|
|
couponUser.setStatus(1);
|
|
|
couponUser.setUseTime(new Date());
|
|
|
liveCouponUserService.updateLiveCouponUser(couponUser);
|
|
|
@@ -3218,15 +3226,18 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // payPrice = 应付价格 = 商品总价 + 邮费 - 优惠券
|
|
|
+ BigDecimal payPrice = totalPrice.add(payPostage).subtract(liveOrder.getDiscountMoney() != null ? liveOrder.getDiscountMoney() : BigDecimal.ZERO);
|
|
|
+ liveOrder.setPayPrice(payPrice);
|
|
|
+ // payMoney = 实付价格(下单时等于应付价格)
|
|
|
+ liveOrder.setPayMoney(payPrice);
|
|
|
+
|
|
|
liveOrder.setItemJson(JSON.toJSONString(fsStoreProduct));
|
|
|
liveOrder.setCreateTime(new Date());
|
|
|
liveOrder.setUpdateTime(new Date());
|
|
|
- liveOrder.setPayDelivery(deliveryMoney);
|
|
|
liveOrder.setProductId(fsStoreProduct.getProductId());
|
|
|
liveOrder.setStatus(OrderInfoEnum.STATUS_1.getValue());
|
|
|
liveOrder.setPayType("1");
|
|
|
- liveOrder.setTotalPrice(payPrice);
|
|
|
- liveOrder.setPayMoney(liveOrder.getTotalPrice().subtract(liveOrder.getDiscountMoney()));
|
|
|
try {
|
|
|
if (baseMapper.insertLiveOrder(liveOrder) > 0) {
|
|
|
LiveOrderItemDTO dto = new LiveOrderItemDTO();
|
|
|
@@ -3253,6 +3264,14 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
liveOrderItem.setProductId(liveOrder.getProductId());
|
|
|
liveOrderItem.setNum(Long.valueOf(liveOrder.getTotalNum()));
|
|
|
liveOrderItem.setJsonInfo(JSON.toJSONString(dto));
|
|
|
+ //计算出库价(净价)- 单个商品情况
|
|
|
+ BigDecimal productTotal = fsStoreProduct.getPrice().multiply(new BigDecimal(liveOrder.getTotalNum()));
|
|
|
+ BigDecimal netPrice = BigDecimal.ZERO;
|
|
|
+ if (productTotal.compareTo(BigDecimal.ZERO) > 0 && liveOrder.getPayPrice() != null) {
|
|
|
+ BigDecimal discountRate = liveOrder.getPayPrice().divide(productTotal, 10, RoundingMode.HALF_UP);
|
|
|
+ netPrice = fsStoreProduct.getPrice().multiply(discountRate).setScale(2, RoundingMode.HALF_UP);
|
|
|
+ }
|
|
|
+ liveOrderItem.setNetPrice(netPrice);
|
|
|
liveOrderItemMapper.insertLiveOrderItem(liveOrderItem);
|
|
|
redisCache.deleteObject("orderKey:" + liveOrder.getOrderKey());
|
|
|
return R.ok("下单成功").put("order", liveOrder);
|
|
|
@@ -3348,6 +3367,55 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
|
|
|
return storePostage;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 按前端 productOrder.vue 逻辑计算每个订单项的出库价(净价)
|
|
|
+ * 折扣率 = 应付金额/产品合计;前 n-1 项:净价=单价*折扣率(保留2位);最后一项:净价=(应付-前项净价*数量之和)/数量
|
|
|
+ */
|
|
|
+ private List<BigDecimal> calcLiveItemNetPrices(BigDecimal orderPayPrice, List<LiveOrderItem> liveOrderItemList) {
|
|
|
+ int n = liveOrderItemList.size();
|
|
|
+ List<BigDecimal> netPrices = new ArrayList<>(n);
|
|
|
+ if (n == 0 || orderPayPrice == null || orderPayPrice.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ for (int i = 0; i < n; i++) {
|
|
|
+ netPrices.add(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ return netPrices;
|
|
|
+ }
|
|
|
+ BigDecimal productTotal = BigDecimal.ZERO;
|
|
|
+ for (LiveOrderItem item : liveOrderItemList) {
|
|
|
+ LiveOrderItemDTO dto = JSON.parseObject(item.getJsonInfo(), LiveOrderItemDTO.class);
|
|
|
+ BigDecimal price = dto.getPrice() != null ? dto.getPrice() : BigDecimal.ZERO;
|
|
|
+ long num = item.getNum() != null ? item.getNum() : 0;
|
|
|
+ productTotal = productTotal.add(price.multiply(BigDecimal.valueOf(num)));
|
|
|
+ }
|
|
|
+ if (productTotal.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ for (int i = 0; i < n; i++) {
|
|
|
+ netPrices.add(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ return netPrices;
|
|
|
+ }
|
|
|
+ BigDecimal discountRate = orderPayPrice.divide(productTotal, 10, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal previousTotal = BigDecimal.ZERO;
|
|
|
+ for (int i = 0; i < n; i++) {
|
|
|
+ LiveOrderItem item = liveOrderItemList.get(i);
|
|
|
+ LiveOrderItemDTO dto = JSON.parseObject(item.getJsonInfo(), LiveOrderItemDTO.class);
|
|
|
+ BigDecimal price = dto.getPrice() != null ? dto.getPrice() : BigDecimal.ZERO;
|
|
|
+ long num = item.getNum() != null ? item.getNum() : 0;
|
|
|
+ if (n == 1) {
|
|
|
+ netPrices.add(price.multiply(discountRate).setScale(2, RoundingMode.HALF_UP));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (i == n - 1) {
|
|
|
+ BigDecimal lastNet = num > 0 ? orderPayPrice.subtract(previousTotal).divide(BigDecimal.valueOf(num), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
|
|
+ netPrices.add(lastNet);
|
|
|
+ } else {
|
|
|
+ BigDecimal net = price.multiply(discountRate).setScale(2, RoundingMode.HALF_UP);
|
|
|
+ netPrices.add(net);
|
|
|
+ previousTotal = previousTotal.add(net.multiply(BigDecimal.valueOf(num)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return netPrices;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public R confirmOrder(LiveOrderConfirmParam param) {
|
|
|
String uuid = IdUtil.randomUUID();
|