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