const { safeOrderBy } = require('../utils/sqlSafety'); const { getDb } = require('../config/database'); const Content = require('./Content'); class Service extends Content { constructor(data = {}) { super(data); this.content_type = 'service'; this.service_id = data.service_id || this.content_id; this.service_type = data.service_type || null; this.pricing_model = data.pricing_model || null; this.delivery_method = data.delivery_method || null; } static create(data) { const db = getDb(); // 先创建Content记录 const contentData = { ...data, content_type: 'service' }; const content = super.create(contentData); // 再创建Service扩展记录 const stmt = db.prepare(` INSERT INTO services (service_id, service_type, pricing_model, delivery_method) VALUES (?, ?, ?, ?) `); stmt.run( content.content_id, data.service_type || null, data.pricing_model || null, data.delivery_method || null ); return this.findById(content.content_id); } static findById(contentId) { const db = getDb(); const stmt = db.prepare(` SELECT c.*, s.service_id, s.service_type, s.pricing_model, s.delivery_method FROM contents c LEFT JOIN services s ON c.content_id = s.service_id WHERE c.content_id = ? `); const row = stmt.get(contentId); if (row && row.metadata) { row.metadata = JSON.parse(row.metadata); } return row; } static update(contentId, data) { const db = getDb(); // 更新Content部分 const contentData = { ...data }; delete contentData.service_id; delete contentData.service_type; delete contentData.pricing_model; delete contentData.delivery_method; super.update(contentId, contentData); // 更新Service扩展部分 const serviceData = {}; if (data.service_type !== undefined) serviceData.service_type = data.service_type; if (data.pricing_model !== undefined) serviceData.pricing_model = data.pricing_model; if (data.delivery_method !== undefined) serviceData.delivery_method = data.delivery_method; if (Object.keys(serviceData).length > 0) { const setClause = Object.keys(serviceData).map(key => `${key} = ?`).join(', '); const values = Object.values(serviceData); const stmt = db.prepare(`UPDATE services SET ${setClause} WHERE service_id = ?`); stmt.run(...values, contentId); } return this.findById(contentId); } static delete(contentId) { const db = getDb(); // 删除Service扩展记录 const stmt1 = db.prepare('DELETE FROM services WHERE service_id = ?'); stmt1.run(contentId); // 删除Content记录 return super.delete(contentId); } static findAll(options = {}) { const db = getDb(); let query = ` SELECT c.*, s.service_id, s.service_type, s.pricing_model, s.delivery_method FROM contents c LEFT JOIN services s ON c.content_id = s.service_id WHERE c.content_type = 'service' `; const params = []; if (options.where) { const conditions = Object.keys(options.where).map(key => { if (key === 'service_type' || key === 'pricing_model' || key === 'delivery_method') { return `s.${key} = ?`; } return `c.${key} = ?`; }); query += ` AND ${conditions.join(' AND ')}`; params.push(...Object.values(options.where)); } if (safeOrderBy(options.orderBy)) { query += ` ORDER BY ${safeOrderBy(options.orderBy)}`; } if (options.limit) { query += ` LIMIT ?`; params.push(options.limit); } if (options.offset) { query += ` OFFSET ?`; params.push(options.offset); } const stmt = db.prepare(query); const rows = stmt.all(...params); return rows.map(row => { if (row.metadata) { row.metadata = JSON.parse(row.metadata); } return row; }); } } module.exports = Service;