initDb.js 5.1 KB

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