新增 用户列表接口

This commit is contained in:
萌狼蓝天 2024-09-24 13:01:35 +08:00
parent 4a5ba456c2
commit 75dcec77f1
8 changed files with 168 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects;
@RestControllerAdvice @RestControllerAdvice
@Slf4j @Slf4j
@ -14,10 +15,19 @@ public class GlobalExceptionHandler {
// 全局异常拦截 // 全局异常拦截
@ExceptionHandler @ExceptionHandler
public void handlerException(Exception e) { 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.getMessage()).reset());
System.err.println(Ansi.ansi().fgRed().a("错误类型:").a(e.getLocalizedMessage()).reset()); // System.err.println(Ansi.ansi().fgRed().a(Objects.equals(e.getLocalizedMessage(), e.getMessage()) ?"":"错误类型:"+e.getLocalizedMessage()).reset());
System.err.println(Ansi.ansi().fgRed().a(e.getCause() == null ? "" : e.getCause().toString()).reset()); // System.err.println(Ansi.ansi().fgRed().a(Arrays.toString(e.getStackTrace())).reset());
System.err.println(Ansi.ansi().fgRed().a("==========ERROR========== END ==========").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");
}
} }
} }

View File

@ -1,6 +1,7 @@
package com.guaiguailang.harmony.controller; package com.guaiguailang.harmony.controller;
import com.guaiguailang.harmony.domain.dto.ParamLogin; import com.guaiguailang.harmony.domain.dto.ParamLogin;
import com.guaiguailang.harmony.domain.dto.ParamUserList;
import com.guaiguailang.harmony.service.UserService; import com.guaiguailang.harmony.service.UserService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@ -28,4 +29,23 @@ public class UserController {
public ResponseEntity loginByAccount(){ public ResponseEntity loginByAccount(){
return ResponseEntity.ok(userService.getUserInfo()); 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());
}
} }

View File

@ -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;
}

View File

@ -0,0 +1,10 @@
package com.guaiguailang.harmony.domain.vo;
import lombok.Data;
@Data
public class UserListNum {
int userAllNum;
int userActivatedNum;
int userDisabledNum;
}

View File

@ -1,14 +1,26 @@
package com.guaiguailang.harmony.mapper; package com.guaiguailang.harmony.mapper;
import com.guaiguailang.harmony.domain.entity.UserInfo; 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.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
@Mapper @Mapper
public interface UserMapper { public interface UserMapper {
@Select("SELECT * from user_info where id=#{id} AND status=1") @Select("SELECT * from user_info where id=#{id} AND status=1")
UserInfo getUserById(@Param("id") Long id); UserInfo getUserById(@Param("id") Long id);
@Select("SELECT * from user_info where username=#{username} AND status=1") @Select("SELECT * from user_info where username=#{username} AND status=1")
UserInfo getUserByAccount(String username); UserInfo getUserByAccount(String username);
List<UserInfo> getUserListActivate(int limit, int end, String merchantCode, Map<String, Object> queryConditions);
List<UserInfo> getUserListDisabled(int limit,int end, String merchantCode, Map<String, Object> queryConditions);
List<UserInfo> getUserListAll(int limit,int end, String merchantCode, Map<String, Object> queryConditions);
UserListNum getUserListNum(String merchantCode);
} }

View File

@ -1,7 +1,12 @@
package com.guaiguailang.harmony.service; package com.guaiguailang.harmony.service;
import com.guaiguailang.harmony.domain.dto.ParamUserList;
import com.guaiguailang.harmony.domain.vo.ResponseResult; import com.guaiguailang.harmony.domain.vo.ResponseResult;
public interface UserService { public interface UserService {
ResponseResult getUserInfo(); ResponseResult getUserInfo();
ResponseResult getUserList(ParamUserList userListParam);
Object getUserListNum();
} }

View File

@ -1,6 +1,7 @@
package com.guaiguailang.harmony.service.impl; package com.guaiguailang.harmony.service.impl;
import cn.dev33.satoken.stp.StpUtil; 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.SystemAction;
import com.guaiguailang.harmony.domain.entity.SystemPermission; import com.guaiguailang.harmony.domain.entity.SystemPermission;
import com.guaiguailang.harmony.domain.entity.SystemRole; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.*;
import java.util.List;
@Slf4j @Slf4j
@Service @Service
@ -80,4 +80,44 @@ public class UserServiceImpl implements UserService {
log.info(userInfoLogin.toString()); log.info(userInfoLogin.toString());
return ResponseResult.success(userInfoLogin); return ResponseResult.success(userInfoLogin);
} }
@Override
public ResponseResult getUserList(ParamUserList userListParam) {
// 判断查询人的身份
Long uid = Long.parseLong(StpUtil.getLoginId().toString());
// 如果是rootadmin 则加载全部数据如果是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<String, Object> queryConditions = new HashMap<>();
queryConditions.put("excludeUid", uid);
List<UserInfo> 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());
// 如果是rootadmin 则加载全部数据如果是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);
}
} }

View File

@ -3,4 +3,56 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.guaiguailang.harmony.mapper.UserMapper"> <mapper namespace="com.guaiguailang.harmony.mapper.UserMapper">
<!-- getUserListActivate 方法 -->
<select id="getUserListActivate" resultType="com.guaiguailang.harmony.domain.entity.UserInfo">
SELECT * FROM user_info
WHERE status = 1
<if test="merchantCode != 'ALL'">
AND merchant_code = #{merchantCode}
</if>
<if test="queryConditions.excludeUid != null">
AND id != #{queryConditions.excludeUid}
</if>
LIMIT #{limit}
</select>
<!-- getUserListDisabled 方法 -->
<select id="getUserListDisabled" resultType="com.guaiguailang.harmony.domain.entity.UserInfo">
SELECT * FROM user_info
WHERE status != 1
<if test="merchantCode != 'ALL'">
AND merchant_code = #{merchantCode}
</if>
<if test="queryConditions.excludeUid != null">
AND id != #{queryConditions.excludeUid}
</if>
LIMIT #{limit}
</select>
<!-- getUserListAll 方法 -->
<select id="getUserListAll" resultType="com.guaiguailang.harmony.domain.entity.UserInfo">
SELECT * FROM user_info
<where>
<if test="merchantCode != 'ALL'">
WHERE merchant_code = #{merchantCode}
</if>
<if test="queryConditions.excludeUid != null">
AND id != #{queryConditions.excludeUid}
</if>
</where>
LIMIT #{limit},#{end}
</select>
<select id="getUserListNum" resultType="com.guaiguailang.harmony.domain.vo.UserListNum">
SELECT
SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS userActivatedNum,
SUM(CASE WHEN status != 1 THEN 1 ELSE 0 END) AS userDisabledNum,
COUNT(*) AS userAllNum
FROM
user_info
<if test="merchantCode != 'ALL'">
WHERE merchant_code = #{merchantCode}
</if>
</select>
</mapper> </mapper>