feat: complete reporting and filing workflows
This commit is contained in:
@@ -294,13 +294,37 @@ export class DictionariesService {
|
||||
|
||||
async listDrainageFields() {
|
||||
const fields = await this.prisma.drainageField.findMany({
|
||||
include: { _count: { select: { channelReportFields: true, commonReportFields: true } } },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
return fields.map(({ _count, ...field }) => ({
|
||||
const fieldIds = fields.map((field) => field.id);
|
||||
const [channelReferences, commonReferences] = fieldIds.length ? await Promise.all([
|
||||
this.prisma.channelReportField.findMany({
|
||||
where: {
|
||||
drainageFieldId: { in: fieldIds },
|
||||
channel: { status: { not: 'deleted' } },
|
||||
},
|
||||
select: { drainageFieldId: true, channelId: true },
|
||||
}),
|
||||
this.prisma.commonReportField.findMany({
|
||||
where: { drainageFieldId: { in: fieldIds } },
|
||||
select: { drainageFieldId: true },
|
||||
}),
|
||||
]) : [[], []];
|
||||
const channelsByField = new Map<string, Set<string>>();
|
||||
for (const reference of channelReferences) {
|
||||
if (!reference.drainageFieldId) continue;
|
||||
const channelIds = channelsByField.get(reference.drainageFieldId) ?? new Set<string>();
|
||||
channelIds.add(reference.channelId);
|
||||
channelsByField.set(reference.drainageFieldId, channelIds);
|
||||
}
|
||||
const commonCountByField = new Map<string, number>();
|
||||
for (const reference of commonReferences) {
|
||||
commonCountByField.set(reference.drainageFieldId, (commonCountByField.get(reference.drainageFieldId) ?? 0) + 1);
|
||||
}
|
||||
return fields.map((field) => ({
|
||||
...field,
|
||||
usageCount: _count.channelReportFields,
|
||||
commonUsageCount: _count.commonReportFields,
|
||||
usageCount: channelsByField.get(field.id)?.size ?? 0,
|
||||
commonUsageCount: commonCountByField.get(field.id) ?? 0,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -326,13 +350,20 @@ export class DictionariesService {
|
||||
|
||||
async deleteDrainageField(id: string) {
|
||||
const [usageCount, commonUsageCount] = await Promise.all([
|
||||
this.prisma.channelReportField.count({ where: { drainageFieldId: id } }),
|
||||
this.prisma.channelReportField.count({
|
||||
where: { drainageFieldId: id, channel: { status: { not: 'deleted' } } },
|
||||
}),
|
||||
this.prisma.commonReportField.count({ where: { drainageFieldId: id } }),
|
||||
]);
|
||||
if (usageCount > 0 || commonUsageCount > 0) {
|
||||
throw new BadRequestException(`该字段已被 ${usageCount} 个通道和 ${commonUsageCount} 个通用配置使用,不能删除`);
|
||||
}
|
||||
return this.prisma.drainageField.delete({ where: { id } });
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
await tx.channelReportField.deleteMany({
|
||||
where: { drainageFieldId: id, channel: { status: 'deleted' } },
|
||||
});
|
||||
return tx.drainageField.delete({ where: { id } });
|
||||
});
|
||||
}
|
||||
|
||||
listCommonReportFields() {
|
||||
|
||||
Reference in New Issue
Block a user