initDb.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const fs = require('fs');
  2. const path = require('path');
  3. const {
  4. pool
  5. } = require('./database');
  6. const bcrypt = require('bcryptjs');
  7. const User = require('../models/User');
  8. async function initDatabase() {
  9. try {
  10. // 读取并执行迁移文件
  11. const migrationsDir = path.join(__dirname, 'migrations');
  12. const migrationFiles = fs.readdirSync(migrationsDir).sort();
  13. for (const file of migrationFiles) {
  14. if (file.endsWith('.sql')) {
  15. const migrationPath = path.join(migrationsDir, file);
  16. const migrationSQL = fs.readFileSync(migrationPath, 'utf8');
  17. console.log(`执行数据库迁移: ${file}`);
  18. await pool.query(migrationSQL);
  19. }
  20. }
  21. // 创建数据库
  22. await pool.query('CREATE DATABASE IF NOT EXISTS notebot');
  23. await pool.query('USE notebot');
  24. // 创建用户表
  25. await pool.query(`
  26. CREATE TABLE IF NOT EXISTS users (
  27. id INT AUTO_INCREMENT PRIMARY KEY,
  28. username VARCHAR(50) NOT NULL UNIQUE,
  29. password VARCHAR(255) NOT NULL,
  30. role ENUM('admin', 'user') DEFAULT 'user',
  31. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  32. )
  33. `);
  34. // 创建群组表
  35. await pool.query(`
  36. CREATE TABLE IF NOT EXISTS groups (
  37. id INT AUTO_INCREMENT PRIMARY KEY,
  38. group_id VARCHAR(255) NOT NULL UNIQUE,
  39. group_name VARCHAR(255) NOT NULL,
  40. group_type ENUM('personal', 'group', 'supergroup') NOT NULL DEFAULT 'group' COMMENT '群组类型:个人用户/私聊群组/超级群组',
  41. in_fee_rate DECIMAL(5,2) DEFAULT 0.00 COMMENT '入款费率',
  42. in_exchange_rate DECIMAL(10,4) DEFAULT 1.0000 COMMENT '入款汇率',
  43. out_fee_rate DECIMAL(5,2) DEFAULT 0.00 COMMENT '出款费率',
  44. out_exchange_rate DECIMAL(10,4) DEFAULT 1.0000 COMMENT '出款汇率',
  45. admin_id INT NOT NULL COMMENT '管理员ID',
  46. creator_id VARCHAR(255) NOT NULL,
  47. is_active BOOLEAN DEFAULT true,
  48. last_join_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '机器人最后加入时间',
  49. last_leave_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '机器人最后离开时间',
  50. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  51. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间'
  52. )
  53. `);
  54. // 创建入款出款记录表
  55. await pool.query(`
  56. CREATE TABLE IF NOT EXISTS transactions (
  57. id INT AUTO_INCREMENT PRIMARY KEY,
  58. time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  59. amount DECIMAL(10,2) NOT NULL COMMENT '金额',
  60. operator_id INT NOT NULL COMMENT '操作人ID',
  61. responder_id INT NOT NULL COMMENT '回复人ID',
  62. group_id VARCHAR(50) NOT NULL COMMENT '关联群组ID',
  63. type ENUM('deposit', 'withdrawal') NOT NULL COMMENT '类型:入款/出款',
  64. status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending' COMMENT '状态',
  65. fee_rate DECIMAL(5,2) DEFAULT NULL COMMENT '费率',
  66. exchange_rate DECIMAL(10,4) DEFAULT NULL COMMENT '汇率',
  67. FOREIGN KEY (operator_id) REFERENCES users(id),
  68. FOREIGN KEY (responder_id) REFERENCES users(id),
  69. FOREIGN KEY (group_id) REFERENCES groups(group_id),
  70. INDEX idx_group_time (group_id, time)
  71. )
  72. `);
  73. // 检查是否存在管理员用户
  74. const adminUser = await User.findByUsername('admin');
  75. if (!adminUser) {
  76. console.log('创建默认管理员用户...');
  77. // 创建默认管理员用户
  78. const adminData = {
  79. username: 'admin',
  80. password: 'admin123', // 默认密码
  81. role: 'admin'
  82. };
  83. await User.create(adminData);
  84. console.log('默认管理员用户创建成功');
  85. } else {
  86. console.log('管理员用户已存在');
  87. }
  88. console.log('数据库初始化完成');
  89. } catch (error) {
  90. console.error('数据库初始化失败:', error);
  91. throw error;
  92. }
  93. }
  94. module.exports = initDatabase;