zj
2025-01-06 0e7b38c2b3af72ea2a7f8a2fcbaad4d78e2c1977
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.gear.customer.swx.biz.impl;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gear.common.constant.SwxConstons;
import com.gear.common.core.redis.RedisCache;
import com.gear.common.exception.CustomerException;
import com.gear.common.utils.jwt.JWTTokenUtils;
import com.gear.common.utils.sign.Md5Utils;
import com.gear.customer.swx.biz.SwxBizUser;
import com.gear.customer.swx.vo.request.*;
import com.gear.customer.swx.vo.response.SwxUserInfoVo;
import com.gear.swx.domain.*;
import com.gear.swx.service.*;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
@Component
public class SwxBizUserImpl implements SwxBizUser {
 
    @Autowired
    private ISwxIdentificationService swxIdentificationService;
 
    @Autowired
    private ISwxUserService swxUserService;
 
    @Autowired
    private RedisCache redisCache;
 
    @Autowired
    private ISwxSettingsService swxSettingsService;
 
    @Autowired
    private ISwxRechargeRecordService swxRechargeRecordService;
 
    @Autowired
    private ISwxWithdrawalRecordService swxWithdrawalRecordService;
 
    @Autowired
    private ISwxNftService swxNftService;
 
    @Autowired
    private ISwxMoneyLogService moneyLogService;
 
    @Autowired
    private ISwxLoanRecordService swxLoanRecordService;
 
    @Autowired
    private ISwxUserLevelService swxUserLevelService;
    @Override
    @Transactional
    public String identification(String userId,SwxUserIdentificationVo swxUserIdentificationVo) throws CustomerException {
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        if (swxUser.getIdefinderStatus() == SwxConstons.SWX_EXAMINE_STATUS_REVIEW||swxUser.getIdefinderStatus()  == SwxConstons.SWX_EXAMINE_STATUS_YES){
            throw new CustomerException("10019");
        }
        SwxIdentification swxIdentification = new SwxIdentification();
        BeanUtils.copyProperties(swxUserIdentificationVo,swxIdentification);
        swxIdentification.setUserId(swxUser.getId());
        swxIdentificationService.save(swxIdentification);
        swxUser.setIdefinderStatus(SwxConstons.SWX_EXAMINE_STATUS_REVIEW);
        swxUserService.updateById(swxUser);
        return "20001";
    }
 
    @Override
    public SwxUserInfoVo getUserInfo(String userId) throws CustomerException{
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw  new CustomerException("10009");
        }
        SwxUserInfoVo info = new SwxUserInfoVo();
        BeanUtils.copyProperties(swxUser,info);
        if (!Strings.isEmpty(swxUser.getLevel())){
            SwxUserLevel userLevel = swxUserLevelService.getById(swxUser.getLevel());
            if (userLevel != null){
                info.setLevelLogo(userLevel.getLogo());
            }
        }
        SwxSettings swxSettings = swxSettingsService.getOne(new QueryWrapper<SwxSettings>().lambda().eq(SwxSettings::getParamKey,"virtual_account"));
 
        if(swxSettings != null && ((Integer.parseInt(swxSettings.getParamValue()))==SwxConstons.NOMARL_STATUS_YES)){
            long exprise = 7L;
           //判断当前系统中是否存在此虚拟账户
            if (redisCache.hasKey("SWX-CUSTOMER-VERTURAL-"+userId)){
                SwxUserInfoVo infoVo= redisCache.getCacheObject("SWX-CUSTOMER-VERTURAL-"+userId);
                info.setVirtuallyAmount(infoVo.getVirtuallyAmount() == null ? new BigDecimal(100000) : infoVo.getVirtuallyAmount());
                info.setVirtuallyBondAmount(infoVo.getVirtuallyBondAmount());
            }else{
                info.setVirtuallyAmount(new BigDecimal(1000000));
                info.setVirtuallyBondAmount(new BigDecimal(0));
                redisCache.setCacheObject("SWX-CUSTOMER-VERTURAL-"+swxUser.getId(),info,604800L, TimeUnit.SECONDS);
            }
            info.setVirtuallyStatus(SwxConstons.NOMARL_STATUS_YES);
        }
        info.setInviteNumber(String.valueOf(swxUser.getInviteNumber()));
        return info;
    }
 
    @Override
    public String recharge(String userId, SwxUserRechargeVo vo) throws CustomerException{
        if(vo.getAmount() == null || vo.getCurrency() == null || Strings.isEmpty(vo.getType())){
            throw new CustomerException("10018");
        }
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        SwxRechargeRecord swxRechargeRecord = new SwxUserRechargeVo();
        BeanUtils.copyProperties(vo,swxRechargeRecord);
        swxRechargeRecord.setUserId(swxUser.getId());
        swxRechargeRecord.setStatus(SwxConstons.SWX_EXAMINE_STATUS_REVIEW);
        swxRechargeRecordService.save(swxRechargeRecord);
        return "20002";
    }
 
    @Override
    public String withdrawal(String userId, SwxUserWithdrawalVo vo)throws CustomerException {
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
 
        SwxSettings swxSettings = swxSettingsService.getOne(new QueryWrapper<SwxSettings>().lambda().eq(SwxSettings::getParamKey,"withdrawal_fee"));
        BigDecimal fee = BigDecimal.ZERO;
        if(swxSettings != null && !Strings.isEmpty(swxSettings.getParamValue())){
            fee =  new BigDecimal(swxSettings.getParamValue());
        }
 
        SwxWithdrawalRecord swxWithdrawalRecord = new SwxWithdrawalRecord();
        BeanUtils.copyProperties(vo,swxWithdrawalRecord);
        swxWithdrawalRecord.setUserId(swxUser.getId());
        swxWithdrawalRecord.setStatus(SwxConstons.SWX_EXAMINE_STATUS_REVIEW);
        BigDecimal withdrawalFee = swxWithdrawalRecord.getAmount().multiply(fee).divide(new BigDecimal(100));
        swxWithdrawalRecord.setWithdrawalFee(withdrawalFee);
        swxWithdrawalRecord.setTrueAmount(swxWithdrawalRecord.getAmount().subtract(withdrawalFee));
        swxWithdrawalRecordService.save(swxWithdrawalRecord);
        return "20003";
    }
 
    @Override
    public IPage<SwxWithdrawalRecord> listWithdrwal(String userId,Integer pageNo,Integer pageSize) throws CustomerException{
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        Page<SwxWithdrawalRecord> page = new Page<SwxWithdrawalRecord>(pageNo, pageSize);
        QueryWrapper<SwxWithdrawalRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SwxWithdrawalRecord::getUserId,userId);
        queryWrapper.lambda().orderByDesc(SwxWithdrawalRecord::getCreateTime);
        IPage<SwxWithdrawalRecord> pageList = swxWithdrawalRecordService.page(page, queryWrapper);
        return pageList;
    }
 
    @Override
    public IPage<SwxRechargeRecord> listRecharge(String userId,Integer pageNo,Integer pageSize) throws CustomerException{
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        QueryWrapper<SwxRechargeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SwxRechargeRecord::getUserId,userId);
        queryWrapper.lambda().orderByDesc(SwxRechargeRecord::getCreateTime);
        Page<SwxRechargeRecord> page = new Page<SwxRechargeRecord>(pageNo, pageSize);
        IPage<SwxRechargeRecord> pageList = swxRechargeRecordService.page(page, queryWrapper);
        return pageList;
    }
 
    @Override
    public List<SwxNft> listNft(String userId) throws CustomerException{
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        QueryWrapper<SwxNft> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SwxNft::getUserId,userId);
        queryWrapper.lambda().orderByDesc(SwxNft::getCreateTime);
        return swxNftService.list(queryWrapper);
    }
 
    @Override
    @Transactional
    public String buyNft(String userId, SwxUserBuyNftVo swxUserBuyNftVo) throws CustomerException {
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        SwxNft swxNft = swxNftService.getById(swxUserBuyNftVo.getId());
        if (swxNft == null || swxNft.getStatus() != SwxConstons.NOMARL_STATUS_YES){
            throw new CustomerException("10020");
        }
        if(Strings.isEmpty(swxUserBuyNftVo.getAddress())){
            throw new CustomerException("10021");
        }
        //判断用户余额是否足够
        if(swxUser.getAmount().compareTo(swxNft.getCurrentPrice()) < 0){
            throw new CustomerException("10011");
        }
        //插入资金日志
        SwxMoneyLog moneyLog = new SwxMoneyLog();
        moneyLog.setInfo("购买nft:"+swxNft.getName());
        moneyLog.setStatus(SwxConstons.NOMARL_STATUS_YES);
        moneyLog.setType(SwxConstons.SWX_MONEY_LOG_TYPE_BUYNFT);
        moneyLog.setSymbol(SwxConstons.SWX_MONEY_TYPE_PAY);
        moneyLog.setTitle("nft公益");
        moneyLog.setOldAmount(swxUser.getAmount());
        //修改用户余额
        swxUser.setAmount(swxUser.getAmount().subtract(swxNft.getCurrentPrice()));
        swxUserService.updateById(swxUser);
        moneyLog.setNowAmount(swxUser.getAmount());
        moneyLog.setUserId(swxUser.getId());
        moneyLog.setBusiId(swxNft.getId());
        moneyLog.setMoney(swxNft.getCurrentPrice());
        moneyLogService.save(moneyLog);
        //修改nft状态
        swxNft.setUserId(userId);
        swxNft.setStatus(SwxConstons.NOMARL_STATUS_NO);
        swxNft.setAddress(swxUserBuyNftVo.getAddress());
        swxNftService.updateById(swxNft);
        return "20004";
    }
 
    @Override
    public String loan(String userId, SwxUserLoanVo swxUserLoanVo) throws CustomerException{
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        //判断用户是否实名认证
        if (swxUser.getIdefinderStatus() != SwxConstons.SWX_EXAMINE_STATUS_YES){
            throw new CustomerException("10022");
        }
 
        //获取借款期限和利率
        if(swxUserLoanVo.getAmount() == null || swxUserLoanVo.getTerm() == null){
            throw new CustomerException("10023");
        }
 
        //获取利率
        SwxSettings rateSettings = swxSettingsService.getOne(new QueryWrapper<SwxSettings>().lambda().eq(SwxSettings::getParamKey,"loan_rate"));
        if(rateSettings == null){
            throw new CustomerException("10018");
        }
 
        String rateStr = rateSettings.getParamValue();
 
        JSONArray jsonArray = JSONArray.parseArray(rateStr);
 
        BigDecimal rate = null;
 
        for(int i = 0; i < jsonArray.size(); i++){
            JSONObject object = jsonArray.getJSONObject(i);
            Integer term = object.getInteger("days");
            if (term == swxUserLoanVo.getTerm()){
                rate = new BigDecimal(object.getString("rate"));
                break;
            }
        }
        if (rate == null){
            throw new CustomerException("10018");
        }
 
        SwxLoanRecord swxLoanRecord = new SwxLoanRecord();
 
        BeanUtils.copyProperties(swxUserLoanVo,swxLoanRecord);
        swxLoanRecord.setUserId(userId);
        //审核中
        swxLoanRecord.setStatus(SwxConstons.SWX_EXAMINE_STATUS_REVIEW);
        swxLoanRecord.setRete(rate);
        swxLoanRecordService.save(swxLoanRecord);
 
 
        return "20001";
    }
 
    @Override
    public IPage<SwxLoanRecord> listLoanRecord(String userId,Integer pageNo,Integer pageSize)throws CustomerException {
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        QueryWrapper<SwxLoanRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SwxLoanRecord::getUserId,userId);
        queryWrapper.lambda().orderByDesc(SwxLoanRecord::getCreateTime);
        Page<SwxLoanRecord> page = new Page<SwxLoanRecord>(pageNo, pageSize);
        IPage<SwxLoanRecord> pageList = swxLoanRecordService.page(page, queryWrapper);
        return pageList;
    }
 
    @Override
    public String repassword(String userId,SwxUserRegisterVo vo) {
        if (Strings.isEmpty(vo.getPassword()) || Strings.isEmpty(vo.getRePaossword()) || !vo.getPassword().equals(vo.getRePaossword())){
            throw new CustomerException("10004");
        }
        SwxUser swxUser = swxUserService.getById(userId);
        if (swxUser == null){
            throw new CustomerException("10009");
        }
        String newPass = Md5Utils.hash(vo.getPassword());
        swxUser.setPassword(newPass);
        swxUserService.updateById(swxUser);
        return "20005";
    }
}