TRX 1 năm trước cách đây
mục cha
commit
0fbc5f36a0

+ 11 - 4
PaymentClient/src/main/java/com/zhongshu/payment/client/ret/ResultContent.java

@@ -32,6 +32,15 @@ public class ResultContent<T> {
     @Setter
     private String msg;
 
+    public String getMsg() {
+        if (msg != null) {
+            return msg;
+        }
+        if (state != null) {
+            return state.getRemark();
+        }
+        return "";
+    }
 
     //异常
     @Getter
@@ -80,13 +89,11 @@ public class ResultContent<T> {
         return build(ResultState.Success, ResultState.Success.getRemark());
     }
 
-    public boolean isSuccess()
-    {
+    public boolean isSuccess() {
         return ResultState.Success.equals(this.state);
     }
 
-    public boolean isFailed()
-    {
+    public boolean isFailed() {
         return !ResultState.Success.equals(this.state);
     }
 }

+ 23 - 0
PaymentClient/src/main/java/com/zhongshu/payment/client/types/WalletState.java

@@ -0,0 +1,23 @@
+package com.zhongshu.payment.client.types;
+
+import lombok.Getter;
+
+/**
+ * 钱包状态
+ *
+ * @author wjf
+ * @date 2024/7/24
+ */
+public enum WalletState {
+
+    Enable("启用"),
+    Disable("已停用"),
+    ;
+
+    @Getter
+    private String remark;
+
+    WalletState(String remark) {
+        this.remark = remark;
+    }
+}

+ 25 - 12
PaymentServer/src/main/java/com/zhongshu/payment/server/core/controller/RechargeController.java

@@ -3,6 +3,7 @@ package com.zhongshu.payment.server.core.controller;
 import com.zhongshu.payment.client.model.param.OrderParam;
 import com.zhongshu.payment.client.model.param.QueryWalletParam;
 import com.zhongshu.payment.client.model.param.RechargeParam;
+import com.zhongshu.payment.client.ret.ResultContent;
 import com.zhongshu.payment.server.core.service.RechargeService;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
@@ -25,48 +26,60 @@ public class RechargeController {
     @Autowired
     RechargeService rechargeService;
 
-    /** 创建充值订单 */
+    /**
+     * 创建充值订单
+     */
     @Operation(summary = "创建充值订单", description = "创建充值订单")
     @PostMapping(value = "create", consumes = MediaType.APPLICATION_JSON_VALUE)
-    public Object create(@RequestBody RechargeParam param){
+    public Object create(@RequestBody RechargeParam param) {
         return rechargeService.create(param);
     }
 
-    /** 下单(获取调用微信支付参数) */
+    /**
+     * 下单(获取调用微信支付参数)
+     */
     @Operation(summary = "下单(获取调用微信支付参数)", description = "下单(获取调用微信支付参数)")
     @PostMapping(value = "order", consumes = MediaType.APPLICATION_JSON_VALUE)
-    public Object order(@RequestBody OrderParam param){
+    public ResultContent order(@RequestBody OrderParam param) {
         return rechargeService.order(param);
     }
 
-    /** 查询订单支付状态 */
+    /**
+     * 查询订单支付状态
+     */
     @Operation(summary = "查询订单支付状态", description = "查询订单支付状态")
     @GetMapping(value = "queryTradeState")
-    public Object queryTradeState(@RequestParam("outTradeNo") String outTradeNo){
+    public Object queryTradeState(@RequestParam("outTradeNo") String outTradeNo) {
         Assert.notNull(outTradeNo, "必填参数校验:缺少必填参数\"outTradeNo\"");
         return rechargeService.queryTradeState(outTradeNo);
     }
 
-    /** 关闭充值订单 */
+    /**
+     * 关闭充值订单
+     */
     @Operation(summary = "关闭充值订单", description = "关闭充值订单")
     @PostMapping(value = "close")
-    public Object close(@RequestParam("outTradeNo") String outTradeNo){
+    public Object close(@RequestParam("outTradeNo") String outTradeNo) {
         Assert.notNull(outTradeNo, "必填参数校验:缺少必填参数\"outTradeNo\"");
         return rechargeService.close(outTradeNo);
     }
 
-    /** 查询充值订单列表 */
+    /**
+     * 查询充值订单列表
+     */
     @Operation(summary = "查询充值订单列表", description = "查询充值订单列表")
     @PostMapping(value = "queryListByWallet")
-    public Object queryListByWallet(@RequestBody QueryWalletParam param){
+    public Object queryListByWallet(@RequestBody QueryWalletParam param) {
         Assert.notNull(param.getWalletId(), "必填参数校验:缺少必填参数\"walletId\"");
         return rechargeService.queryListByWallet(param.getWalletId(), param.getYear(), param.getMonth());
     }
 
-    /** 查询充值订单详情 */
+    /**
+     * 查询充值订单详情
+     */
     @Operation(summary = "查询充值订单详情", description = "查询充值订单详情")
     @GetMapping(value = "queryInfoById")
-    public Object queryInfoById(@RequestParam("outTradeNo") String outTradeNo){
+    public Object queryInfoById(@RequestParam("outTradeNo") String outTradeNo) {
         Assert.notNull(outTradeNo, "必填参数校验:缺少必填参数\"outTradeNo\"");
         return rechargeService.queryInfoById(outTradeNo);
     }

+ 16 - 0
PaymentServer/src/main/java/com/zhongshu/payment/server/core/dataConfig/TimeConfig.java

@@ -0,0 +1,16 @@
+package com.zhongshu.payment.server.core.dataConfig;
+
+/**
+ * 限时等配置
+ *
+ * @author TRX
+ * @date 2024/8/21
+ */
+public class TimeConfig {
+
+    /**
+     * 用户充值订单过期时间,15分钟
+     */
+    public static long rechargeExpireTime = 15 * 60 * 1000l;
+
+}

+ 32 - 0
PaymentServer/src/main/java/com/zhongshu/payment/server/core/domain/wallet/RechargeRecord.java

@@ -2,6 +2,7 @@ package com.zhongshu.payment.server.core.domain.wallet;
 
 import com.github.microservice.components.data.mongo.mongo.domain.SuperEntity;
 import com.zhongshu.card.client.model.org.OrganizationUserModel;
+import com.zhongshu.card.client.utils.DateUtils;
 import com.zhongshu.payment.client.model.PrePayModel;
 import com.zhongshu.payment.client.model.TransferModel;
 import com.zhongshu.payment.client.types.PaymentType;
@@ -17,6 +18,7 @@ import java.math.BigDecimal;
 
 /**
  * 充值记录
+ *
  * @author wjf
  * @date 2024/7/25
  */
@@ -97,4 +99,34 @@ public class RechargeRecord extends SuperEntity {
 
     private String remark;
 
+    @Schema(description = "年份,如:2024")
+    private Integer year;
+
+    @Schema(description = "月份,如:6")
+    private Integer month;
+
+    @Schema(description = "第几周,如:32")
+    private Integer week;
+
+    @Schema(description = "当前月的第几天")
+    private Integer dayOfMonth;
+
+    @Schema(description = "当前年的第几天")
+    private Integer dayOfYear;
+
+    @Schema(description = "当前的第几小时")
+    private Integer hourOfDay;
+
+    @Schema(description = "消费创建时间")
+    private String paymentTime;
+
+    public void setTimes() {
+        this.year = DateUtils.getCurrentYear();
+        this.dayOfYear = DateUtils.getCurrentDayInYear();
+        this.month = DateUtils.getCurrentMonthInYear();
+        this.week = DateUtils.getCurrentWeekInYear();
+        this.dayOfMonth = DateUtils.getCurrentDayInMonth();
+        this.hourOfDay = DateUtils.getCurrentHourOfDay();
+        this.paymentTime = DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS);
+    }
 }

+ 5 - 1
PaymentServer/src/main/java/com/zhongshu/payment/server/core/domain/wallet/Wallet.java

@@ -3,6 +3,7 @@ package com.zhongshu.payment.server.core.domain.wallet;
 import com.github.microservice.components.data.mongo.mongo.domain.SuperEntity;
 import com.zhongshu.card.client.model.org.OrganizationUserModel;
 import com.zhongshu.payment.client.types.DataState;
+import com.zhongshu.payment.client.types.WalletState;
 import com.zhongshu.payment.client.types.WalletType;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
@@ -31,6 +32,9 @@ public class Wallet extends SuperEntity {
     @Indexed
     private String oid;
 
+    @Schema(description = "钱包管关联的对象数据ID")
+    private String walletDataId;
+
     @Schema(description = "用户userId")
     @Indexed
     private String userId;
@@ -47,7 +51,7 @@ public class Wallet extends SuperEntity {
 
     @Schema(description = "钱包状态:是否可用")
     @Indexed
-    private DataState dataState = DataState.Enable;
+    private WalletState dataState = WalletState.Enable;
 
     @Schema(description = "钱包类型")
     @Indexed

+ 77 - 47
PaymentServer/src/main/java/com/zhongshu/payment/server/core/service/RechargeService.java

@@ -18,13 +18,11 @@ import com.zhongshu.payment.client.model.RechargeRecordModel;
 import com.zhongshu.payment.client.model.param.OrderParam;
 import com.zhongshu.payment.client.model.param.RechargeParam;
 import com.zhongshu.payment.client.ret.ResultContent;
-import com.zhongshu.payment.client.types.DataState;
-import com.zhongshu.payment.client.types.RechargeState;
-import com.zhongshu.payment.client.types.TradeType;
-import com.zhongshu.payment.client.types.WalletType;
+import com.zhongshu.payment.client.types.*;
 import com.zhongshu.payment.server.core.dao.RechargeRecordDao;
 import com.zhongshu.payment.server.core.dao.WalletDao;
 import com.zhongshu.payment.server.core.dao.WxPayConfigDao;
+import com.zhongshu.payment.server.core.dataConfig.TimeConfig;
 import com.zhongshu.payment.server.core.domain.wallet.RechargeRecord;
 import com.zhongshu.payment.server.core.domain.wallet.Wallet;
 import com.zhongshu.payment.server.core.service.impl.WalletFeignServiceImpl;
@@ -45,6 +43,7 @@ import java.util.Map;
 
 /**
  * 充值服务
+ *
  * @author wjf
  * @date 2024/7/25
  */
@@ -87,26 +86,29 @@ public class RechargeService {
 //        return outTradeNo;
 //    }
 
-    /** 创建充值订单 */
-    public Object create(RechargeParam param){
+    /**
+     * 创建充值订单
+     */
+    public Object create(RechargeParam param) {
         String userId = authHelper.getCurrentUser().getUserId();
         Wallet userWallet = walletDao.findTop1ById(param.getWalletId());
         //判断钱包是否初始化
-        if (userWallet==null){
+        if (userWallet == null) {
             return ResultContent.buildFail("用户钱包未开通");
         }
 
-        if (userWallet.getDataState().equals(DataState.Disable)){
+        if (userWallet.getDataState().equals(WalletState.Disable)) {
             return ResultContent.buildFail("钱包已停用");
         }
 
-        if (userWallet.getWalletType().equals(WalletType.Shop)){
+        if (userWallet.getWalletType().equals(WalletType.Shop)) {
             return ResultContent.buildFail("商户提现暂未开通线上充值功能");
         }
 
+        // 创建充值订单
         RechargeRecord rechargeRecord = new RechargeRecord();
         org.springframework.beans.BeanUtils.copyProperties(param, rechargeRecord, "total");
-        String outTradeNo =CommonUtil.UUID();
+        String outTradeNo = CommonUtil.UUID();
         rechargeRecord.setOutTradeNo(outTradeNo);
         rechargeRecord.setTotal(BigDecimal.valueOf(param.getTotal()));
         rechargeRecord.setOid(userWallet.getOid());
@@ -114,11 +116,12 @@ public class RechargeService {
         rechargeRecord.setUserId(userId);
         rechargeRecord.setCommit(false);
 
+        // 查询用户在学校的信息
         OrgUserDetailParam orgUserDetailParam = new OrgUserDetailParam();
         orgUserDetailParam.setOid(param.getSchoolId());
         orgUserDetailParam.setUserId(userWallet.getUserId());
         com.zhongshu.card.client.ret.ResultContent<OrganizationUserModel> orgUserDetail = organizationFeignService.getOrgUserDetail(orgUserDetailParam);
-        if (orgUserDetail.isFailed()){
+        if (orgUserDetail.isFailed()) {
             return ResultContent.buildFail("获取用户信息失败");
         }
         rechargeRecord.setUserInfo(orgUserDetail.getContent());
@@ -126,7 +129,10 @@ public class RechargeService {
         rechargeRecord.setWallet(userWallet);
         long createTIme = System.currentTimeMillis();
         rechargeRecord.setCreateTime(createTIme);
-        rechargeRecord.setExpireTime(createTIme + 15*60*1000);
+        rechargeRecord.setTimes();
+
+        // 订单过期时间
+        rechargeRecord.setExpireTime(createTIme + TimeConfig.rechargeExpireTime);
         RechargeRecord record = rechargeRecordDao.save(rechargeRecord);
         RechargeRecordModel model = toModel(record);
         return ResultContent.buildSuccess(model);
@@ -134,7 +140,7 @@ public class RechargeService {
 
     private RechargeRecordModel toModel(RechargeRecord record) {
         RechargeRecordModel model = new RechargeRecordModel();
-        if (record!=null){
+        if (record != null) {
             BeanUtils.copyProperties(record, model, "total");
             model.setTotal(record.getTotal().intValue());
             model.setPrePayModel(record.getPrePayModel());
@@ -143,22 +149,35 @@ public class RechargeService {
         return model;
     }
 
-    /** 下单 */
+    /**
+     * 下单 (微信下单)
+     */
     @Transactional
-    public Object order(OrderParam param){
+    public ResultContent order(OrderParam param) {
         RechargeRecord record = rechargeRecordDao.findByOutTradeNo(param.getOutTradeNo());
-        if (record==null){
-            return ResultContent.buildFail("充值订单:"+param.getOutTradeNo()+"不存在");
+        if (record == null) {
+            return ResultContent.buildFail("充值订单:" + param.getOutTradeNo() + "不存在");
+        }
+        // 检查订单状态
+        RechargeState rechargeState = record.getRechargeState();
+        if (rechargeState != RechargeState.NOTPAY) {
+            return ResultContent.buildFail(String.format("订单状态不为:%s", RechargeState.NOTPAY.getRemark()));
+        }
+        if (record.getExpireTime() <= System.currentTimeMillis()) {
+            record.setRechargeState(RechargeState.CLOSED);
+            rechargeRecordDao.save(record);
+            return ResultContent.buildFail("订单支付过期,已关闭");
         }
 
-        if (StringUtil.isNullOrEmpty(param.getOpenid())){
-            return ResultContent.buildFail("缺少用户授权参数");
+        if (StringUtil.isNullOrEmpty(param.getOpenid())) {
+            return ResultContent.buildFail("缺少用户授权参数openId");
         }
 
+        // 根据appId 得到微信支付配置
         ProjectWxPayParam wxPayParam = new ProjectWxPayParam();
         wxPayParam.setAppId(record.getAppid());
         com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> paySetResult = paySettingFeignService.getProjectWxPayConfig(wxPayParam);
-        if (paySetResult.isFailed()){
+        if (paySetResult.isFailed()) {
             return ResultContent.buildFail("获取微信支付配置失败");
         }
 
@@ -174,9 +193,9 @@ public class RechargeService {
         Payer payer = new Payer();
         payer.setOpenid(param.getOpenid());
         request.setPayer(payer);
-
+        // 微信下单
         ResultContent<PrePayModel> prepayResult = wxPaymentService.prepay(request, paySetResult.getContent());
-        if (prepayResult.isFailed()){
+        if (prepayResult.isFailed()) {
             return prepayResult;
         }
         record.setOpenid(param.getOpenid());
@@ -187,38 +206,42 @@ public class RechargeService {
         return prepayResult;
     }
 
-    /** 查询订单支付状态 */
-    public Object queryTradeState(String outTradeNo){
+    /**
+     * 查询订单支付状态
+     */
+    public Object queryTradeState(String outTradeNo) {
         RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
-        if (record==null){
-            return ResultContent.buildFail("充值订单:"+outTradeNo+"不存在");
+        if (record == null) {
+            return ResultContent.buildFail("充值订单:" + outTradeNo + "不存在");
         }
-        if (record.getRechargeState().equals(RechargeState.NOTPAY) && !record.isCommit()){
+        if (record.getRechargeState().equals(RechargeState.NOTPAY) && !record.isCommit()) {
             return ResultContent.buildSuccess(RechargeState.NOTPAY.name());
         }
 
         ProjectWxPayParam wxPayParam = new ProjectWxPayParam();
         wxPayParam.setAppId(record.getAppid());
         com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> paySetResult = paySettingFeignService.getProjectWxPayConfig(wxPayParam);
-        if (paySetResult.isFailed()){
+        if (paySetResult.isFailed()) {
             return ResultContent.buildFail("获取微信支付配置失败");
         }
         ResultContent<Transaction> queryResult = wxPaymentService.queryOrderByOutTradeNo(outTradeNo, paySetResult.getContent());
-        if (queryResult.isSuccess()){
+        if (queryResult.isSuccess()) {
             return ResultContent.buildSuccess(queryResult.getContent().getTradeState().name());
         }
         return queryResult;
     }
 
 
-    /** 关闭充值订单 */
-    public Object close(String outTradeNo){
+    /**
+     * 关闭充值订单
+     */
+    public Object close(String outTradeNo) {
         RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
-        if (record==null){
-            return ResultContent.buildFail("充值订单:"+outTradeNo+"不存在");
+        if (record == null) {
+            return ResultContent.buildFail("充值订单:" + outTradeNo + "不存在");
         }
 
-        if (record.getRechargeState().equals(RechargeState.NOTPAY)){
+        if (record.getRechargeState().equals(RechargeState.NOTPAY)) {
             record.setRechargeState(RechargeState.CLOSED);
             rechargeRecordDao.save(record);
             return ResultContent.buildSuccess();
@@ -227,7 +250,7 @@ public class RechargeService {
         ProjectWxPayParam wxPayParam = new ProjectWxPayParam();
         wxPayParam.setAppId(record.getAppid());
         com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> paySetResult = paySettingFeignService.getProjectWxPayConfig(wxPayParam);
-        if (paySetResult.isFailed()){
+        if (paySetResult.isFailed()) {
             return ResultContent.buildFail("获取微信支付配置失败");
         }
         record.setRechargeState(RechargeState.CLOSED);
@@ -235,12 +258,12 @@ public class RechargeService {
         return wxPaymentService.closeOrder(outTradeNo, paySetResult.getContent());
     }
 
-    public Object closeNoPay(String outTradeNo){
+    public Object closeNoPay(String outTradeNo) {
         RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
-        if (record==null){
-            return ResultContent.buildFail("充值订单:"+outTradeNo+"不存在");
+        if (record == null) {
+            return ResultContent.buildFail("充值订单:" + outTradeNo + "不存在");
         }
-        if (!record.getRechargeState().equals(RechargeState.NOTPAY)){
+        if (!record.getRechargeState().equals(RechargeState.NOTPAY)) {
             return ResultContent.buildSuccess("订单状态不是未支付");
         }
         record.setRechargeState(RechargeState.CLOSED);
@@ -248,17 +271,20 @@ public class RechargeService {
         return ResultContent.buildSuccess();
     }
 
-    /** 查询充值订单列表 */
-    public Object queryListByWallet(String walletId, Integer year, Integer month){
+    /**
+     * 查询充值订单列表
+     */
+    public Object queryListByWallet(String walletId, Integer year, Integer month) {
 
         Wallet wallet = walletDao.findTop1ById(walletId);
         //判断钱包是否初始化
-        if (wallet==null){
+        if (wallet == null) {
             return ResultContent.buildFail("用户钱包未开通");
         }
 
         Long monthStartTime = DateUtils.getMonthStartTime(year, month);
         Long monthEndTime = DateUtils.getMonthEndTime(year, month);
+        // 查询钱包的充值记录
         List<RechargeRecord> list = rechargeRecordDao.ListByWallet(wallet, monthStartTime, monthEndTime);
 
         Integer chargeTotal = rechargeRecordDao.sumTotalByWalletId(walletId, RechargeState.SUCCESS, TradeType.Charge, monthStartTime, monthEndTime);
@@ -267,17 +293,21 @@ public class RechargeService {
         return ResultContent.buildSuccess(Map.of("list", list.stream().map(this::toModel).toList(), "chargeTotal", chargeTotal, "transferTotal", transferTotal));
     }
 
-    /** 查询充值订单详情 */
-    public Object queryInfoById(String outTradeNo){
+    /**
+     * 查询充值订单详情
+     */
+    public Object queryInfoById(String outTradeNo) {
         RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
         return ResultContent.buildSuccess(toModel(record));
     }
 
-    /** 获取用户openid */
+    /**
+     * 获取用户openid
+     */
     @SneakyThrows
-    public String getOpenId(String jscode){
+    public String getOpenId(String jscode) {
         MiniAppUserInfoVo miniAppUserInfo = wechatCUtil.getMiniAppUserInfo(jscode);
-        if (miniAppUserInfo==null || io.netty.util.internal.StringUtil.isNullOrEmpty(miniAppUserInfo.getOpenid())){
+        if (miniAppUserInfo == null || io.netty.util.internal.StringUtil.isNullOrEmpty(miniAppUserInfo.getOpenid())) {
             log.info("无法获取用户信息");
             return null;
         }

+ 2 - 1
PaymentServer/src/main/java/com/zhongshu/payment/server/core/service/impl/WalletFeignServiceImpl.java

@@ -6,6 +6,7 @@ import com.zhongshu.payment.client.ret.ResultContent;
 import com.zhongshu.payment.client.service.WalletFeignService;
 import com.zhongshu.payment.client.types.DataState;
 import com.zhongshu.payment.client.types.TradeType;
+import com.zhongshu.payment.client.types.WalletState;
 import com.zhongshu.payment.client.types.WalletType;
 import com.zhongshu.payment.server.core.dao.WalletDao;
 import com.zhongshu.payment.server.core.dao.WalletFlowDao;
@@ -57,7 +58,7 @@ public class WalletFeignServiceImpl implements WalletFeignService {
         wallet.setOid(oid);
         wallet.setAmount(BigDecimal.ZERO);
         wallet.setAmount(BigDecimal.ZERO);
-        wallet.setDataState(DataState.Enable);
+        wallet.setDataState(WalletState.Enable);
         wallet.setWalletType(walletType);
         walletDao.save(wallet);
         return wallet;

+ 45 - 18
PaymentServer/src/main/java/com/zhongshu/payment/server/core/service/wallet/WalletService.java

@@ -11,6 +11,7 @@ import com.zhongshu.payment.client.model.WalletOrderModel;
 import com.zhongshu.payment.client.model.param.GetWalletParam;
 import com.zhongshu.payment.client.ret.ResultContent;
 import com.zhongshu.payment.client.types.DataState;
+import com.zhongshu.payment.client.types.WalletState;
 import com.zhongshu.payment.client.types.WalletType;
 import com.zhongshu.payment.server.core.dao.RechargeRecordDao;
 import com.zhongshu.payment.server.core.dao.WalletDao;
@@ -59,44 +60,70 @@ public class WalletService {
 //        return ResultContent.buildSuccess();
 //    }
 
-    /** 查询用户钱包 */
-    public Object getWalletByUser(GetWalletParam param){
+    /**
+     * 查询用户钱包
+     */
+    public Object getWalletByUser(GetWalletParam param) {
         String userId = authHelper.getCurrentUser().getUserId();
 
         ProjectWxPayParam projectWxPayParam = new ProjectWxPayParam();
         projectWxPayParam.setAppId(param.getAppid());
         com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> payConfigResult = paySettingFeignService.getProjectWxPayConfig(projectWxPayParam);
-        if (payConfigResult.isFailed()){
+        if (payConfigResult.isFailed()) {
             return ResultContent.buildFail("获取支付配置信息失败");
         }
-
+        // 关联的项目ID
         String oid = payConfigResult.getContent().getProjectOid();
 
-        if (param.getWalletType().equals(WalletType.User) && walletDao.existsByUserIdAndOidAndWalletType(userId, oid, WalletType.User)){
-            return ResultContent.buildContent(toModel(walletDao.findByUserIdAndOidAndWalletType(userId, oid, param.getWalletType())));
+        // 判断钱包是否存在
+        if (param.getWalletType().equals(WalletType.User) && walletDao.existsByUserIdAndOidAndWalletType(userId, oid, WalletType.User)) {
+            return ResultContent.buildSuccess(toModel(walletDao.findByUserIdAndOidAndWalletType(userId, oid, param.getWalletType())));
         }
-        if (param.getWalletType().equals(WalletType.Shop) && walletDao.existsByShopIdAndOidAndWalletType(param.getShopId(), oid, WalletType.Shop)){
-            if (StringUtil.isNullOrEmpty(param.getShopId())){
+
+        // 判断商户的钱包是否存在
+        if (param.getWalletType().equals(WalletType.Shop)) {
+            if (StringUtil.isNullOrEmpty(param.getShopId())) {
                 return ResultContent.buildFail("钱包类型为Shop时,shopId不能为空");
             }
-            return ResultContent.buildContent(toModel(walletDao.findByShopIdAndOidAndWalletType(param.getShopId(), oid, param.getWalletType())));
+            if (walletDao.existsByShopIdAndOidAndWalletType(param.getShopId(), oid, WalletType.Shop)) {
+                return ResultContent.buildSuccess(toModel(walletDao.findByShopIdAndOidAndWalletType(param.getShopId(), oid, param.getWalletType())));
+            }
         }
+        // 初始钱包
+        Wallet wallet = initUserWallet(oid, userId, param.getShopId(), param.getWalletType());
+
+        return ResultContent.buildSuccess(toModel(wallet));
+    }
 
+    /**
+     * 初始用户/商户钱包
+     *
+     * @param projectOid
+     * @param userId
+     * @param shopId
+     * @return
+     */
+    public Wallet initUserWallet(String projectOid, String userId, String shopId, WalletType walletType) {
         Wallet wallet = new Wallet();
-        wallet.setOid(oid);
+        wallet.setOid(projectOid);
         wallet.setUserId(userId);
         wallet.setAmount(BigDecimal.ZERO);
         wallet.setAmount(BigDecimal.ZERO);
-        wallet.setDataState(DataState.Enable);
-        wallet.setShopId(param.getShopId());
-        wallet.setWalletType(param.getWalletType());
+        wallet.setDataState(WalletState.Enable);
+        wallet.setShopId(shopId);
+        wallet.setWalletType(walletType);
+        if (walletType == WalletType.User) {
+            wallet.setWalletDataId(userId);
+        } else if (walletType == WalletType.Shop) {
+            wallet.setWalletDataId(shopId);
+        }
         walletDao.save(wallet);
-        return ResultContent.buildSuccess(toModel(wallet));
+        return wallet;
     }
 
-    public Object getTransferTotal(String walletId){
+    public Object getTransferTotal(String walletId) {
         Wallet wallet = walletDao.findTop1ById(walletId);
-        if (wallet==null){
+        if (wallet == null) {
             return ResultContent.buildFail("钱包未开通");
         }
         WalletOrderModel walletOrderModel = new WalletOrderModel();
@@ -107,9 +134,9 @@ public class WalletService {
         return ResultContent.buildContent(walletOrderModel);
     }
 
-    private WalletModel toModel(Wallet wallet){
+    private WalletModel toModel(Wallet wallet) {
         WalletModel model = new WalletModel();
-        if (wallet!=null){
+        if (wallet != null) {
             BeanUtils.copyPropertiesWithoutNull(wallet, model);
         }
         return model;