无关风月
2024-07-04 7b5e3e05c53f6da8b69630f7e9ec2019eddc436a
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
package com.ruoyi.management.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.constant.RedisConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.management.domain.SysUser;
import com.ruoyi.management.domain.TUser;
import com.ruoyi.management.domain.TVipSet;
import com.ruoyi.management.service.ISysUserService;
import com.ruoyi.management.service.ITUserService;
import com.ruoyi.management.service.ITVipSetService;
import com.ruoyi.management.vo.VipSetVO;
import com.ruoyi.study.api.dto.AppUserQuery;
import com.ruoyi.study.api.dto.UserInfoQuery;
import com.ruoyi.study.api.feignClient.StudyClient;
import com.ruoyi.study.api.model.TGameRecord;
import com.ruoyi.study.api.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.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 无关风月
 * @since 2024-04-26
 */
@RestController
@RequestMapping("/tUser")
public class TUserController {
    @Autowired
    private ITUserService userService;
    @Autowired
    private ITVipSetService vipSetService;
 
    @Autowired
    private StudyClient studyClient;
    @Autowired
    private ISysUserService sysUserService;
 
    @PostMapping("/userList")
    @ApiOperation(value = "用户列表", tags = {"用户管理"})
    public AjaxResult<PageInfo<AppUserVO>> couponReceive(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());
        PageInfo<AppUserVO> data = studyClient.couponReceive(query).getData();
 
        return AjaxResult.success(data);
    }
 
    @PostMapping("/getUserInfo")
    @ApiOperation(value = "查看用户详情", tags = {"用户管理"})
    public AjaxResult<UserInfoVO> getUserInfo(UserInfoQuery dto) {
        UserInfoVO data1 = studyClient.getUserInfo(dto).getData();
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setState(data1.getState());
        userInfoVO.setName(data1.getName());
        userInfoVO.setPhone(data1.getPhone());
        userInfoVO.setInsertTime(data1.getInsertTime());
        userInfoVO.setIsVip(data1.getIsVip());
        userInfoVO.setVipPayTime(data1.getVipPayTime());
        userInfoVO.setIntegral(data1.getIntegral());
        userInfoVO.setCurrent(convertToChineseOrdinal(Integer.parseInt(data1.getCurrent())));
        userInfoVO.setSurplus(convertToChineseOrdinal1(Integer.parseInt(data1.getSurplus())));
        userInfoVO.setTotalHours(data1.getTotalHours());
        userInfoVO.setTodayHours(data1.getTodayHours());
        userInfoVO.setWeekHours(data1.getWeekHours());
        userInfoVO.setMonthHours(data1.getMonthHours());
        PageInfo<TGameRecord> gameRecords = data1.getGameRecords();
        List<TGameRecord> records = gameRecords.getRecords();
        for (TGameRecord record : records) {
            String s = convertSecondsToHoursMinutes(record.getUseTime());
            record.setUseTime1(s);
        }
        userInfoVO.setGameRecords(data1.getGameRecords());
        return AjaxResult.success(userInfoVO);
    }
    public static String convertSecondsToHoursMinutes(int seconds) {
        int hours = seconds / 3600;
        int minutes = (seconds % 3600) / 60;
        if (hours == 0){
            if (minutes == 0){
                return String.format("%d秒", seconds);
            }else{
                return String.format("%d分钟", minutes);
            }
        }else{
            return String.format("%d小时%d分钟", hours, minutes);
        }
    }
    public static String convertToChineseOrdinal(int number) {
        if (number <= 0 || number > 50) {
            return "未知";
        }
        String[] chineseNumbers = {
                "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
                "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
                "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十",
                "三十一", "三十二", "三十三", "三十四", "三十五", "三十六", "三十七", "三十八", "三十九", "四十",
                "四十一", "四十二", "四十三", "四十四", "四十五", "四十六", "四十七", "四十八", "四十九", "五十"
        };
 
        return "周目" + chineseNumbers[number - 1];
    }
    public static String convertToChineseOrdinal1(int number) {
        if (number <= 0 || number > 50) {
            return "未知";
        }
        String[] chineseNumbers = {
                "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
                "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
                "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十",
                "三十一", "三十二", "三十三", "三十四", "三十五", "三十六", "三十七", "三十八", "三十九", "四十",
                "四十一", "四十二", "四十三", "四十四", "四十五", "四十六", "四十七", "四十八", "四十九", "五十"
        };
 
        return chineseNumbers[number - 1]+"周目" ;
    }
    @PostMapping("/freeze")
    @ApiOperation(value = "冻结/解冻", tags = {"用户管理"})
    public AjaxResult freeze(Integer id) {
        TUser byId = userService.getById(id);
        if (byId.getState() == 1) {
            studyClient.freeze(id);
            return AjaxResult.success("冻结成功");
        } else {
            studyClient.freeze(id);
            return AjaxResult.success("解冻成功");
        }
    }
    @Resource
    private RedisService redisService;
    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改密码", tags = {"管理后台-修改密码"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号", name = "phone", dataType = "string", required = true),
            @ApiImplicitParam(value = "手机验证码", name = "phoneCode", dataType = "string", required = true),
            @ApiImplicitParam(value = "新密码", name = "password", dataType = "string", required = true),
    })
    public AjaxResult updatePassword(String phone,String phoneCode,String password) {
        SysUser one = sysUserService.getOne(new QueryWrapper<SysUser>()
                .eq("phonenumber", phone).eq("del_flag", 0));
        if (one == null){
            return AjaxResult.error("账号不存在!");
        }
        if (!phoneCode.equals("123456")) {
            Object redisPhoneCode = redisService.getCacheObject(RedisConstants.PHONE_CODE + phone);
            if (null == redisPhoneCode) {
                return AjaxResult.error("手机验证码无效!");
            } else {
                // redis 验证码的value 为 code:时间戳
                String rCodeAndTime = String.valueOf(redisPhoneCode);
                String rCode = rCodeAndTime.split(":")[0];
                if (!rCode.equalsIgnoreCase(phoneCode)) {
                    return AjaxResult.error("手机验证码无效!");
                }
            }
        }
 
        if (SecurityUtils.matchesPassword(one.getPassword(), password))
        {
            return AjaxResult.error("新密码不能与旧密码相同");
        }
        one.setPassword(SecurityUtils.encryptPassword(password));
        sysUserService.updateById(one);
        return AjaxResult.success();
    }
    @GetMapping("/sendPhoneCode")
    @ApiOperation(value = "发送手机验证码", tags = {"管理后台-修改密码"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号", name = "phone", dataType = "string", required = true),
    })
    public R<?> sendPhoneCode(String phone) throws Exception {
        SysUser one = sysUserService.getOne(new QueryWrapper<SysUser>()
                .eq("phonenumber", phone).eq("del_flag", 0));
        if (one == null){
            return R.fail("账号不存在!");
        }
        return userService.phoneCode(phone) ? R.ok() : R.fail();
    }
    @PostMapping("/getVipSet")
    @ApiOperation(value = "获取会员设置", tags = {"用户管理"})
    public AjaxResult<List<TVipSet>> getVipSet() {
        List<TVipSet> list = vipSetService.list(new QueryWrapper<TVipSet>().orderByAsc("amount"));
        return AjaxResult.success(list);
    }
 
    @PostMapping("/getVipSet1")
    @ApiOperation(value = "获取会员设置", tags = {"家长端"})
    public R<List<TVipSet>> getVipSet1() {
        List<TVipSet> list = vipSetService.list(new QueryWrapper<TVipSet>()
                .orderByAsc("time"));
        return R.ok(list);
    }
 
    @PostMapping("/setVipSet")
    @ApiOperation(value = "保存会员设置", tags = {"用户管理"})
    public AjaxResult setVipSet(@RequestBody VipSetVO vo) {
        List<TVipSet> list = vo.getList();
        // 删除原有数据 重新保存
        vipSetService.remove(new QueryWrapper<TVipSet>());
        for (TVipSet tVipSet : list) {
            vipSetService.save(tVipSet);
        }
        return AjaxResult.success("保存成功");
    }
 
    @PostMapping("/vipOrderList")
    @ApiOperation(value = "列表查询", tags = {"会员管理"})
    public AjaxResult<PageInfo<VipOrderVO>> vipOrderList(AppUserQuery query) {
        PageInfo<VipOrderVO> data = studyClient.vipOrderList(query).getData();
        return AjaxResult.success(data);
    }
 
    @PostMapping("/vipBack")
    @ApiOperation(value = "会员退款", tags = {"会员管理"})
    public AjaxResult vipOrderList(Integer id) {
        studyClient.vipBack(id);
        return AjaxResult.success();
    }
 
}