diff --git a/src/main/java/asia/yulinling/workflow/controller/TestController.java b/src/main/java/asia/yulinling/workflow/controller/TestController.java index cc6f692..f984ef6 100644 --- a/src/main/java/asia/yulinling/workflow/controller/TestController.java +++ b/src/main/java/asia/yulinling/workflow/controller/TestController.java @@ -4,8 +4,12 @@ import asia.yulinling.workflow.constant.Status; import asia.yulinling.workflow.exception.JsonException; import asia.yulinling.workflow.exception.PageException; import asia.yulinling.workflow.model.ApiResponse; +import asia.yulinling.workflow.model.PageResult; +import asia.yulinling.workflow.model.vo.user.UserVO; +import asia.yulinling.workflow.service.UserService; import cn.hutool.core.lang.Dict; import cn.hutool.json.JSONUtil; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -24,7 +28,9 @@ import java.util.Map; */ @Slf4j @RestController +@RequiredArgsConstructor public class TestController { + private final UserService userService; /** * 测试方法 GET @@ -49,13 +55,23 @@ public class TestController { } @GetMapping("/json") - public ApiResponse jsonException() { + public ApiResponse jsonException() { throw new JsonException(Status.UNKNOWN_ERROR); } @GetMapping("/page") - public ApiResponse PageException() { + public ApiResponse PageException() { throw new PageException(Status.UNKNOWN_ERROR); } + @GetMapping("/testPage") + public ApiResponse> testPage() { + PageResult pageResult = new PageResult<>(1, 1, 10, null); + return ApiResponse.ofSuccess(pageResult); + } + + @GetMapping("/users") + public ApiResponse> usersPage() { + return userService.getUserList(); + } } diff --git a/src/main/java/asia/yulinling/workflow/handler/GlobalExceptionHandler.java b/src/main/java/asia/yulinling/workflow/handler/GlobalExceptionHandler.java index dc40aae..d376811 100644 --- a/src/main/java/asia/yulinling/workflow/handler/GlobalExceptionHandler.java +++ b/src/main/java/asia/yulinling/workflow/handler/GlobalExceptionHandler.java @@ -30,7 +30,7 @@ public class GlobalExceptionHandler { */ @ExceptionHandler(Exception.class) @ResponseBody - public ApiResponse jsonErrorHandler(JsonException e) { + public ApiResponse jsonErrorHandler(JsonException e) { log.error(e.getMessage()); return ApiResponse.ofException(e); } diff --git a/src/main/java/asia/yulinling/workflow/model/ApiResponse.java b/src/main/java/asia/yulinling/workflow/model/ApiResponse.java index a603977..598d5d3 100644 --- a/src/main/java/asia/yulinling/workflow/model/ApiResponse.java +++ b/src/main/java/asia/yulinling/workflow/model/ApiResponse.java @@ -13,7 +13,7 @@ import lombok.Data; * @since 2025/6/5 */ @Data -public class ApiResponse { +public class ApiResponse { /** * 状态码 */ @@ -55,8 +55,8 @@ public class ApiResponse { * @param data 返回数据 * @return ApiResponse */ - public static ApiResponse of(Integer code, String message, Object data) { - return new ApiResponse(code, message, data); + public static ApiResponse of(Integer code, String message, T data) { + return new ApiResponse<>(code, message, data); } /** @@ -65,7 +65,7 @@ public class ApiResponse { * @param data 返回数据 * @return ApiResponse */ - public static ApiResponse ofSuccess(Object data) { + public static ApiResponse ofSuccess(T data) { return ofStatus(Status.OK, data); } @@ -75,8 +75,8 @@ public class ApiResponse { * @param message 返回内容 * @return ApiResponse */ - public static ApiResponse ofMessage(String message) { - return ofStatus(Status.OK, message); + public static ApiResponse ofMessage(String message) { + return of(Status.OK.getCode(), message, null); } /** @@ -85,10 +85,9 @@ public class ApiResponse { * @param status 状态{@link Status} * @return ApiResponse */ - public static ApiResponse ofStatus(Status status) { + public static ApiResponse ofStatus(Status status) { return ofStatus(status, null); } - /** * 构造一个有状态且带数据的Api返回 * @@ -96,11 +95,10 @@ public class ApiResponse { * @param data 返回数据 * @return ApiResponse */ - public static ApiResponse ofStatus(Status status, Object data) { + public static ApiResponse ofStatus(Status status, T data) { return of(status.getCode(), status.getMessage(), data); } - /** * 构造一个异常且带数据的Api返回 * @@ -109,7 +107,7 @@ public class ApiResponse { * @return ApiResponse * @param {@link BaseException} 子类 */ - public static ApiResponse ofException(T t, Object data) { + public static ApiResponse ofException(T t, T data) { return of(t.getCode(), t.getMessage(), data); } /** @@ -119,7 +117,7 @@ public class ApiResponse { * @return ApiResponse * @param {@link BaseException} 子类 */ - public static ApiResponse ofException(T t) { + public static ApiResponse ofException(T t) { return ofException(t, null); } } diff --git a/src/main/java/asia/yulinling/workflow/model/PageResult.java b/src/main/java/asia/yulinling/workflow/model/PageResult.java new file mode 100644 index 0000000..50eabf5 --- /dev/null +++ b/src/main/java/asia/yulinling/workflow/model/PageResult.java @@ -0,0 +1,80 @@ +package asia.yulinling.workflow.model; + +import lombok.Data; + +import java.util.List; + +/** + * 分页返回结果 + * + * @author YLL + * @since 2025/6/8 + */ +@Data +public class PageResult { + private long total; // 总记录数 + private int totalPage; // 总页数 + private int currentPage; // 当前页码 + private int pageSize; // 每页数量 + private List list; // 当前页数据 + + private boolean hasNext; // 是否有下一页 + private boolean hasPrev; // 是否有上一页 + private boolean firstPage; // 是否是首页 + private boolean lastPage; // 是否是尾页 + private int navigatePages; // 导航页码数,如:显示5个页码 [1,2,3,4,5] + private int[] navigatePageNums; // 导航页码数组 + private int from; // 当前页第一条数据在总数据中的位置(从1开始) + private int to; // 当前页最后一条数据在总数据中的位置 + + // 构造方法(可选传参) + public PageResult(long total, int currentPage, int pageSize, List list) { + this.total = total; + this.currentPage = currentPage; + this.pageSize = pageSize; + this.list = list; + + // 自动计算 + this.totalPage = (int) Math.ceil((double) total / pageSize); + this.hasNext = currentPage < totalPage; + this.hasPrev = currentPage > 1; + this.firstPage = currentPage == 1; + this.lastPage = currentPage == totalPage; + this.from = (currentPage - 1) * pageSize + 1; + this.to = Math.min(currentPage * pageSize, (int) total); + + // 可选:导航页码生成逻辑(需要时可扩展) + navigatePageNums = calculateNavigatePageNumbers(currentPage, totalPage, navigatePages); + } + + private int[] calculateNavigatePageNumbers(int currentPage, int totalPage, int navigatePages) { + int[] navigatePageNums; + int startPage = 1; + int endPage = totalPage; + + // 中间页码计算逻辑 + if (totalPage > navigatePages) { + int half = navigatePages / 2; + startPage = currentPage - half; + endPage = currentPage + half; + + if (startPage < 1) { + startPage = 1; + endPage = navigatePages; + } + + if (endPage > totalPage) { + endPage = totalPage; + startPage = endPage - navigatePages + 1; + if (startPage < 1) startPage = 1; + } + } + + navigatePageNums = new int[endPage - startPage + 1]; + for (int i = 0; i < navigatePageNums.length; i++) { + navigatePageNums[i] = startPage + i; + } + + return navigatePageNums; + } +} diff --git a/src/main/java/asia/yulinling/workflow/model/vo/user/UserVO.java b/src/main/java/asia/yulinling/workflow/model/vo/user/UserVO.java new file mode 100644 index 0000000..3911a62 --- /dev/null +++ b/src/main/java/asia/yulinling/workflow/model/vo/user/UserVO.java @@ -0,0 +1,34 @@ +package asia.yulinling.workflow.model.vo.user; + +import lombok.Data; + +/** + *

+ * 用户信息VO + *

+ * + * @author YLL + * @since 2025/6/8 + */ +@Data +public class UserVO { + /** + * 用户名 + */ + private String name; + + /** + * 邮箱 + */ + private String email; + + /** + * 手机号 + */ + private String phone; + + /** + * 状态 -1:删除 0:警用 1:启用 + */ + private Integer status; +} diff --git a/src/main/java/asia/yulinling/workflow/service/UserService.java b/src/main/java/asia/yulinling/workflow/service/UserService.java new file mode 100644 index 0000000..03c32cf --- /dev/null +++ b/src/main/java/asia/yulinling/workflow/service/UserService.java @@ -0,0 +1,23 @@ +package asia.yulinling.workflow.service; + +import asia.yulinling.workflow.model.ApiResponse; +import asia.yulinling.workflow.model.PageResult; +import asia.yulinling.workflow.model.entity.User; +import asia.yulinling.workflow.model.vo.user.UserVO; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** + *

+ * 用户接口 + *

+ * + * @author YLL + * @since 2025/6/8 + */ +@Transactional +public interface UserService { + + ApiResponse> getUserList(); +} diff --git a/src/main/java/asia/yulinling/workflow/service/impl/UserServiceImpl.java b/src/main/java/asia/yulinling/workflow/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..e7bde10 --- /dev/null +++ b/src/main/java/asia/yulinling/workflow/service/impl/UserServiceImpl.java @@ -0,0 +1,58 @@ +package asia.yulinling.workflow.service.impl; + +import asia.yulinling.workflow.mapper.UserMapper; +import asia.yulinling.workflow.model.ApiResponse; +import asia.yulinling.workflow.model.PageResult; +import asia.yulinling.workflow.model.entity.User; +import asia.yulinling.workflow.model.vo.user.UserVO; +import asia.yulinling.workflow.service.UserService; +import cn.hutool.core.util.ArrayUtil; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + *

+ * 用户接口 + *

+ * + * @author YLL + * @since 2025/6/8 + */ +@Service +@RequiredArgsConstructor +public class UserServiceImpl implements UserService { + + /** 注入用户Mapper */ + private final UserMapper userMapper; + + @Override + public ApiResponse> getUserList() { + + List userVOList = new ArrayList<>(); + + List users = userMapper.selectList(null); + + if (ArrayUtil.isNotEmpty(users)) { + for (User user : users) { + UserVO userVO = new UserVO(); + userVO.setName(user.getName()); + userVO.setEmail(user.getEmail()); + userVO.setPhone(user.getPhone()); + userVO.setStatus(user.getStatus()); + userVOList.add(userVO); + } + PageResult pageResult = new PageResult<>( + users.toArray().length, 1, users.toArray().length / 10, userVOList + ); + + return ApiResponse.ofSuccess(pageResult); + } + + return ApiResponse.ofSuccess(null); + } +} diff --git a/src/test/java/asia/yulinling/workflow/service/UserServiceTest.java b/src/test/java/asia/yulinling/workflow/service/UserServiceTest.java new file mode 100644 index 0000000..77eb07d --- /dev/null +++ b/src/test/java/asia/yulinling/workflow/service/UserServiceTest.java @@ -0,0 +1,32 @@ +package asia.yulinling.workflow.service; + +import asia.yulinling.workflow.WorkFlowMainTests; +import asia.yulinling.workflow.model.ApiResponse; +import asia.yulinling.workflow.model.PageResult; +import asia.yulinling.workflow.model.entity.User; +import asia.yulinling.workflow.model.vo.user.UserVO; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +/** + *

+ * 用户测试类 + *

+ * + * @author YLL + * @since 2025/6/8 + */ +public class UserServiceTest extends WorkFlowMainTests { + @Autowired + private UserService userService; + + @Test + public void getUserList() { + ApiResponse> users =userService.getUserList(); + + Assert.assertEquals(200,users.getCode().intValue()); + } +}