'use strict'; const Service = require('egg').Service; class FastWhereService extends Service { // 构建树形结构 buildTree(data, pid = 0) { const tree = []; data.forEach(item => { if (item.pid === pid) { const children = this.buildTree(data, item.id); if (children.length > 0) { item.children = children; } tree.push(item); } }); return tree; } // 根据type获取父节点及其所有子节点(树形结构) async getTreeByType(type) { const { ctx } = this; try { // 1. 首先找到所有指定type的父节点 const parentNodes = await ctx.model.FastWhere.findAll({ where: { type }, order: [[ 'id', 'ASC' ]], }); // 2. 获取所有数据用于构建树 const allData = await ctx.model.FastWhere.findAll({ order: [[ 'id', 'ASC' ]], }); // 3. 转换所有数据为普通对象(触发getter) const plainAllData = allData.map(item => item.toJSON()); // 4. 为每个父节点构建子树 const result = []; for (const parent of parentNodes) { const parentPlain = parent.toJSON(); // 转换为普通对象 const tree = this.buildSubTree(parentPlain, plainAllData); result.push(tree); } return result; } catch (error) { ctx.logger.error('[FastWhereService.getTreeByType] error:', error); throw error; } } // 构建以某个节点为根的子树 buildSubTree(root, allData) { const tree = { ...root, children: [], }; // 递归查找子节点 const findChildren = (parentId, parentNode) => { allData.forEach(item => { if (item.pid === parentId) { const childNode = { ...item, children: [], }; parentNode.children.push(childNode); findChildren(item.id, childNode); } }); }; findChildren(root.id, tree); return tree; } // 根据type获取父节点,并包含所有子节点(扁平结构) async getWithChildrenByType(type) { const { ctx } = this; try { // 1. 首先找到所有指定type的父节点 const parentNodes = await ctx.model.FastWhere.findAll({ where: { type }, order: [[ 'id', 'ASC' ]], }); // 2. 为每个父节点获取所有子节点 const result = []; for (const parent of parentNodes) { const parentPlain = parent.toJSON(); // 转换为普通对象 const allChildren = await this.getAllChildren(parent.id); result.push({ ...parentPlain, children: allChildren, }); } return result; } catch (error) { ctx.logger.error('[FastWhereService.getWithChildrenByType] error:', error); throw error; } } // 递归获取所有子节点(包括孙子节点等) async getAllChildren(pid) { const { ctx } = this; // 获取直接子节点 const directChildren = await ctx.model.FastWhere.findAll({ where: { pid }, order: [[ 'id', 'ASC' ]], }); // 递归获取每个子节点的子节点 const childrenWithDescendants = []; for (const child of directChildren) { const childPlain = child.toJSON(); // 转换为普通对象 const grandchildren = await this.getAllChildren(child.id); childrenWithDescendants.push({ ...childPlain, children: grandchildren, }); } return childrenWithDescendants; } // 根据类型获取条件组(仅父节点) async getByType(type) { const { ctx } = this; const results = await ctx.model.FastWhere.findAll({ where: { type }, order: [[ 'id', 'ASC' ]], }); // 转换为普通对象 return results.map(item => item.toJSON()); } // 获取所有父级组 async getGroups() { const { ctx } = this; const results = await ctx.model.FastWhere.findAll({ where: { type: 'group' }, order: [[ 'id', 'ASC' ]], }); // 转换为普通对象 return results.map(item => item.toJSON()); } // 根据父级ID获取直接子条件(仅一层) async getChildrenByPid(pid) { const { ctx } = this; const results = await ctx.model.FastWhere.findAll({ where: { pid }, order: [[ 'id', 'ASC' ]], }); // 转换为普通对象 return results.map(item => item.toJSON()); } // 根据父级ID获取所有层级子条件(树形) async getTreeChildrenByPid(pid) { const { ctx } = this; try { // 获取所有数据 const allData = await ctx.model.FastWhere.findAll({ order: [[ 'id', 'ASC' ]], }); // 转换为普通对象 const plainAllData = allData.map(item => item.toJSON()); // 找到父节点 const parentNode = plainAllData.find(item => item.id === pid); if (!parentNode) { return null; } // 构建子树 return this.buildSubTree(parentNode, plainAllData); } catch (error) { ctx.logger.error('[FastWhereService.getTreeChildrenByPid] error:', error); throw error; } } // 添加快速查询条件 async create(data) { const { ctx } = this; const result = await ctx.model.FastWhere.create(data); return result.toJSON(); // 转换为普通对象 } // 更新快速查询条件 async update(id, data) { const { ctx } = this; const result = await ctx.model.FastWhere.update(data, { where: { id }, }); if (result[0] === 0) { ctx.throw(404, '记录不存在'); } const updated = await this.findById(id); return updated.toJSON(); // 转换为普通对象 } // 根据ID查找 async findById(id) { const { ctx } = this; const result = await ctx.model.FastWhere.findByPk(id); if (!result) { ctx.throw(404, '记录不存在'); } return result.toJSON(); // 转换为普通对象 } // 删除快速查询条件 async delete(id) { const { ctx } = this; // 先检查是否有子节点 const children = await ctx.model.FastWhere.count({ where: { pid: id }, }); if (children > 0) { ctx.throw(400, '请先删除子节点'); } const result = await ctx.model.FastWhere.destroy({ where: { id }, }); if (result === 0) { ctx.throw(404, '记录不存在'); } return result; } } module.exports = FastWhereService;