initDb.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const { pool } = require('./database');
  2. const bcrypt = require('bcryptjs');
  3. const initDatabase = async () => {
  4. try {
  5. // 创建数据库
  6. await pool.query('CREATE DATABASE IF NOT EXISTS notebot');
  7. await pool.query('USE notebot');
  8. // 创建用户表
  9. await pool.query(`
  10. CREATE TABLE IF NOT EXISTS users (
  11. id INT AUTO_INCREMENT PRIMARY KEY,
  12. username VARCHAR(50) NOT NULL UNIQUE,
  13. password VARCHAR(255) NOT NULL,
  14. role ENUM('admin', 'user') DEFAULT 'user',
  15. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  16. )
  17. `);
  18. // 创建群组表
  19. await pool.query(`
  20. CREATE TABLE IF NOT EXISTS groups (
  21. id INT AUTO_INCREMENT PRIMARY KEY,
  22. group_id VARCHAR(50) NOT NULL UNIQUE,
  23. group_name VARCHAR(100) NOT NULL,
  24. group_type ENUM('personal', 'public', 'private') NOT NULL DEFAULT 'public' COMMENT '群组类型:个人用户/公开群聊/隐私群聊',
  25. is_active BOOLEAN DEFAULT TRUE,
  26. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  27. )
  28. `);
  29. // 修改群组表,添加新字段
  30. try {
  31. await pool.query(`
  32. ALTER TABLE groups
  33. ADD COLUMN group_type ENUM('personal', 'public', 'private') NOT NULL DEFAULT 'public' COMMENT '群组类型:个人用户/公开群聊/隐私群聊' AFTER group_name,
  34. ADD COLUMN fee_rate DECIMAL(5,2) DEFAULT 0.00 COMMENT '费率' AFTER group_type,
  35. ADD COLUMN exchange_rate DECIMAL(10,4) DEFAULT 1.0000 COMMENT '汇率' AFTER fee_rate,
  36. ADD COLUMN creator_id INT NOT NULL COMMENT '创建人ID' AFTER exchange_rate,
  37. ADD COLUMN admin_id INT NOT NULL COMMENT '管理员ID' AFTER creator_id
  38. `);
  39. } catch (error) {
  40. // 如果列已存在,忽略错误
  41. if (!error.message.includes('Duplicate column name')) {
  42. throw error;
  43. }
  44. }
  45. // 添加外键约束
  46. // try {
  47. // await pool.query(`
  48. // ALTER TABLE groups
  49. // ADD CONSTRAINT fk_groups_creator
  50. // FOREIGN KEY (creator_id) REFERENCES users(id),
  51. // ADD CONSTRAINT fk_groups_admin
  52. // FOREIGN KEY (admin_id) REFERENCES users(id)
  53. // `);
  54. // } catch (error) {
  55. // // 如果约束已存在,忽略错误
  56. // if (!error.message.includes('Duplicate key name')) {
  57. // throw error;
  58. // }
  59. // }
  60. // 创建入款出款记录表
  61. await pool.query(`
  62. CREATE TABLE IF NOT EXISTS money_records (
  63. id INT AUTO_INCREMENT PRIMARY KEY,
  64. time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  65. amount DECIMAL(10,2) NOT NULL COMMENT '金额',
  66. operator_id INT NOT NULL COMMENT '操作人ID',
  67. responder_id INT NOT NULL COMMENT '回复人ID',
  68. group_id VARCHAR(50) NOT NULL COMMENT '关联群组ID',
  69. type ENUM('deposit', 'withdrawal') NOT NULL COMMENT '类型:入款/出款',
  70. status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending' COMMENT '状态',
  71. FOREIGN KEY (operator_id) REFERENCES users(id),
  72. FOREIGN KEY (responder_id) REFERENCES users(id),
  73. FOREIGN KEY (group_id) REFERENCES groups(group_id),
  74. INDEX idx_group_time (group_id, time)
  75. )
  76. `);
  77. // 创建交易表
  78. await pool.query(`
  79. CREATE TABLE IF NOT EXISTS transactions (
  80. id INT AUTO_INCREMENT PRIMARY KEY,
  81. group_id VARCHAR(50) NOT NULL,
  82. group_name VARCHAR(100) NOT NULL,
  83. type ENUM('deposit', 'withdrawal') NOT NULL,
  84. amount DECIMAL(10,2) NOT NULL,
  85. time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  86. INDEX idx_group_time (group_id, time)
  87. )
  88. `);
  89. // 创建默认管理员账户
  90. const adminPassword = await bcrypt.hash('admin123', 10);
  91. await pool.query(`
  92. INSERT INTO users (username, password, role)
  93. VALUES ('admin', ?, 'admin')
  94. ON DUPLICATE KEY UPDATE id=id
  95. `, [adminPassword]);
  96. console.log('数据库初始化成功');
  97. } catch (error) {
  98. console.error('数据库初始化失败:', error);
  99. throw error;
  100. }
  101. };
  102. module.exports = initDatabase;