44323
2024-05-17 32866c0896969b3ac6f3eaddf0420507708c9c52
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package com.ruoyi.study.controller;
 
 
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.management.api.feignClient.ManagementClient;
import com.ruoyi.management.api.model.TPage;
import com.ruoyi.management.api.model.TVipSet;
import com.ruoyi.study.domain.TUser;
import com.ruoyi.study.domain.TVipOrder;
import com.ruoyi.study.dto.AppUserQuery;
import com.ruoyi.study.dto.UserInfoQuery;
import com.ruoyi.study.service.ITUserService;
import com.ruoyi.study.service.IVipOrderService;
import com.ruoyi.study.utils.PayMoneyUtil;
import com.ruoyi.study.utils.UUIDUtil;
import com.ruoyi.study.vo.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * <p>
 * 用户管理 控制器
 * </p>
 *
 * @author 无关风月
 * @since 2024-04-26
 */
@RestController
@RequestMapping("/base/user")
public class TUserController {
    @Autowired
    private ITUserService userService;
 
    @Autowired
    private IVipOrderService vipOrderService;
    @Autowired
    private ManagementClient managementClient;
    @PostMapping("/vipInfo")
    @ApiOperation(value = "会员中心-获取会员说明、当前登录用户是否为会员、会员购买规格", tags = {"家长端-个人中心"})
    public AjaxResult<List<VipInfoVO>> vipInfo() {
        List<VipInfoVO> vipInfoVOS = new ArrayList<>();
        List<TVipSet> data = managementClient.getVipSet1().getData();
        for (TVipSet datum : data) {
            VipInfoVO vipInfoVO = new VipInfoVO();
            vipInfoVO.setInfo(datum.getInfo());
            vipInfoVO.setId(datum.getId());
//            vipInfoVO.setIsVip();
            vipInfoVO.setTime(datum.getTime());
            vipInfoVO.setAmount(datum.getAmount());
        }
        return AjaxResult.ok(vipInfoVOS);
    }
    @Autowired
    private PayMoneyUtil payMoneyUtil;
 
    @PostMapping("/order")
    @ApiOperation(value = "购买会员下单操作", tags = {"家长端-个人中心"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header"),
            @ApiImplicitParam(name = "payType", value = "支付类型 1=微信 2=支付宝", required = true),
            @ApiImplicitParam(name = "id", value = "会员规格id", required = true),
    })
    public AjaxResult order(Integer payType,Integer id) throws Exception {
        TVipOrder tVipOrder = new TVipOrder();
        List<TVipSet> data = managementClient.getVipSet1().getData();
        Integer time = 0;
        for (TVipSet datum : data) {
            if (datum.getId() == id){
                tVipOrder.setMoney(datum.getAmount());
                time = datum.getTime();
            }
        }
        tVipOrder.setPayState(1);
//        tVipOrder.setUserId();
//        tVipOrder.setPayTime();
//        tVipOrder.setBackTime();
//        tVipOrder.setTime();
        tVipOrder.setPayType(payType);
        tVipOrder.setCount(time);
        switch (payType){
            case 1:
                return payMoneyUtil.weixinpay
                        ("购买会员", "",
                                id+"_"+tVipOrder.getId() + "_"+
                                        UUIDUtil.getRandomCode(8),
                                tVipOrder.getMoney().toString(),
                                "/base/wxPayBuyVip", "APP", "");
            case 2:
                return payMoneyUtil.alipay
                        ("购买会员",
                                "购买会员下单支付",
                                "",
                                id+"_"+tVipOrder.getId()+"_"+
                                        UUIDUtil.getRandomCode(8),
                                tVipOrder.getMoney().toString(),
                                "/base/aliPayBuyVip");
        }
        return AjaxResult.success();
    }
    @ResponseBody
    @PostMapping("/base/aliPayBuyVip")
    public void addVipPaymentAliCallback(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String> map = payMoneyUtil.alipayCallback(request);
            if (null != map) {
                String out_trade_no = map.get("out_trade_no");
                String trade_no = map.get("trade_no");
                // 会员规格id
                String s = out_trade_no.split("_")[0];
                Integer integer = Integer.valueOf(s);
                Integer time  = 0;
                for (TVipSet datum : managementClient.getVipSet1().getData()) {
                    if (datum.getId() == integer){
                        time = datum.getTime();
                    }
                }
                // 订单id
                String s1 = out_trade_no.split("_")[1];
                Integer integer1 = Integer.valueOf(s1);
                TVipOrder byId = vipOrderService.getById(integer1);
                byId.setPayState(2);
                byId.setTransactionId(trade_no);
                byId.setOutTradeNo(out_trade_no);
                byId.setPayTime(new Date());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, time);
                Date dateAfterOneMonth = calendar.getTime();
                byId.setTime(dateAfterOneMonth);
                // 修改订单状态
                vipOrderService.updateById(byId);
                TUser byId1 = userService.getById(byId.getUserId());
                if (byId1.getVipPayTime() == null){
                    // 是否是首次充值会员
                    byId1.setVipPayTime(new Date());
                }
                byId1.setVipEndTime(dateAfterOneMonth);
                // 修改用户会员续期信息
                userService.updateById(byId1);
                PrintWriter out = response.getWriter();
                out.write("success");
                out.flush();
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @ResponseBody
    @PostMapping("/base/wxPayBuyVip")
    public void wxPayBuyPackage(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String> map = payMoneyUtil.weixinpayCallback(request);
            if (null != map) {
                // 内部订单号
                String out_trade_no = map.get("out_trade_no");
                // 微信订单号
                String transaction_id = map.get("transaction_id");
                String result = map.get("result");
                // 会员规格id
                String s = out_trade_no.split("_")[0];
                Integer integer = Integer.valueOf(s);
                Integer time  = 0;
                for (TVipSet datum : managementClient.getVipSet1().getData()) {
                    if (datum.getId() == integer){
                        time = datum.getTime();
                    }
                }
                // 订单id
                String s1 = out_trade_no.split("_")[1];
                Integer integer1 = Integer.valueOf(s1);
                TVipOrder byId = vipOrderService.getById(integer1);
                byId.setPayState(2);
                byId.setTransactionId(transaction_id);
                byId.setOutTradeNo(out_trade_no);
                byId.setPayTime(new Date());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, time);
                Date dateAfterOneMonth = calendar.getTime();
                byId.setTime(dateAfterOneMonth);
                // 修改订单状态
                vipOrderService.updateById(byId);
                TUser byId1 = userService.getById(byId.getUserId());
                if (byId1.getVipPayTime() == null){
                    // 是否是首次充值会员
                    byId1.setVipPayTime(new Date());
                }
                byId1.setVipEndTime(dateAfterOneMonth);
                // 修改用户会员续期信息
                userService.updateById(byId1);
                PrintWriter out = response.getWriter();
                out.write(result);
                out.flush();
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @PostMapping("/getPage")
    @ApiOperation(value = "获取注意事项", tags = {"家长端-注意事项"})
    public AjaxResult<TPage> getPage() {
        List<TPage> data = managementClient.getPage1().getData();
        for (TPage datum : data) {
            if (datum.getType() == 4){
                return AjaxResult.success(datum);
            }
        }
        return AjaxResult.success();
    }
    @Autowired
    private TokenService tokenService;
//    @Autowired
//    private HWSendSms hwSendSms;
//    @ResponseBody
//    @PostMapping("/getSMSCode")
//    @ApiOperation(value = "获取短信验证码", tags = {"家长端-登录注册"})
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "类型(1:登录,2:注册,3:修改密码,4:忘记密码,5:修改绑定手机号)", name = "type", dataType = "int", required = true),
//            @ApiImplicitParam(value = "电话号码", name = "phone", dataType = "string", required = true)
//    })
//    public AjaxResult getSMSCode(Integer type, String phone) {
//        if (ToolUtil.isEmpty(phone)) {
//            return AjaxResult.paranErr("phone");
//        }
//        if (ToolUtil.isEmpty(type)) {
//            return AjaxResult.paranErr("type");
//        }
//        try {
//            if (type == 2) {
//                AppUser tAppUser = appUserService.selectOne(new EntityWrapper<AppUser>()
//                        .eq("phone", phone).ne("state", 3));
//                if (null != tAppUser) {
//                    return AjaxResult.error("账号已存在");
//                }
//            }
//            if (type == 5) {
//                AppUser tAppUser = appUserService.selectOne(new EntityWrapper<AppUser>()
//                        .eq("phone", phone).ne("state", 3));
//                if (null != tAppUser) {
//                    return AjaxResult.error("账号已存在");
//                }
//            }
//            String numberRandom = UUIDUtil.getNumberRandom(6);
//            String templateCode = "";
//            if (type == 1 || type == 2) {
//                templateCode = "SMS_161275250";
//            }
//            if (type == 3 || type == 4) {
//                templateCode = "SMS_160960014";
//            }
////            aLiSendSms.sendSms(phone, templateCode, "{\"code\":\"" + numberRandom + "\"}");
//            hwSendSms.sendSms(numberRandom,phone);
//            redisUtil.setStrValue(phone, numberRandom, 300);
//            return AjaxResult.success();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultUtil.runErr();
//        }
//    }
 
    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "修改个人资料", tags = {"家长端-个人中心"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header"),
            @ApiImplicitParam(name = "name", value = "姓名 改什么就只传什么"),
            @ApiImplicitParam(name = "phone", value = "电话 改什么就只传什么"),
            @ApiImplicitParam(name = "headImg", value = "头像 改什么就只传什么"),
    })
    public AjaxResult updateUserInfo(String name, String phone,String headImg) {
        // todo 获取用户id
        Long userid = tokenService.getLoginUser().getUserid();
        TUser byId = userService.getById(userid);
        if (StringUtils.hasLength(name)){
            byId.setName(name);
        }
        if (StringUtils.hasLength(phone)){
            // 先判断手机号是否和当前用户手机号一致
//            if (byId == null){
//                return AjaxResult.tokenError("登录失效");
//            }
            if (phone.equals(byId.getPhone())){
                return AjaxResult.error("更换的手机号不能和原手机号相同");
            }
            byId.setPhone(phone);
        }
        if (StringUtils.hasLength(headImg)){
            byId.setHeadImg(headImg);
        }
        userService.updateById(byId);
        return AjaxResult.success("修改成功");
    }
    @PostMapping("/vipBack/{id}")
    @ApiOperation(value = "会员退款", tags = {"管理后台-会员退款"})
    public R vipBack(@PathVariable("id") Integer id) throws AlipayApiException {
        TVipOrder byId = vipOrderService.getById(id);
        // 外部订单号
        String transactionId = byId.getTransactionId();
        // 内部订单号
        String outTradeNo = byId.getOutTradeNo();
        switch (byId.getPayType()){
            case 1:
                 // 微信退款
                Map<String, String> stringStringMap = payMoneyUtil.wxRefund(transactionId, outTradeNo, byId.getMoney().toString(), byId.getMoney().toString(), "/base/wxRefund");
                if (stringStringMap.get("code").equals("SUCCESS")){
                    byId.setPayState(3);
                    byId.setBackTime(new Date());
                    vipOrderService.updateById(byId);
                    // 用户的vip剩余时间减少
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.MONTH, -byId.getCount());
                    Date dateAfterOneMonth = calendar.getTime();
                    Integer userId = byId.getUserId();
                    TUser byId1 = userService.getById(userId);
                    // 判断用户是不是第一次充值
                    List<TVipOrder> list = vipOrderService.list(new QueryWrapper<TVipOrder>()
                            .eq("userId", userId)
                            .eq("payState", 2)
                            .orderByDesc("createTime"));
                    int size = list.size();
                    if (size == 0){
                        // 证明这是用户第一次充值会员 将首次充值会员时间和会员到期时间清空
                        byId1.setVipEndTime(null);
                        byId1.setVipPayTime(null);
                        userService.updateById(byId1);
                    }else{
                        // 最近的一次充值会员时间
                        TVipOrder tVipOrder = list.get(0);
                        // 将会员到期时间回退到上一次
                        byId1.setVipEndTime(tVipOrder.getTime());
                        userService.updateById(byId1);
                    }
 
                    return R.ok();
                }else{
                    return R.fail(stringStringMap.get("msg"));
                }
            case 2:
                // 支付宝退款
                Map<String, String> stringStringMap1 = payMoneyUtil.aliRefund(transactionId,byId.getMoney().toString());
                if (stringStringMap1.get("code").equals("10000")){
                    byId.setPayState(3);
                    byId.setBackTime(new Date());
                    vipOrderService.updateById(byId);
                    // 用户的vip剩余时间减少
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.MONTH, -byId.getCount());
                    Date dateAfterOneMonth = calendar.getTime();
                    Integer userId = byId.getUserId();
                    TUser byId1 = userService.getById(userId);
                    // 判断用户是不是第一次充值
                    List<TVipOrder> list = vipOrderService.list(new QueryWrapper<TVipOrder>()
                            .eq("userId", userId)
                            .eq("payState", 2)
                            .orderByDesc("createTime"));
                    int size = list.size();
                    if (size == 0){
                        // 证明这是用户第一次充值会员 将首次充值会员时间和会员到期时间清空
                        byId1.setVipEndTime(null);
                        byId1.setVipPayTime(null);
                        userService.updateById(byId1);
                    }else{
                        // 最近的一次充值会员时间
                        TVipOrder tVipOrder = list.get(0);
                        // 将会员到期时间回退到上一次
                        byId1.setVipEndTime(tVipOrder.getTime());
                        userService.updateById(byId1);
                    }
                    return R.ok();
                }else{
                    return R.fail(stringStringMap1.get("msg"));
                }
        }
        return R.ok();
    }
    @PostMapping("/userList")
    @ApiOperation(value = "用户列表", tags = {"管理后台-用户管理"})
    public R<PageInfo<AppUserVO>> couponReceive(@RequestBody AppUserQuery query) {
        PageInfo<AppUserVO> res = new PageInfo<>(query.getPageNumber(), query.getPageSize());
        List<AppUserVO> list =  userService.listAll(query);
        for (AppUserVO appUserVO : list) {
            if (appUserVO.getVipEndTime() == null){
                appUserVO.setIsVip(0);
            }else{
                if (appUserVO.getVipEndTime().getTime() > System.currentTimeMillis()){
                    appUserVO.setIsVip(1);
                }else{
                    appUserVO.setIsVip(0);
                }
            }
        }
        res.setRecords(list);
        res.setTotal(list.size());
        return R.ok(res);
    }
 
    @PostMapping("/getUserInfo")
    @ApiOperation(value = "查看用户详情", tags = {"管理后台-用户管理"})
    public R<UserInfoVO> getUserInfo(@RequestBody UserInfoQuery dto) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        UserInfoVO res = new UserInfoVO();
        PageInfo<UserGameRecordVO> list = new PageInfo<>(dto.getPageNumber(), dto.getPageSize());
 
 
        TUser byId = userService.getById(dto.getId());
        String name = byId.getName();
        String phone = byId.getPhone();
        Date insertTime = byId.getInsertTime();
        Date vipPayTime = byId.getVipPayTime();
        Date vipEndTime = byId.getVipEndTime();
        if (vipEndTime == null){
            res.setIsVip(0);
        }else{
            if (vipEndTime.getTime() > System.currentTimeMillis()){
                res.setIsVip(1);
            }else{
                res.setIsVip(0);
            }
        }
        if (vipPayTime!=null){
            res.setVipPayTime(format.format(vipPayTime));
        }
        res.setState(byId.getState());
        res.setName(name);
        res.setPhone(phone);
        res.setInsertTime(format.format(insertTime));
        res.setIntegral(byId.getIntegral());
        // todo 查询进度
        res.setCurrent(0);
        res.setSurplus(0);
        res.setTotalHours(0d);
        res.setTodayHours(0d);
        res.setWeekHours(0d);
        res.setMonthHours(0d);
        // todo 查询用户的游戏记录
        List<UserGameRecordVO> userGameRecordVOS = new ArrayList<>();
        list.setRecords(userGameRecordVOS);
        list.setTotal(0);
        res.setGameRecords(list);
        return R.ok(res);
    }
    @PostMapping("/freeze/{id}")
    @ApiOperation(value = "冻结/解冻", tags = {"管理后台-用户管理"})
    public R freeze(@PathVariable("id")  Integer id) {
        TUser byId = userService.getById(id);
        if (byId.getState() == 1) {
            byId.setState(2);
            userService.updateById(byId);
            return R.ok("冻结成功");
        }else {
            byId.setState(1);
            userService.updateById(byId);
            return R.ok("解冻成功");
        }
    }
 
 
    @PostMapping("/vipOrderList")
    @ApiOperation(value = "列表查询", tags = {"管理后台-会员管理"})
    public R<PageInfo<VipOrderVO>> vipOrderList(@RequestBody AppUserQuery query) {
        PageInfo<VipOrderVO> res = new PageInfo<>(query.getPageNumber(), query.getPageSize());
        List<VipOrderVO> list =  vipOrderService.listAll(query);
        res.setRecords(list);
        res.setTotal(list.size());
        return R.ok(res);
    }
}