liujie
2025-06-04 6008ff1aad2de3a1b1d85fcd8fae89e217ccfa22
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.ruoyi.web.controller.system;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.SaveUserBankDto;
import com.ruoyi.system.dto.UserWithdrawalDto;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.UserAccountDetailQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.GetWithdrawalDetailVo;
import com.ruoyi.system.vo.RegionVo;
import com.ruoyi.system.vo.UserAccountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
 
@Slf4j
@RestController
@RequestMapping("/user-account")
@Api(tags = "用户钱包模块")
public class UserAccountController {
    @Autowired
    private TbUserService userService;
 
    @Autowired
    private TokenService tokenService;
 
    @Autowired
    private TbBankService bankService;
 
    @Autowired
    private TbAccountDetailService accountDetailService;
 
    @Autowired
    private TbWithdrawalService withdrawalService;
 
 
 
    @ApiOperation(value = "获取用户钱包信息",tags = {"用户钱包模块"})
    @GetMapping("/getUserAccount")
    public R<UserAccountVo> getUserAccount() {
        UserAccountVo userAccountVo = new UserAccountVo();
        LoginUser loginUser = tokenService.getLoginUser();
        TbUser user = userService.getById(loginUser.getUserId());
        userAccountVo.setBalance(user.getBalance());
 
        List<TbAccountDetail> list = accountDetailService.list(new LambdaQueryWrapper<TbAccountDetail>().eq(TbAccountDetail::getCategory, 1).eq(TbAccountDetail::getUserId, user.getId()));
        BigDecimal reduce = list.stream().filter(e->e.getStatus()==2).map(TbAccountDetail::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        userAccountVo.setWithdrawalBalance(reduce);
 
        BigDecimal reduce1 = list.stream().filter(e->e.getStatus()==1).map(TbAccountDetail::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        userAccountVo.setAuditBalance(reduce1);
        List<TbAccountDetail> list1 = accountDetailService.list(new LambdaQueryWrapper<TbAccountDetail>().eq(TbAccountDetail::getCategory, 2).eq(TbAccountDetail::getStatus, 1).eq(TbAccountDetail::getUserId, user.getId()));
        BigDecimal reduce2 = list1.stream().map(TbAccountDetail::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        userAccountVo.setEntryBalance(reduce2);
 
        return R.ok(userAccountVo);
    }
 
 
 
 
 
    @ApiOperation(value = "获取用户钱包信息-收支明细",tags = {"用户钱包模块"})
    @GetMapping("/getUserAccountDetail")
    public R<Page<TbAccountDetail>> getUserAccountDetail(UserAccountDetailQuery query) {
        LoginUser loginUser = tokenService.getLoginUser();
        LambdaQueryWrapper<TbAccountDetail> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(query.getStartTime()) && StringUtils.isNotEmpty(query.getEndTime())){
            wrapper.between(TbAccountDetail::getCreateTime,query.getStartTime(),query.getEndTime());
        }
        wrapper.eq(TbAccountDetail::getUserId,loginUser.getUserId());
        wrapper.orderByDesc(TbAccountDetail::getCreateTime);
        Page<TbAccountDetail> page = accountDetailService.page(new Page<>(query.getPageNum(),query.getPageSize()),wrapper);
        return R.ok(page);
    }
 
 
    @ApiOperation(value = "申请提现",tags = {"用户钱包模块"})
    @PostMapping("/withdrawal")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public R<?> withdrawal(@RequestBody @Valid UserWithdrawalDto dto) {
        if(dto.getAmount()<=0){
            return R.fail("提现金额不能小于0");
        }
        LoginUser loginUser = tokenService.getLoginUser();
        TbUser user = userService.getById(loginUser.getUserId());
        // 判断是否有卡
        List<TbBank> list = bankService.list(new LambdaQueryWrapper<TbBank>().eq(TbBank::getUserId, loginUser.getUserId()).eq(TbBank::getIsDelete, 0));
        if(list.isEmpty()){
            return R.fail("请先绑定银行卡");
        }
        if(user.getBalance().doubleValue()<dto.getAmount()){
            return R.fail("余额不足");
        }
        TbBank tbBank = list.get(0);
        TbWithdrawal withdrawal = new TbWithdrawal();
        withdrawal.setUserId(loginUser.getUserId().toString());
        withdrawal.setMoney(new BigDecimal(dto.getAmount()));
        withdrawal.setCardNo(tbBank.getCardNo());
        withdrawal.setUsername(tbBank.getUsername());
        withdrawal.setStatus(0);
        user.setBalance(user.getBalance().subtract(new BigDecimal(dto.getAmount())));
        userService.updateById(user);
        withdrawalService.save(withdrawal);
 
        // 存储记录
        TbAccountDetail accountDetail = new TbAccountDetail();
        accountDetail.setUserId(loginUser.getUserId().toString());
        accountDetail.setType(2);
        accountDetail.setCategory(1);
        accountDetail.setStatus(1);
        accountDetail.setMoney(new BigDecimal(dto.getAmount()));
        accountDetail.setSourceId(withdrawal.getId());
        accountDetailService.save(accountDetail);
        return R.ok();
    }
 
 
 
    @ApiOperation(value = "获取用户绑定的卡",tags = {"用户钱包模块"})
    @GetMapping("/getUserBank")
    public R<TbBank> getUserBank() {
        LoginUser loginUser = tokenService.getLoginUser();
        TbBank tbBank = bankService.getOne(new LambdaQueryWrapper<TbBank>().eq(TbBank::getUserId, loginUser.getUserId()).eq(TbBank::getIsDelete, 0));
        return R.ok(tbBank);
    }
 
 
    @ApiOperation(value = "保存银行卡",tags = {"用户钱包模块"})
    @PostMapping("/saveUserBank")
    public R<?> getUserBank(@Valid @RequestBody SaveUserBankDto dto) {
        LoginUser loginUser = tokenService.getLoginUser();
        TbBank tbBank = bankService.getOne(new LambdaQueryWrapper<TbBank>().eq(TbBank::getUserId, loginUser.getUserId()).eq(TbBank::getIsDelete, 0));
        if(tbBank==null){
            tbBank = new TbBank();
            tbBank.setUserId(loginUser.getUserId().toString());
            tbBank.setCardNo(dto.getCardNo());
            tbBank.setUsername(dto.getUsername());
            tbBank.setIsDelete(0);
            bankService.save(tbBank);
        }else {
            tbBank.setCardNo(dto.getCardNo());
            tbBank.setUsername(dto.getUsername());
            bankService.updateById(tbBank);
        }
        return R.ok();
    }
 
 
    @ApiOperation(value = "提现说明需求",tags = {"用户钱包模块"})
    @GetMapping("/getWithdrawalDetail")
    public R<GetWithdrawalDetailVo> getWithdrawalDetail(@RequestParam String sourceId) {
        TbWithdrawal withdrawal =  withdrawalService.getById(sourceId);
        GetWithdrawalDetailVo vo = new GetWithdrawalDetailVo();
        vo.setImg(withdrawal.getImg());
        vo.setRemark(withdrawal.getRemark());
        return R.ok(vo);
    }
 
 
 
 
 
 
}