123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 'use strict';
- /**
- * 标准库基类(请勿使用该类创造示例)
- *
- * @author Mai
- * @date 2018/3/13
- * @version
- */
- const BaseTreeService = require('../base/base_tree_service');
- class StandardLib extends BaseTreeService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @param {String} tableName - 表名
- * @return {void}
- */
- constructor(ctx, setting, tableName) {
- super(ctx, setting);
- this.tableName = tableName;
- this.stdType = '';
- }
- /**
- * 实例中具体的dataId使用字段不相同,统一赋值到source下
- * @param {Object|Array} data
- */
- setSourceData(data) {
- if (!data) { return; }
- const datas = data instanceof Array ? data : [data];
- for (const d of datas) {
- if (d) {
- d.source = this.stdType + '-' + d[this.setting.mid] + '-' + d[this.setting.kid];
- }
- }
- }
- /**
- * 根据dataId和listId获取指定标准节点
- * @param {Number} listId - 标准库id
- * @param {Number} dataId - dataId(chapter_id|bill_id)
- * @returns {Promise<*>}
- */
- async getDataByDataId(listId, dataId) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere(this.setting.mid, {
- value: listId,
- operate: '='
- });
- this.sqlBuilder.setAndWhere(this.setting.kid, {
- value: dataId,
- operate: '='
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const node = await this.db.queryOne(sql, sqlParam);
- this.setSourceData(node);
- return node;
- }
- /**
- * 根据full_path获取数据 full_path Like ‘1.2.3%’(传参full_path = '1.2.3%')
- * @param {Number} listId - 标准库id
- * @param {String} full_path - 路径
- * @return {Promise<void>}
- */
- async getDataByFullPath(listId, full_path) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere(this.setting.mid, {
- value: listId,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere(this.setting.fullPath, {
- value: this.db.escape(full_path),
- operate: 'Like',
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const resultData = await this.db.query(sql, sqlParam);
- this.setSourceData(resultData);
- return resultData;
- }
- /**
- * 根据full_path检索自己及所有父项
- * @param {Number} listId - 标准库id
- * @param {Array|String} fullPath - 节点完整路径
- * @returns {Promise<*>}
- * @private
- */
- async getFullLevelDataByFullPath(listId, fullPath) {
- const explodePath = this.ctx.helper.explodePath(fullPath);
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere(this.setting.mid, {
- value: listId,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere(this.setting.fullPath, {
- value: explodePath,
- operate: 'in'
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const data = await this.db.query(sql, sqlParam);
- this.setSourceData(data);
- return data;
- };
- }
- module.exports = StandardLib;
|