diff --git a/backend/services/mining-admin-service/src/application/services/users.service.ts b/backend/services/mining-admin-service/src/application/services/users.service.ts index 6d918f92..7894113f 100644 --- a/backend/services/mining-admin-service/src/application/services/users.service.ts +++ b/backend/services/mining-admin-service/src/application/services/users.service.ts @@ -664,7 +664,6 @@ export class UsersService { /** * 获取用户认种分类账 - * TODO: 从 adoption-service 同步认种数据 */ async getPlantingLedger(accountSequence: string, page: number, pageSize: number) { const user = await this.prisma.syncedUser.findUnique({ @@ -675,22 +674,64 @@ export class UsersService { throw new NotFoundException(`用户 ${accountSequence} 不存在`); } - // 返回空数据,数据需要从 adoption-service 同步 + // 查询认种记录总数 + const total = await this.prisma.syncedAdoption.count({ + where: { accountSequence }, + }); + + // 分页查询认种记录 + const adoptions = await this.prisma.syncedAdoption.findMany({ + where: { accountSequence }, + orderBy: { adoptionDate: 'desc' }, + skip: (page - 1) * pageSize, + take: pageSize, + }); + + // 汇总统计 + const summary = await this.prisma.syncedAdoption.aggregate({ + where: { accountSequence }, + _count: { id: true }, + _sum: { treeCount: true }, + _min: { adoptionDate: true }, + _max: { adoptionDate: true }, + }); + + // 计算总金额(每棵树 * 树数量) + const allAdoptions = await this.prisma.syncedAdoption.findMany({ + where: { accountSequence }, + select: { treeCount: true, contributionPerTree: true }, + }); + + let totalAmount = 0; + for (const a of allAdoptions) { + totalAmount += a.treeCount * Number(a.contributionPerTree); + } + + // 格式化认种记录 + const items = adoptions.map((a) => ({ + id: a.id, + originalAdoptionId: a.originalAdoptionId.toString(), + treeCount: a.treeCount, + adoptionDate: a.adoptionDate, + status: a.status || 'ACTIVE', + contributionPerTree: a.contributionPerTree.toString(), + totalContribution: (a.treeCount * Number(a.contributionPerTree)).toString(), + })); + return { summary: { - totalOrders: 0, - totalTreeCount: 0, - totalAmount: '0', - effectiveTreeCount: 0, - firstPlantingAt: null, - lastPlantingAt: null, + totalOrders: summary._count.id || 0, + totalTreeCount: summary._sum.treeCount || 0, + totalAmount: totalAmount.toString(), + effectiveTreeCount: summary._sum.treeCount || 0, // 有效认种数默认等于总数 + firstPlantingAt: summary._min.adoptionDate || null, + lastPlantingAt: summary._max.adoptionDate || null, }, - items: [], - total: 0, + items, + total, page, pageSize, - totalPages: 0, - note: '认种数据需要从 adoption-service 同步', + totalPages: Math.ceil(total / pageSize), }; }