blob: e78b8b45ae24899e36a99bbcdba9dd95f8e722ab [file] [log] [blame]
xiukira687b9cb2025-05-29 15:15:02 +08001package com.g9.g9backend.controller;
2
xiukiradbe96222025-06-06 21:25:09 +08003import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
xiukiraac78f3d2025-06-08 23:38:10 +08005import com.baomidou.mybatisplus.core.metadata.IPage;
6import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
xiukiradbe96222025-06-06 21:25:09 +08007import com.g9.g9backend.pojo.*;
8import com.g9.g9backend.pojo.DTO.*;
9import com.g9.g9backend.service.*;
xiukira687b9cb2025-05-29 15:15:02 +080010import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
xiukirab9490272025-06-06 21:53:52 +080012import org.springframework.http.HttpStatus;
xiukiradbe96222025-06-06 21:25:09 +080013import org.springframework.http.ResponseEntity;
xiukirad0a7a082025-06-05 16:28:08 +080014import org.springframework.web.bind.annotation.*;
15
xiukiraac78f3d2025-06-08 23:38:10 +080016import java.util.*;
xiukiradbe96222025-06-06 21:25:09 +080017
xiukira687b9cb2025-05-29 15:15:02 +080018
19/**
20 * ResourceController 资源控制器类,处理与资源相关的请求
21 *
22 * @author hcy
23 */
24@RestController
25@RequestMapping("/resource")
26public class ResourceController {
27
xiukiradbe96222025-06-06 21:25:09 +080028 private final ResourceService resourceService;
29
30 private final GameplayService gameplayService;
31
32 private final RewardService rewardService;
33
34 private final UserUploadService userUploadService;
35
36 private final CommunityService communityService;
37
38 private final UserService userService;
39
40 private final UserPurchaseService userPurchaseService;
41
42 private final UserLikeService userLikeService;
43
44 private final UserCollectionService userCollectionService;
45
46 private final NotificationService notificationService;
47
xiukiraac78f3d2025-06-08 23:38:10 +080048 private final ResourceVersionService resourceVersionService;
49
50 private final SearchHistoryService searchHistoryService;
51
52 private final GameVersionService gameVersionService;
53
54 private final TorrentRecordService torrentRecordService;
55
56 private final HotTrendService hotTrendService;
57
58 public ResourceController(ResourceService resourceService, GameplayService gameplayService, RewardService rewardService, UserUploadService userUploadService, CommunityService communityService, UserService userService, UserPurchaseService userPurchaseService, UserLikeService userLikeService, UserCollectionService userCollectionService, NotificationService notificationService, ResourceVersionService resourceVersionService, SearchHistoryService searchHistoryService, GameVersionService gameVersionService, TorrentRecordService torrentRecordService, HotTrendService hotTrendService) {
xiukiradbe96222025-06-06 21:25:09 +080059 this.resourceService = resourceService;
60 this.gameplayService = gameplayService;
61 this.rewardService = rewardService;
62 this.userUploadService = userUploadService;
63 this.communityService = communityService;
64 this.userService = userService;
65 this.userPurchaseService = userPurchaseService;
66 this.userLikeService = userLikeService;
67 this.userCollectionService = userCollectionService;
68 this.notificationService = notificationService;
xiukiraac78f3d2025-06-08 23:38:10 +080069 this.resourceVersionService = resourceVersionService;
70 this.searchHistoryService = searchHistoryService;
71 this.gameVersionService = gameVersionService;
72 this.torrentRecordService = torrentRecordService;
73 this.hotTrendService = hotTrendService;
xiukiradbe96222025-06-06 21:25:09 +080074 }
75
xiukira687b9cb2025-05-29 15:15:02 +080076 private final Logger logger = LoggerFactory.getLogger(ResourceController.class);
xiukiradbe96222025-06-06 21:25:09 +080077
78 /**
79 * 上传资源
80 *
81 * @param postResourceDTO 上传资源信息
82 * @return 上传资源结果
83 */
84 @PostMapping
85 public ResponseEntity<String> uploadResource(@RequestBody PostResourceDTO postResourceDTO) {
86 // 存资源
87 Resource resource = postResourceDTO.getResource();
88 resourceService.save(resource);
89 // 存玩法列表
90 String[] gameplayList = postResourceDTO.getGameplayList();
91 for (String gameplayName : gameplayList) {
92 Gameplay gameplay = new Gameplay();
93 gameplay.setGameplayName(gameplayName);
94 gameplay.setResourceId(postResourceDTO.getResource().getResourceId());
95 gameplayService.save(gameplay);
96 }
97 // 完成对应悬赏
98 if (postResourceDTO.getCompleteRewardId() != 0) {
99 UpdateWrapper<Reward> rewardUpdate = new UpdateWrapper<>();
100 rewardUpdate.eq("reward_id", postResourceDTO.getCompleteRewardId()).set("completed_by", postResourceDTO.getUserId()).set("completed_at", postResourceDTO.getResource().getUploadTime()).set("resource_id", postResourceDTO.getResource().getResourceId());
101 rewardService.update(rewardUpdate);
102 }
103 // 存用户上传表
104 UserUpload userUpload = new UserUpload();
105 userUpload.setUserId(postResourceDTO.getUserId());
106 userUpload.setResourceId(postResourceDTO.getResource().getResourceId());
107 userUploadService.save(userUpload);
108 // 创建资源社区
109 Community community = new Community();
110 community.setCommunityName(postResourceDTO.getResource().getResourceName());
xiukira197532d2025-06-09 21:15:50 +0800111 community.setCommunityPicture(postResourceDTO.getResource().getResourcePicture());
112 community.setDescription(postResourceDTO.getResource().getResourceDetail());
xiukiradbe96222025-06-06 21:25:09 +0800113 community.setType(postResourceDTO.getResource().getClassify());
114 community.setResourceId(postResourceDTO.getResource().getResourceId());
115 communityService.save(community);
116 return ResponseEntity.ok("");
117 }
118
119 /**
xiukiraac78f3d2025-06-08 23:38:10 +0800120 * 上传版本
121 *
122 * @param resourceVersion 上传版本信息
123 * @return 上传版本结果
124 */
125 @PostMapping("/version")
126 public ResponseEntity<String> uploadResourceVersion(@RequestBody ResourceVersion resourceVersion) {
127 resourceVersionService.save(resourceVersion);
128 return ResponseEntity.ok("");
129 }
130
131 /**
xiukiradbe96222025-06-06 21:25:09 +0800132 * 购买资源
133 *
134 * @param userResourceDTO 购买资源信息
135 * @return 购买资源结果
136 */
137 @PostMapping("purchase")
138 public ResponseEntity<String> purchaseResource(@RequestBody UserResourceDTO userResourceDTO) {
139
140 QueryWrapper<User> userQuery = new QueryWrapper<>();
141 userQuery.eq("user_id", userResourceDTO.getUserId());
142 User user = userService.getOne(userQuery);
143
144 QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>();
145 ResourceQuery.eq("resource_id", userResourceDTO.getResourceId());
146 Resource resource = resourceService.getOne(ResourceQuery);
147
148 if (user.getCredits() < resource.getPrice()) {
149 // 积分余额不足
150 logger.info("The balance of points is insufficient to cover the price of this resource: {}", resource.getPrice());
151 return ResponseEntity.status(412).body("");
152 } else {
153 // 扣除用户积分
154 UpdateWrapper<User> userUpdate = new UpdateWrapper<>();
155 userUpdate.eq("user_id", user.getUserId()).set("credits", user.getCredits() - resource.getPrice());
156 userService.update(userUpdate);
157 // 添加购买资源记录
158 UserPurchase userPurchase = new UserPurchase();
159 userPurchase.setUserId(user.getUserId());
160 userPurchase.setResourceId(resource.getResourceId());
161 userPurchaseService.save(userPurchase);
162 // 给上传该资源的用户发送通知
163 Notification notification = new Notification();
164 QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>();
165 userUploadQuery.eq("resource_id", userResourceDTO.getResourceId());
166 UserUpload userUpload = userUploadService.getOne(userUploadQuery);
167 notification.setUserId(userUpload.getUserId());
168 notification.setTitle("资源被购买");
169 notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 购买了!");
170 notification.setCreateAt(new Date());
171 notification.setRead(false);
172 notification.setTriggeredBy(userResourceDTO.getUserId());
173 notification.setRelatedId(userResourceDTO.getResourceId());
174 notificationService.save(notification);
175 return ResponseEntity.ok("");
176 }
177 }
178
179 /**
180 * 点赞资源
181 *
182 * @param userResourceDTO 点赞资源信息
183 * @return 点赞资源结果
184 */
185 @PostMapping("like")
186 public ResponseEntity<String> likeResource(@RequestBody UserResourceDTO userResourceDTO) {
187
188 QueryWrapper<User> userQuery = new QueryWrapper<>();
189 userQuery.eq("user_id", userResourceDTO.getUserId());
190 User user = userService.getOne(userQuery);
191
192 QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>();
193 ResourceQuery.eq("resource_id", userResourceDTO.getResourceId());
194 Resource resource = resourceService.getOne(ResourceQuery);
195
196 UserLike userLike = new UserLike();
197 userLike.setUserId(userResourceDTO.getUserId());
198 userLike.setResourceId(userResourceDTO.getResourceId());
199 userLikeService.save(userLike);
200
201 // 给上传该资源的用户发送通知
202 Notification notification = new Notification();
203 QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>();
204 userUploadQuery.eq("resource_id", userResourceDTO.getResourceId());
205 UserUpload userUpload = userUploadService.getOne(userUploadQuery);
206 notification.setUserId(userUpload.getUserId());
207 notification.setTitle("资源被点赞");
208 notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 点赞了!");
209 notification.setCreateAt(new Date());
210 notification.setRead(false);
211 notification.setTriggeredBy(userResourceDTO.getUserId());
212 notification.setRelatedId(userResourceDTO.getResourceId());
213 notificationService.save(notification);
214
215 return ResponseEntity.ok("");
216 }
217
218 /**
219 * 收藏资源
220 *
221 * @param userResourceDTO 收藏资源信息
222 * @return 收藏资源结果
223 */
224 @PostMapping("collection")
225 public ResponseEntity<String> collectResource(@RequestBody UserResourceDTO userResourceDTO) {
226
227 QueryWrapper<User> userQuery = new QueryWrapper<>();
228 userQuery.eq("user_id", userResourceDTO.getUserId());
229 User user = userService.getOne(userQuery);
230
231 QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>();
232 ResourceQuery.eq("resource_id", userResourceDTO.getResourceId());
233 Resource resource = resourceService.getOne(ResourceQuery);
234
235 UserCollection userCollection = new UserCollection();
236 userCollection.setUserId(userResourceDTO.getUserId());
237 userCollection.setResourceId(userResourceDTO.getResourceId());
238 userCollectionService.save(userCollection);
239
240
241 // 给上传该资源的用户发送通知
242 Notification notification = new Notification();
243 QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>();
244 userUploadQuery.eq("resource_id", userResourceDTO.getResourceId());
245 UserUpload userUpload = userUploadService.getOne(userUploadQuery);
246 notification.setUserId(userUpload.getUserId());
247 notification.setTitle("资源被收藏");
248 notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 收藏了!");
249 notification.setCreateAt(new Date());
250 notification.setRead(false);
251 notification.setTriggeredBy(userResourceDTO.getUserId());
252 notification.setRelatedId(userResourceDTO.getResourceId());
253 notificationService.save(notification);
254
255 return ResponseEntity.ok("");
256 }
xiukirab9490272025-06-06 21:53:52 +0800257
258 /**
259 * 删除资源
260 *
261 * @param resourceId 资源id
262 * @param userId 用户id
263 * @param password 密码
264 * @return 删除资源结果
265 */
266 @DeleteMapping
267 @ResponseStatus(HttpStatus.NO_CONTENT)
268 public ResponseEntity<String> userDelete(@RequestParam int resourceId, @RequestParam int userId, @RequestParam String password) {
269 logger.warn("Delete resource with id: {}", resourceId);
270 // 根据用户id查询该用户
271 QueryWrapper<User> userQuery = new QueryWrapper<>();
272 userQuery.eq("user_id", userId);
273 User userCheck = userService.getOne(userQuery);
274
275 // 只允许在用户上传资源页进行删除操作,所以不需要验证该资源是由该用户上传的
276 if (userCheck.getPassword().equals(password)) {
277 // 删除资源
278 resourceService.removeById(resourceId);
279 return ResponseEntity.noContent().build();
280 } else {
281 logger.warn("Delete failed. Incorrect password for account: {}", password);
282 return ResponseEntity.status(408).body("");
283 }
284 }
285
286 /**
287 * 取消点赞
288 *
289 * @param userId 用户id
290 * @param resourceId 资源id
291 * @return 取消点赞结果
292 */
293 @DeleteMapping("like")
294 @ResponseStatus(HttpStatus.NO_CONTENT)
295 public ResponseEntity<String> likeDelete(@RequestParam int userId, @RequestParam int resourceId) {
296 QueryWrapper<UserLike> userLikeQuery = new QueryWrapper<>();
297 userLikeQuery.eq("user_id", userId).eq("resource_id", resourceId);
298 userLikeService.remove(userLikeQuery);
299 return ResponseEntity.noContent().build();
300 }
301
302 /**
303 * 取消收藏
304 *
305 * @param userId 用户id
306 * @param resourceId 资源id
307 * @return 取消收藏结果
308 */
309 @DeleteMapping("collection")
310 @ResponseStatus(HttpStatus.NO_CONTENT)
311 public ResponseEntity<String> collectionDelete(@RequestParam int userId, @RequestParam int resourceId) {
312 QueryWrapper<UserCollection> userCollectionQuery = new QueryWrapper<>();
313 userCollectionQuery.eq("user_id", userId).eq("resource_id", resourceId);
314 userCollectionService.remove(userCollectionQuery);
315 return ResponseEntity.noContent().build();
316 }
xiukiraac78f3d2025-06-08 23:38:10 +0800317
318 /**
319 * 搜索资源
320 *
321 * @param userId 用户id
322 * @param searchValue 输入(模糊搜索)
323 * @param classify 类型名称
324 * @param gameplayList 主要玩法列表
325 * @param gameVersionList 版本列表
326 * @param pageNumber 第几页
327 * @param rows 行数
328 * @return 搜索资源结果
329 */
330 @GetMapping("/search")
331 public ResponseEntity<GetResourcePageDTO> getResourceSearch(@RequestParam int userId, @RequestParam String searchValue, @RequestParam String classify, @RequestParam String[] gameplayList, @RequestParam String[] gameVersionList, @RequestParam int pageNumber, @RequestParam int rows) {
332 IPage<Resource> page = new Page<>(pageNumber, rows);
333 QueryWrapper<Resource> resourceWrapper = new QueryWrapper<>();
334 // 搜索名称字段不为空时,根据搜索名称进行模糊匹配,并将该次搜索存到搜索历史表
335 if (!Objects.equals(searchValue, "")) {
336 resourceWrapper.like("resource_name", searchValue);
337 SearchHistory searchHistory = new SearchHistory();
338 searchHistory.setSearchContent(searchValue);
339 searchHistory.setUserId(userId);
340 searchHistoryService.save(searchHistory);
341 }
342 // 分类字段不为空时,根据类别筛选
343 if (!Objects.equals(classify, "")) {
344 resourceWrapper.eq("classify", classify);
345 }
346
347 // 主要玩法列表不为空时,根据主要玩法列表筛选(用户传入一个玩法列表,需要去 gameplay 表中查询该资源的所有玩法列表,只有当用户传入的玩法列表的每个值都在资源实际有的玩法列表中时,该资源才符合条件)
348 if (gameplayList.length > 0) {
349 // 该子查询统计了用户传入的玩法列表中有多少玩法是在资源的实际玩法列表中有的
350 String subQuery = "SELECT COUNT(*) " +
351 "FROM gameplay gp " +
352 "WHERE gp.resource_id = resource.resource_id " +
353 "AND gp.gameplay_name IN (" +
354 String.join(",", Arrays.stream(gameplayList).map(name -> "'" + name + "'").toArray(String[]::new)) + ")";
355 // 只有当子查询返回的数量等于用户传入的玩法列表长度时,说明用户传入的玩法列表完全被包含在资源的玩法列表中
356 resourceWrapper.apply("(" + subQuery + ") = " + gameplayList.length);
357 }
358
359 // 游戏版本列表不为空时,根据游戏版本列表筛选
360 if (gameVersionList.length > 0) {
361 // 拼接游戏版本列表的 IN 子句
362 String gameVersionInClause = String.join(",", Arrays.stream(gameVersionList)
363 .map(v -> "'" + v + "'")
364 .toArray(String[]::new));
365
366 // 修改 HAVING 条件逻辑,只检查输入版本列表是否是实际版本列表的子集
367 String resourceCondition = "EXISTS ("
368 + "SELECT 1 "
369 + "FROM resource_version rv "
370 + "JOIN game_version gv ON gv.resource_version_id = rv.resource_version_id "
371 + "WHERE rv.resource_id = resource.resource_id "
372 + "GROUP BY rv.resource_id "
373 + "HAVING COUNT(DISTINCT CASE WHEN gv.game_version_name IN (" + gameVersionInClause + ") THEN gv.game_version_name END) = " + gameVersionList.length
374 + ")";
375
376 // 应用条件
377 resourceWrapper.apply(resourceCondition);
378 }
379
380 IPage<Resource> resourcePage = resourceService.page(page, resourceWrapper);
381 List<Resource> resourceList = resourcePage.getRecords();
382 long total = resourcePage.getTotal();
383 long pages = resourcePage.getPages();
384 long current = resourcePage.getCurrent();
385 long size = resourcePage.getSize();
386
387 List<List<Gameplay>> gameplayLists = new ArrayList<>();
388 for (Resource resource : resourceList) {
389 // 对于每个资源,获取游戏玩法列表
390 QueryWrapper<Gameplay> gameplayQuery = new QueryWrapper<>();
391 gameplayQuery.eq("resource_id", resource.getResourceId());
392 List<Gameplay> allGameplayList = gameplayService.list(gameplayQuery);
393 gameplayLists.add(allGameplayList);
394 }
395
396 GetResourcePageDTO getResourceSearchDTO = new GetResourcePageDTO(resourceList, gameplayLists, total, pages, current, size);
397 return ResponseEntity.ok(getResourceSearchDTO);
398 }
399
400 /**
401 * 获取资源信息
402 *
403 * @param resourceId 资源id
404 * @param userId 用户id
405 * @return 获取资源信息结果
406 */
407 @GetMapping("/info")
408 public ResponseEntity<GetResourceInfoDTO> getResourceInfo(@RequestParam int resourceId, @RequestParam int userId) {
409 // 获取Resource表信息
410 Resource resource = resourceService.getById(resourceId);
411
412 // 获取Gameplay列表
413 List<Gameplay> gameplayList = gameplayService.list(new QueryWrapper<Gameplay>().eq("resource_id", resourceId));
414
415 // 获取ResourceVersion列表
416 List<ResourceVersion> resourceVersionList = resourceVersionService.list(new QueryWrapper<ResourceVersion>().eq("resource_id", resourceId));
417
418 // 获取GameVersion二维列表
419 List<List<GameVersion>> gameVersionLists = new ArrayList<>();
420 for (ResourceVersion resourceVersion : resourceVersionList) {
421 List<GameVersion> gameVersionList = gameVersionService.list(new QueryWrapper<GameVersion>().eq("resource_version_id", resourceVersion.getResourceVersionId()));
422 gameVersionLists.add(gameVersionList);
423 }
424
425 // 获取TorrentRecord二维列表
426 List<List<TorrentRecord>> torrentRecordLists = new ArrayList<>();
427 for (ResourceVersion resourceVersion : resourceVersionList) {
428 List<TorrentRecord> torrentRecordList = torrentRecordService.list(new QueryWrapper<TorrentRecord>().eq("resource_version_id", resourceVersion.getResourceVersionId()));
429 torrentRecordLists.add(torrentRecordList);
430 }
431
432 // 获取用户是否收藏
433 QueryWrapper<UserCollection> userCollectionQuery = new QueryWrapper<>();
434 userCollectionQuery.eq("user_id", userId).eq("resource_id", resourceId);
435 boolean isCollect = userCollectionService.getOne(userCollectionQuery) != null;
436
437 // 获取用户是否点赞
438 QueryWrapper<UserLike> userLikeQuery = new QueryWrapper<>();
439 userLikeQuery.eq("user_id", userId).eq("resource_id", resourceId);
440 boolean isLike = userLikeService.getOne(userLikeQuery) != null;
441
442 // 获取用户是否购买
443 QueryWrapper<UserPurchase> userPurchaseQuery = new QueryWrapper<>();
444 userPurchaseQuery.eq("user_id", userId).eq("resource_id", resourceId);
445 boolean isPurchase = userPurchaseService.getOne(userPurchaseQuery) != null;
446
447 // 获取用户是否上传
448 QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>();
449 userUploadQuery.eq("user_id", userId).eq("resource_id", resourceId);
450 boolean isUpload = userUploadService.getOne(userUploadQuery) != null;
451
452 // 获取上传者ID
453 QueryWrapper<UserUpload> findUploaderQuery = new QueryWrapper<>();
454 findUploaderQuery.eq("resource_id", resourceId);
455 int uploaderId = userUploadService.getOne(findUploaderQuery).getUserId();
456
457 GetResourceInfoDTO getResourceInfoDTO = new GetResourceInfoDTO(resource, gameplayList, resourceVersionList, gameVersionLists, torrentRecordLists, isCollect, isLike, isPurchase, isUpload, uploaderId);
458 return ResponseEntity.ok(getResourceInfoDTO);
459 }
460
461 /**
462 * 获取热门资源
463 *
464 * @param classify 资源分类
465 * @param pageNumber 页数
466 * @param rows 行数
467 * @return 获取热门资源结果
468 */
469 @GetMapping("/hot")
470 public ResponseEntity<GetResourcePageDTO> getHotResource(@RequestParam String classify, @RequestParam int pageNumber, @RequestParam int rows) {
471 IPage<Resource> page = new Page<>(pageNumber, rows);
472 QueryWrapper<Resource> resourceQuery = new QueryWrapper<>();
473 resourceQuery.eq("classify", classify);
474 // 动态计算热度并按降序排序
475 resourceQuery.orderByDesc("(downloads * 0.25 + likes * 0.25 + collections * 0.25 + comments * 0.25)");
476 IPage<Resource> resourcePage = resourceService.page(page, resourceQuery);
477 List<Resource> resourceList = resourcePage.getRecords();
478 long total = resourcePage.getTotal();
479 long pages = resourcePage.getPages();
480 long current = resourcePage.getCurrent();
481 long size = resourcePage.getSize();
482
483 List<List<Gameplay>> gameplayLists = new ArrayList<>();
484 for (Resource resource : resourceList) {
485 // 对于每个资源,获取游戏玩法列表
486 QueryWrapper<Gameplay> gameplayQuery = new QueryWrapper<>();
487 gameplayQuery.eq("resource_id", resource.getResourceId());
488 List<Gameplay> allGameplayList = gameplayService.list(gameplayQuery);
489 gameplayLists.add(allGameplayList);
490 }
491
492 GetResourcePageDTO getResourcePageDTO = new GetResourcePageDTO(resourceList, gameplayLists, total, pages, current, size);
493 return ResponseEntity.ok(getResourcePageDTO);
494 }
495
496 /**
497 * 获取热门资源幻灯片数据
498 *
499 * @return 获取用户收藏结果
500 */
501 @GetMapping("/hot/slide")
502 public ResponseEntity<GetResourceHotSlideDTO> getResourceHotSlide() {
503 QueryWrapper<Resource> resourceQuery = new QueryWrapper<>();
504 // 动态计算热度并按降序排序
505 resourceQuery.orderByDesc("(downloads * 0.25 + likes * 0.25 + collections * 0.25 + comments * 0.25)");
506 // 限制返回3条记录
507 resourceQuery.last("LIMIT 3");
508 List<Resource> resourceList = resourceService.list(resourceQuery);
509
510 GetResourceHotSlideDTO getResourceHotSlideDTO = new GetResourceHotSlideDTO(resourceList);
511 return ResponseEntity.ok(getResourceHotSlideDTO);
512 }
513
514 /**
515 * 获取热门资源趋势图
516 *
517 * @return 获取热门资源趋势图结果
518 */
519 @GetMapping("/hot-trend")
520 public ResponseEntity<GetResourceHotTrendDTO> getResourceHotTrend() {
521 List<HotTrend> hotTrendList = hotTrendService.list();
522 GetResourceHotTrendDTO getResourceHotTrendDTO = new GetResourceHotTrendDTO(hotTrendList);
523 return ResponseEntity.ok(getResourceHotTrendDTO);
524 }
525
526 /**
527 * 修改资源信息
528 *
529 * @param putResourceInfoDTO 要修改的资源信息
530 * @return 修改资源信息结果
531 */
532 @PutMapping("/info")
533 public ResponseEntity<String> putResourceInfo(@RequestBody PutResourceInfoDTO putResourceInfoDTO) {
534 // 检查资源名是否重复
535 QueryWrapper<Resource> resourceQuery = new QueryWrapper<>();
536 resourceQuery.ne("resource_id", putResourceInfoDTO.getResourceId()).eq("resource_name", putResourceInfoDTO.getResourceName());
537 Resource resource = resourceService.getOne(resourceQuery);
538
539 if (resource != null) {
540 // 资源名重复
541 logger.warn("Modification attempt failed. Resource name already exists: {}", putResourceInfoDTO.getResourceName());
542 return ResponseEntity.status(411).body("");
543 }
544
545 UpdateWrapper<Resource> resourceUpdate = new UpdateWrapper<>();
546 resourceUpdate.eq("resource_id", putResourceInfoDTO.getResourceId())
547 .set("resource_name", putResourceInfoDTO.getResourceName())
548 .set("resource_picture", putResourceInfoDTO.getResourcePicture())
549 .set("resource_summary", putResourceInfoDTO.getResourceSummary())
550 .set("resource_detail", putResourceInfoDTO.getResourceDetail())
551 .set("price", putResourceInfoDTO.getPrice());
552 resourceService.update(resourceUpdate);
553
554 QueryWrapper<Gameplay> gameplayQuery = new QueryWrapper<>();
555 gameplayQuery.eq("resource_id", putResourceInfoDTO.getResourceId());
556 gameplayService.remove(gameplayQuery);
557
558 for (int i = 0; i < putResourceInfoDTO.getGameplayList().length; i++) {
559 Gameplay gameplay = new Gameplay();
560 gameplay.setGameplayName(putResourceInfoDTO.getGameplayList()[i]);
561 gameplay.setResourceId(putResourceInfoDTO.getResourceId());
562 gameplayService.save(gameplay);
563 }
564
565 return ResponseEntity.ok("");
566 }
xiukiradbe96222025-06-06 21:25:09 +0800567}