فهرست منبع

延时分账定时器

plf 4 سال پیش
والد
کامیت
0484ee5144

+ 7 - 0
base-module/src/main/java/com/caimei/module/base/entity/bo/Payment.java

@@ -38,6 +38,13 @@ public class Payment implements Serializable {
      */
     private String bankCode;
 
+    /**
+     * 用户类型(银联支付使用)
+     * 企业:ENTERPRISE
+     * 个人:USER
+     */
+    private String userType;
+
     /**
      * 页面回调地址
      */

+ 126 - 0
base-module/src/main/java/com/caimei/module/base/entity/po/CmPayShop.java

@@ -0,0 +1,126 @@
+package com.caimei.module.base.entity.po;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import lombok.Data;
+
+/**
+ * cm_pay_shop
+ * @author 
+ */
+@Data
+public class CmPayShop implements Serializable {
+    private Long id;
+
+    /**
+     * 供应商Id
+     */
+    private Integer shopID;
+
+    /**
+     * 付款单名称
+     */
+    private String name;
+
+    /**
+     * 付款账号的户名
+     */
+    private String bankAccountName;
+
+    /**
+     * 付款账号
+     */
+    private String bankAccount;
+
+    /**
+     * 付款账号的开户行
+     */
+    private String bankName;
+
+    /**
+     *  付款账号的类型 0公账, 1私账
+     */
+    private String type;
+
+    /**
+     * 付供应商总金额
+     */
+    private BigDecimal totalAmount;
+
+    /**
+     * 余额支付
+     */
+    private BigDecimal balancePayFee;
+
+    /**
+     * 转账支付
+     */
+    private BigDecimal transferPayFee;
+
+    /**
+     * 付款方式 1建设银行7297, 2中信银行0897, 3中信银行7172, 4广发银行0115, 5广发银行5461
+     */
+    private String payType;
+
+    /**
+     * 付款抹平金额(总)
+     */
+    private BigDecimal wipePayment;
+
+    /**
+     * 付款抹平备注(文字)
+     */
+    private String wipeRemarks;
+
+    /**
+     * 付款抹平备注(图片),以"##"隔开
+     */
+    private String wipeRemarkImages;
+
+    /**
+     * 抹平申请时间
+     */
+    private String wipeTime;
+
+    /**
+     * 申请人ID
+     */
+    private Integer applicant;
+
+    /**
+     * 申请时间
+     */
+    private String applyTime;
+
+    /**
+     * 审核人ID
+     */
+    private Integer reviewer;
+
+    /**
+     * 审核时间
+     */
+    private String reviewTime;
+
+    /**
+     * 付款时间
+     */
+    private String payTime;
+
+    /**
+     * 审核状态  0待审核,  1审核通过  2审核不通过
+     */
+    private String status;
+
+    /**
+     * 审核不通过原因
+     */
+    private String reason;
+
+    /**
+     * 删除标记 0 否,其余是
+     */
+    private String delFlag;
+
+    private static final long serialVersionUID = 1L;
+}

+ 5 - 0
base-module/src/main/java/com/caimei/module/base/entity/po/CmReceiptOrderRelation.java

@@ -44,6 +44,11 @@ public class CmReceiptOrderRelation implements Serializable {
      */
     private String orderRequestNo;
 
+    /**
+     * 分账状态:0待分账,1已分账(只针对线上支付)
+     */
+    private String splitStatus;
+
     /**
      * 删除标记 0 否,其余是
      */

+ 0 - 5
base-module/src/main/java/com/caimei/module/base/entity/po/CmSplitAccount.java

@@ -49,11 +49,6 @@ public class CmSplitAccount implements Serializable {
      */
     private BigDecimal splitAccount;
 
-    /**
-     * 分账手续费
-     */
-    private BigDecimal handlingFee;
-
     /**
      * 米花科技平台唯一流水号
      */

+ 7 - 0
base-module/src/main/java/com/caimei/module/base/entity/vo/OrderRelationVo.java

@@ -13,4 +13,11 @@ import lombok.EqualsAndHashCode;
 @EqualsAndHashCode(callSuper = true)
 @Data
 public class OrderRelationVo extends CmReceiptOrderRelation {
+    /**
+     * 付款类型:1建设银行7297、2广发银行0115、3中信银行7172、4中信银行0897、5中信银行0897-财付通、6中信银行0897-支付宝、
+     * 7线上-支付宝、8线上-微信支付、9线上-快钱支付、10口头返佣、11广发银行5461、12PC-B2B网银、13PC-微信支付、14PC-支付宝、
+     * 15小程序-微信支付、16余额抵扣
+     */
+    private String payType;
+
 }

+ 16 - 0
base-module/src/main/java/com/caimei/module/base/entity/vo/PayShopVo.java

@@ -0,0 +1,16 @@
+package com.caimei.module.base.entity.vo;
+
+import com.caimei.module.base.entity.po.CmPayShop;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/3/4
+ */
+@Data
+@EqualsAndHashCode(callSuper = true)
+public class PayShopVo extends CmPayShop {
+}

+ 62 - 0
pay/src/main/java/com/caimei/module/pay/dao/PayDao.java

@@ -97,4 +97,66 @@ public interface PayDao {
      * @return
      */
     BigDecimal findShipping(@Param("orderId") Long orderId, @Param("shopId") Integer shopId);
+
+    /**
+     * 查询未分账已支付收款
+     *
+     * @param currentTime 延迟时间
+     * @return
+     */
+    List<OrderRelationVo> findUnallocatedAccount(String currentTime);
+
+    /**
+     * 查询分账给子商户号
+     *
+     * @param mbOrderId 米花平台交易流水号
+     * @return
+     */
+    List<SplitAccountVo> findByMbOrderId(String mbOrderId);
+
+    /**
+     * 查询已付供应商金额
+     *
+     * @param shopOrderId
+     * @return
+     */
+    BigDecimal findPaidShop(Integer shopOrderId);
+
+    /**
+     * 保存付款单
+     *
+     * @param payShop
+     */
+    void insertPayShop(PayShopVo payShop);
+
+    /**
+     * 保存付款记录
+     *
+     * @param shopRecord
+     */
+    void insertPayShopRecord(PayShopRecordVo shopRecord);
+
+    /**
+     * 修改收款分账状态
+     *
+     * @param mbOrderId
+     */
+    void updateBySplitStatus(String mbOrderId);
+
+    /**
+     * 修改子订单付款状态及付款金额
+     *
+     * @param shopOrderId 子订单id
+     * @param paidShop    已支付金额
+     * @param payStatus   付款状态
+     */
+    void updateShopOrderByPayStatus(@Param("shopOrderId") Integer shopOrderId, @Param("paidShop") BigDecimal paidShop, @Param("payStatus") String payStatus);
+
+    /**
+     * 修改主订单付款状态
+     *
+     * @param orderId
+     * @param payStatus
+     */
+    void updateOrderByPayStatus(@Param("orderId") Integer orderId, @Param("payStatus") String payStatus);
 }

+ 14 - 0
pay/src/main/java/com/caimei/module/pay/service/PayService.java

@@ -17,6 +17,7 @@ import java.io.IOException;
 public interface PayService {
     /**
      * 获取线上支付开关状态
+     *
      * @return
      */
     JsonModel getPayOnLineSwitch();
@@ -86,4 +87,17 @@ public interface PayService {
      * @return
      */
     String secondHandPayCallBack(String data) throws Exception;
+
+    /**
+     * 延时分账
+     */
+    void delayedSplitting(String notifyUrl);
+
+    /**
+     * 延时分账异步通知回调
+     *
+     * @param data 回调参数
+     * @return SUCCESS告诉第三方回调成功
+     */
+    String delayedSplittingCallback(String data) throws Exception;
 }

+ 166 - 92
pay/src/main/java/com/caimei/module/pay/service/impl/PayServiceImpl.java

@@ -12,6 +12,7 @@ import com.caimei.module.pay.util.*;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
+import org.apache.http.client.utils.DateUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -144,7 +145,7 @@ public class PayServiceImpl implements PayService {
         if (MathUtil.compare(MathUtil.mul(order.getPayableAmount(), 100), payment.getPayAmount()) < 0) {
             return model.error("付款金额错误");
         }
-        List<SplitAccountVo> splitBillDetail = null;
+        //List<SplitAccountVo> splitBillDetail = null;
         try {
             // 时间戳
             long time = System.currentTimeMillis() / 1000;
@@ -158,15 +159,16 @@ public class PayServiceImpl implements PayService {
             String attach = order.getOrderID() + "," + payment.getPayType();
             json.put("attach", attach);
             //分账详情
-            splitBillDetail = splitBillDetail(order, payment);
+            /*splitBillDetail = splitBillDetail(order, payment);
             String parameters = ledgerParameters(splitBillDetail, order.getOrderID());
-            json.put("splitBillDetail", parameters);
+            log.info("分账参数: " + parameters);
+            json.put("splitBillDetail", parameters);*/
             String sign = PayUtils.buildSign(json, merKey);
             json.put("sign", sign);
             String data = PayUtils.buildDataPrivate(json, merKey);
             result = PayUtils.httpGet("https://platform.mhxxkj.com/paygateway/mbpay/order/v1", merAccount, data);
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("错误信息", e);
             return model.error("支付失败");
         }
         String code = result.getString("code");
@@ -177,7 +179,7 @@ public class PayServiceImpl implements PayService {
         }
 
         //保存分账信息
-        saveSplitBillDetail(splitBillDetail, result);
+        //saveSplitBillDetail(splitBillDetail, result);
         return model.success(result);
     }
 
@@ -281,7 +283,7 @@ public class PayServiceImpl implements PayService {
         //待分账总金额
         BigDecimal splitAmount = payAmount;
         //总手续费
-        BigDecimal procedureFee;
+        BigDecimal procedureFee = BigDecimal.ZERO;
         if ("UNIONPAY".equals(payment.getPayWay())) {
             procedureFee = new BigDecimal(8);
         } else {
@@ -291,8 +293,7 @@ public class PayServiceImpl implements PayService {
                 procedureFee = new BigDecimal("0.01");
             }
         }
-        //剩余手续费
-        BigDecimal remainingFee = procedureFee;
+        splitAmount = MathUtil.sub(splitAmount, procedureFee);
         List<OrderProductVo> orderProductList = payDao.fandAllOrderProduct(order.getOrderID().intValue());
         for (OrderProductVo orderProduct : orderProductList) {
             BigDecimal costPrice = MathUtil.mul(orderProduct.getCostPrice(), orderProduct.getNum());
@@ -308,26 +309,19 @@ public class PayServiceImpl implements PayService {
                 if (paidAmount != null && MathUtil.compare(paidAmount, 0) > 0) {
                     costPrice = MathUtil.sub(costPrice, paidAmount);
                 }
-                //本次分账手续费
-                BigDecimal handlingFee;
                 //待分账金额>=本次待分账金额
                 if (MathUtil.compare(splitAmount, costPrice) > -1) {
-                    handlingFee = MathUtil.mul(procedureFee, MathUtil.div(costPrice, payAmount, 2), 2);
                     splitAmount = MathUtil.sub(splitAmount, costPrice);
                 } else {
                     costPrice = splitAmount;
-                    handlingFee = MathUtil.mul(procedureFee, MathUtil.div(costPrice, payAmount, 2), 2);
                     splitAmount = BigDecimal.ZERO;
                 }
-                costPrice = MathUtil.sub(costPrice, handlingFee);
-                remainingFee = MathUtil.sub(remainingFee, handlingFee);
                 String commercialCode = payDao.findCommercialCode(orderProduct.getShopID());
                 SplitAccountVo splitAccount = new SplitAccountVo();
                 splitAccount.setOrderId(order.getOrderID().intValue());
                 splitAccount.setOrderProductId(orderProduct.getOrderProductID());
                 splitAccount.setShopId(orderProduct.getShopID().intValue());
                 splitAccount.setSplitAccount(costPrice);
-                splitAccount.setHandlingFee(handlingFee);
                 splitAccount.setProductType("1");
                 if (StringUtils.isNotBlank(commercialCode)) {
                     //供应商拥有子商户号
@@ -361,27 +355,20 @@ public class PayServiceImpl implements PayService {
             for (ShopOrderVo shopOrder : shopOrderList) {
                 //运费
                 BigDecimal shopPostFee = shopOrder.getShopPostFee();
-                //本次分账手续费
-                BigDecimal handlingFee;
                 if (MathUtil.compare(shopPostFee, 0) > 0) {
                     BigDecimal shipping = payDao.findShipping(order.getOrderID(), shopOrder.getShopID());
                     shopPostFee = MathUtil.sub(shopPostFee, shipping);
                     if (MathUtil.compare(splitAmount, shopPostFee) > -1) {
-                        handlingFee = MathUtil.mul(procedureFee, MathUtil.div(shopPostFee, payAmount, 2), 2);
                         splitAmount = MathUtil.sub(splitAmount, shipping);
                     } else {
                         shopPostFee = splitAmount;
-                        handlingFee = MathUtil.mul(procedureFee, MathUtil.div(splitAmount, payAmount, 2), 2);
                         splitAmount = BigDecimal.ZERO;
                     }
-                    shopPostFee = MathUtil.sub(shopPostFee, handlingFee);
-                    remainingFee = MathUtil.sub(remainingFee, handlingFee);
                     String commercialCode = payDao.findCommercialCode(Long.valueOf(shopOrder.getShopID()));
                     SplitAccountVo splitAccount = new SplitAccountVo();
                     splitAccount.setOrderId(order.getOrderID().intValue());
                     splitAccount.setShopId(shopOrder.getShopID());
                     splitAccount.setSplitAccount(shopPostFee);
-                    splitAccount.setHandlingFee(handlingFee);
                     splitAccount.setProductType("2");
                     if (StringUtils.isNotBlank(commercialCode)) {
                         //供应商拥有子商户号
@@ -402,77 +389,12 @@ public class PayServiceImpl implements PayService {
             SplitAccountVo splitAccount = new SplitAccountVo();
             splitAccount.setOrderId(order.getOrderID().intValue());
             splitAccount.setSplitAccount(splitAmount);
-            splitAccount.setHandlingFee(remainingFee);
             splitAccount.setProductType("3");
             splitAccount.setType("2");
             splitAccount.setSubUserNo(privateAccountNo);
             list.add(splitAccount);
         }
         return list;
-
-        /*List<OrderProductVo> orderProductList = payDao.fandAllOrderProduct(order.getOrderID().intValue());
-        //私账-无票总金额
-        BigDecimal privateAccountAmount = BigDecimal.ZERO;
-        //公账-普票总金额
-        BigDecimal commonAmount = BigDecimal.ZERO;
-        if ("0".equals(order.getSecondHandOrderFlag())) {
-            //非二手订单才统计
-            for (OrderProductVo orderProduct : orderProductList) {
-                //不含税-不能开票,则分账到私账银行卡
-                if ("0".equals(orderProduct.getIncludedTax()) && "3".equals(orderProduct.getInvoiceType())) {
-                    privateAccountAmount = MathUtil.add(privateAccountAmount, orderProduct.getShouldPayFee());
-                } else if ("2".equals(orderProduct.getInvoiceType())) {
-                    //普通发票
-                    commonAmount = MathUtil.add(commonAmount, orderProduct.getShouldPayFee());
-                }
-            }
-            order.setPayTotalFee(MathUtil.add(order.getPayTotalFee(), order.getPromotionFullReduction()));
-        }
-        if (MathUtil.compare(privateAccountAmount, order.getPayTotalFee()) == 0 || "1".equals(order.getSecondHandOrderFlag())) {
-            //二手订单与不含税-不能开票,全部分给私账
-            Map<String, String> map = new HashMap<>(3);
-            map.put("subUserNo", privateAccountNo);
-            map.put("splitBillType", "2");
-            map.put("splitBillValue", MathUtil.mul(fashionableAmount, 100).toString());
-            list.add(map);
-        } else {
-            //分给私账金额
-            BigDecimal privateAccount = MathUtil.div(MathUtil.mul(fashionableAmount, privateAccountAmount), order.getPayTotalFee(), 8);
-            log.info("分给私账金额>>>>>>>>>>>>>>>>>>>>>>>" + privateAccount);
-            privateAccount = MathUtil.div(MathUtil.mul(fashionableAmount, privateAccountAmount), order.getPayTotalFee(), 2);
-            //分给公账-普票金额
-            BigDecimal commonInvoice = MathUtil.div(MathUtil.mul(fashionableAmount, commonAmount), order.getPayTotalFee(), 2);
-            if (MathUtil.compare(privateAccount, 0) != 0) {
-                //私账-无票
-                Map<String, String> map1 = new HashMap<>(3);
-                map1.put("subUserNo", privateAccountNo);
-                map1.put("splitBillType", "2");
-                //分账金额,单位/分
-                BigDecimal splitBillValue = MathUtil.round(MathUtil.mul(privateAccount, 100), 0);
-                map1.put("splitBillValue", String.valueOf(splitBillValue));
-                list.add(map1);
-            }
-            if (MathUtil.compare(commonInvoice, 0) != 0) {
-                //公账-普票
-                Map<String, String> map3 = new HashMap<>(3);
-                map3.put("subUserNo", commonInvoiceNo);
-                map3.put("splitBillType", "2");
-                BigDecimal splitBillValue = MathUtil.round(MathUtil.mul(commonInvoice, 100), 0);
-                map3.put("splitBillValue", String.valueOf(splitBillValue));
-                list.add(map3);
-            }
-            BigDecimal publicAccount = MathUtil.add(privateAccount, commonInvoice);
-            if (MathUtil.compare(publicAccount, fashionableAmount) != 0) {
-                //公账-专票
-                Map<String, String> map2 = new HashMap<>(3);
-                map2.put("subUserNo", publicAccountNo);
-                map2.put("splitBillType", "2");
-                BigDecimal splitBillValue = MathUtil.round(MathUtil.mul(MathUtil.sub(fashionableAmount, publicAccount), 100), 0);
-                map2.put("splitBillValue", String.valueOf(splitBillValue));
-                list.add(map2);
-            }
-        }*/
-        //return JSONObject.toJSONString(list);
     }
 
     @Override
@@ -584,10 +506,11 @@ public class PayServiceImpl implements PayService {
         relation.setMbOrderId(mbOrderId);
         relation.setOrderRequestNo(orderRequestNo);
         relation.setDelFlag("0");
+        relation.setSplitStatus("0");
         payDao.insertOrderRelation(relation);
         log.info(">>>>>>>>>>>>>>>>>>>>>>>保存付款金额到收款记录," + amount);
         //修改分账付款状态
-        payDao.updateSplitAccountByPay(mbOrderId);
+        //payDao.updateSplitAccountByPay(mbOrderId);
         //判断是否是充值商品
         getRechargeGoods(order, amount);
         return "SUCCESS";
@@ -757,7 +680,7 @@ public class PayServiceImpl implements PayService {
         try {
             data = PayUtils.buildDataPrivate(json, merKey);
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("错误信息", e);
         }
         JSONObject result = PayUtils.httpGet("https://platform.mhxxkj.com/paygateway/mbpay/order/query/v1_1", merAccount, data);
         String code = result.getString("code");
@@ -802,7 +725,7 @@ public class PayServiceImpl implements PayService {
             String data = PayUtils.buildDataPrivate(json, merKey);
             result = PayUtils.httpGet("https://platform.mhxxkj.com/paygateway/mbpay/order/v1", merAccount, data);
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("错误信息", e);
             return model.error("支付失败");
         }
         String code = result.getString("code");
@@ -821,10 +744,8 @@ public class PayServiceImpl implements PayService {
         splitAccount.setType("2");
         if ("UNIONPAY".equals(payment.getPayWay())) {
             splitAccount.setSplitAccount(BigDecimal.valueOf(92));
-            splitAccount.setHandlingFee(new BigDecimal(8));
         } else {
             splitAccount.setSplitAccount(BigDecimal.valueOf(99.62));
-            splitAccount.setHandlingFee(new BigDecimal("0.38"));
         }
         JSONObject data = result.getJSONObject("data");
         splitAccount.setMbOrderId(data.getString("mbOrderId"));
@@ -873,4 +794,157 @@ public class PayServiceImpl implements PayService {
         payDao.updateSplitAccountByPay(mbOrderId);
         return "SUCCESS";
     }
+
+    @Override
+    public void delayedSplitting(String notifyUrl) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(new Date());
+        calendar.add(Calendar.DATE, -1);
+        String currentTime = DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
+        List<OrderRelationVo> orderRelations = payDao.findUnallocatedAccount(currentTime);
+        if (orderRelations != null && orderRelations.size() > 0) {
+            for (OrderRelationVo orderRelation : orderRelations) {
+                log.info("订单id" + orderRelation.getOrderID() + ">>>进入延时分账");
+                OrderVo order = payDao.findOrder(orderRelation.getOrderID());
+                Payment payment = new Payment();
+                payment.setPayAmount(MathUtil.mul(orderRelation.getAssociateAmount(), 100).intValue());
+                if ("12".equals(orderRelation.getPayType())) {
+                    //网银支付
+                    payment.setPayWay("UNIONPAY");
+                }
+                List<SplitAccountVo> splitBillDetail = splitBillDetail(order, payment);
+                String parameters = ledgerParameters(splitBillDetail, order.getOrderID());
+                log.info("分账参数: " + parameters);
+
+                //第三方分账接口
+                JSONObject result = null;
+                try {
+                    // 时间戳
+                    long time = System.currentTimeMillis() / 1000;
+                    JSONObject json = new JSONObject();
+                    json.put("merAccount", merAccount);
+                    json.put("orderId", orderRelation.getOrderRequestNo());
+                    json.put("requestNo", orderRelation.getOrderRequestNo());
+                    json.put("mbOrderId", orderRelation.getMbOrderId());
+                    json.put("time", time);
+                    json.put("splitBillDetail", parameters);
+                    json.put("notifyUrl", notifyUrl);
+                    log.info("回调接口>>>" + notifyUrl);
+                    String sign = PayUtils.buildSign(json, merKey);
+                    json.put("sign", sign);
+                    String data = PayUtils.buildDataPrivate(json, merKey);
+                    result = PayUtils.httpGet("https://platform.mhxxkj.com/paygateway/mbpay/splitOrder/v1", merAccount, data);
+                } catch (Exception e) {
+                    log.error("错误信息", e);
+                }
+                if (result != null) {
+                    String code = result.getString("code");
+                    if (!"000000".equals(code)) {
+                        String msg = result.getString("msg");
+                        log.info("第三方延迟分账失败>>>>>>>msg:" + msg);
+                    } else {
+                        //保存分账记录
+                        for (SplitAccountVo splitAccount : splitBillDetail) {
+                            splitAccount.setMbOrderId(orderRelation.getMbOrderId());
+                            splitAccount.setOrderRequestNo(orderRelation.getOrderRequestNo());
+                            splitAccount.setPayStatus("1");
+                            payDao.insertSplitAccount(splitAccount);
+                        }
+                        log.info("此订单分账结束");
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    public String delayedSplittingCallback(String data) throws Exception {
+        //公钥解密
+        JSONObject json = PayUtils.decryptDataPublic(data, publicKey);
+        log.info("公钥解密>>>>>>" + json);
+        //公钥验签
+        String signaa = json.getString("sign");
+        json.remove("sign");
+        String signbb = PayUtils.buildSign(json, publicKey);
+        if (!signaa.equals(signbb)) {
+            return "验签失败";
+        }
+        String mbOrderId = json.getString("mbOrderId");
+        String status = json.getString("status");
+        log.info("分账状态>>>" + status);
+        if ("FAILED".equals(status)) {
+            return "分账失败";
+        }
+
+        //修改收款分账状态
+        payDao.updateBySplitStatus(mbOrderId);
+
+        List<SplitAccountVo> splitAccountList = payDao.findByMbOrderId(mbOrderId);
+        if (splitAccountList != null && splitAccountList.size() > 0) {
+            //本次付供应商金额(分账金额)
+            BigDecimal splitAmount = BigDecimal.ZERO;
+            Integer orderId = null;
+            Integer shopId = null;
+            Integer shopOrderId = null;
+            for (SplitAccountVo account : splitAccountList) {
+                splitAmount = MathUtil.add(splitAmount, account.getSplitAccount());
+                orderId = account.getOrderId();
+                shopId = account.getShopId();
+            }
+            //已付供应商金额
+            BigDecimal paidAmount = payDao.findPaidShop(shopOrderId);
+            List<ShopOrderVo> shopOrderList = payDao.findShopOrder(Long.valueOf(orderId));
+            String payStatus = "";
+            //子订单是否全部付款
+            boolean isPay = true;
+            for (ShopOrderVo shopOrder : shopOrderList) {
+                if (shopId.equals(shopOrder.getShopID())) {
+                    shopOrderId = shopOrder.getShopOrderID();
+                    BigDecimal paidShop = MathUtil.add(paidAmount, splitAmount);
+                    if (MathUtil.compare(shopOrder.getShouldPayShopAmount(), paidShop) == 0) {
+                        payStatus = "2";
+                    } else {
+                        payStatus = "3";
+                    }
+                    //修改子订单付款状态及付款金额
+                    payDao.updateShopOrderByPayStatus(shopOrderId, paidShop, payStatus);
+                } else if (!"3".equals(shopOrder.getPayStatus())) {
+                    isPay = false;
+                }
+            }
+            String currentTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
+            //保存付供应商记录
+            PayShopVo payShop = new PayShopVo();
+            payShop.setShopID(shopId);
+            payShop.setName("线上支付分账");
+            payShop.setTotalAmount(splitAmount);
+            payShop.setPayType("6");
+            payShop.setStatus("1");
+            payShop.setDelFlag("0");
+            payShop.setApplyTime(currentTime);
+            payShop.setReviewTime(currentTime);
+
+            payDao.insertPayShop(payShop);
+            PayShopRecordVo shopRecord = new PayShopRecordVo();
+            shopRecord.setShopID(shopId);
+            shopRecord.setShopOrderID(shopOrderId);
+            shopRecord.setPayAmount(splitAmount);
+            shopRecord.setPayType("6");
+            shopRecord.setPayTime(currentTime);
+            shopRecord.setPayShopID(payShop.getId().intValue());
+            shopRecord.setStatus("2");
+            shopRecord.setDelFlag("0");
+            payDao.insertPayShopRecord(shopRecord);
+
+            //修改主订单修改状态
+            if ("3".equals(payStatus) && isPay) {
+                payDao.updateOrderByPayStatus(orderId, payStatus);
+            } else {
+                payDao.updateOrderByPayStatus(orderId, "2");
+            }
+        }
+        return "SUCCESS";
+    }
+
+
 }

+ 104 - 6
pay/src/main/resources/com-caimei-module-pay/PayMapper.xml

@@ -239,12 +239,14 @@
             parameterType="com.caimei.module.base.entity.vo.OrderRelationVo">
         INSERT INTO `cm_receipt_order_relation` (
           `relationType`, `receiptID`, `associateAmount`,
-          `orderID`, `delFlag`, mbOrderId, orderRequestNo
+          `orderID`, `delFlag`, mbOrderId, orderRequestNo,
+          splitStatus
         )
         VALUES
           (
             #{relationType}, #{receiptID}, #{associateAmount},
-            #{orderID}, #{delFlag},#{mbOrderId},#{orderRequestNo}
+            #{orderID}, #{delFlag},#{mbOrderId},#{orderRequestNo},
+            #{splitStatus}
           )
     </insert>
 
@@ -384,14 +386,14 @@
         INSERT INTO `cm_split_account` (
           `orderId`, `productId`, `orderProductId`,
           `shopId`, `type`, `subUserNo`, `splitAccount`,
-          `handlingFee`, `mbOrderId`, `orderRequestNo`,
+          `mbOrderId`, `orderRequestNo`,
           `payStatus`, `productType`
         )
         VALUES
           (
             #{orderId}, #{productId}, #{orderProductId},
             #{shopId}, #{type}, #{subUserNo}, #{splitAccount},
-            #{handlingFee}, #{mbOrderId}, #{orderRequestNo},
+            #{mbOrderId}, #{orderRequestNo},
             #{payStatus}, #{productType}
           );
     </insert>
@@ -435,7 +437,7 @@
 
     <select id="findPaidAmount" resultType="java.math.BigDecimal">
         SELECT
-          SUM(splitAccount + handlingFee)
+          SUM(splitAccount)
         FROM
           cm_split_account
         WHERE
@@ -450,7 +452,7 @@
 
     <select id="findShipping" resultType="java.math.BigDecimal">
         SELECT
-          SUM(splitAccount + handlingFee)
+          SUM(splitAccount)
         FROM
           cm_split_account
         WHERE
@@ -459,4 +461,100 @@
           AND productType = 2
           AND payStatus = 1
     </select>
+
+    <select id="findUnallocatedAccount" resultType="com.caimei.module.base.entity.vo.OrderRelationVo">
+        SELECT
+        cror.*, cdr.payType
+        FROM
+        cm_receipt_order_relation cror
+        LEFT JOIN cm_discern_receipt cdr ON cror.receiptID = cdr.id
+        WHERE
+        cror.relationType = 2
+        AND cdr.payWay = 1
+        AND cror.delFlag = 0
+        AND cror.mbOrderId IS NOT NULL
+        AND cror.splitStatus = 0
+        AND cdr.receiptDate <![CDATA[  <=  ]]> #{currentTime}
+    </select>
+    
+    <select id="findByMbOrderId" resultType="com.caimei.module.base.entity.vo.SplitAccountVo">
+        SELECT
+          *
+        FROM
+          cm_split_account
+        WHERE
+          TYPE = 4
+          AND payStatus = 1
+          AND splitAccount > 0
+          AND mbOrderId = #{mbOrderId}
+    </select>
+
+    <select id="findPaidShop" resultType="java.math.BigDecimal">
+        SELECT
+          SUM(payAmount)
+        FROM
+          cm_pay_shop_record
+        WHERE
+          STATUS = 1
+          AND delFlag = 0
+          AND shopOrderID = #{shopOrderId}
+    </select>
+
+    <insert id="insertPayShop" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
+    insert into cm_pay_shop (shopID, `name`, bankAccountName,
+      bankAccount, bankName, `type`,
+      totalAmount, balancePayFee, transferPayFee,
+      payType, wipePayment, wipeRemarks,
+      wipeRemarkImages, wipeTime, applicant,
+      applyTime, reviewer, reviewTime,
+      payTime, `status`, reason,
+      delFlag)
+    values (#{shopID,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{bankAccountName,jdbcType=VARCHAR},
+      #{bankAccount,jdbcType=VARCHAR}, #{bankName,jdbcType=VARCHAR}, #{type,jdbcType=CHAR},
+      #{totalAmount,jdbcType=DECIMAL}, #{balancePayFee,jdbcType=DECIMAL}, #{transferPayFee,jdbcType=DECIMAL},
+      #{payType,jdbcType=VARCHAR}, #{wipePayment,jdbcType=DECIMAL}, #{wipeRemarks,jdbcType=VARCHAR},
+      #{wipeRemarkImages,jdbcType=VARCHAR}, #{wipeTime,jdbcType=VARCHAR}, #{applicant,jdbcType=INTEGER},
+      #{applyTime,jdbcType=VARCHAR}, #{reviewer,jdbcType=INTEGER}, #{reviewTime,jdbcType=VARCHAR},
+      #{payTime,jdbcType=VARCHAR}, #{status,jdbcType=CHAR}, #{reason,jdbcType=VARCHAR},
+      #{delFlag,jdbcType=VARCHAR})
+    </insert>
+
+    <insert id="insertPayShopRecord" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
+        INSERT INTO `caimei`.`cm_pay_shop_record` (
+          `shopID`, `shopOrderID`, `shopOrderNo`,
+          `payAmount`, `wipePayment`, `payType`,
+          `payTime`, `wipeTime`, `payShopID`,
+          `status`, `delFlag`
+        )
+        VALUES
+          (
+            #{shopID}, #{shopOrderID}, #{shopOrderNo},
+            #{payAmount}, #{wipePayment}, #{payType},
+            #{payTime}, #{wipeTime}, #{payShopID},
+            #{status}, #{delFlag}
+          );
+    </insert>
+
+    <update id="updateBySplitStatus">
+        UPDATE cm_receipt_order_relation SET splitStatus = 1 WHERE mbOrderId = #{mbOrderId}
+    </update>
+    
+    <update id="updateShopOrderByPayStatus">
+        UPDATE
+          cm_shop_order
+        SET
+          payStatus = #{payStatus},
+          payedShopAmount = #{paidShop}
+        WHERE
+          shopOrderID = #{shopOrderId}
+    </update>
+
+    <update id="updateOrderByPayStatus">
+        UPDATE
+          cm_order
+        SET
+          payStatus = #{payStatus}
+        WHERE
+          orderID = #{orderId}
+    </update>
 </mapper>