standard_lib.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. /**
  3. * 标准库基类(请勿使用该类创造示例)
  4. *
  5. * @author Mai
  6. * @date 2018/3/13
  7. * @version
  8. */
  9. const BaseTreeService = require('../base/base_tree_service');
  10. class StandardLib extends BaseTreeService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @param {String} tableName - 表名
  16. * @return {void}
  17. */
  18. constructor(ctx, setting, tableName) {
  19. super(ctx, setting);
  20. this.tableName = tableName;
  21. this.stdType = '';
  22. }
  23. /**
  24. * 实例中具体的dataId使用字段不相同,统一赋值到source下
  25. * @param {Object|Array} data
  26. */
  27. setSourceData(data) {
  28. if (!data) { return; }
  29. const datas = data instanceof Array ? data : [data];
  30. for (const d of datas) {
  31. if (d) {
  32. d.source = this.stdType + '-' + d[this.setting.mid] + '-' + d[this.setting.kid];
  33. }
  34. }
  35. }
  36. /**
  37. * 根据dataId和listId获取指定标准节点
  38. * @param {Number} listId - 标准库id
  39. * @param {Number} dataId - dataId(chapter_id|bill_id)
  40. * @returns {Promise<*>}
  41. */
  42. async getDataByDataId(listId, dataId) {
  43. this.initSqlBuilder();
  44. this.sqlBuilder.setAndWhere(this.setting.mid, {
  45. value: listId,
  46. operate: '='
  47. });
  48. this.sqlBuilder.setAndWhere(this.setting.kid, {
  49. value: dataId,
  50. operate: '='
  51. });
  52. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  53. const node = await this.db.queryOne(sql, sqlParam);
  54. this.setSourceData(node);
  55. return node;
  56. }
  57. /**
  58. * 根据full_path获取数据 full_path Like ‘1.2.3%’(传参full_path = '1.2.3%')
  59. * @param {Number} listId - 标准库id
  60. * @param {String} full_path - 路径
  61. * @return {Promise<void>}
  62. */
  63. async getDataByFullPath(listId, full_path) {
  64. this.initSqlBuilder();
  65. this.sqlBuilder.setAndWhere(this.setting.mid, {
  66. value: listId,
  67. operate: '=',
  68. });
  69. this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  70. value: this.db.escape(full_path),
  71. operate: 'Like',
  72. });
  73. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  74. const resultData = await this.db.query(sql, sqlParam);
  75. this.setSourceData(resultData);
  76. return resultData;
  77. }
  78. /**
  79. * 根据full_path检索自己及所有父项
  80. * @param {Number} listId - 标准库id
  81. * @param {Array|String} fullPath - 节点完整路径
  82. * @returns {Promise<*>}
  83. * @private
  84. */
  85. async getFullLevelDataByFullPath(listId, fullPath) {
  86. const explodePath = this.ctx.helper.explodePath(fullPath);
  87. this.initSqlBuilder();
  88. this.sqlBuilder.setAndWhere(this.setting.mid, {
  89. value: listId,
  90. operate: '=',
  91. });
  92. this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  93. value: explodePath,
  94. operate: 'in'
  95. });
  96. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  97. const data = await this.db.query(sql, sqlParam);
  98. this.setSourceData(data);
  99. return data;
  100. };
  101. }
  102. module.exports = StandardLib;