基于mall4j产品的二开项目后端
lee
2024-12-18 921461a3f906d74403aeb6a27051deb77eca10fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.app.param.PayUserParam;
import com.yami.shop.bean.enums.PayEntry;
import com.yami.shop.bean.model.UserExtension;
import com.yami.shop.bean.pay.PayInfoDto;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.enums.PayType;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.Arith;
import com.yami.shop.manager.impl.PayManager;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.PayInfoService;
import com.yami.shop.service.UserExtensionService;
import com.yami.shop.user.common.model.UserBalance;
import com.yami.shop.user.common.model.UserBalanceLog;
import com.yami.shop.user.common.service.UserBalanceLogService;
import com.yami.shop.user.common.service.UserBalanceService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;
 
/**
 * @author LHD on 2020/03/02.
 */
@Tag(name = "余额支付接口")
@RestController
@RequestMapping("/p/balance")
@AllArgsConstructor
public class PayBalanceController {
 
    /**
     * 会员信息
     */
    private final UserBalanceService userBalanceService;
    private final UserBalanceLogService userBalanceLogService;
    private final UserExtensionService userExtensionService;
    private final Snowflake snowflake;
    private final PayManager payManager;
    private final PayInfoService payInfoService;
 
    /**
     * 会员余额充值接口
     */
    @PostMapping("/pay")
    @Operation(summary = "会员余额充值" , description = "会员余额充值")
    @SneakyThrows
    public ServerResponseEntity<?> payVip(HttpServletResponse httpResponse, @RequestBody PayUserParam payCardParam) {
        YamiUser user = SecurityUtils.getUser();
        if (!user.getEnabled()) {
            // 您已被禁用,不能购买,请联系平台客服
            throw new YamiShopBindException("yami.order.pay.user.disable");
        }
        if (payCardParam.getPayType().equals(PayType.SCOREPAY.value()) || payCardParam.getPayType().equals(PayType.BALANCE.value())){
            // 请使用微信或支付宝进行支付
            throw new YamiShopBindException("yami.user.payType.error");
        }
        UserExtension userExtension = userExtensionService.getOne(new LambdaQueryWrapper<UserExtension>().eq(UserExtension::getUserId, user.getUserId()));
        UserBalance userBalance = null;
        // 自定义金额
        double balance;
        if (Objects.equals(payCardParam.getId(), -1L)) {
            balance = Arith.add(userExtension.getTotalBalance(), payCardParam.getCustomRechargeAmount());
        }
        // 购买
        else {
            userBalance = userBalanceService.getById(payCardParam.getId());
            if (Objects.isNull(userBalance)) {
                // 当前余额充值选项已被删除
                throw new YamiShopBindException("yami.user.blance.not.exist");
            }
            balance = Arith.add(userExtension.getTotalBalance(), userBalance.getRechargeAmount());
        }
        if (balance > Constant.MAX_USER_BALANCE) {
            // 您的余额加充值的余额将大于最大余额,不能进行充值操作
            throw new YamiShopBindException("yami.balance.than.max.balance");
        }
        // 使用雪花算法生成支付单号
        String payNo = String.valueOf(snowflake.nextId());
        Date now = new Date();
 
        //生成余额充值日志
        UserBalanceLog userBalanceLog = new UserBalanceLog();
        userBalanceLog.setCreateTime(now);
        userBalanceLog.setIoType(1);
        userBalanceLog.setType(1);
        userBalanceLog.setIsPayed(0);
        userBalanceLog.setPayNo(payNo);
        userBalanceLog.setUserId(user.getUserId());
        if (Objects.equals(payCardParam.getId(), -1L)) {
            userBalanceLog.setBalanceId(-1L);
            userBalanceLog.setChangeBalance(payCardParam.getCustomRechargeAmount());
        } else {
            userBalanceLog.setBalanceId(userBalance.getBalanceId());
            userBalanceLog.setChangeBalance(userBalance.getRechargeAmount());
        }
        userBalanceLogService.save(userBalanceLog);
        payCardParam.setNeedAmount(userBalanceLog.getChangeBalance());
        payCardParam.setPayNo(payNo);
        payCardParam.setOrderIds(userBalanceLog.getBalanceLogId());
 
        PayInfoDto payInfo = payInfoService.recharge(user.getUserId(), payCardParam);
        payInfo.setBizUserId(user.getBizUserId());
        payInfo.setPayType(payCardParam.getPayType());
        payInfo.setApiNoticeUrl("/notice/pay/" + PayEntry.RECHARGE.value() + "/" + payCardParam.getPayType());
        payInfo.setReturnUrl(payCardParam.getReturnUrl());
 
        return payManager.doPay(httpResponse, payInfo);
    }
 
    @GetMapping("/isPay")
    @Operation(summary = "获取余额充值支付信息" , description = "获取余额充值支付信息")
    public ServerResponseEntity<Integer> isPay(){
        YamiUser user = SecurityUtils.getUser();
        UserBalanceLog userBalanceLog = userBalanceLogService.getMaxCrtTimeByUserId(user.getUserId());
        return ServerResponseEntity.success(userBalanceLog.getIsPayed());
    }
 
}