From f6d15260d3fdcb326869f541a266bc4688a1b101 Mon Sep 17 00:00:00 2001
From: zhibing.pu <393733352@qq.com>
Date: 星期一, 24 六月 2024 15:07:07 +0800
Subject: [PATCH] 优化

---
 cloud-server-competition/src/main/java/com/dsh/competition/service/impl/CompetitionServiceImpl.java |   78 ++++++++++++++++++++++++---------------
 1 files changed, 48 insertions(+), 30 deletions(-)

diff --git a/cloud-server-competition/src/main/java/com/dsh/competition/service/impl/CompetitionServiceImpl.java b/cloud-server-competition/src/main/java/com/dsh/competition/service/impl/CompetitionServiceImpl.java
index 81c645a..9159174 100644
--- a/cloud-server-competition/src/main/java/com/dsh/competition/service/impl/CompetitionServiceImpl.java
+++ b/cloud-server-competition/src/main/java/com/dsh/competition/service/impl/CompetitionServiceImpl.java
@@ -1,5 +1,8 @@
 package com.dsh.competition.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.alibaba.nacos.common.utils.UuidUtils;
 import com.alipay.api.AlipayApiException;
 import com.alipay.api.AlipayClient;
@@ -75,6 +78,10 @@
 
     @Autowired
     private PayMoneyUtil payMoneyUtil;
+
+
+    @Autowired
+    private CompetitionService cttService;
 
 
     private String aliAppid = "2021004105665036";//支付宝appid
@@ -226,10 +233,11 @@
     @Override
     public ResultUtil paymentCompetition(Integer uid, PaymentCompetitionVo paymentCompetitionVo) throws Exception {
         AppUser appUser = appUserClient.queryAppUser(uid);
-        String[] split = paymentCompetitionVo.getIds().split(";");
+        JSONArray jsonArray = JSON.parseArray(paymentCompetitionVo.getIds());
         Competition competition = this.getById(paymentCompetitionVo.getId());
 
-        List<PaymentCompetition> list = paymentCompetitionService.list(new QueryWrapper<PaymentCompetition>().eq("competitionId", paymentCompetitionVo.getId()).ne("payStatus", 3));
+        List<PaymentCompetition> list = paymentCompetitionService.list(new QueryWrapper<PaymentCompetition>()
+                .eq("competitionId", paymentCompetitionVo.getId()).ne("payStatus", 3));
         List<Long> pays = new ArrayList<>();
         for (PaymentCompetition paymentCompetition : list) {
             pays.add(paymentCompetition.getId());
@@ -241,34 +249,24 @@
         }
 
 
-//        int competitionId = userCompetitionService.count(new QueryWrapper<UserCompetition>().eq("competitionId", paymentCompetitionVo.getId()));
-        if ((competitionId + split.length) > competition.getApplicantsNumber()) {
+        if ((competitionId + jsonArray.size()) > competition.getApplicantsNumber()) {
             return ResultUtil.error("报名人数超过最大限制");
         }
         BigDecimal money = null;
         if (paymentCompetitionVo.getPayType() == 1 || paymentCompetitionVo.getPayType() == 2) {
-            money = new BigDecimal(competition.getCashPrice()).multiply(new BigDecimal(split.length)).setScale(2, RoundingMode.HALF_EVEN);
+            money = new BigDecimal(competition.getCashPrice()).multiply(new BigDecimal(jsonArray.size())).setScale(2, RoundingMode.HALF_EVEN);
         }
         if (paymentCompetitionVo.getPayType() == 3) {//玩湃币
-            money = new BigDecimal(competition.getPlayPaiCoin()).multiply(new BigDecimal(split.length)).setScale(2, RoundingMode.HALF_EVEN);
+            money = new BigDecimal(competition.getPlayPaiCoin()).multiply(new BigDecimal(jsonArray.size())).setScale(2, RoundingMode.HALF_EVEN);
             if (money.compareTo(new BigDecimal(appUser.getPlayPaiCoins())) > 0) {
                 return new ResultUtil(2, "报名失败,玩湃币不足,请充值");
             }
         }
         if (paymentCompetitionVo.getPayType() == 4) {//课程
-            money = new BigDecimal(competition.getClassPrice()).multiply(new BigDecimal(split.length)).setScale(2, RoundingMode.HALF_EVEN);
-            for (String s : split) {
-//                Participant participant = participantService.getById(s);
-//                Student student = studentClient.queryStudentByPhone(participant.getPhone());
-//                if(null == student){
-//                    return ResultUtil.error(participant.getName() + "不是学员,无法使用课时支付。");
-//                }
-                // 2.0
-                Integer integer = coursePackagePaymentClient.queryResidueClassHourById(paymentCompetitionVo.getCoursePaymentId());
-                if (new BigDecimal(integer).compareTo(new BigDecimal(competition.getClassPrice())) < 0) {
-                    return new ResultUtil(3, "剩余课时不足,无法完成支付。");
-
-                }
+            money = new BigDecimal(competition.getClassPrice()).multiply(new BigDecimal(jsonArray.size())).setScale(2, RoundingMode.HALF_EVEN);
+            Integer integer = coursePackagePaymentClient.queryResidueClassHourById(paymentCompetitionVo.getCoursePaymentId());
+            if (new BigDecimal(integer).compareTo(money) < 0) {
+                return new ResultUtil(3, "剩余课时不足,无法完成支付。");
             }
         }
 
@@ -285,11 +283,15 @@
         paymentCompetition.setInsertTime(new Date());
         paymentCompetitionService.save(paymentCompetition);
 
-        for (String s : split) {
+        for (int i = 0; i < jsonArray.size(); i++) {
+            JSONObject jsonObject = jsonArray.getJSONObject(i);
+            Integer id = jsonObject.getInteger("id");
+            Integer isStudent = jsonObject.getInteger("isStudent");
             UserCompetition userCompetition = new UserCompetition();
             userCompetition.setAppUserId(uid);
             userCompetition.setCompetitionId(paymentCompetitionVo.getId());
-            userCompetition.setParticipantId(Integer.valueOf(s));
+            userCompetition.setParticipantId(id);
+            userCompetition.setParticipantType(isStudent == 0 ? 2 : 1);
             userCompetition.setPaymentCompetitionId(paymentCompetition.getId());
             userCompetition.setInsertTime(new Date());
             userCompetitionService.save(userCompetition);
@@ -321,21 +323,26 @@
             paymentCompetition.setPayOrderNo("");
             paymentCompetitionService.updateById(paymentCompetition);
 
-//            competition.setApplicantsNumber(competition.getApplicantsNumber() + 1);
             this.updateById(competition);
         }
         if (paymentCompetitionVo.getPayType() == 4) {//课程
-            for (String s : split) {
-//                Participant participant = participantService.getById(s);
-//                Student student = studentClient.queryStudentByPhone(participant.getPhone());
+            for (int i = 0; i < jsonArray.size(); i++) {
+                JSONObject jsonObject = jsonArray.getJSONObject(i);
+                Integer id = jsonObject.getInteger("id");
+                Integer isStudent = jsonObject.getInteger("isStudent");
+                if(isStudent == 0){
+                    continue;
+                }
+                //扣减课时
                 PaymentDeductionClassHour paymentDeductionClassHour = new PaymentDeductionClassHour();
-                paymentDeductionClassHour.setId(Integer.valueOf(s));
+                paymentDeductionClassHour.setId(id);
                 paymentDeductionClassHour.setClassHour(competition.getClassPrice());
                 paymentDeductionClassHour.setCode(code);
                 paymentDeductionClassHour.setCourseId(paymentCompetitionVo.getCoursePaymentId());
                 paymentDeductionClassHour.setUid(uid);
                 coursePackagePaymentClient.paymentDeductionClassHour(paymentDeductionClassHour);
             }
+
             paymentCompetition = paymentCompetitionService.getById(paymentCompetition.getId());
             paymentCompetition.setAppUserId(null);
             paymentCompetition.setPayStatus(2);
@@ -359,7 +366,8 @@
      * @throws Exception
      */
     public ResultUtil weChatPaymentCompetition(String code, BigDecimal money) throws Exception {
-        ResultUtil weixinpay = payMoneyUtil.weixinpay("报名赛事", "", code, money.toString(), "/base/competition/weChatPaymentCompetitionCallback", "APP", "");
+        ResultUtil weixinpay = payMoneyUtil.weixinpay("报名赛事", "", code, money.toString(),
+                "/base/competition/weChatPaymentCompetitionCallback", "APP", "");
         if (weixinpay.getCode() == 200) {
             new Thread(new Runnable() {
                 @Override
@@ -371,7 +379,8 @@
                             int min = 5000;
                             wait += (min * num);
                             Thread.sleep(wait);
-                            PaymentCompetition paymentCompetition = paymentCompetitionService.getOne(new QueryWrapper<PaymentCompetition>().eq("code", code).eq("payType", 1));
+                            PaymentCompetition paymentCompetition = paymentCompetitionService.getOne(new QueryWrapper<PaymentCompetition>()
+                                    .eq("code", code).eq("payType", 1));
                             if (paymentCompetition.getPayStatus() == 2) {
                                 break;
                             }
@@ -389,7 +398,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) {
                                     paymentCompetition.setAppUserId(null);
                                     paymentCompetition.setState(3);
                                     userCompetitionService.remove(new QueryWrapper<UserCompetition>().eq("paymentCompetitionId", paymentCompetition.getId()));
@@ -401,9 +410,13 @@
                                     paymentCompetition.setPayTime(new Date());
                                     paymentCompetition.setPayOrderNo(transaction_id);
                                     paymentCompetitionService.updateById(paymentCompetition);
+
+                                    Competition competition = cttService.getById(paymentCompetition.getCompetitionId());
+                                    competition.setApplicantsNumber(competition.getApplicantsNumber() + 1);
+                                    cttService.updateById(competition);
                                     break;
                                 }
-                                if ("USERPAYING".equals(s)) {
+                                if ("USERPAYING".equals(s) || "NOTPAY".equals(s)) {
                                     num++;
                                 }
                             }
@@ -471,6 +484,11 @@
                                     paymentCompetition.setPayTime(new Date());
                                     paymentCompetition.setPayOrderNo(tradeNo);
                                     paymentCompetitionService.updateById(paymentCompetition);
+
+                                    Competition competition = cttService.getById(paymentCompetition.getCompetitionId());
+                                    competition.setApplicantsNumber(competition.getApplicantsNumber() + 1);
+                                    cttService.updateById(competition);
+
                                     // 结算资金到商户账号
                                     payMoneyUtil.confirm(smid,code,tradeNo,money.toString());
                                     //分账

--
Gitblit v1.7.1