基于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
/*
 * 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.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
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.UserLevel;
import com.yami.shop.user.common.model.UserLevelLog;
import com.yami.shop.user.common.service.UserLevelLogService;
import com.yami.shop.user.common.service.UserLevelService;
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/level")
@AllArgsConstructor
public class PayLevelController {
 
    private final UserLevelService userLevelService;
    private final UserExtensionService userExtensionService;
    private final UserLevelLogService userLevelLogService;
    private final PayManager payManager;
    private final PayInfoService payInfoService;
    private final Snowflake snowflake;
 
    /**
     * 会员支付接口
     */
    @PostMapping("/payLevel")
    @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");
        }
        UserExtension userExtension = userExtensionService.getOne(new LambdaQueryWrapper<UserExtension>()
                                            .eq(UserExtension::getUserId, user.getUserId()));
        UserLevel userLevel = userLevelService.getById(payCardParam.getId());
 
        //如果会员等级为空
        if(userLevel == null || !Objects.equals(userLevel.getLevelType(),1)) {
            // 会员等级不存在或当前等级不为付费会员等级!
            throw new YamiShopBindException("yami.user.level.no.exist");
        }
        if(Objects.equals(userExtension.getLevelType(),1) && userExtension.getLevel() > userLevel.getLevel()){
            // 用户无法在会员期间购买低等级会员
            throw new YamiShopBindException("yami.user.pay.level.check");
        }
        if(userExtension.getGrowth() < userLevel.getNeedGrowth()){
            // 用户成长值不足,无法购买
            throw new YamiShopBindException("yami.user.growh.no.enough");
        }
 
        String payNo = String.valueOf(snowflake.nextId());
 
        Date now = new Date();
        //生成等级日志
        UserLevelLog userLevelLog = new UserLevelLog();
        userLevelLog.setGiftTime(now);
        userLevelLog.setCreateTime(now);
        userLevelLog.setLevel(userLevel.getLevel());
        userLevelLog.setState(0);
        userLevelLog.setPayNo(payNo);
        userLevelLog.setIsPayed(0);
        userLevelLog.setPayAmount(userLevel.getNeedAmount());
        userLevelLog.setLevelName(userLevel.getLevelName());
        userLevelLog.setTerm(userLevel.getTerm());
        userLevelLog.setTermType(userLevel.getTermType());
        userLevelLog.setPayType(payCardParam.getPayType());
        userLevelLog.setUserId(userExtension.getUserId());
        //插入一条等级日志
        userLevelLogService.save(userLevelLog);
 
        payCardParam.setPayNo(payNo);
        payCardParam.setOrderIds(userLevelLog.getLevelLogId());
        payCardParam.setNeedAmount(userLevel.getNeedAmount());
 
        PayInfoDto payInfo = payInfoService.buyVip(user.getUserId(), payCardParam);
 
        payInfo.setBizUserId(user.getBizUserId());
        payInfo.setPayType(payCardParam.getPayType());
        payInfo.setApiNoticeUrl("/notice/pay/" + PayEntry.VIP.value() + "/" + payCardParam.getPayType());
        payInfo.setReturnUrl(payCardParam.getReturnUrl());
        return payManager.doPay(httpResponse, payInfo);
    }
 
    @GetMapping("/isPay")
    public ServerResponseEntity<Integer> isPay(){
        YamiUser user = SecurityUtils.getUser();
        UserLevelLog uLog = userLevelLogService.getMaxCrtTimeByUserId(user.getUserId());
        return ServerResponseEntity.success(uLog.getIsPayed());
    }
}