diff --git a/src/main/java/com/guaiguailang/harmony/config/GlobalExceptionHandler.java b/src/main/java/com/guaiguailang/harmony/config/GlobalExceptionHandler.java index 2b2e310..6fb7b15 100644 --- a/src/main/java/com/guaiguailang/harmony/config/GlobalExceptionHandler.java +++ b/src/main/java/com/guaiguailang/harmony/config/GlobalExceptionHandler.java @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.fusesource.jansi.Ansi; import java.util.Arrays; +import java.util.Objects; @RestControllerAdvice @Slf4j @@ -14,10 +15,19 @@ public class GlobalExceptionHandler { // 全局异常拦截 @ExceptionHandler public void handlerException(Exception e) { - System.err.println(Ansi.ansi().fgRed().a("==========ERROR==========START==========").reset()); - System.err.println(Ansi.ansi().fgRed().a("错误消息:").a(e.getMessage()).reset()); - System.err.println(Ansi.ansi().fgRed().a("错误类型:").a(e.getLocalizedMessage()).reset()); - System.err.println(Ansi.ansi().fgRed().a(e.getCause() == null ? "" : e.getCause().toString()).reset()); - System.err.println(Ansi.ansi().fgRed().a("==========ERROR========== END ==========").reset()); + +// System.err.println(Ansi.ansi().fgRed().a("错误消息:").a(e.getMessage()).reset()); +// System.err.println(Ansi.ansi().fgRed().a(Objects.equals(e.getLocalizedMessage(), e.getMessage()) ?"":"错误类型:"+e.getLocalizedMessage()).reset()); +// System.err.println(Ansi.ansi().fgRed().a(Arrays.toString(e.getStackTrace())).reset()); + // 打印堆栈跟踪中的第一个元素(通常是出错的地方) + StackTraceElement[] stackTrace = e.getStackTrace(); + if (stackTrace.length > 0) { + StackTraceElement element = stackTrace[0]; // 第一个元素通常是抛出异常的点 + String errorMessage = "["+element.getLineNumber()+"]"+element.getFileName()+" ("+element.getClassName()+")"; + System.err.println(Ansi.ansi().fgRed().a(errorMessage).reset()); + System.err.println(Ansi.ansi().fgRed().a("错误原因:"+e.getMessage()).reset()+"\n"); + } + + } } diff --git a/src/main/java/com/guaiguailang/harmony/controller/UserController.java b/src/main/java/com/guaiguailang/harmony/controller/UserController.java index 7ea1dbe..715e145 100644 --- a/src/main/java/com/guaiguailang/harmony/controller/UserController.java +++ b/src/main/java/com/guaiguailang/harmony/controller/UserController.java @@ -1,6 +1,7 @@ package com.guaiguailang.harmony.controller; import com.guaiguailang.harmony.domain.dto.ParamLogin; +import com.guaiguailang.harmony.domain.dto.ParamUserList; import com.guaiguailang.harmony.service.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -28,4 +29,23 @@ public class UserController { public ResponseEntity loginByAccount(){ return ResponseEntity.ok(userService.getUserInfo()); } + + @Operation( + summary = "用户列表", + description = "加载用户列表", + tags = {"用户相关接口"} + ) + @GetMapping("/list") + public ResponseEntity getUserList(ParamUserList userListParam){ + return ResponseEntity.ok(userService.getUserList(userListParam)); + } + @Operation( + summary = "用户数量", + description = "加载用户数量(全部、已激活、异常)", + tags = {"用户相关接口"} + ) + @GetMapping("/num") + public ResponseEntity getUserListNum(){ + return ResponseEntity.ok(userService.getUserListNum()); + } } diff --git a/src/main/java/com/guaiguailang/harmony/domain/dto/ParamUserList.java b/src/main/java/com/guaiguailang/harmony/domain/dto/ParamUserList.java new file mode 100644 index 0000000..55ce3fb --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/domain/dto/ParamUserList.java @@ -0,0 +1,12 @@ +package com.guaiguailang.harmony.domain.dto; + +import lombok.Data; + +@Data +public class ParamUserList { + // 查询用户类型 all activate disable + String userType; + // 分页 + int pageSize; + int currentPage; +} diff --git a/src/main/java/com/guaiguailang/harmony/domain/vo/UserListNum.java b/src/main/java/com/guaiguailang/harmony/domain/vo/UserListNum.java new file mode 100644 index 0000000..bd31d09 --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/domain/vo/UserListNum.java @@ -0,0 +1,10 @@ +package com.guaiguailang.harmony.domain.vo; + +import lombok.Data; + +@Data +public class UserListNum { + int userAllNum; + int userActivatedNum; + int userDisabledNum; +} diff --git a/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java b/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java index 605730b..2f8aaf4 100644 --- a/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java +++ b/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java @@ -1,14 +1,26 @@ package com.guaiguailang.harmony.mapper; import com.guaiguailang.harmony.domain.entity.UserInfo; +import com.guaiguailang.harmony.domain.vo.UserListNum; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import java.util.List; +import java.util.Map; + @Mapper public interface UserMapper { @Select("SELECT * from user_info where id=#{id} AND status=1") UserInfo getUserById(@Param("id") Long id); @Select("SELECT * from user_info where username=#{username} AND status=1") UserInfo getUserByAccount(String username); + + List getUserListActivate(int limit, int end, String merchantCode, Map queryConditions); + + List getUserListDisabled(int limit,int end, String merchantCode, Map queryConditions); + + List getUserListAll(int limit,int end, String merchantCode, Map queryConditions); + + UserListNum getUserListNum(String merchantCode); } diff --git a/src/main/java/com/guaiguailang/harmony/service/UserService.java b/src/main/java/com/guaiguailang/harmony/service/UserService.java index 3b7a725..322952f 100644 --- a/src/main/java/com/guaiguailang/harmony/service/UserService.java +++ b/src/main/java/com/guaiguailang/harmony/service/UserService.java @@ -1,7 +1,12 @@ package com.guaiguailang.harmony.service; +import com.guaiguailang.harmony.domain.dto.ParamUserList; import com.guaiguailang.harmony.domain.vo.ResponseResult; public interface UserService { ResponseResult getUserInfo(); + + ResponseResult getUserList(ParamUserList userListParam); + + Object getUserListNum(); } diff --git a/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java b/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java index 8158da5..55a8c42 100644 --- a/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java +++ b/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java @@ -1,6 +1,7 @@ package com.guaiguailang.harmony.service.impl; import cn.dev33.satoken.stp.StpUtil; +import com.guaiguailang.harmony.domain.dto.ParamUserList; import com.guaiguailang.harmony.domain.entity.SystemAction; import com.guaiguailang.harmony.domain.entity.SystemPermission; import com.guaiguailang.harmony.domain.entity.SystemRole; @@ -14,8 +15,7 @@ import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; @Slf4j @Service @@ -80,4 +80,44 @@ public class UserServiceImpl implements UserService { log.info(userInfoLogin.toString()); return ResponseResult.success(userInfoLogin); } + + @Override + public ResponseResult getUserList(ParamUserList userListParam) { + // 判断查询人的身份 + Long uid = Long.parseLong(StpUtil.getLoginId().toString()); + // 如果是root,admin 则加载全部数据,如果是agent,则加载对应的用户 + UserInfo userInfo = userMapper.getUserById(uid); + String merchantCode= Objects.equals(userInfo.getRoleId(), "root") || Objects.equals(userInfo.getRoleId(), "admin") ?"ALL":userInfo.getMerchantCode(); + // 计算查询limit + int limit = (userListParam.getCurrentPage()-1) * userListParam.getPageSize(); + int end = limit + userListParam.getPageSize(); + // 创建一个查询条件来排除当前用户 + Map queryConditions = new HashMap<>(); + queryConditions.put("excludeUid", uid); + + List userInfos; + + if (Objects.equals(userListParam.getUserType(), "activate")) { + // 如果有额外的激活状态条件,也可以加入到queryConditions中,比如限制不能查询身份等级比自己高的 | todo 这个任务涉及到新增字段 + userInfos = userMapper.getUserListActivate(limit, end, merchantCode, queryConditions); + } else if (Objects.equals(userListParam.getUserType(), "disabled")) { + // 同上,可以加入禁用状态条件 + userInfos = userMapper.getUserListDisabled(limit, end, merchantCode, queryConditions); + } else { + userInfos = userMapper.getUserListAll(limit, end, merchantCode, queryConditions); + } + return ResponseResult.success(userInfos); + } + + @Override + public Object getUserListNum() { + // 判断查询人的身份 + Long uid = Long.parseLong(StpUtil.getLoginId().toString()); + // 如果是root,admin 则加载全部数据,如果是agent,则加载对应的用户 + UserInfo userInfo = userMapper.getUserById(uid); + String merchantCode= Objects.equals(userInfo.getRoleId(), "root") || Objects.equals(userInfo.getRoleId(), "admin") ?"ALL":userInfo.getMerchantCode(); + // 查询用户数量 + UserListNum userListNum = userMapper.getUserListNum(merchantCode); + return ResponseResult.success(userListNum); + } } diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml index f4dfc39..68b0124 100644 --- a/src/main/resources/mapper/UserMapper.xml +++ b/src/main/resources/mapper/UserMapper.xml @@ -3,4 +3,56 @@ "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> + + + + + + + + + + + \ No newline at end of file