feat: stats relation
This commit is contained in:
parent
f753f8dec2
commit
fb47a11712
2 changed files with 36 additions and 1 deletions
|
|
@ -66,6 +66,13 @@ export class ProductGroup extends Controller {
|
||||||
|
|
||||||
const [result, total] = await prisma.$transaction([
|
const [result, total] = await prisma.$transaction([
|
||||||
prisma.productGroup.findMany({
|
prisma.productGroup.findMany({
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
type: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
||||||
where,
|
where,
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
|
|
@ -74,7 +81,30 @@ export class ProductGroup extends Controller {
|
||||||
prisma.productGroup.count({ where }),
|
prisma.productGroup.count({ where }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return { result, page, pageSize, total };
|
const statsProduct = await prisma.productType.findMany({
|
||||||
|
include: {
|
||||||
|
_count: { select: { product: true } },
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
productGroupId: { in: result.map((v) => v.id) },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
result: result.map((v) => ({
|
||||||
|
...v,
|
||||||
|
_count: {
|
||||||
|
...v._count,
|
||||||
|
product: statsProduct.reduce(
|
||||||
|
(a, c) => (c.productGroupId === v.id ? a + c._count.product : a),
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
total,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("{groupId}")
|
@Get("{groupId}")
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,11 @@ export class ProductType extends Controller {
|
||||||
} satisfies Prisma.ProductTypeWhereInput;
|
} satisfies Prisma.ProductTypeWhereInput;
|
||||||
const [result, total] = await prisma.$transaction([
|
const [result, total] = await prisma.$transaction([
|
||||||
prisma.productType.findMany({
|
prisma.productType.findMany({
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: { product: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
||||||
where,
|
where,
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue