From 9486766c806fe1d9e082b2fd02ea1cc558f1b443 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期四, 08 五月 2025 09:21:57 +0800
Subject: [PATCH] bug修改

---
 cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java |  167 +++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 142 insertions(+), 25 deletions(-)

diff --git a/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java b/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
index ade018d..6cc0dff 100644
--- a/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
+++ b/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
@@ -4,11 +4,16 @@
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.alipay.api.AlipayClient;
+import com.alipay.api.DefaultAlipayClient;
+import com.alipay.api.request.AlipayTradeOrderSettleRequest;
+import com.alipay.api.response.AlipayTradeOrderSettleResponse;
 import com.alipay.api.response.AlipayTradeQueryResponse;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.dsh.other.entity.*;
+import com.dsh.other.feignclient.StoreClient;
 import com.dsh.other.feignclient.account.AppUserClient;
 import com.dsh.other.feignclient.account.model.AppUser;
 import com.dsh.other.feignclient.activity.CouponClient;
@@ -25,6 +30,7 @@
 import org.aspectj.weaver.ast.Var;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
@@ -69,6 +75,12 @@
 
     @Autowired
     private TBackRecordService backRecordService;
+
+    @Autowired
+    private ISiteBookingService iSiteBookingService;
+
+    @Autowired
+    private ISiteService siteService;
 
 
     /**
@@ -155,10 +167,7 @@
     }
 
 
-    @Autowired
-    private ISiteBookingService iSiteBookingService;
-    @Autowired
-    private ISiteService siteService;
+
 
     /**
      * 获取场地预约日期数据
@@ -174,9 +183,7 @@
         System.out.println("=======date======" + day);
         List<String> strings = new ArrayList<>();
 
-//        List<SiteBooking> siteBookings = siteClient.listBooks(id);
         List<SiteBooking> siteBookings = iSiteBookingService.list(new QueryWrapper<SiteBooking>().eq("siteId", id).ne("status", 5).eq("nextName", siteName).like("times", day));
-
 
         if (siteName == null || siteName.equals("")) {
             siteBookings = iSiteBookingService.list(new QueryWrapper<SiteBooking>().eq("siteId", id).ne("status", 5).like("times", day));
@@ -329,7 +336,6 @@
      */
     @Override
     public ResultUtil reservationSite(Integer uid, ReservationSite reservationSite) throws Exception {
-
         Site site = this.getById(reservationSite.getId());
         AppUser appUser = appUserClient.queryAppUser(uid);
         String[] split = reservationSite.getTimes().split(";");
@@ -360,7 +366,6 @@
                 payMoney = new BigDecimal(site.getPlayPaiCoinOne()).multiply(new BigDecimal(split.length)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
             }
             if (appUser.getPlayPaiCoins().compareTo(payMoney.intValue()) < 0) {
-
                 return new ResultUtil(3, "玩湃币不足");
             }
         } else {
@@ -368,18 +373,14 @@
                 payMoney = new BigDecimal(site.getCashPrice()).multiply(new BigDecimal(split.length)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
             } else {
                 payMoney = new BigDecimal(site.getCashPriceOne()).multiply(new BigDecimal(split.length)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
-
             }
         }
 
         //校验优惠券
         Long couponId = reservationSite.getCouponId();
         if (couponId != null && couponId != 0) {
-
             if (null != couponId && reservationSite.getPayType() != 3) {
                 UserCoupon userCoupon = userCouponClient.queryUserCouponById(new QueryUserCouponByIdAndUserId(reservationSite.getCouponId(), uid));
-
-
                 if (userCoupon != null && userCoupon.getStatus() == 2) {
                     return ResultUtil.error("优惠券已被核销");
                 }
@@ -389,7 +390,6 @@
                     return ResultUtil.error("优惠券已过期");
                 }
                 if (userCoupon != null && coupon.getType() == 1) {//满减
-//                {"conditionalAmount":50,"deductionAmount":10,"experienceName":""}
                     JSONObject jsonObject = JSON.parseObject(coupon.getContent());
                     Double num1 = jsonObject.getDouble("conditionalAmount");
                     Double num2 = jsonObject.getDouble("deductionAmount");
@@ -434,7 +434,6 @@
         siteBooking.setState(1);
         siteBooking.setInsertTime(new Date());
 
-        // 2.0
         siteBooking.setNextName(reservationSite.getNextName());
         siteBooking.setIsHalf(reservationSite.getIsHalf());
         siteBooking.setHalfName(reservationSite.getHalfName());
@@ -443,7 +442,6 @@
 
         if (reservationSite.getPayType() == 1) {//微信支付
             ResultUtil resultUtil = weChatPaymentSite(payMoney, siteBooking);
-
             return resultUtil;
         }
         if (reservationSite.getPayType() == 2) {//支付宝支付
@@ -470,7 +468,16 @@
     public ResultUtil weChatPaymentSite(Double paymentPrice, SiteBooking siteBooking) throws Exception {
         String code = siteBooking.getOrderNo();
         Integer id = siteBooking.getId();
-        ResultUtil weixinpay = payMoneyUtil.weixinpay("预约场地", id.toString(), code, paymentPrice.toString(), "/base/site/weChatPaymentSiteCallback", "APP", "");
+        Site byId = siteService.getById(siteBooking.getSiteId());
+        // 是否分账 0否1是
+        int isFenZhang= 1;
+        String merchantNumber = "";
+        if (byId.getOperatorId()==null || byId.getOperatorId()==0){
+            // 平台
+            isFenZhang = 0;
+        }
+        ResultUtil weixinpay = payMoneyUtil.weixinpay("预约场地-"+isFenZhang, id.toString(), code, paymentPrice.toString(),
+                "/base/site/weChatPaymentSiteCallback", "APP", "");
         if (weixinpay.getCode() == 200) {
             new Thread(new Runnable() {
                 @Override
@@ -500,7 +507,7 @@
                                 Map<String, String> data1 = resultUtil.getData();
                                 String s = data1.get("trade_state");
                                 String transaction_id = data1.get("transaction_id");
-                                if ("REFUND".equals(s) || "NOTPAY".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s) || num == 10) {
+                                if ("REFUND".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s) || num == 10) {
                                     break;
                                 }
                                 if ("SUCCESS".equals(s)) {
@@ -508,9 +515,12 @@
                                     siteBooking.setStatus(1);
                                     siteBooking.setPayOrderNo(transaction_id);
                                     siteBookingService.updateById(siteBooking);
+
+
+
                                     break;
                                 }
-                                if ("USERPAYING".equals(s)) {
+                                if ("USERPAYING".equals(s) || "NOTPAY".equals(s)) {
                                     num++;
                                 }
                             }
@@ -524,7 +534,11 @@
         return weixinpay;
     }
 
-
+    private String smid = "2088330203191220";//平台支付宝商户号
+    @Autowired
+    private IOperatorUserService operatorUserService;
+    @Autowired
+    private StoreClient storeClient;
     /**
      * 课程支付宝支付
      *
@@ -535,7 +549,21 @@
     public ResultUtil aliPaymentSite(Double paymentPrice, SiteBooking siteBooking) throws Exception {
         String code = siteBooking.getOrderNo();
         Integer id = siteBooking.getId();
-        ResultUtil alipay = payMoneyUtil.alipay("预约场地", "预约场地", id.toString(), code, paymentPrice.toString(), "/base/site/aliPaymentSiteCallback");
+        // 判断预约的门店 属于哪个运营商
+        Integer storeId = siteBooking.getStoreId();
+        Store byId = storeService.getById(storeId);
+        Integer operatorId = byId.getOperatorId();
+        String smid1 = "";
+        if (operatorId == null ){
+            // 平台的门店
+            smid1 = smid;
+        }else if (operatorId == 0){
+            smid1 = smid;
+        }else{
+            smid1 = siteService.getSMIDByOperatorId(operatorId);
+
+        }
+        ResultUtil alipay = payMoneyUtil.alipay(smid1,"预约场地", "预约场地", id.toString(), code, paymentPrice.toString(), "/base/site/aliPaymentSiteCallback");
         System.out.println("预约场地----" + alipay.getCode());
         if (alipay.getCode() == 200) {
             new Thread(new Runnable() {
@@ -574,6 +602,21 @@
                                     siteBooking.setPayOrderNo(tradeNo);
                                     siteBookingService.updateById(siteBooking);
                                     System.err.println("======完成支付");
+                                    // 判断预约的门店 属于哪个运营商
+                                    Integer storeId = siteBooking.getStoreId();
+                                    Store byId = storeService.getById(storeId);
+                                    Integer operatorId = byId.getOperatorId();
+                                    String smid1 = "";
+                                    if (operatorId == null || operatorId == 0){
+                                        // 平台的门店 不冻结资金不做分账处理
+                                        payMoneyUtil.confirm1(smid1,code,tradeNo,paymentPrice.toString());
+                                    }else{
+                                        smid1 = siteService.getSMIDByOperatorId(operatorId);
+                                        payMoneyUtil.confirm(smid1,code,tradeNo,paymentPrice.toString());
+                                        // 分账
+                                        extracted(operatorId, new BigDecimal(paymentPrice.toString()), tradeNo);
+                                    }
+
                                     break;
                                 }
                                 if ("WAIT_BUYER_PAY".equals(s)) {
@@ -590,7 +633,53 @@
         return alipay;
     }
 
+    private void extracted(Integer operatorId, BigDecimal coursePackageOrder1, String tradeNo) throws Exception {
+        // 分账
+        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
+                "2021004105665036",
+                "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCi5i9nW/hGLJ3A06cZxTQdviFC7THpdSihoTYGLr9q006hu0V26ecBMY/o4w5bvIX0Ok/yofmZsVcCJpAPvbXL/uqVrIjnRRxXiaeBFThlxoBUTdunvbUSDYfzlEhJr5NvUKI6H6lz2niXlQGx4qy8Hau4ccWit9kM8jwUvsBVQoFgJA+xrjMvooA7YLopQtpOD+UJr5thApTSf1xrnr1W12yolTLEH15JmNV372cqXrYUuqnY0QsaPtxeqJUGAOcGdVLllQ7easEznP8DFBvDdHATcmp2SHNQDUEWN6MCVPbMgY06NQVqAXxqjTAYSVh+6TRu6bofPmpYC3TZB003AgMBAAECggEBAJAcR2+PA3NBYUYHeFrqBRMS8uX8ZR19kjZ7IgoSLTFaQsP9opRylPSPXhrPVBKAE5leRQAHn4MCSlESwHvMfxo7KFjFTFAc6dffZZpipYQUOc9bGampwJh58/3e/pyBgVMG6J23CPf/HJQtNFSkjd/V9+ayb/9l2dUEL3bC0fAZ/dbx8HsxdLw8wn3fLlWLj68hO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KCAQEAmu8n/4yTHWbn7VOrNc9OsLtDL1bEQ8gC1dHkj8Wy5z0mkaOsjJRIG/28ze12M0V8jdCKuuDr5Z1OPKiqf+XO3ypguEh+mYUVMBM/cZodDFQfTY1TKLWjvQCuaqlA+QUTCK6f7T7stsgyQ1o9Jj0rXZDz6PM4QHSTzjrLIBaeqM5WIBvH+fy/X+QG5Utd+/UT0kc0JyvuKhZ65yVUd/C9VcwJJAPliRsAQNrqYterwAJ9zvw9tF11wj9W0XgJ8Ccu4x3gR1vrlLRJJo/OA97RmxPQ+5hSacWQZCUd1dwiBq+YCrKVHGTj14izRHXrLc0yBlRXo7tBOIqcy3IsvKVthQIDAQAB",
+                "RSA2");
+        AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();
+        String randomCode = UUIDUtil.getRandomCode(16);
 
+        String proportionByOperatorId = operatorUserService.getOne(new QueryWrapper<OperatorUser>()
+                .eq("operatorId",operatorId)).getAlipayProportion();
+        // 支付宝分账比例
+        String s1 = proportionByOperatorId.split(",")[1];
+        double i = Double.parseDouble(s1);
+        double v = i * 0.01;
+        BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(v));
+        // 分账金额
+        BigDecimal bigDecimal2 = coursePackageOrder1.multiply(bigDecimal1).setScale(2, RoundingMode.DOWN);
+        request.setBizContent("{" +
+                "  \"out_request_no\":\"" + randomCode + "\"," +
+                "  \"trade_no\":\"" + tradeNo + "\"," +
+                "  \"royalty_parameters\":[" +
+                "    {" +
+                "      \"royalty_type\":\"transfer\"," +
+                "      \"trans_out\":\"" + storeClient.getSMIDByOperatorId(operatorId)+ "\"," +
+                "      \"trans_out_type\":\"userId\"," +
+                "      \"trans_in_type\":\"userId\"," +
+                "      \"trans_in\":\"" + smid + "\"," +
+                "      \"amount\":" + bigDecimal2 + "," +
+                "      \"desc\":\"平台服务费\"," +
+                "      \"royalty_scene\":\"平台服务费\"," +
+                "    }" +
+                "  ]," +
+                "  \"extend_params\":{" +
+                "    \"royalty_finish\":\"true\"" +
+                "  }," +
+                "  \"royalty_mode\":\"async\"" +
+                "}");
+        AlipayTradeOrderSettleResponse response = alipayClient.execute(request);
+        if (response.isSuccess()) {
+            System.out.println("调用成功");
+        } else {
+            System.out.println("调用失败");
+        }
+    }
     /**
      * 玩湃币支付
      *
@@ -654,7 +743,7 @@
     @Override
     public ResultUtil cancelMySite(Integer uid, Integer id) throws Exception {
         SiteBooking siteBooking = siteBookingService.getById(id);
-
+        Site byId = siteService.getById(siteBooking.getSiteId());
         SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
         Date startTime = siteBooking.getStartTime();
         if (format.format(new Date()).equals(format.format(startTime)) && siteBooking.getStatus() != 0) {
@@ -662,7 +751,6 @@
         }
 
         if (siteBooking.getStatus() == 0) {
-//            return ResultUtil.error("请先进行支付");
             siteBooking.setStatus(5);
             siteBookingService.updateById(siteBooking);
             AppUser appUser = appUserClient.queryAppUser(siteBooking.getAppUserId());
@@ -679,12 +767,32 @@
             return ResultUtil.error("不能进行取消操作");
         }
         if (siteBooking.getPayType() == 1) {//微信支付
-            Map<String, String> map = payMoneyUtil.wxRefund(siteBooking.getPayOrderNo(), siteBooking.getOrderNo(), siteBooking.getPayMoney().toString(), siteBooking.getPayMoney().toString(), "/base/site/cancelMySiteCallback");
+            if (StringUtils.hasLength(siteBooking.getFenzhangNo())){
+                // 是分账订单 如果分账金额不为0 那么回退分账金额
+                if (siteBooking.getFenzhangAmount()!=null && siteBooking.getFenzhangAmount().compareTo(BigDecimal.ZERO)>0){
+                    String randomCode = UUIDUtil.getRandomCode(16);
+                    String randomCode1 = UUIDUtil.getRandomCode(16);
+                    if (byId.getOperatorId()!=null && byId.getOperatorId()!=0){
+                        // 微信商户号
+                        String s2 =siteService.getmerchantNumberByOperatorId(byId.getOperatorId());
+                        ResultUtil resultUtil = payMoneyUtil.fenzhangRefund(siteBooking.getFenzhangNo(), siteBooking.getFenzhangAmount().multiply(new BigDecimal("100")), s2, randomCode, randomCode1);
+                        if (!resultUtil.getCode().equals(200)){
+                            System.err.println("分账回退失败 原因是:"+resultUtil.getMsg()+resultUtil.getData());
+                        }else{
+                            siteBooking.setFenzhangRefundNo(resultUtil.getMsg());
+                            siteBookingService.updateById(siteBooking);
+                        }
+
+                    }
+                }
+            }
+            Map<String, String> map = payMoneyUtil.wxRefund(siteBooking.getPayOrderNo(), siteBooking.getOrderNo(), siteBooking.getPayMoney().toString(),
+                    siteBooking.getPayMoney().toString(), "/base/site/cancelMySiteCallback");
             if (null == map) {
                 return ResultUtil.error("取消退款异常");
             }
-            String result_code = map.get("result_code");
-            if (!"SUCCESS".equals(result_code)) {
+            String return_code = map.get("return_code");
+            if (!"SUCCESS".equals(return_code)) {
                 return ResultUtil.error(map.get("return_msg"));
             }
             siteBooking.setCancelUserId(uid);
@@ -894,4 +1002,13 @@
         return siteMapper.changeState(siteIds, type);
 
     }
+
+    @Override
+    public String getSMIDByOperatorId(Integer id) {
+        return siteMapper.getSMIDByOperatorId(id);
+    }
+    @Override
+    public String getmerchantNumberByOperatorId(Integer id) {
+        return siteMapper.getmerchantNumberByOperatorId(id);
+    }
 }

--
Gitblit v1.7.1