const EntityContentJoin = require('../models/EntityContentJoin'); const Entity = require('../models/Entity'); const Content = require('../models/Content'); const Joi = require('joi'); class EntityContentJoinService { // 验证关联数据 static validateJoin(data, isUpdate = false) { const schema = Joi.object({ entity_id: isUpdate ? Joi.string().uuid() : Joi.string().uuid().required(), content_id: isUpdate ? Joi.string().uuid() : Joi.string().uuid().required(), join_note: Joi.string().allow(null, '') }); return schema.validate(data, { abortEarly: false }); } // 创建关联 static create(data) { const { error, value } = this.validateJoin(data); if (error) { throw new Error(`验证失败: ${error.details.map(d => d.message).join(', ')}`); } // 检查实体是否存在 const entity = Entity.findById(value.entity_id); if (!entity) { throw new Error('实体不存在'); } // 检查内容是否存在 const content = Content.findById(value.content_id); if (!content) { throw new Error('内容不存在'); } // 检查是否已存在相同的关联 const existingJoins = EntityContentJoin.findByEntityId(value.entity_id); const duplicateJoin = existingJoins.find(join => join.content_id === value.content_id); if (duplicateJoin) { throw new Error('该实体与内容的关联已存在'); } return EntityContentJoin.create(value); } // 获取关联详情 static getById(joinId) { const join = EntityContentJoin.findById(joinId); if (!join) { throw new Error('关联不存在'); } return join; } // 获取实体的所有关联 static getByEntityId(entityId) { return EntityContentJoin.findByEntityId(entityId); } // 获取内容的所有关联 static getByContentId(contentId) { return EntityContentJoin.findByContentId(contentId); } // 获取所有关联 static getList(options = {}) { return EntityContentJoin.findAll(options); } // 更新关联 static update(joinId, data) { // 检查关联是否存在 const existingJoin = EntityContentJoin.findById(joinId); if (!existingJoin) { throw new Error('关联不存在'); } // 如果要修改实体或内容,检查新实体或内容是否存在 if (data.entity_id && data.entity_id !== existingJoin.entity_id) { const entity = Entity.findById(data.entity_id); if (!entity) { throw new Error('实体不存在'); } } if (data.content_id && data.content_id !== existingJoin.content_id) { const content = Content.findById(data.content_id); if (!content) { throw new Error('内容不存在'); } } const { error, value } = this.validateJoin(data, true); if (error) { throw new Error(`验证失败: ${error.details.map(d => d.message).join(', ')}`); } return EntityContentJoin.update(joinId, value); } // 删除关联 static delete(joinId) { const join = EntityContentJoin.findById(joinId); if (!join) { throw new Error('关联不存在'); } return EntityContentJoin.delete(joinId); } // 删除实体与内容的关联 static deleteByEntityAndContent(entityId, contentId) { return EntityContentJoin.deleteByEntityAndContent(entityId, contentId); } // 获取关联总数 static count() { return EntityContentJoin.count(); } // 分页查询 static paginate(page = 1, pageSize = 10, options = {}) { const offset = (page - 1) * pageSize; const list = EntityContentJoin.findAll({ ...options, limit: pageSize, offset }); const total = this.count(); return { list, total, page, pageSize, totalPages: Math.ceil(total / pageSize) }; } } module.exports = EntityContentJoinService;