"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserModel = void 0; const bcryptjs_1 = __importDefault(require("bcryptjs")); const database_1 = require("../config/database"); class UserModel { static async create(params) { try { const { username, password, role = 'user', email } = params; const salt = await bcryptjs_1.default.genSalt(10); const hashedPassword = await bcryptjs_1.default.hash(password, salt); const emailValue = email || null; const uuidResult = await (0, database_1.executeQuery)('SELECT UUID() as uuid'); const id = uuidResult[0].uuid; const query = ` INSERT INTO users (id, username, password, email, role, created_at, updated_at) VALUES (?, ?, ?, ?, ?, NOW(), NOW()) `; await (0, database_1.executeQuery)(query, [id, username, hashedPassword, emailValue, role]); const user = await this.getById(id); if (!user) { throw new Error('创建用户失败,无法获取创建的用户信息'); } return user; } catch (error) { console.error('创建用户失败:', error); throw error; } } static async getById(id) { try { const query = ` SELECT id, username, password, email, role, created_at, updated_at FROM users WHERE id = ? `; const result = await (0, database_1.executeQuery)(query, [id]); if (result.length === 0) { return null; } return result[0]; } catch (error) { console.error('根据ID获取用户失败:', error); throw error; } } static async getByUsername(username) { try { const query = ` SELECT id, username, password, email, role, created_at, updated_at FROM users WHERE username = ? `; const result = await (0, database_1.executeQuery)(query, [username]); if (result.length === 0) { return null; } return result[0]; } catch (error) { console.error('根据用户名获取用户失败:', error); throw error; } } static async update(id, updates) { try { const updateFields = []; const params = []; if (updates.username) { updateFields.push('username = ?'); params.push(updates.username); } if (updates.role) { updateFields.push('role = ?'); params.push(updates.role); } if (updates.email) { updateFields.push('email = ?'); params.push(updates.email); } updateFields.push('updated_at = NOW()'); if (updateFields.length === 1) { return await this.getById(id); } params.push(id); const query = ` UPDATE users SET ${updateFields.join(', ')} WHERE id = ? `; await (0, database_1.executeQuery)(query, params); return await this.getById(id); } catch (error) { console.error('更新用户失败:', error); throw error; } } static async updatePassword(id, newPassword) { try { const salt = await bcryptjs_1.default.genSalt(10); const hashedPassword = await bcryptjs_1.default.hash(newPassword, salt); const query = ` UPDATE users SET password = ?, updated_at = NOW() WHERE id = ? `; await (0, database_1.executeQuery)(query, [hashedPassword, id]); } catch (error) { console.error('更新用户密码失败:', error); throw error; } } static async delete(id) { try { const query = 'DELETE FROM users WHERE id = ?'; const result = await (0, database_1.executeQuery)(query, [id]); return result.affectedRows > 0; } catch (error) { console.error('删除用户失败:', error); throw error; } } static async getAll(limit, offset) { try { let query = ` SELECT id, username, password, email, role, created_at, updated_at FROM users ORDER BY created_at DESC `; const params = []; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } const result = await (0, database_1.executeQuery)(query, params); return result; } catch (error) { console.error('获取所有用户失败:', error); throw error; } } } exports.UserModel = UserModel; //# sourceMappingURL=user.js.map