From ac3e9a7f0b21fb630049f9e191bd760eefd21467 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期六, 16 八月 2025 18:04:38 +0800
Subject: [PATCH] 管理后台会员管理

---
 cloud-server-account/src/main/java/com/dsh/account/service/impl/VipPaymentServiceImpl.java |  172 +++++++++++++++++++++++++++++----------------------------
 1 files changed, 87 insertions(+), 85 deletions(-)

diff --git a/cloud-server-account/src/main/java/com/dsh/account/service/impl/VipPaymentServiceImpl.java b/cloud-server-account/src/main/java/com/dsh/account/service/impl/VipPaymentServiceImpl.java
index f74c4f8..55b5a3d 100644
--- a/cloud-server-account/src/main/java/com/dsh/account/service/impl/VipPaymentServiceImpl.java
+++ b/cloud-server-account/src/main/java/com/dsh/account/service/impl/VipPaymentServiceImpl.java
@@ -1,25 +1,37 @@
 package com.dsh.account.service.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.alipay.api.response.AlipayTradeQueryResponse;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.dsh.account.entity.TAppUser;
+import com.dsh.account.entity.Vip;
 import com.dsh.account.entity.VipPayment;
 import com.dsh.account.feignclient.activity.UserConponClient;
+import com.dsh.account.feignclient.activity.UserCouponPaymentClient;
+import com.dsh.account.feignclient.activity.model.GrantCoupon;
+import com.dsh.account.feignclient.other.VipClient;
 import com.dsh.account.mapper.VipPaymentMapper;
 import com.dsh.account.model.IncomeQuery;
 import com.dsh.account.service.IVipPaymentService;
 import com.dsh.account.service.TAppUserService;
+import com.dsh.account.util.DateUtil;
 import com.dsh.account.util.PayMoneyUtil;
 import com.dsh.account.util.RedisUtil;
 import com.dsh.account.util.ResultUtil;
+import com.dsh.account.util.wx.WxV3PayConfig;
+import com.dsh.account.vo.CouponVipResp;
+import org.apache.commons.lang.time.DateUtils;
 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;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
@@ -43,6 +55,9 @@
 
     @Autowired
     private RedisUtil redisUtil;
+    @Autowired
+    private VipClient vipClient;
+
 
 
     /**
@@ -53,14 +68,26 @@
      * @throws Exception
      */
     @Override
-    public ResultUtil addVipPayment(Integer uid, Integer payType) throws Exception {
+    public ResultUtil addVipPayment(Integer uid, Integer payType, Integer vipId) throws Exception {
+        if (vipId == null) {
+            return ResultUtil.error("会员id未传参");
+        }
+        List<Vip> vipByIds = vipClient.getVipByIds(vipId.toString());
+        if (vipByIds.isEmpty()) {
+            return ResultUtil.error("会员不存在");
+        }
+        Vip vip = vipByIds.get(0);
         VipPayment vipPayment = new VipPayment();
         vipPayment.setCode(VipPayment.CODE());
-        vipPayment.setAmount(0.01D);
+        vipPayment.setAmount(vip.getPrice().doubleValue());
         vipPayment.setAppUserId(uid);
         vipPayment.setInsertTime(new Date());
         vipPayment.setPayStatus(1);
         vipPayment.setState(1);
+        vipPayment.setPayType(payType);
+        vipPayment.setVipId(vip.getId());
+        vipPayment.setCouponJson(vip.getCouponJson());
+        vipPayment.setTicketJson(vip.getTicketJson());
         this.baseMapper.insert(vipPayment);
         if (payType == 1) {//微信
             return weixinpay(vipPayment);
@@ -81,86 +108,13 @@
      */
     public ResultUtil weixinpay(VipPayment vipPayment) throws Exception {
         String code = vipPayment.getCode();
-        ResultUtil weixinpay = payMoneyUtil.weixinpay("购买年度会员", "", code, vipPayment.getAmount().toString(),
+        ResultUtil weixinpay = payMoneyUtil.weixinpay("购买会员", "", code, vipPayment.getAmount().toString(),
                 "/base/appUser/addVipPaymentWeChatCallback", "APP", "");
-        if (weixinpay.getCode() == 200) {
-            new Thread(new Runnable() {
-                @Override
-                public void run() {
-                    try {
-                        int num = 1;
-                        int wait = 0;
-                        while (num <= 10) {
-                            int min = 5000;
-                            wait += (min * num);
-                            VipPayment vipPayment1 = VipPaymentServiceImpl.this.getOne(new QueryWrapper<VipPayment>().eq("code", code));
-                            if (vipPayment1.getPayStatus() == 2) {
-                                return;
-                            }
-                            /**
-                             * SUCCESS--支付成功
-                             * REFUND--转入退款
-                             * NOTPAY--未支付
-                             * CLOSED--已关闭
-                             * REVOKED--已撤销(刷卡支付)
-                             * USERPAYING--用户支付中
-                             * PAYERROR--支付失败(其他原因,如银行返回失败)
-                             * ACCEPT--已接收,等待扣款
-                             */
-                            ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryWXOrder(code, "");
-                            if (resultUtil.getCode() == 200) {
-                                Map<String, String> map = resultUtil.getData();
-                                String trade_type = map.get("trade_type");
-                                String trade_state = map.get("trade_state");
-                                String transaction_id = map.get("transaction_id");
-                                if ("REFUND".equals(trade_state) || "NOTPAY".equals(trade_state) || "CLOSED".equals(trade_state) || "REVOKED".equals(trade_state) || "PAYERROR".equals(trade_state)) {
-                                    vipPayment1.setState(3);
-                                    VipPaymentServiceImpl.this.updateById(vipPayment1);
-                                    return;
-                                }
-                                if ("SUCCESS".equals(trade_state)) {
-                                    vipPayment1.setPayStatus(2);
-                                    vipPayment1.setPayTime(new Date());
-                                    vipPayment1.setOrderNumber(transaction_id);
-                                    VipPaymentServiceImpl.this.updateById(vipPayment1);
-                                    TAppUser appUser = appUserService.getById(vipPayment1.getAppUserId());
-                                    Date vipEndTime = appUser.getVipEndTime();
-                                    if (null == vipEndTime) {
-                                        vipEndTime = new Date();
-                                    }
-                                    Calendar calendar = Calendar.getInstance();
-                                    calendar.setTime(vipEndTime);
-                                    calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
-                                    appUser.setIsVip(1);
-                                    appUser.setVipEndTime(calendar.getTime());
-                                    appUserService.updateById(appUser);
-                                    // 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限
-                                    List<Long> longs = userConponClient.queryCouponByUser(appUser.getId());
-                                    redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600);
-                                    return;
-                                }
-                                if ("USERPAYING".equals(trade_state) || "ACCEPT".equals(trade_state)) {
-                                    Thread.sleep(wait);
-                                    num++;
-                                }
-                            } else {
-                                Thread.sleep(wait);
-                                num++;
-                            }
-                            if (10 == num) {
-                                vipPayment1.setState(3);
-                                VipPaymentServiceImpl.this.updateById(vipPayment1);
-                            }
-                        }
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                    }
-                }
-            }).start();
-        }
+//        ResultUtil weixinpay = payMoneyUtil.weixinpayV3(WxV3PayConfig.smidVx,"购买年度会员",
+//                code,"/base/appUser/addVipPaymentWeChatCallback1",
+//                Long.valueOf(vipPayment.getAmount().toString()));
         return weixinpay;
     }
-
 
     /**
      * 支付宝
@@ -169,10 +123,16 @@
      * @return
      * @throws Exception
      */
+    private String smid = "2088330203191220";//平台支付宝商户号
+
     public ResultUtil alipay(VipPayment vipPayment) throws Exception {
         String code = vipPayment.getCode();
-        ResultUtil alipay = payMoneyUtil.alipay("购买年度会员", "购买年度会员", vipPayment.getAppUserId() + "", code, vipPayment.getAmount().toString(),
-                "/base/appUser/addVipPaymentAliCallback");
+        String amount = vipPayment.getAmount().toString();
+        ResultUtil alipay = payMoneyUtil.alipay
+                (smid, "购买年度会员", "购买年度会员",
+                        vipPayment.getAppUserId() + "",
+                        code, vipPayment.getAmount().toString(),
+                        "/base/appUser/addVipPaymentAliCallback");
         if (alipay.getCode() == 200) {
             new Thread(new Runnable() {
                 @Override
@@ -204,6 +164,7 @@
                                 if ("TRADE_CLOSED".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus)) {
                                     vipPayment1.setState(3);
                                     VipPaymentServiceImpl.this.updateById(vipPayment1);
+
                                     return;
                                 }
                                 if ("TRADE_SUCCESS".equals(tradeStatus)) {
@@ -226,6 +187,8 @@
                                     // 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限
                                     List<Long> longs = userConponClient.queryCouponByUser(appUser.getId());
                                     redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600);
+                                    Thread.sleep(30000);
+                                    payMoneyUtil.confirm(smid, code, tradeNo, amount);
                                     return;
                                 }
                                 if ("WAIT_BUYER_PAY".equals(tradeStatus)) {
@@ -274,12 +237,51 @@
         if (null == vipEndTime) {
             vipEndTime = new Date();
         }
-        Calendar calendar = Calendar.getInstance();
-        calendar.setTime(vipEndTime);
-        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
+        // 将Date类型 转化为LocalDateTime
+        LocalDateTime localDateTime = LocalDateTime.ofInstant(vipEndTime.toInstant(), ZoneId.systemDefault());
+        List<Vip> vipByIds = vipClient.getVipByIds(vipPayment1.getVipId() + "");
+        if (vipByIds.isEmpty()) {
+            return ResultUtil.error("会员不存在");
+        }
+        Vip vip = vipByIds.get(0);
+        Integer time = vip.getTime();
+
+        // 根据time和时间类型 决定给会员加多少天
+        switch (vip.getTimeType()) {
+            case 1:
+                // 天
+                localDateTime.plusDays(time);
+                break;
+            case 2:
+                // 月
+                localDateTime.plusMonths(time);
+
+                break;
+            case 3:
+                // 年
+                localDateTime.plusYears(time);
+                break;
+        }
         appUser.setIsVip(1);
-        appUser.setVipEndTime(calendar.getTime());
+        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
+        appUser.setVipEndTime(date);
         appUserService.updateById(appUser);
+        // 会员权益送券和门票
+        String couponJson = vipPayment1.getCouponJson();
+        GrantCoupon grantCoupon = new GrantCoupon();
+
+        if (StringUtils.hasLength(couponJson)) {
+            grantCoupon.setCouponJson(couponJson);
+            grantCoupon.setUserId(vipPayment1.getAppUserId());
+            grantCoupon.setVipPaymentId(vipPayment1.getId());
+        }
+        String ticketJson = vipPayment1.getTicketJson();
+        if (StringUtils.hasLength(ticketJson)) {
+            grantCoupon.setTicketJson(ticketJson);
+        }
+        // 远程调用 给用户送优惠券和门票
+        userConponClient.granCoupon(grantCoupon);
+
         // 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限
         List<Long> longs = userConponClient.queryCouponByUser(appUser.getId());
         redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600);

--
Gitblit v1.7.1