无关风月
2024-09-14 3f481005be717250a2ea87ff9367aa84d6a3eb13
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
package com.xinquan.user.controller.client;
 
 
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinquan.common.core.domain.R;
import com.xinquan.common.core.utils.page.BeanUtils;
import com.xinquan.common.core.utils.page.PageDTO;
import com.xinquan.common.core.web.domain.BaseModel;
import com.xinquan.common.security.utils.SecurityUtils;
import com.xinquan.order.api.feign.RemoteOrderService;
import com.xinquan.system.api.RemoteBannerService;
import com.xinquan.system.api.domain.AppUser;
import com.xinquan.system.api.domain.AppUserTree;
import com.xinquan.system.api.domain.AppUserViewingHistory;
import com.xinquan.system.api.domain.UserLevelSetting;
import com.xinquan.system.api.domain.vo.*;
import com.xinquan.user.domain.dto.UserAnswerDTO;
import com.xinquan.user.domain.vo.TagVO;
import com.xinquan.user.service.AppUserService;
import com.xinquan.user.service.AppUserTreeService;
import com.xinquan.user.service.AppUserViewingHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
 
import static com.xinquan.common.core.enums.TreeLevelEnum.TOWERING_TREES;
 
/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-08-21
 */
@Api(tags = {"用户相关接口"})
@Slf4j
@RestController
@RequestMapping("/client/app-user")
@RequiredArgsConstructor
public class ClientAppUserController {
 
    private final AppUserService appUserService;
    @Resource
    private AppUserViewingHistoryService appUserViewingHistoryService;
 
    @Resource
    private AppUserTreeService appUserTreeService;
    @Resource
    private RemoteBannerService remoteBannerService;
    @Resource
    private RemoteOrderService remoteOrderService;
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDateTime = dateTime.format(formatter);
        System.err.println(formattedDateTime);
    }
    /**
     * 爱心助力榜单-分页
     *
     * @param pageCurr    分页参数,当前页码
     * @param pageSize    分页参数,每页数量
     * @return 课程分页列表
     */
    @PostMapping("/myInviteRankList")
    @ApiOperation(value = "我的助力-分页", tags = {"个人中心"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分页参数,当前页码", name = "pageCurr", required = true, dataType = "Integer"),
            @ApiImplicitParam(value = "分页参数,每页数量", name = "pageSize", required = true, dataType = "Integer")
    })
    public R<PageDTO<InviteRankListVO>> myInviteRankList(
            @RequestParam(value = "pageCurr", defaultValue = "1") Integer pageCurr,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Long userId = SecurityUtils.getUserId();
        if (userId == 0)return R.tokenError("登录失效");
        Page<AppUser> page = appUserService.lambdaQuery().ne(AppUser::getUserStatus, 3)
                .eq(AppUser::getInviteUserId, userId)
                .page(new Page<>(pageCurr, pageSize));
        // 查询登录用户邀请了哪些人
        List<Long> collect = page.getRecords().stream().map(AppUser::getId).collect(Collectors.toList());
        if (collect.isEmpty())return R.ok(PageDTO.empty(page));
        StringBuilder stringBuilder = new StringBuilder();
        collect.forEach(id -> stringBuilder.append(id).append(","));
        // 去除最后一位
        StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        String[] split = stringBuilder1.toString().split(",");
        for (int i = 0; i < page.getRecords().size(); i++) {
            AppUser appUser = page.getRecords().get(i);
            appUser.setMoney(new BigDecimal(split[i]));
        }
        // 根据佣金金额 从大到小排序
        page.getRecords().sort((o1, o2) -> o2.getMoney().compareTo(o1.getMoney()));
        return R.ok(PageDTO.of(page, InviteRankListVO.class));
    }
    /**
     * 爱心助力榜单-分页
     *
     * @param pageCurr    分页参数,当前页码
     * @param pageSize    分页参数,每页数量
     * @return 课程分页列表
     */
    @PostMapping("/inviteRankList")
    @ApiOperation(value = "爱心助力榜单-分页", tags = {"个人中心"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分页参数,当前页码", name = "pageCurr", required = true, dataType = "Integer"),
            @ApiImplicitParam(value = "分页参数,每页数量", name = "pageSize", required = true, dataType = "Integer")
    })
    public R<PageDTO<InviteRankListVO>> inviteRankList(
            @RequestParam(value = "pageCurr", defaultValue = "1") Integer pageCurr,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<AppUser> page = appUserService.lambdaQuery().ne(AppUser::getUserStatus, 3)
                .page(new Page<>(pageCurr, pageSize));
        for (AppUser appUser : page.getRecords()) {
            int size = appUserService.lambdaQuery().ne(AppUser::getUserStatus, 3)
                    .eq(AppUser::getInviteUserId, appUser.getId())
                    .list().size();
            appUser.setCount(size);
        }
        // 根据帮助人数 从大到小排序
        page.getRecords().sort((o1, o2) -> o2.getCount() - o1.getCount());
        return R.ok(PageDTO.of(page, InviteRankListVO.class)) ;
    }
    @PostMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息", tags = {"个人中心"})
    public R<AppUserInfoVO> getUserInfo() {
        Long userId = SecurityUtils.getUserId();
        if (userId == 0)return R.tokenError("登录失效");
        AppUserInfoVO appUserInfoVO = new AppUserInfoVO();
        AppUserVO currentUser = appUserService.getCurrentUser();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (currentUser.getVipExpireTime() == null){
            appUserInfoVO.setIsVip(2);
        }else if (currentUser.getVipExpireTime().isAfter(LocalDateTime.now())){
            appUserInfoVO.setIsVip(1);
            String formattedDateTime =currentUser.getVipExpireTime().format(formatter);
            appUserInfoVO.setVipExpireTime(formattedDateTime);
        }else{
            String formattedDateTime =currentUser.getVipExpireTime().format(formatter);
            appUserInfoVO.setVipExpireTime(formattedDateTime);
            appUserInfoVO.setIsVip(2);
        }
        // 查询用户累计学习天数
        List<AppUserViewingHistory> com = appUserViewingHistoryService.cumulative(userId);
        appUserInfoVO.setCumulative(com.size());
        // 查询用户今日学习多少分钟
        int temp = appUserViewingHistoryService.today(userId);
        if (temp == 0){
            appUserInfoVO.setToday(0);
        }else if (temp<60){
            // 不足一分钟按一分钟计算
            appUserInfoVO.setToday(1);
        }else{
            appUserInfoVO.setToday(temp/60);
        }
        // 查询用户连续观看天数
        List<AppUserViewingHistory> list = appUserViewingHistoryService.lambdaQuery().eq(AppUserViewingHistory::getAppUserId, userId)
                .eq(AppUserViewingHistory::getViewingType, 1)
                .orderByDesc(BaseModel::getCreateTime).list();
        Set<LocalDate> viewingDates = list.stream()
                .map(record -> LocalDate.parse(record.getCreateTime().toLocalDate().toString(), formatter))
                .collect(Collectors.toCollection(HashSet::new));
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        // 计算连续观看天数
        int consecutiveDays = 0;
        LocalDate currentDate = today;
        // 如果今天没有观看 也进入循环判断
        while (viewingDates.contains(currentDate)||LocalDate.parse(currentDate.toString(), formatter).equals(today)) {
            if (!viewingDates.contains(currentDate)){
                // 如果今天没有观看
                currentDate = currentDate.minusDays(1);
                continue;
            }
            consecutiveDays++;
            currentDate = currentDate.minusDays(1);
        }
        appUserInfoVO.setToday(consecutiveDays);
        List<AppUserTree> list1 = appUserTreeService.lambdaQuery().eq(AppUserTree::getAppUserId, userId)
                .list();
        // 查询用户等级最高的那颗树苗
        AppUserTree tree = list1.stream().max((o1, o2) -> {
            if (o1.getTreeLevelType() > o2.getTreeLevelType()) {
                return 1;
            } else if (o1.getTreeLevelType() < o2.getTreeLevelType()) {
                return -1;
            } else {
                return 0;
            }
        }).orElse(null);
        // 查询疗愈等级 名称 图标
        int level = 1;
        if (tree != null){
            level = tree.getTreeLevelType();
        }
        appUserInfoVO.setLevel(level);
        // 根据等级查询疗愈名称和图标
        UserLevelSetting data = remoteBannerService.getIconNameByLevel(level).getData();
        appUserInfoVO.setLevelName(data.getLevelName());
        appUserInfoVO.setLevelIcon(data.getLevelIcon());
        return R.ok(appUserInfoVO);
    }
    @PostMapping("/getUserDetail")
    @ApiOperation(value = "获取个人资料", tags = {"个人中心"})
    public R<AppUser> getUserDetail() {
        Long userId = SecurityUtils.getUserId();
        if (userId == 0)return R.tokenError("登录失效");
        AppUser appUser = appUserService.lambdaQuery().eq(AppUser::getId, userId).one();
        return R.ok(appUser);
    }
    @PostMapping("/getTotalEnergyValue")
    @ApiOperation(value = "获取用户当前累计能量值",tags = "树苗打卡站")
    public R getTotalEnergyValue() {
        Long userId = SecurityUtils.getUserId();
        if (userId==0)return R.tokenError("登录失效");
        AppUser byId = appUserService.getById(userId);
        return R.ok(byId.getTotalEnergyValue());
    }
    @PostMapping("/updateUserDetail")
    @ApiOperation(value = "修改个人资料", tags = {"个人中心"})
    public R<AppUser> updateUserDetail(UpdateAppUserDTO dto) {
        Long userId = SecurityUtils.getUserId();
        if (userId == 0)return R.tokenError("登录失效");
        AppUser byId = appUserService.getById(userId);
        BeanUtils.copyProperties(dto, byId);
//        LambdaUpdateWrapper<AppUser> updateWrapper = new LambdaUpdateWrapper<>(AppUser.class);
//        updateWrapper.set(AppUser::getNickname, byId.getNickname());
//        updateWrapper.set(AppUser::getSignature, byId.getSignature());
//        updateWrapper.set(AppUser::getAvatar, byId.getAvatar());
//        updateWrapper.set(AppUser::getGender, byId.getGender());
//        updateWrapper.set(AppUser::getBirthday, byId.getBirthday());
//        updateWrapper.set(AppUser::getEducation, byId.getEducation());
//        updateWrapper.set(AppUser::getIndustry, byId.getIndustry());
//        updateWrapper.set(AppUser::getCompany, byId.getCompany());
//        updateWrapper.set(AppUser::getOccupation, byId.getOccupation());
//        updateWrapper.set(AppUser::getLocation, byId.getLocation());
//        updateWrapper.set(AppUser::getHometown, byId.getHometown());
//        appUserService.update(byId, )
        return R.ok();
    }
    /**
     * 获取当前登录用户信息
     *
     * @return 用户信息
     * @see AppUserVO
     */
    @PostMapping("/getCurrentUser")
    @ApiOperation(value = "获取当前用户信息", tags = {"用户端-用户信息相关接口"})
    public R<AppUserVO> getCurrentUser() {
        return R.ok(appUserService.getCurrentUser());
    }
 
    /**
     * 通过手机号查询用户端用户信息
     *
     * @return 用户信息
     * @see AppUserVO
     */
    @PostMapping("/getUserByPhone")
    @ApiOperation(value = "根据用户手机号查询用户信息")
    public R<AppUserDetailVO> getCurrentUser(String phone) {
        AppUser one = appUserService.lambdaQuery().eq(AppUser::getCellPhone, phone).one();
        AppUserDetailVO appUserDetailVO = new AppUserDetailVO();
        appUserDetailVO.setId(one.getId());
        appUserDetailVO.setCellPhone(one.getCellPhone());
        appUserDetailVO.setAvatar(one.getAvatar());
        appUserDetailVO.setNickname(one.getNickname());
        return R.ok(appUserDetailVO);
    }
    @PostMapping("/getUserBalance")
    @ApiOperation(value = "查询当前用户余额")
    public R<String> getUserBalance() {
        Long userId = SecurityUtils.getUserId();
        if (userId == 0)return R.tokenError("登录失效");
        AppUser one = appUserService.lambdaQuery().eq(AppUser::getUserId, userId).one();
        return R.ok(one.getBalance().toString());
    }
 
    /**
     * 获取问题二的标签列表
     *
     * @return List<TagVO>
     */
    @GetMapping("/getTagList")
    @ApiOperation(value = "问题二获取用户标签列表", tags = {"用户端-计划引导相关接口"})
    public R<List<TagVO>> getTagList() {
        List<TagVO> voList = appUserService.getTagList();
        return R.ok(voList);
    }
 
    /**
     * 保存计划引导页用户的答案
     *
     * @param dto 用户计划引导答案数据传输对象
     * @return
     */
    @PostMapping("/saveUserAnswers")
    @ApiOperation(value = "保存计划引导页用户的答案", tags = {"用户端-计划引导相关接口"})
    public R<?> saveUserAnswers(@Validated @RequestBody UserAnswerDTO dto) {
        appUserService.saveUserAnswers(dto);
        return R.ok();
    }
 
 
}