2
0
فهرست منبع

加入 模板 seed 执行

yourname 5 ماه پیش
والد
کامیت
37c224dac1

+ 4 - 0
src/server/api.ts

@@ -4,6 +4,7 @@ import usersRouter from './api/users/index'
 import authRoute from './api/auth/index'
 import { AuthContext } from './types/context'
 import { AppDataSource } from './data-source'
+import { seedExcelTemplates } from './modules/excel/excel-template.seed'
 
 const api = new OpenAPIHono<AuthContext>()
 
@@ -20,6 +21,9 @@ api.use('/api/v1/*', async (c, next) => {
 api.use('/api/v1/*', async (c, next) => {
   if(!AppDataSource.isInitialized) {
     await AppDataSource.initialize();
+    
+    // 执行seed(通常在应用启动时调用)
+    seedExcelTemplates().catch(err => console.error('Seed失败:', err));
   }
   await next();
 })

+ 2 - 1
src/server/data-source.ts

@@ -5,6 +5,7 @@ import process from 'node:process'
 // 实体类导入
 import { UserEntity as User } from "./modules/users/user.entity"
 import { Role } from "./modules/users/role.entity"
+import { ExcelTemplate } from "./modules/excel/excel-template.entity"
 
 export const AppDataSource = new DataSource({
   type: "mysql",
@@ -14,7 +15,7 @@ export const AppDataSource = new DataSource({
   password: process.env.DB_PASSWORD || "",
   database: process.env.DB_DATABASE || "d8dai",
   entities: [
-    User, Role
+    User, Role, ExcelTemplate
   ],
   migrations: [],
   synchronize: process.env.DB_SYNCHRONIZE !== "false",

+ 98 - 0
src/server/modules/excel/excel-template.entity.ts

@@ -0,0 +1,98 @@
+import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
+import { z } from '@hono/zod-openapi';
+
+@Entity('excel_templates')
+export class ExcelTemplate {
+  @PrimaryGeneratedColumn({ unsigned: true })
+  id!: number;
+
+  @Column({
+    name: 'template_name',
+    type: 'varchar',
+    length: 255,
+    nullable: false,
+    comment: '模板名称'
+  })
+  templateName!: string;
+
+  @Column({
+    name: 'template_key',
+    type: 'varchar',
+    length: 50,
+    nullable: false,
+    comment: '模板唯一标识键'
+  })
+  templateKey!: string;
+
+  @Column({
+    name: 'template_config',
+    type: 'json',
+    nullable: false,
+    comment: 'Excel模板配置(存储header配置、字段映射规则等)'
+  })
+  templateConfig!: Record<string, any>;
+
+  @Column({
+    name: 'created_by',
+    type: 'int',
+    nullable: false,
+    comment: '创建人ID(关联member_auth_users.id)'
+  })
+  createdBy!: number;
+
+  @Column({
+    name: 'is_deleted',
+    type: 'tinyint',
+    default: 0,
+    comment: '是否删除 0-正常 1-删除'
+  })
+  isDeleted!: number;
+
+  @Column({
+    name: 'created_at',
+    type: 'timestamp',
+    default: () => 'CURRENT_TIMESTAMP',
+    comment: '创建时间'
+  })
+  createdAt!: Date;
+
+  @Column({
+    name: 'updated_at',
+    type: 'timestamp',
+    default: () => 'CURRENT_TIMESTAMP',
+    onUpdate: 'CURRENT_TIMESTAMP',
+    comment: '更新时间'
+  })
+  updatedAt!: Date;
+}
+
+export const ExcelTemplateSchema = z.object({
+  id: z.number().int().positive().openapi({
+    description: 'ID'
+  }),
+  templateName: z.string().max(255).openapi({
+    description: '模板名称',
+    example: '默认Excel模板'
+  }),
+  templateKey: z.string().max(50).openapi({
+    description: '模板唯一标识键',
+    example: 'default_template'
+  }),
+  templateConfig: z.record(z.any()).openapi({
+    description: 'Excel模板配置'
+  }),
+  createdBy: z.number().int().openapi({
+    description: '创建人ID',
+    example: 1
+  }),
+  isDeleted: z.number().int().min(0).max(1).default(0).openapi({
+    description: '是否删除 0-正常 1-删除',
+    example: 0
+  }),
+  createdAt: z.date().openapi({
+    description: '创建时间'
+  }),
+  updatedAt: z.date().openapi({
+    description: '更新时间'
+  })
+});

+ 54 - 0
src/server/modules/excel/excel-template.seed.ts

@@ -0,0 +1,54 @@
+import { AppDataSource } from '@/server/data-source';
+import { ExcelTemplate } from './excel-template.entity';
+
+export async function seedExcelTemplates() {
+  const templateRepository = AppDataSource.getRepository(ExcelTemplate);
+
+  await AppDataSource.transaction(async (transactionalEntityManager) => {
+    // 检查是否已有数据
+    const count = await transactionalEntityManager.count(ExcelTemplate);
+    if (count > 0) {
+      console.log('Excel模板数据已存在,跳过seed');
+      return;
+    }
+
+    // 插入默认模板数据
+    const templates = [
+      {
+        templateName: '默认Excel模板',
+        templateKey: 'default_template',
+        templateConfig: {
+          sheets: [
+            {
+              headerRowIndex: 5,
+              orderNumberRow: 2,
+              orderNumberCol: 3,
+              dataStartRow: 6,
+              endMarker: "订单统计数据:",
+              sheetName: "五金清单",
+              exportFields: ["订单号", "序号", "名称", "颜色", "长度", "宽度", "数量", "单位", "工艺说明"],
+              fieldMappings: {
+                "序号": "index",
+                "名称": "name",
+                "颜色": "color",
+                "长度": "length",
+                "宽度": "width",
+                "数量": "quantity",
+                "单位": "unit",
+                "工艺说明": "processDescription",
+                "订单号": "orderNumber"
+              },
+              requiredFields: ["名称", "数量"]
+            }
+          ],
+          activeSheetIndex: 0
+        },
+        createdBy: 1
+      },
+      // 可以添加更多模板数据...
+    ];
+
+    await transactionalEntityManager.save(ExcelTemplate, templates);
+    console.log(`成功插入${templates.length}条Excel模板数据`);
+  });
+}