接口优化
This commit is contained in:
@@ -1,99 +1,88 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotion.common.PageResult;
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.PageRequest;
|
||||
import com.emotion.dto.response.BaseResponse;
|
||||
import com.emotion.entity.Achievement;
|
||||
import com.emotion.dto.request.achievement.*;
|
||||
import com.emotion.dto.response.achievement.AchievementResponse;
|
||||
import com.emotion.service.AchievementService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 成就控制器
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
* @date 2025-09-08
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/achievement")
|
||||
@Tag(name = "成就管理", description = "成就的增删改查功能")
|
||||
public class AchievementController {
|
||||
|
||||
@Autowired
|
||||
private AchievementService achievementService;
|
||||
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/**
|
||||
* 分页查询成就
|
||||
*/
|
||||
@Operation(summary = "分页查询成就", description = "分页查询成就列表")
|
||||
@GetMapping("/page")
|
||||
public Result<PageResult<AchievementResponse>> getPage(@Validated PageRequest request) {
|
||||
IPage<Achievement> page = achievementService.getPage(request);
|
||||
List<AchievementResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<AchievementResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(page.getCurrent());
|
||||
pageResult.setSize(page.getSize());
|
||||
pageResult.setTotal(page.getTotal());
|
||||
pageResult.setPages(page.getPages());
|
||||
pageResult.setRecords(responses);
|
||||
|
||||
public Result<PageResult<AchievementResponse>> getPage(@Validated AchievementPageRequest request) {
|
||||
PageResult<AchievementResponse> pageResult = achievementService.getPageWithResponse(request);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取成就
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<AchievementResponse> getById(@PathVariable String id) {
|
||||
Achievement achievement = achievementService.getById(id);
|
||||
if (achievement == null) {
|
||||
@Operation(summary = "根据ID获取成就", description = "根据ID获取成就详情")
|
||||
@GetMapping("/detail")
|
||||
public Result<AchievementResponse> getById(@RequestParam String id) {
|
||||
AchievementResponse response = achievementService.getAchievementResponseById(id);
|
||||
if (response == null) {
|
||||
return Result.notFound("成就不存在");
|
||||
}
|
||||
return Result.success(convertToResponse(achievement));
|
||||
return Result.success(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建成就
|
||||
*/
|
||||
@PostMapping
|
||||
public Result<AchievementResponse> create(@RequestBody Achievement achievement) {
|
||||
boolean saved = achievementService.save(achievement);
|
||||
if (!saved) {
|
||||
@Operation(summary = "创建成就", description = "创建新的成就")
|
||||
@PostMapping("/create")
|
||||
public Result<AchievementResponse> create(@RequestBody @Validated AchievementCreateRequest request) {
|
||||
AchievementResponse response = achievementService.createAchievementWithResponse(request);
|
||||
if (response == null) {
|
||||
return Result.error("创建失败");
|
||||
}
|
||||
return Result.success(convertToResponse(achievement));
|
||||
return Result.success("创建成功", response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新成就
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
public Result<AchievementResponse> update(@PathVariable String id, @RequestBody Achievement achievement) {
|
||||
achievement.setId(id);
|
||||
boolean updated = achievementService.updateById(achievement);
|
||||
if (!updated) {
|
||||
@Operation(summary = "更新成就", description = "更新指定成就")
|
||||
@PutMapping("/update")
|
||||
public Result<AchievementResponse> update(@RequestBody @Validated AchievementUpdateRequest request) {
|
||||
AchievementResponse response = achievementService.updateAchievementWithResponse(request);
|
||||
if (response == null) {
|
||||
return Result.error("更新失败");
|
||||
}
|
||||
Achievement updatedAchievement = achievementService.getById(id);
|
||||
return Result.success(convertToResponse(updatedAchievement));
|
||||
return Result.success("更新成功", response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除成就
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Void> delete(@PathVariable String id) {
|
||||
@Operation(summary = "删除成就", description = "删除指定成就")
|
||||
@DeleteMapping("/delete")
|
||||
public Result<Void> delete(@RequestParam String id) {
|
||||
boolean deleted = achievementService.removeById(id);
|
||||
if (!deleted) {
|
||||
return Result.error("删除失败");
|
||||
@@ -104,55 +93,48 @@ public class AchievementController {
|
||||
/**
|
||||
* 根据分类查询成就
|
||||
*/
|
||||
@GetMapping("/category/{category}")
|
||||
public Result<List<AchievementResponse>> getByCategory(@PathVariable String category) {
|
||||
List<Achievement> achievements = achievementService.getByCategory(category);
|
||||
List<AchievementResponse> responses = achievements.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
@Operation(summary = "根据分类查询成就", description = "根据分类查询成就列表")
|
||||
@GetMapping("/byCategory")
|
||||
public Result<List<AchievementResponse>> getByCategory(@RequestParam String category) {
|
||||
List<AchievementResponse> responses = achievementService.getByCategoryWithResponse(category);
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据稀有度查询成就
|
||||
*/
|
||||
@GetMapping("/rarity/{rarity}")
|
||||
public Result<List<AchievementResponse>> getByRarity(@PathVariable String rarity) {
|
||||
List<Achievement> achievements = achievementService.getByRarity(rarity);
|
||||
List<AchievementResponse> responses = achievements.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
@Operation(summary = "根据稀有度查询成就", description = "根据稀有度查询成就列表")
|
||||
@GetMapping("/byRarity")
|
||||
public Result<List<AchievementResponse>> getByRarity(@RequestParam String rarity) {
|
||||
List<AchievementResponse> responses = achievementService.getByRarityWithResponse(rarity);
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询已解锁的成就
|
||||
*/
|
||||
@Operation(summary = "查询已解锁的成就", description = "查询已解锁的成就列表")
|
||||
@GetMapping("/unlocked")
|
||||
public Result<List<AchievementResponse>> getUnlockedAchievements() {
|
||||
List<Achievement> achievements = achievementService.getUnlockedAchievements();
|
||||
List<AchievementResponse> responses = achievements.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
List<AchievementResponse> responses = achievementService.getUnlockedAchievementsWithResponse();
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询未解锁的成就
|
||||
*/
|
||||
@Operation(summary = "查询未解锁的成就", description = "查询未解锁的成就列表")
|
||||
@GetMapping("/locked")
|
||||
public Result<List<AchievementResponse>> getLockedAchievements() {
|
||||
List<Achievement> achievements = achievementService.getLockedAchievements();
|
||||
List<AchievementResponse> responses = achievements.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
List<AchievementResponse> responses = achievementService.getLockedAchievementsWithResponse();
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计已解锁成就数量
|
||||
*/
|
||||
@GetMapping("/count/unlocked")
|
||||
@Operation(summary = "统计已解锁成就数量", description = "统计已解锁成就数量")
|
||||
@GetMapping("/unlockedCount")
|
||||
public Result<Long> countUnlockedAchievements() {
|
||||
Long count = achievementService.countUnlockedAchievements();
|
||||
return Result.success(count);
|
||||
@@ -161,7 +143,8 @@ public class AchievementController {
|
||||
/**
|
||||
* 统计未解锁成就数量
|
||||
*/
|
||||
@GetMapping("/count/locked")
|
||||
@Operation(summary = "统计未解锁成就数量", description = "统计未解锁成就数量")
|
||||
@GetMapping("/lockedCount")
|
||||
public Result<Long> countLockedAchievements() {
|
||||
Long count = achievementService.countLockedAchievements();
|
||||
return Result.success(count);
|
||||
@@ -170,9 +153,10 @@ public class AchievementController {
|
||||
/**
|
||||
* 解锁成就
|
||||
*/
|
||||
@PutMapping("/{id}/unlock")
|
||||
public Result<Void> unlockAchievement(@PathVariable String id) {
|
||||
boolean unlocked = achievementService.unlockAchievement(id, java.time.LocalDateTime.now());
|
||||
@Operation(summary = "解锁成就", description = "解锁指定成就")
|
||||
@PutMapping("/unlock")
|
||||
public Result<Void> unlockAchievement(@RequestBody @Validated AchievementUnlockRequest request) {
|
||||
boolean unlocked = achievementService.unlockAchievement(request.getId(), LocalDateTime.now());
|
||||
if (!unlocked) {
|
||||
return Result.error("解锁失败");
|
||||
}
|
||||
@@ -182,9 +166,10 @@ public class AchievementController {
|
||||
/**
|
||||
* 更新成就进度
|
||||
*/
|
||||
@PutMapping("/{id}/progress")
|
||||
public Result<Void> updateProgress(@PathVariable String id, @RequestParam Double progress) {
|
||||
boolean updated = achievementService.updateProgress(id, progress);
|
||||
@Operation(summary = "更新成就进度", description = "更新指定成就的进度")
|
||||
@PutMapping("/updateProgress")
|
||||
public Result<Void> updateProgress(@RequestBody @Validated AchievementProgressUpdateRequest request) {
|
||||
boolean updated = achievementService.updateProgress(request.getId(), request.getProgress());
|
||||
if (!updated) {
|
||||
return Result.error("更新进度失败");
|
||||
}
|
||||
@@ -192,36 +177,12 @@ public class AchievementController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为响应对象
|
||||
* 查询最近解锁的成就
|
||||
*/
|
||||
private AchievementResponse convertToResponse(Achievement achievement) {
|
||||
AchievementResponse response = new AchievementResponse();
|
||||
BeanUtils.copyProperties(achievement, response);
|
||||
response.setId(achievement.getId());
|
||||
if (achievement.getCreateTime() != null) {
|
||||
response.setCreateTime(achievement.getCreateTime().format(DATE_TIME_FORMATTER));
|
||||
}
|
||||
if (achievement.getUpdateTime() != null) {
|
||||
response.setUpdateTime(achievement.getUpdateTime().format(DATE_TIME_FORMATTER));
|
||||
}
|
||||
return response;
|
||||
@Operation(summary = "查询最近解锁的成就", description = "查询最近解锁的成就列表")
|
||||
@GetMapping("/recent")
|
||||
public Result<List<AchievementResponse>> getRecentlyUnlocked(@RequestParam(defaultValue = "10") Integer limit) {
|
||||
List<AchievementResponse> responses = achievementService.getRecentlyUnlockedWithResponse(limit);
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 成就响应类
|
||||
*/
|
||||
@lombok.Data
|
||||
@lombok.EqualsAndHashCode(callSuper = true)
|
||||
public static class AchievementResponse extends BaseResponse {
|
||||
private String title;
|
||||
private String description;
|
||||
private String category;
|
||||
private String rarity;
|
||||
private String conditionType;
|
||||
private String conditionValue;
|
||||
private Double progress;
|
||||
private String unlockedTime;
|
||||
private Integer isHidden;
|
||||
private String iconUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user