import { Injectable } from '@nestjs/common'; import { Prisma } from '@prisma/client'; import { ErrorCode, TemplateStatus } from '@artveris/shared'; import { PrismaService } from '../prisma/prisma.service'; import { BusinessException } from '../common/exceptions/business.exception'; import { buildPage } from '../common/utils/pagination.util'; import { buildOrderBy } from '../common/utils/sort.util'; import { CreateTemplateDto, QueryTemplateDto, UpdateTemplateDto, TemplateFieldDto } from './dto/template.dto'; @Injectable() export class TemplateService { constructor(private readonly prisma: PrismaService) {} private fieldsCreate(fields: TemplateFieldDto[]) { return fields.map((f, i) => ({ fieldKey: f.fieldKey, label: f.label, type: f.type, required: f.required ?? false, sortOrder: f.sortOrder ?? i, })); } async create(dto: CreateTemplateDto) { return this.prisma.template.create({ data: { name: dto.name, remark: dto.remark, fields: { create: this.fieldsCreate(dto.fields) }, }, include: { fields: { orderBy: { sortOrder: 'asc' } } }, }); } async list(query: QueryTemplateDto) { const where: Prisma.TemplateWhereInput = { deletedAt: null }; if (query.keyword) where.name = { contains: query.keyword }; if (query.status) where.status = query.status; const orderBy = buildOrderBy(query.sortField, query.sortOrder, ['createdAt', 'updatedAt'] as const, { id: 'desc' }); const [list, total] = await this.prisma.$transaction([ this.prisma.template.findMany({ where, include: { fields: { orderBy: { sortOrder: 'asc' } }, _count: { select: { artworks: true } } }, orderBy, skip: query.skip, take: query.take, }), this.prisma.template.count({ where }), ]); return buildPage(list, total, query.page, query.pageSize); } // 供建档表单选择的启用模板(含字段) async options() { return this.prisma.template.findMany({ where: { deletedAt: null, status: TemplateStatus.ENABLED }, include: { fields: { orderBy: { sortOrder: 'asc' } } }, orderBy: { id: 'desc' }, }); } async get(id: number) { const tpl = await this.prisma.template.findFirst({ where: { id, deletedAt: null }, include: { fields: { orderBy: { sortOrder: 'asc' } } }, }); if (!tpl) throw new BusinessException(ErrorCode.TEMPLATE_NOT_FOUND, '模板不存在', 404); return tpl; } async update(id: number, dto: UpdateTemplateDto) { await this.get(id); return this.prisma.$transaction(async (tx) => { if (dto.fields) { await tx.templateField.deleteMany({ where: { templateId: id } }); await tx.templateField.createMany({ data: this.fieldsCreate(dto.fields).map((f) => ({ ...f, templateId: id })), }); } return tx.template.update({ where: { id }, data: { name: dto.name, remark: dto.remark, status: dto.status }, include: { fields: { orderBy: { sortOrder: 'asc' } } }, }); }); } async remove(id: number) { await this.get(id); const used = await this.prisma.artwork.count({ where: { templateId: id, deletedAt: null } }); if (used > 0) { throw new BusinessException(ErrorCode.TEMPLATE_IN_USE, '模板已被作品使用,无法删除(可改为停用)'); } await this.prisma.template.update({ where: { id }, data: { deletedAt: new Date() } }); return { success: true }; } }