This commit is contained in:
2026-07-01 08:27:26 +08:00
parent 9f4577d3d8
commit 25f0d8e97b
56 changed files with 5298 additions and 2 deletions
@@ -0,0 +1,88 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../../common/prisma/prisma.module';
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
import type { AdminUsersQueryDto } from './dto/admin-query.dto';
@Injectable()
export class AdminUsersService {
constructor(private readonly prisma: PrismaService) {}
async list(query: AdminUsersQueryDto) {
const page = query.page ?? 1;
const pageSize = query.pageSize ?? 20;
const where: Prisma.UserWhereInput = {};
if (query.phone) where.phone = { contains: query.phone };
if (query.userNo) where.userNo = { contains: query.userNo };
if (query.deviceKey) where.deviceKey = query.deviceKey;
if (query.status !== undefined) where.status = query.status;
if (query.phoneVerified === '1') where.phoneVerifiedAt = { not: null };
if (query.phoneVerified === '0') where.phoneVerifiedAt = null;
const [items, total] = await Promise.all([
this.prisma.user.findMany({
where,
orderBy: { createdAt: 'desc' },
skip: (page - 1) * pageSize,
take: pageSize,
select: {
id: true,
userNo: true,
deviceKey: true,
phone: true,
phoneVerifiedAt: true,
mergedIntoUserId: true,
nickname: true,
status: true,
createdAt: true,
updatedAt: true,
_count: { select: { orders: true } },
},
}),
this.prisma.user.count({ where }),
]);
return serializeBigInt({
items: items.map((u) => ({
...u,
orderCount: u._count.orders,
_count: undefined,
})),
total,
page,
pageSize,
});
}
async detail(id: bigint) {
const user = await this.prisma.user.findUnique({
where: { id },
include: {
cityPref: true,
mergedInto: { select: { id: true, userNo: true, phone: true, nickname: true } },
orders: {
orderBy: { createdAt: 'desc' },
take: 5,
select: {
id: true,
orderNo: true,
status: true,
payAmount: true,
createdAt: true,
},
},
_count: { select: { mergedFrom: true, orders: true, addresses: true } },
},
});
if (!user) throw new NotFoundException('用户不存在');
return serializeBigInt({
...user,
mergedFromCount: user._count.mergedFrom,
orderCount: user._count.orders,
addressCount: user._count.addresses,
_count: undefined,
});
}
}