优化联系人分组统计逻辑,分别提取好友和群组标签ID以提高准确性,更新相关注释以增强代码可读性,同时调整数据库查询条件以支持更灵活的分组查询。

This commit is contained in:
超级老白兔
2025-10-24 17:20:09 +08:00
parent 28cbcea4f9
commit 7d8573b52a
3 changed files with 40 additions and 10 deletions

View File

@@ -206,8 +206,13 @@ export const getGroupStatistics = async (
labels: ContactGroupByLabel[],
customerId?: number,
): Promise<ContactGroupByLabel[]> => {
const realGroupIds = labels
.filter(item => item.id !== 0)
// 分别提取好友标签和群标签的ID
const friendGroupIds = labels
.filter(item => item.id !== 0 && Number(item.groupType) === 1)
.map(item => item.id);
const chatGroupIds = labels
.filter(item => item.id !== 0 && Number(item.groupType) === 2)
.map(item => item.id);
const groupedData: ContactGroupByLabel[] = [];
@@ -218,12 +223,12 @@ export const getGroupStatistics = async (
if (Number(label.groupType) === 1) {
// 好友分组
if (label.id === 0) {
// 未分组:不属于任何标签的好友
// 未分组:不属于任何好友标签的好友
count = await ContactManager.getContactCount(
userId,
"friend",
customerId,
realGroupIds,
friendGroupIds, // 只排除好友标签
true, // 排除已分组的
);
} else {
@@ -239,12 +244,12 @@ export const getGroupStatistics = async (
} else if (Number(label.groupType) === 2) {
// 群组分组
if (label.id === 0) {
// 默认群分组:不属于任何标签的群
// 默认群分组:不属于任何标签的群
count = await ContactManager.getContactCount(
userId,
"group",
customerId,
realGroupIds,
chatGroupIds, // 只排除群标签
true, // 排除已分组的
);
} else {

View File

@@ -174,8 +174,13 @@ const ContactListSimple: React.FC<WechatFriendsProps> = ({
}));
try {
// 根据当前标签的 groupType 计算正确的 realGroupIds
const realGroupIds = labels
.filter(item => item.id !== 0)
.filter(
item =>
item.id !== 0 &&
Number(item.groupType) === Number(label.groupType),
)
.map(item => item.id);
const contacts = await getContactsByGroup(
@@ -231,8 +236,13 @@ const ContactListSimple: React.FC<WechatFriendsProps> = ({
const currentPage = groupData.pages[groupKey] || 1;
const nextPage = currentPage + 1;
// 根据当前标签的 groupType 计算正确的 realGroupIds
const realGroupIds = labels
.filter(item => item.id !== 0)
.filter(
item =>
item.id !== 0 &&
Number(item.groupType) === Number(label.groupType),
)
.map(item => item.id);
const newContacts = await getContactsByGroup(

View File

@@ -326,6 +326,14 @@ export class ContactManager {
exclude: boolean = false,
): Promise<number> {
try {
console.log("getContactCount 调用参数:", {
userId,
type,
customerId,
groupIds,
exclude,
});
const conditions: any[] = [
{ field: "userId", operator: "equals", value: userId },
{ field: "type", operator: "equals", value: type },
@@ -353,14 +361,21 @@ export class ContactManager {
// 包含指定分组
conditions.push({
field: "groupId",
operator: "in",
operator: "anyOf",
value: groupIds,
});
}
}
console.log("查询条件:", conditions);
const contacts =
await contactUnifiedService.findWhereMultiple(conditions);
console.log(
`查询结果数量: ${contacts.length}, type: ${type}, groupIds: ${groupIds}`,
);
return contacts.length;
} catch (error) {
console.error("获取联系人数量失败:", error);
@@ -408,7 +423,7 @@ export class ContactManager {
// 包含指定分组
conditions.push({
field: "groupId",
operator: "in",
operator: "anyOf",
value: groupIds,
});
}