fix(admin-service): 认种筛选改用 PlantingOrderQueryView 实时查询

UserQueryView.personalAdoptionCount 预计算字段未被事件同步更新(始终为 0),
导致前端选"已认种"后查不到数据。

修复:认种数过滤(minAdoptions/maxAdoptions)不再查 UserQueryView 的
stale 字段,改为实时 groupBy 查询 PlantingOrderQueryView
(status='MINING_ENABLED'),与 getBatchUserStats 展示数据源保持一致。

- 已认种(minAdoptions>=1):accountSequence IN 有订单的集合
- 未认种(maxAdoptions=0):accountSequence NOT IN 有订单的集合

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-02-03 08:09:12 -08:00
parent 50dc18a224
commit 17ecc9954f
1 changed files with 28 additions and 10 deletions

View File

@ -24,6 +24,32 @@ export class UserQueryRepositoryImpl implements IUserQueryRepository {
const where = this.buildWhereClause(filters); const where = this.buildWhereClause(filters);
const orderBy = this.buildOrderBy(sort); const orderBy = this.buildOrderBy(sort);
// 认种筛选UserQueryView.personalAdoptionCount 可能未同步,
// 改为实时查询 PlantingOrderQueryView与 getBatchUserStats 数据源一致)
if (filters.minAdoptions !== undefined || filters.maxAdoptions !== undefined) {
const adoptedAccounts = await this.prisma.plantingOrderQueryView.groupBy({
by: ['accountSequence'],
where: { status: 'MINING_ENABLED' },
_count: { id: true },
});
const adoptedSeqs = new Set(adoptedAccounts.map(a => a.accountSequence));
if (filters.minAdoptions !== undefined && filters.minAdoptions > 0) {
// 已认种accountSequence 必须在有 MINING_ENABLED 订单的集合中
where.accountSequence = {
...(typeof where.accountSequence === 'object' ? where.accountSequence as any : {}),
in: [...adoptedSeqs],
};
}
if (filters.maxAdoptions !== undefined && filters.maxAdoptions === 0) {
// 未认种accountSequence 不在有 MINING_ENABLED 订单的集合中
where.accountSequence = {
...(typeof where.accountSequence === 'object' ? where.accountSequence as any : {}),
notIn: [...adoptedSeqs],
};
}
}
const [items, total] = await Promise.all([ const [items, total] = await Promise.all([
this.prisma.userQueryView.findMany({ this.prisma.userQueryView.findMany({
where, where,
@ -264,16 +290,8 @@ export class UserQueryRepositoryImpl implements IUserQueryRepository {
where.inviterSequence = filters.hasInviter ? { not: null } : null; where.inviterSequence = filters.hasInviter ? { not: null } : null;
} }
// 认种数范围 // 认种数范围:不再使用 personalAdoptionCount预计算字段可能未同步
if (filters.minAdoptions !== undefined || filters.maxAdoptions !== undefined) { // 改为在 findMany 中实时查询 PlantingOrderQueryView 处理
where.personalAdoptionCount = {};
if (filters.minAdoptions !== undefined) {
where.personalAdoptionCount.gte = filters.minAdoptions;
}
if (filters.maxAdoptions !== undefined) {
where.personalAdoptionCount.lte = filters.maxAdoptions;
}
}
// 注册时间范围 // 注册时间范围
if (filters.registeredAfter || filters.registeredBefore) { if (filters.registeredAfter || filters.registeredBefore) {