fix(mining-admin-service): getPlantingLedger从synced_adoptions读取真实数据

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-12 04:02:28 -08:00
parent 9a34e9d399
commit bc191791e8
1 changed files with 53 additions and 12 deletions

View File

@ -664,7 +664,6 @@ export class UsersService {
/** /**
* *
* TODO: adoption-service
*/ */
async getPlantingLedger(accountSequence: string, page: number, pageSize: number) { async getPlantingLedger(accountSequence: string, page: number, pageSize: number) {
const user = await this.prisma.syncedUser.findUnique({ const user = await this.prisma.syncedUser.findUnique({
@ -675,22 +674,64 @@ export class UsersService {
throw new NotFoundException(`用户 ${accountSequence} 不存在`); 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 { return {
summary: { summary: {
totalOrders: 0, totalOrders: summary._count.id || 0,
totalTreeCount: 0, totalTreeCount: summary._sum.treeCount || 0,
totalAmount: '0', totalAmount: totalAmount.toString(),
effectiveTreeCount: 0, effectiveTreeCount: summary._sum.treeCount || 0, // 有效认种数默认等于总数
firstPlantingAt: null, firstPlantingAt: summary._min.adoptionDate || null,
lastPlantingAt: null, lastPlantingAt: summary._max.adoptionDate || null,
}, },
items: [], items,
total: 0, total,
page, page,
pageSize, pageSize,
totalPages: 0, totalPages: Math.ceil(total / pageSize),
note: '认种数据需要从 adoption-service 同步',
}; };
} }