123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- const { pool } = require('../config/database');
- // 创建群组表
- const createGroupTable = async () => {
- try {
- await pool.query(`
- CREATE TABLE IF NOT EXISTS groups (
- id INT AUTO_INCREMENT PRIMARY KEY,
- group_id VARCHAR(50) NOT NULL UNIQUE,
- group_name VARCHAR(100) NOT NULL,
- creator_id VARCHAR(50) NOT NULL,
- is_active BOOLEAN DEFAULT TRUE,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
- )
- `);
- } catch (error) {
- console.error('创建群组表失败:', error);
- }
- };
- // 初始化表
- createGroupTable();
- // 群组相关方法
- const Group = {
- // 获取所有群组
- findAll: async () => {
- const [rows] = await pool.query(
- 'SELECT * FROM groups ORDER BY created_at DESC'
- );
- return rows;
- },
- // 根据ID查找群组
- findById: async (id) => {
- const [rows] = await pool.query(
- 'SELECT * FROM groups WHERE id = ?',
- [id]
- );
- return rows[0];
- },
- // 根据群组ID查找群组
- findByGroupId: async (groupId) => {
- const [rows] = await pool.query(
- 'SELECT * FROM groups WHERE group_id = ?',
- [groupId]
- );
- return rows[0];
- },
- // 创建群组
- create: async (groupData) => {
- const [result] = await pool.query(
- 'INSERT INTO groups (group_id, group_name, creator_id) VALUES (?, ?, ?)',
- [groupData.groupId, groupData.groupName, groupData.creatorId]
- );
- return result.insertId;
- },
- // 更新群组
- update: async (id, groupData) => {
- await pool.query(
- 'UPDATE groups SET group_name = ?, is_active = ? WHERE id = ?',
- [groupData.groupName, groupData.isActive, id]
- );
- },
- // 删除群组
- delete: async (id) => {
- await pool.query('DELETE FROM groups WHERE id = ?', [id]);
- }
- };
- module.exports = Group;
|