Browse Source

机构类型

TRX 1 year ago
parent
commit
52bdc2a35e

+ 19 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/payment/WalletRechargeModel.java

@@ -1,6 +1,7 @@
 package com.zhongshu.card.client.model.payment;
 
 import com.zhongshu.card.client.model.base.SuperModel;
+import com.zhongshu.card.client.model.org.UserCountModel;
 import com.zhongshu.card.client.utils.type.RechargeType;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
@@ -20,9 +21,18 @@ public class WalletRechargeModel extends SuperModel {
     @Schema(description = "关联钱包信息")
     private WalletModel wallet;
 
+    @Schema(description = "用户信息")
+    private UserCountModel userCount;
+
     @Schema(description = "用户userId")
     private String userId;
 
+    @Schema(description = "用户名称")
+    private String userName;
+
+    @Schema(description = "用户电话号码")
+    private String phone;
+
     @Schema(description = "充值类型:")
     private RechargeType rechargeType;
 
@@ -46,4 +56,13 @@ public class WalletRechargeModel extends SuperModel {
 
     @Schema(description = "关联的退款订单数据ID")
     private String aboutDataId;
+
+    @Schema(description = "年份,如:2024")
+    private String year;
+
+    @Schema(description = "月份,如06")
+    private String month;
+
+    @Schema(description = "可阅读的时间")
+    private String timeStr;
 }

+ 1 - 1
FullCardClient/src/main/java/com/zhongshu/card/client/model/payment/WalletRechargeParam.java

@@ -19,6 +19,6 @@ public class WalletRechargeParam extends SuperParam {
     @Schema(description = "用户ID")
     private String userId;
 
-    @Schema(description = "充值金额")
+    @Schema(description = "充值金额:单位:分")
     private BigDecimal amount;
 }

+ 85 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/controller/payment/WalletController.java

@@ -0,0 +1,85 @@
+package com.zhongshu.card.server.core.controller.payment;
+
+import com.github.microservice.auth.security.annotations.ResourceAuth;
+import com.github.microservice.auth.security.helper.AuthHelper;
+import com.github.microservice.auth.security.type.AuthType;
+import com.zhongshu.card.client.model.base.IDParam;
+import com.zhongshu.card.client.model.payment.*;
+import com.zhongshu.card.client.model.school.DeviceInfoModel;
+import com.zhongshu.card.client.model.school.DeviceInfoSearch;
+import com.zhongshu.card.client.ret.ResultContent;
+import com.zhongshu.card.server.core.domain.payment.Wallet;
+import com.zhongshu.card.server.core.service.payment.WalletService;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @author TRX
+ * @date 2024/6/29
+ */
+@Slf4j
+@RestController
+@RequestMapping("wallet")
+@Tag(name = "个人-钱包")
+public class WalletController {
+
+    @Autowired
+    WalletService walletService;
+
+    @Autowired
+    AuthHelper authHelper;
+
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "初始当前用户钱包")
+    @RequestMapping(value = "initUserWallet", method = {RequestMethod.GET})
+    public ResultContent<Wallet> initUserWallet() {
+        String userId = authHelper.getCurrentUser().getUserId();
+        return walletService.initUserWallet(userId);
+    }
+
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "得到当前用户钱包")
+    @RequestMapping(value = "getUserWallet", method = {RequestMethod.GET})
+    public ResultContent<WalletModel> getUserWallet() {
+        String userId = authHelper.getCurrentUser().getUserId();
+        return walletService.getUserWallet(userId);
+    }
+
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "当前用户充值")
+    @RequestMapping(value = "rechargeCurrent", method = {RequestMethod.POST})
+    public ResultContent rechargeCurrent(@RequestBody WalletRechargeParam param) {
+        String userId = authHelper.getCurrentUser().getUserId();
+        param.setUserId(userId);
+        return walletService.addWalletRecharge(param);
+    }
+
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "得到用户的充值列表", description = "得到用户的充值列表: 如果userId为空,则查询当前用户的")
+    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
+    public ResultContent<Page<WalletRechargeModel>> page(
+            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
+            @Parameter(required = false) WalletRechargeSearch param) {
+        if (StringUtils.isEmpty(param.getUserId())) {
+            param.setUserId(authHelper.getCurrentUser().getUserId());
+        }
+        return walletService.page(param, pageable);
+    }
+
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "得到充值详情")
+    @RequestMapping(value = "rechargeCurrent", method = {RequestMethod.POST})
+    public ResultContent<WalletRechargeModel> rechargeCurrent(@RequestBody IDParam param) {
+        return walletService.getUserWalletRecharge(param.getId());
+    }
+
+}

+ 14 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/domain/payment/WalletRecharge.java

@@ -2,6 +2,7 @@ package com.zhongshu.card.server.core.domain.payment;
 
 import com.zhongshu.card.client.utils.type.RechargeType;
 import com.zhongshu.card.server.core.domain.base.SuperMain;
+import com.zhongshu.card.server.core.domain.org.UserAccount;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
@@ -29,9 +30,19 @@ public class WalletRecharge extends SuperMain {
     @DBRef(lazy = true)
     private Wallet wallet;
 
+    @Schema(description = "关联用户")
+    @DBRef(lazy = true)
+    private UserAccount userAccount;
+
     @Schema(description = "用户userId")
     private String userId;
 
+    @Schema(description = "用户名称")
+    private String userName;
+
+    @Schema(description = "用户电话号码")
+    private String phone;
+
     @Schema(description = "充值类型:")
     private RechargeType rechargeType;
 
@@ -41,6 +52,9 @@ public class WalletRecharge extends SuperMain {
     @Schema(description = "月份,如06")
     private String month;
 
+    @Schema(description = "可阅读的时间")
+    private String timeStr;
+
     @Schema(description = "充值金额")
     private BigDecimal amount;
 

+ 1 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/base/SuperService.java

@@ -161,7 +161,7 @@ public abstract class SuperService {
         }
     }
 
-    public void initOidSearchParamNoTip(SuperSearch param) {
+    public void initOidSearchParamNoCheckOid(SuperSearch param) {
         String oid = param.getOid();
         if (StringUtils.isEmpty(oid)) {
             param.setOid(getCurrentOid());

+ 3 - 2
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/org/UserAccountServiceImpl.java

@@ -163,7 +163,7 @@ public class UserAccountServiceImpl extends SuperService implements UserAccountS
                 userCountDao.save(temp);
             } else {
                 // 编辑用户
-                BeanUtils.copyProperties(param, temp, "id", "loginName","userId", "userType", "loginType");
+                BeanUtils.copyProperties(param, temp, "id", "loginName", "userId", "userType", "loginType");
                 temp.setSpellCode(CommonUtil.getPinyin(param.getName()));
                 userCountDao.save(temp);
             }
@@ -583,8 +583,9 @@ public class UserAccountServiceImpl extends SuperService implements UserAccountS
     }
 
     public UserCountModel toModel(UserAccount entity) {
-        UserCountModel model = new UserCountModel();
+        UserCountModel model = null;
         if (ObjectUtils.isNotEmpty(entity)) {
+            model = new UserCountModel();
             BeanUtils.copyProperties(entity, model);
         }
         return model;

+ 90 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/payment/WalletService.java

@@ -1,14 +1,17 @@
 package com.zhongshu.card.server.core.service.payment;
 
 import com.github.microservice.components.data.base.util.PageEntityUtil;
+import com.zhongshu.card.client.model.org.UserCountModel;
 import com.zhongshu.card.client.model.payment.WalletModel;
 import com.zhongshu.card.client.model.payment.WalletRechargeModel;
+import com.zhongshu.card.client.model.payment.WalletRechargeParam;
 import com.zhongshu.card.client.model.payment.WalletRechargeSearch;
 import com.zhongshu.card.client.model.school.BookInfoModel;
 import com.zhongshu.card.client.model.school.BookInfoSearch;
 import com.zhongshu.card.client.ret.ResultContent;
 import com.zhongshu.card.client.service.org.UserAccountService;
 import com.zhongshu.card.client.utils.type.DataState;
+import com.zhongshu.card.client.utils.type.RechargeType;
 import com.zhongshu.card.server.core.dao.org.UserCountDao;
 import com.zhongshu.card.server.core.dao.payment.WalletDao;
 import com.zhongshu.card.server.core.dao.payment.WalletRechargeDao;
@@ -19,6 +22,8 @@ import com.zhongshu.card.server.core.domain.school.BookInfo;
 import com.zhongshu.card.server.core.service.base.SuperService;
 import com.zhongshu.card.server.core.service.org.UserAccountServiceImpl;
 import com.zhongshu.card.server.core.util.BeanUtils;
+import com.zhongshu.card.server.core.util.CommonUtil;
+import com.zhongshu.card.server.core.util.DateUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -26,8 +31,10 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * @author TRX
@@ -49,6 +56,8 @@ public class WalletService extends SuperService {
     @Autowired
     UserAccountServiceImpl userAccountService;
 
+    private final ReentrantLock lock = new ReentrantLock();
+
     /**
      * 初始当前用户钱包
      *
@@ -76,6 +85,7 @@ public class WalletService extends SuperService {
         Wallet wallet = walletDao.findByUserId(userId);
         if (ObjectUtils.isEmpty(wallet)) {
             wallet = new Wallet();
+            initEntityNoCheckOid(wallet);
             wallet.setAmount(BigDecimal.ZERO);
             wallet.setUserId(userId);
             wallet.setUserAccount(userAccount);
@@ -131,12 +141,77 @@ public class WalletService extends SuperService {
      * @return
      */
     public ResultContent<Page<WalletRechargeModel>> page(WalletRechargeSearch param, Pageable pageable) {
-        initOidSearchParamNoTip(param);
+        initOidSearchParamNoCheckOid(param);
         Page<WalletRecharge> page = walletRechargeDao.page(pageable, param);
         return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
     }
 
+    /**
+     * 充值
+     *
+     * @param param
+     * @return
+     */
+    @Transactional
+    public ResultContent addWalletRecharge(WalletRechargeParam param) {
+        String userId = param.getUserId();
+        UserAccount userAccount = userCountDao.findTopByUserId(userId);
+        if (ObjectUtils.isEmpty(userAccount)) {
+            return ResultContent.buildFail(String.format("用户不存在:%s", userId));
+        }
+        Wallet wallet = getUserWalletByUserId(userId);
+        // 充值金额
+        BigDecimal amount = param.getAmount();
+        if (CommonUtil.bigDecimalIsEmpty(amount)) {
+            return ResultContent.buildFail(String.format("充值金额不能小于等于0"));
+        }
+
+        try {
+            getLock().lock();
+            WalletRecharge walletRecharge = new WalletRecharge();
+            initEntityNoCheckOid(walletRecharge);
+            BigDecimal preAmount = wallet.getAmount();
+            if (CommonUtil.bigDecimalIsEmpty(preAmount)) {
+                preAmount = BigDecimal.ZERO;
+            }
+            BigDecimal afterAmount = preAmount.add(amount);
+            wallet.setAmount(afterAmount);
+
+            walletRecharge.setPreAmount(preAmount);
+            walletRecharge.setAmount(amount);
+            walletRecharge.setRechargeType(RechargeType.Active);
+            walletRecharge.setAfterAmount(afterAmount);
+            walletRecharge.setUserId(userId);
+            walletRecharge.setUserAccount(userAccount);
+            walletRecharge.setWallet(wallet);
+            walletRecharge.setUserName(userAccount.getName());
+            walletRecharge.setPhone(userAccount.getPhone());
+            walletRecharge.setYear(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyy));
+            walletRecharge.setMonth(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternMM));
+            walletRecharge.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
+
+            walletDao.save(wallet);
+            walletRechargeDao.save(walletRecharge);
+        } catch (Exception e) {
+            e.printStackTrace();
+            getLock().unlock();
+        }
+        return ResultContent.buildSuccess();
+    }
 
+    /**
+     * 得到充值详情
+     *
+     * @param id
+     * @return
+     */
+    public ResultContent<WalletRechargeModel> getUserWalletRecharge(String id) {
+        WalletRecharge walletRecharge = walletRechargeDao.findTopById(id);
+        if (ObjectUtils.isEmpty(walletRecharge)) {
+            return ResultContent.buildFail(String.format("数据ID不存在:%s", id));
+        }
+        return ResultContent.buildSuccess(toModelIncludeUser(walletRecharge));
+    }
 
     //---------------------------充值 end--------------------------
 
@@ -158,4 +233,18 @@ public class WalletService extends SuperService {
         return model;
     }
 
+    public WalletRechargeModel toModelIncludeUser(WalletRecharge entity) {
+        WalletRechargeModel model = null;
+        if (ObjectUtils.isNotEmpty(entity)) {
+            model = new WalletRechargeModel();
+            BeanUtils.copyProperties(entity, model);
+            UserCountModel userAccount = userAccountService.toModel(entity.getUserAccount());
+            model.setUserCount(userAccount);
+        }
+        return model;
+    }
+
+    public ReentrantLock getLock() {
+        return lock;
+    }
 }

+ 4 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/util/DateUtils.java

@@ -69,6 +69,10 @@ public class DateUtils {
 
     public final static String patternyyyySSS = "yyyy-MM-dd HH:mm:ss SSS";
 
+    public final static String patternyyyy = "yyyy";
+
+    public final static String patternMM = "MM";
+
     public static String paresTime(Long time, String pattern) {
         if (time == null || time <= 0) {
             return "";