Browse Source

提交订单拆分子订单

zhijiezhao 3 years ago
parent
commit
b7df689651

+ 5 - 0
src/main/java/com/caimei365/order/mapper/SubmitMapper.java

@@ -5,6 +5,7 @@ import com.caimei365.order.model.po.*;
 import com.caimei365.order.model.vo.AddressVo;
 import com.caimei365.order.model.vo.LadderPriceVo;
 import com.caimei365.order.model.vo.PromotionsVo;
+import com.caimei365.order.model.vo.ShopOrderVo;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
@@ -145,4 +146,8 @@ public interface SubmitMapper {
     List<Integer> findLowOrder(@Param("userId") Integer userId, @Param("orderTime") Date orderTime);
 
     void updateOnlinePayFlag(Integer onlinePayFlag, Integer orderId);
+
+    List<OrderShopPo> findShops(List<String> productIdList);
+
+    Integer findSplitResult(List<String> productIdList);
 }

+ 5 - 0
src/main/java/com/caimei365/order/service/impl/PayOrderServiceImpl.java

@@ -343,6 +343,7 @@ public class PayOrderServiceImpl implements PayOrderService {
      * @param payLinkDto {
      *                   orderId       订单ID
      *                   vipRecordId   会员购买记录Id
+     *                   couponRecordId 优惠券购买记录id
      *                   }
      */
     @Override
@@ -446,9 +447,12 @@ public class PayOrderServiceImpl implements PayOrderService {
         AuthUserVo authUser = null;
         // 链接类型:1订单,2超级会员,3认证通会员
         int type = 0;
+        // 订单标识,0非订单,1订单
+        int orderFlag = 0;
         // 订单支付记录
         List<DiscernReceiptVo> discernReceiptList = null;
         if (null != orderPayLink.getShopOrderId()) {
+            orderFlag = 1;
             type = 1;
             // 订单信息
             OrderVo order = orderCommonMapper.getOrderByShopOrderId(orderPayLink.getShopOrderId());
@@ -499,6 +503,7 @@ public class PayOrderServiceImpl implements PayOrderService {
             time = dateFormat.format(orderPayLink.getEffectiveTime());
         }
         Map<String, Object> map = new HashMap<>(5);
+        map.put("orderFlag",orderFlag);
         map.put("type", type);
         map.put("orderPayLink", orderPayLink);
         map.put("userName", userName);

+ 129 - 29
src/main/java/com/caimei365/order/service/impl/SubmitServiceImpl.java

@@ -34,6 +34,7 @@ import java.util.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
 
 import static com.alibaba.fastjson.JSON.parseArray;
 import static com.alibaba.fastjson.JSON.parseObject;
@@ -129,7 +130,6 @@ public class SubmitServiceImpl implements SubmitService {
     public ResponseJson<Map<String, Object>> generateOrder(SubmitDto submitDto) {
         // 获取机构用户Id
         Integer clubUserId = baseMapper.getUserIdByClubId(submitDto.getClubId());
-        submitDto.getUnionId();
         unionId = submitDto.getUnionId();
         if (null == clubUserId || clubUserId == 0) {
             return ResponseJson.error("机构用户信息异常!", null);
@@ -300,7 +300,7 @@ public class SubmitServiceImpl implements SubmitService {
 
     public ResponseJson<Map<String, Object>> saveOrder(OrderParamBo orderParamBo) {
         log.info("******************** 提交订单逻辑处理 start *******************");
-        /*
+        /**
          * 初始化主订单
          */
         OrderPo mainOrder = new OrderPo();
@@ -322,8 +322,6 @@ public class SubmitServiceImpl implements SubmitService {
         mainOrder.setUpdateDate(curDateStr);
         // 订单状态 0 有效  其它无效
         mainOrder.setDelFlag(0);
-
-
         //协销下单
         if (3 == orderParamBo.getCartType()) {
             mainOrder.setSpId(orderParamBo.getServiceProviderId());
@@ -364,7 +362,7 @@ public class SubmitServiceImpl implements SubmitService {
         // 是否开发票
         mainOrder.setInvoiceFlag(orderParamBo.getInvoiceType());
 
-        /*
+        /**
          * 整理订单商品信息
          */
         // 商品总数量
@@ -412,14 +410,10 @@ public class SubmitServiceImpl implements SubmitService {
         for (Object infoObject : orderInfo) {
             JSONObject shopInfo = (JSONObject) infoObject;
             Integer shopId = (Integer) shopInfo.get("shopId");
-            String splitCode = (String) shopInfo.get("splitCode");
             String shopNote = (String) shopInfo.get("note");
             if (null == shopId) {
                 return ResponseJson.error("供应商数据异常!", null);
             }
-            if (StringUtils.isBlank(splitCode)) {
-                onlinePay.set(false);
-            }
             JSONArray productArr = (JSONArray) shopInfo.get("productInfo");
             if (null == productArr) {
                 return ResponseJson.error("订单商品数据异常!", null);
@@ -430,7 +424,6 @@ public class SubmitServiceImpl implements SubmitService {
              * 初始化子订单
              */
             OrderShopPo shopOrder = new OrderShopPo();
-            shopOrder.setSplitCode(splitCode);
             shopOrder.setShopId(shopId);
             shopOrder.setShopName(shopName);
             shopOrder.setNote(shopNote);
@@ -638,7 +631,6 @@ public class SubmitServiceImpl implements SubmitService {
                             if (promotions.getMode() == 1 || MathUtil.compare(productFee, promotions.getTouchPrice()) >= 0) {
                                 // 满减
                                 if (promotions.getMode() == 2) {
-
                                     if (promotions.getDiscount() != null && promotions.getDiscount() == 1) {
                                         //叠加优惠计算
                                         //叠加倍数
@@ -792,7 +784,11 @@ public class SubmitServiceImpl implements SubmitService {
                 orderProductList.add(product);
                 productIdList.add(product.getProductId().toString());
             }
-
+            Integer count = submitMapper.findSplitResult(productIdList);
+            if (count > 0) {
+                //有商品无分帐号,走线下
+                onlinePay.set(false);
+            }
             if (null != shopPromotions) {
                 // 是否满足满减满赠
                 if (MathUtil.compare(shopPromotionFee.get(), shopPromotions.getTouchPrice()) >= 0) {
@@ -933,7 +929,7 @@ public class SubmitServiceImpl implements SubmitService {
         mainOrder.setPresentCount(presentCount.get());
         //促销赠品数量
         mainOrder.setPromotionalGiftsCount(promotionalGiftsCount.get());
-        /*
+        /**
          * 处理优惠券
          */
         Double couponAmount = 0d;
@@ -948,7 +944,7 @@ public class SubmitServiceImpl implements SubmitService {
             couponAmount = coupon.getCouponAmount();
         }
         mainOrder.setCouponAmount(couponAmount);
-        /*
+        /**
          * 计算运费
          */
         if (3 != orderParamBo.getCartType()) {
@@ -1070,16 +1066,14 @@ public class SubmitServiceImpl implements SubmitService {
             mainOrder.setClauseName("无条款");
         }
 
-        /*
+        /**
          * 保存主订单数据,返回订单Id
          */
         submitMapper.insertMainOrder(mainOrder);
         log.info("【提交订单】>>>>>>>>>>>>>>>>>>>>>>>>>>新增主订单(insert[cm_order])orderId:" + mainOrder.getOrderId());
 
 
-
-
-        /*
+        /**
          * 保存订单促销,返回订单促销Id
          */
         promotionList.forEach(promotions -> {
@@ -1090,7 +1084,18 @@ public class SubmitServiceImpl implements SubmitService {
         /**
          * 线上单分摊优惠
          */
+        if (presentCount.get() > 0) {
+            //有赠品不能线上,赠品成本无法分账
+            onlinePay.set(false);
+        }
         if (onlinePay.get()) {
+            //在店铺满减算完之后重新根据商品的splitCode排shopOrderList
+            List<OrderShopPo> shops = submitMapper.findShops(productIdList);
+            //子订单分多子订单,满减分摊到原子订单分开的子订单,无满减重算佣金/应付
+            if (shops.size() != shopOrderList.size()) {
+                //找要拆分的子订单,重赋值needpay计算不包含分摊优惠的金额
+                findSplitShopOrder(orderProductList, shopOrderList, shops, orderParamBo);
+            }
             //线上支付分摊优惠总和到子订单
             //经理折扣+优惠券+满减 提交订单的时候无经理折扣
             double allDiscount = MathUtil.add(couponAmount, promotionFullReduction.get()).doubleValue();
@@ -1098,16 +1103,8 @@ public class SubmitServiceImpl implements SubmitService {
             AtomicDouble oldBrokerage = new AtomicDouble(0);
             //超级会员价/复购价/阶梯价/促销活动冲突,只能生效一个,促销活动满减计入优惠分摊,子订单售价=NeedPayAmount+promotionFullReduction
             shopOrderList.forEach(orderShopPo -> {
-                // 单品满减和店铺满减会存入shoporder信息中
-                if (null != orderShopPo.getPromotionFullReduction() && orderShopPo.getPromotionFullReduction() > 0) {
-                    //不含分摊优惠的子订单金额
-                    orderShopPo.setOldPrice(MathUtil.add(orderShopPo.getNeedPayAmount(), orderShopPo.getPromotionFullReduction()).doubleValue());
-                    //此处佣金未算优惠券和满减金额,不用加上分摊优惠金额
-                    orderShopPo.setOldBrokerage(orderShopPo.getBrokerage());
-                } else {
-                    orderShopPo.setOldPrice(orderShopPo.getNeedPayAmount());
-                    orderShopPo.setOldBrokerage(orderShopPo.getBrokerage());
-                }
+                orderShopPo.setOldPrice(orderShopPo.getNeedPayAmount());
+                orderShopPo.setOldBrokerage(orderShopPo.getBrokerage());
                 oldBrokerage.set(MathUtil.add(oldBrokerage.get(), orderShopPo.getOldBrokerage()).doubleValue());
             });
             // 按分摊前的佣金排序
@@ -1608,7 +1605,7 @@ public class SubmitServiceImpl implements SubmitService {
         //应付订单金额
         info.put("payTotalFee", mainOrder.getPayTotalFee());
         //线上线下按钮onlinePayFlag:0线上,1线下
-        info.put("onlinePayFlag",onlinePayFlag);
+        info.put("onlinePayFlag", onlinePayFlag);
         //真实需要付款金额
         info.put("payableAmount", mainOrder.getPayableAmount());
         if (isPaySuccessFlag) {
@@ -1626,6 +1623,109 @@ public class SubmitServiceImpl implements SubmitService {
         return ResponseJson.success("操作成功!", info);
     }
 
+    private void findSplitShopOrder(List<OrderProductPo> orderProductList, List<OrderShopPo> shopOrderList, List<OrderShopPo> shops, OrderParamBo orderParamBo) {
+        //统计拆分的shop
+        List<OrderShopPo> collect = shops.stream().filter(s -> shops.stream().filter(so -> so.getShopId().equals(s.getShopId())).count() > 1).collect(Collectors.toList());
+        //对拆分的shop重新计算子订单佣金,应付,不计入店铺满减
+        ArrayList<OrderShopPo> orderShopPos = new ArrayList<>();
+        for (OrderShopPo shop : collect) {
+            // 供应商名称
+            String shopName = baseMapper.getShopNameById(shop.getShopId());
+            /*
+             * 初始化子订单
+             */
+            List<OrderShopPo> oldShop = shopOrderList.stream().filter(s -> s.getShopId().equals(shop.getShopId())).collect(Collectors.toList());
+            OrderShopPo shopOrder = new OrderShopPo();
+            shopOrder.setSplitCode(shop.getSplitCode());
+            shopOrder.setShopId(shop.getShopId());
+            shopOrder.setShopName(shopName);
+            shopOrder.setNote(oldShop.get(0).getNote());
+            // 用户Id
+            shopOrder.setUserId(oldShop.get(0).getUserId());
+            shopOrder.setClubId(oldShop.get(0).getClubId());
+            // todo ??
+            if (null != oldShop.get(0).getOrderPromotionsId()) {
+                shopOrder.setOrderPromotionsId(oldShop.get(0).getOrderPromotionsId());
+            }
+            if (3 == orderParamBo.getCartType()) {
+                // 协销订单
+                shopOrder.setSpId(orderParamBo.getBuyUserId());
+            }
+            // 商品总数量
+            AtomicInteger shopProductCount = new AtomicInteger(0);
+            // 佣金 =  应付采美
+            AtomicDouble brokerage = new AtomicDouble(0);
+            // 需要支付金额
+            AtomicDouble needPayAmount = new AtomicDouble(0);
+            // 商品总金额
+            AtomicDouble shopAmount = new AtomicDouble(0);
+            // 付供应商 商品费
+            AtomicDouble shopProductAmount = new AtomicDouble(0);
+            // 付供应商税费
+            AtomicDouble shopTaxFee = new AtomicDouble(0);
+            // 商品费
+            AtomicDouble shopProductFee = new AtomicDouble(0);
+            // 该供应商下该分帐号的商品
+            List<OrderProductPo> shopProducts = orderProductList.stream().filter(po -> po.getSplitCode().equals(shop.getSplitCode()) && po.getShopId().equals(shop.getShopId())).collect(Collectors.toList());
+            // 遍历所有商品
+            for (OrderProductPo product : shopProducts) {
+                /*
+                 * 子订单金额计算
+                 */
+                // 供应商商品数量
+                shopProductCount.set(shopProductCount.get() + product.getNum());
+                // 佣金 = 应付采美
+                brokerage.set(MathUtil.add(brokerage.get(), product.getCmFee()).doubleValue());
+                // 需要支付金额 shouldPayFee +运费
+                needPayAmount.set(MathUtil.add(needPayAmount.get(), product.getShouldPayFee()).doubleValue());
+                // 统计 总金额 包括税费
+                shopProductFee.set(MathUtil.add(shopProductFee.get(), product.getTotalFee()).doubleValue());
+                // 商品总金额
+                shopAmount.set(MathUtil.add(shopAmount.get(), product.getTotalAmount()).doubleValue());
+                // 付供应商 商品费
+                shopProductAmount.set(MathUtil.add(shopProductAmount.get(), product.getShopProductAmount()).doubleValue());
+                // 付供应商税费
+                shopTaxFee.set(MathUtil.add(shopTaxFee.get(), product.getShouldPayTotalTax()).doubleValue());
+            }
+
+            // 佣金 采美应收
+            shopOrder.setBrokerage(brokerage.get());
+            // 可退款金额 = 余额抵扣金额
+            shopOrder.setCanRefundAmount(needPayAmount.get());
+            // 购买商品数
+            shopOrder.setItemCount(shopProductCount.get());
+            // 总价(含税) = totalFee
+            shopOrder.setTotalAmount(shopProductFee.get());
+            // 总金额 = 订单商品totalAmount
+            shopOrder.setProductAmount(shopAmount.get());
+            // 需要支付金额 shouldPayFee +运费
+            shopOrder.setNeedPayAmount(needPayAmount.get());
+            // 付供应商 商品费=成本价*(购买数量  + 赠品数量)
+            shopOrder.setShopProductAmount(shopProductAmount.get());
+            // 付给供应商税费
+            shopOrder.setShopTaxFee(shopTaxFee.get());
+            // 付供应商 = 商品费 + 运费 + 税费
+            shopOrder.setShouldPayShopAmount(MathUtil.add(shopProductAmount.get(), shopTaxFee.get()).doubleValue());
+            // 付给供应商运费
+            shopOrder.setShopPostFee(0d);
+            // 已付供应商金额
+            shopOrder.setPayedShopAmount(0d);
+            // (付款供应商)付款状态:1待付款、2部分付款、3已付款
+            shopOrder.setPayStatus(1);
+            if (3 == orderParamBo.getCartType()) {
+                // 协销订单
+                shopOrder.setOrderType(0);
+            } else {
+                // 普通订单
+                shopOrder.setOrderType(1);
+            }
+            orderShopPos.add(shopOrder);
+            //把原shoporderlist拆分的shop剔除,加入拆分后的shop
+            shopOrderList.removeIf(s -> s.getShopId().equals(shopOrder.getShopId()));
+        }
+        shopOrderList.addAll(orderShopPos);
+    }
+
     /**
      * 生成充值商品订单
      *

+ 20 - 0
src/main/resources/mapper/SubmitMapper.xml

@@ -245,4 +245,24 @@
           and orderTime <![CDATA[ > ]]> #{orderTime}
           and userID = #{userId}
     </select>
+    <select id="findShops" resultType="com.caimei365.order.model.po.OrderShopPo">
+        SELECT DISTINCT
+            s.shopID AS shopId,
+            p.splitCode
+        FROM shop s
+        LEFT JOIN product p ON p.shopID = s.shopID
+        WHERE p.productID IN
+        <foreach collection="productIdList" open="(" separator="," close=")" item="productId">
+            #{productId}
+        </foreach>
+        GROUP BY s.shopID,p.splitCode
+    </select>
+    <select id="findSplitResult" resultType="java.lang.Integer">
+        SELECT COUNT(*) FROM product
+        WHERE productId IN
+        <foreach collection="productIdList" open="(" separator="," close=")" item="productId">
+            #{productId}
+        </foreach>
+        AND (splitcode IS NULL OR splitcode ='')
+    </select>
 </mapper>

+ 6 - 0
src/test/java/com/caimei365/order/OrderApplicationTests.java

@@ -2,9 +2,13 @@
 //
 //import com.alibaba.fastjson.JSON;
 //import com.alibaba.fastjson.JSONObject;
+//import com.caimei365.order.mapper.SubmitMapper;
+//import com.caimei365.order.model.po.OrderShopPo;
+//import com.caimei365.order.model.vo.ShopOrderVo;
 //import org.junit.jupiter.api.Test;
 //import org.springframework.boot.test.context.SpringBootTest;
 //
+//import javax.annotation.Resource;
 //import java.io.IOException;
 //import java.io.InputStream;
 //import java.lang.reflect.Type;
@@ -19,6 +23,8 @@
 // */
 //@SpringBootTest
 //class OrderApplicationTests {
+//    @Resource
+//    private SubmitMapper submitMapper;
 //
 //    @Test
 //    void contextLoads() throws IOException {