feat(数据库): 扩展数据库查询操作符并优化联系人分组逻辑

- 在 DatabaseService 中添加新的查询操作符(notEqual, aboveOrEqual, belowOrEqual, contains, anyOf, notIn, between)
- 修改 createContractList 方法以支持未分组联系人的查询
- 添加调试日志用于跟踪缓存结果和真实分组
This commit is contained in:
超级老白兔
2025-09-18 15:04:06 +08:00
parent 5b5a05d8c5
commit d110c97424
3 changed files with 55 additions and 7 deletions

View File

@@ -12,16 +12,36 @@ export const createContractList = async (
countLables: ContactGroupByLabel[],
) => {
// 根据 groupType 决定查询不同的服务
const realGroup = countLables
.filter(item => item.id !== 0)
.map(item => item.id);
console.log(realGroup);
const dataByLabels = [];
for (const label of countLables) {
let data;
if (label.groupType === 1) {
// groupType: 1, 查询 contractService
data = await contractService.findWhere("groupId", label.id);
// 过滤出 kfSelected 对应的联系人
if (kfSelected && kfSelected != 0) {
data = data.filter(contact => contact.wechatAccountId === kfSelected);
console.log(label.groupName);
if (label.id == 0) {
data = await contractService.findWhereMultiple([
{
field: "groupId",
operator: "notIn",
value: realGroup,
},
]);
// 过滤出 kfSelected 对应的联系人
if (kfSelected && kfSelected != 0) {
data = data.filter(contact => contact.wechatAccountId === kfSelected);
}
} else {
data = await contractService.findWhere("groupId", label.id);
// 过滤出 kfSelected 对应的联系人
if (kfSelected && kfSelected != 0) {
data = data.filter(contact => contact.wechatAccountId === kfSelected);
}
}
// console.log(`标签 ${label.groupName} 对应的联系人数据:`, data);
} else if (label.groupType === 2) {
// groupType: 2, 查询 weChatGroupService

View File

@@ -131,6 +131,8 @@ export const useCkChatStore = createPersistStore<CkChatState>(
lastSearchKeyword = state.searchKeyword;
}
console.log("cachedResult", cachedResult);
return cachedResult;
};
})(),

View File

@@ -238,27 +238,53 @@ export class DatabaseService<T> {
async findWhereMultiple(
conditions: {
field: keyof T;
operator: "equals" | "above" | "below" | "startsWith";
operator:
| "equals"
| "notEqual"
| "above"
| "below"
| "aboveOrEqual"
| "belowOrEqual"
| "startsWith"
| "anyOf"
| "notIn"
| "between"
| "contains";
value: any;
value2?: any; // 用于 between 操作符
}[],
): Promise<T[]> {
let collection = this.table.toCollection();
for (const condition of conditions) {
const { field, operator, value } = condition;
const { field, operator, value, value2 } = condition;
collection = collection.and(item => {
const fieldValue = (item as any)[field];
switch (operator) {
case "equals":
return fieldValue === value;
case "notEqual":
return fieldValue !== value;
case "above":
return fieldValue > value;
case "below":
return fieldValue < value;
case "aboveOrEqual":
return fieldValue >= value;
case "belowOrEqual":
return fieldValue <= value;
case "startsWith":
return (
typeof fieldValue === "string" && fieldValue.startsWith(value)
);
case "contains":
return typeof fieldValue === "string" && fieldValue.includes(value);
case "anyOf":
return Array.isArray(value) && value.includes(fieldValue);
case "notIn":
return Array.isArray(value) && !value.includes(fieldValue);
case "between":
return fieldValue >= value && fieldValue <= (value2 ?? value);
default:
return true;
}