const config = require('../config'); const mysql = require('mysql2/promise'); // 初始化无害化登记表 async function initHarmlessRegistrationTable() { let connection = null; try { // 创建数据库连接 console.log('测试数据库连接...'); connection = await mysql.createConnection({ host: config.DB_CONFIG.host, port: config.DB_CONFIG.port, user: config.DB_CONFIG.user, password: config.DB_CONFIG.password, database: config.DB_CONFIG.database }); console.log('数据库连接成功'); // 检查表是否存在 console.log('检查无害化登记表是否存在...'); const [tables] = await connection.execute( "SHOW TABLES LIKE 'government_harmless_registrations'" ); // 如果表存在,删除它 if (tables.length > 0) { console.log('无害化登记表已存在,删除它...'); await connection.execute('DROP TABLE government_harmless_registrations'); console.log('无害化登记表删除成功'); } // 创建无害化登记表 console.log('创建无害化登记表...'); await connection.execute(` CREATE TABLE government_harmless_registrations ( id VARCHAR(36) PRIMARY KEY NOT NULL, registrationNumber VARCHAR(50) NOT NULL UNIQUE COMMENT '登记编号', animalType VARCHAR(50) NOT NULL COMMENT '动物类型', quantity INT NOT NULL COMMENT '数量', reason TEXT NOT NULL COMMENT '无害化处理原因', processingMethod VARCHAR(100) NOT NULL COMMENT '处理方式', processingPlace VARCHAR(100) NOT NULL COMMENT '处理场所', processingDate DATE NOT NULL COMMENT '处理日期', registrant VARCHAR(50) NOT NULL COMMENT '登记人', status ENUM('待处理', '处理中', '已完成', '已取消') NOT NULL DEFAULT '待处理' COMMENT '状态', createTime DATETIME NOT NULL COMMENT '创建时间', updateTime DATETIME NOT NULL COMMENT '更新时间', INDEX idx_registrationNumber (registrationNumber), INDEX idx_status (status), INDEX idx_processingDate (processingDate) ) COMMENT '无害化登记管理表' `); console.log('无害化登记表创建成功!'); // 添加测试数据 console.log('开始添加测试数据...'); const testData = [ [ '1', 'WH20240601001', '牛', 5, '疾病死亡', '焚烧处理', '银川市无害化处理中心', '2024-06-01', '张兽医', '已完成', '2024-06-01 08:30:00', '2024-06-02 14:20:00' ], [ '2', 'WH20240602002', '羊', 10, '自然灾害', '深埋处理', '中卫市无害化处理中心', '2024-06-02', '李技术员', '处理中', '2024-06-02 09:15:00', '2024-06-02 16:45:00' ], [ '3', 'WH20240603003', '猪', 8, '检疫不合格', '化制处理', '吴忠市无害化处理中心', '2024-06-03', '王检疫员', '待处理', '2024-06-03 10:00:00', '2024-06-03 10:00:00' ], [ '4', 'WH20240604004', '牛', 3, '意外死亡', '焚烧处理', '石嘴山市无害化处理中心', '2024-06-04', '赵管理员', '已取消', '2024-06-04 11:20:00', '2024-06-04 15:30:00' ], [ '5', 'WH20240605005', '羊', 12, '疫情防控', '深埋处理', '固原市无害化处理中心', '2024-06-05', '陈兽医', '已完成', '2024-06-05 09:45:00', '2024-06-06 11:15:00' ] ]; // 批量插入数据 for (const row of testData) { await connection.execute( `INSERT INTO government_harmless_registrations (id, registrationNumber, animalType, quantity, reason, processingMethod, processingPlace, processingDate, registrant, status, createTime, updateTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, row ); } console.log('测试数据添加成功!添加了', testData.length, '条数据。'); } catch (error) { console.error('初始化无害化登记表失败:', error); } finally { // 关闭数据库连接 if (connection) { await connection.close(); } } } // 执行初始化 initHarmlessRegistrationTable();