查询已关注用户功能实现报告
文章目录
目标
实现一个查询已关注用户的功能,具体流程如下:
- 根据用户ID查询已关注的用户ID。
- 根据已关注的用户ID查询用户信息。
- 按照置顶排序和昵称首字母或拼音首字母排序返回前端。
逻辑要求
- 根据用户ID查询已关注的用户ID。
- 根据已关注的用户ID查询用户信息。
- 按照置顶排序和昵称首字母或拼音首字母排序返回前端。
- 置顶用户优先显示。
- 昵称为空或不符合字母规则的用户归类到“#”分类。
开发思路
1. 查询已关注的用户ID
首先,根据用户ID查询用户关注表user_follow
中的已关注用户ID。
2. 查询用户信息
根据已关注的用户ID,从用户信息表user_info
中查询用户的详细信息。
3. 排序逻辑
- 初始化结果集,包含从A到Z的字母分类和一个“#”分类。
- 遍历查询到的用户信息,根据用户的置顶状态和昵称首字母或拼音首字母进行分类。
- 置顶用户单独分类。
- 昵称为空或不符合字母规则的用户归类到“#”分类。
详细实现
使用的依赖
在项目中使用了pinyin4j
库来处理中文字符的拼音转换。添加以下依赖到pom.xml
文件中:
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
获取拼音首字母的代码
import net.sourceforge.pinyin4j.PinyinHelper;
// Method to get the pinyin first letter of a Chinese character
private static String getPinyinFirstLetter(char chineseCharacter) {
try {
// 提取字符的首字母
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(chineseCharacter);
return String.valueOf(pinyinArray[0].charAt(0)).toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
代码解析
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(chineseCharacter);
获取汉字的拼音数组。return String.valueOf(pinyinArray[0].charAt(0)).toUpperCase();
返回拼音的首字母并转换为大写。- 捕获异常并返回null。
查询已关注用户的代码
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.Valid;
import java.util.*;
@PostMapping("/searchFollowUser")
@SaCheckLogin
public R searchFollowUser(@RequestBody @Valid SearchFollowUserForm form) {
Map param = BeanUtil.beanToMap(form);
ArrayList<HashMap> list = userService.searchFollowUser(param);
Map<String, List<Map<String, Object>>> result = new HashMap<>();
// Initialize the result map with keys from A to Z and #
result.put("pin", new ArrayList<>());
for (char c = 'A'; c <= 'Z'; c++) {
result.put(String.valueOf(c), new ArrayList<>());
}
result.put("#", new ArrayList<>());
for (HashMap<String, Object> map : list) {
Integer pin = (Integer) map.get("pin");
if (pin == 1){
result.get("pin").add(map);
continue;
}
String nickName = (String) map.get("nick_name");
if (nickName != null && !nickName.isEmpty()) {
char firstChar = nickName.charAt(0);
String key;
// Check if the first character is a letter
if (String.valueOf(firstChar).matches("[A-Za-z]")) {
key = String.valueOf(firstChar).toUpperCase();
} else {
// Convert Chinese character to pinyin first letter
key = getPinyinFirstLetter(firstChar);
if (key == null || !key.matches("[A-Za-z]")) {
key = "#";
}
}
result.get(key).add(map);
} else {
// Handle cases where nick_name is null or empty
result.get("#").add(map);
}
}
return R.ok().put("result", result);
}
代码解析
Map param = BeanUtil.beanToMap(form);
将表单数据转换为Map。ArrayList<HashMap> list = userService.searchFollowUser(param);
查询已关注的用户信息。- 初始化结果集,包含从A到Z的字母分类和一个“#”分类。
- 遍历查询到的用户信息,根据用户的置顶状态和昵称首字母或拼音首字母进行分类。
- 置顶用户单独分类。
- 昵称为空或不符合字母规则的用户归类到“#”分类。
数据库表结构
用户关注表 user_follow
-- ----------------------------
-- Table structure for user_follow
-- ----------------------------
DROP TABLE IF EXISTS `user_follow`;
CREATE TABLE `user_follow` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键,自增长',
`user_id` int NULL DEFAULT NULL COMMENT '外键,关联用户信息表',
`follow_user_id` int NULL DEFAULT NULL COMMENT '外键,关联用户信息表',
`pin` tinyint NULL DEFAULT 0 COMMENT '置顶,0为未置顶,1为置顶。默认为0',
PRIMARY KEY (`id`) USING BTREE
);
用户信息表 user_info
CREATE TABLE `user_info` (
`uuid` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'UUID,当做用户信息卡的编号',
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '姓名',
`sex` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '性别',
`phone` char(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机号码',
`email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '电子邮箱',
`birthday` date NULL DEFAULT NULL COMMENT '出生日期',
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '居住地址',
`height` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '身高(m),格式1.83'
);
膳逸
Base URLs:
Authentication
shanyi-api
POST 查询已关注的用户
POST /user/searchFollowUser
根据userId查询follow_user_id,再找出user和user_info中的信息,按照置顶排序和昵称首字母或拼音首字母排序返回前端,pin类为置顶。
Body 请求参数
{
"userId": "4",
"nickName": "",
"name": ""
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
---|---|---|---|---|
token | header | string | 否 | none |
body | body | object | 否 | none |
» userId | body | string | 是 | none |
» nickName | body | string | 否 | none |
» name | body | string | 否 | none |
返回示例
成功
{
"msg": "success",
"result": {
"A": [],
"B": [
{
"birthday": "2003-11-01",
"address": "广东省佛山市",
"goal": "考",
"create_time": "2024-04-29",
"sex": "男",
"weight": "55.0",
"diseases": [
"蚕豆病"
],
"dislikes": [
"酸菜角"
],
"avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/6ibtnStKibXDUTibnu052w40ps0DKqcUZBYaAiberiasgSsTGXwCLAicmXng2gE48ZnibYrmnPIiaVlNWjpCYiby85ibgM8Q/132",
"follow_user_id": 5,
"pin": 0,
"user_id": 4,
"phone": "1362**",
"nick_name": "绊象",
"name": "陈**",
"id": 5,
"email": "253**@qq.com",
"status": 1,
"height": "1.78",
"bmi": "17.36",
"likes": [
"番茄炒鸡蛋"
]
}
],
"C": [],
"D": [],
"E": [],
"F": [],
"G": [],
"H": [],
"I": [],
"J": [],
"K": [],
"L": [
{
"birthday": "1901-01-01",
"address": "山东济南",
"goal": "增肌",
"sex": "男",
"weight": "80.2",
"diseases": [],
"dislikes": [
"湘菜"
],
"avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/5icHNmy3LXpUEY3JHKrm4P0twqobiaYuemvXr4s6aK2rJKQ7RkVOaPw4nRSkEfwErCIzkSUyvibFicJ37IA5Yppumg/132",
"follow_user_id": 3,
"pin": 0,
"user_id": 4,
"phone": "155***",
"nick_name": "LHPlanet",
"name": "李**",
"id": 3,
"email": "752**@qq.com",
"status": 1,
"height": "1.94",
"bmi": "21.31",
"likes": [
"大米"
]
}
],
"M": [],
"N": [],
"O": [],
"P": [],
"Q": [],
"R": [],
"S": [],
"pin": [],
"T": [],
"U": [],
"V": [],
"W": [],
"X": [],
"Y": [],
"Z": [],
"#": []
},
"code": 200
}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
---|---|---|---|
200 | OK | 成功 | Inline |
返回数据结构
状态码 200
名称 | 类型 | 必选 | 约束 | 中文名 | 说明 |
---|---|---|---|---|---|
» code | integer | true | none | none | |
» msg | string | true | none | none | |
» result | object | true | none | none | |
»» A | [string] | true | none | none | |
»» B | [object] | true | none | none | |
»»» birthday | string | false | none | none | |
»»» address | string | false | none | none | |
»»» goal | string | false | none | none | |
»»» create_time | string | false | none | none | |
»»» sex | string | false | none | none | |
»»» weight | string | false | none | none | |
»»» diseases | [string] | false | none | none | |
»»» dislikes | [string] | false | none | none | |
»»» avatar | string | false | none | none | |
»»» follow_user_id | integer | false | none | none | |
»»» pin | integer | false | none | none | |
»»» user_id | integer | false | none | none | |
»»» phone | string | false | none | none | |
»»» nick_name | string | false | none | none | |
»»» name | string | false | none | none | |
»»» id | integer | false | none | none | |
string | false | none | none | ||
»»» status | integer | false | none | none | |
»»» height | string | false | none | none | |
»»» bmi | string | false | none | none | |
»»» likes | [string] | false | none | none | |
»» C | [string] | true | none | none | |
»» D | [string] | true | none | none | |
»» E | [string] | true | none | none | |
»» F | [string] | true | none | none | |
»» G | [string] | true | none | none | |
»» H | [string] | true | none | none | |
»» I | [string] | true | none | none | |
»» J | [string] | true | none | none | |
»» K | [string] | true | none | none | |
»» L | [object] | true | none | none | |
»»» birthday | string | false | none | none | |
»»» address | string | false | none | none | |
»»» goal | string | false | none | none | |
»»» sex | string | false | none | none | |
»»» weight | string | false | none | none | |
»»» diseases | [string] | false | none | none | |
»»» dislikes | [string] | false | none | none | |
»»» avatar | string | false | none | none | |
»»» follow_user_id | integer | false | none | none | |
»»» pin | integer | false | none | none | |
»»» user_id | integer | false | none | none | |
»»» phone | string | false | none | none | |
»»» nick_name | string | false | none | none | |
»»» name | string | false | none | none | |
»»» id | integer | false | none | none | |
string | false | none | none | ||
»»» status | integer | false | none | none | |
»»» height | string | false | none | none | |
»»» bmi | string | false | none | none | |
»»» likes | [string] | false | none | none | |
»» M | [string] | true | none | none | |
»» N | [string] | true | none | none | |
»» O | [string] | true | none | none | |
»» P | [string] | true | none | none | |
»» Q | [string] | true | none | none | |
»» R | [string] | true | none | none | |
»» S | [string] | true | none | none | |
»» pin | [string] | true | none | none | |
»» T | [string] | true | none | none | |
»» U | [string] | true | none | none | |
»» V | [string] | true | none | none | |
»» W | [string] | true | none | none | |
»» X | [string] | true | none | none | |
»» Y | [string] | true | none | none | |
»» Z | [string] | true | none | none | |
»» # | [string] | true | none | none |
数据模型
结论
通过上述代码实现了查询已关注用户的功能,确保了用户信息的准确性和排序的合理性。具体好处如下:
- 准确性:根据用户ID查询已关注的用户ID,并从用户信息表中获取详细信息,确保数据的准确性。
- 排序合理性:按照置顶状态和昵称首字母或拼音首字母进行分类和排序,确保用户信息的展示顺序合理。
- 用户体验:通过分类展示已关注的用户信息,提升用户体验。
- 可维护性:清晰的代码结构和详细的注释,便于后续维护和扩展。
- 扩展性:使用
pinyin4j
库处理中文字符的拼音转换,便于后续扩展和修改。
通过这些好处,可以确保查询已关注用户功能的准确、合理和易用,满足用户和系统的需求。