standard_lib.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. /**
  3. * 标准库基类(请勿使用该类创造示例)
  4. *
  5. * @author Mai
  6. * @date 2018/3/13
  7. * @version
  8. */
  9. const BaseService = require('../base/base_service');
  10. class StandardLib extends BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @param {String} tableName - 表名
  16. * @return {void}
  17. */
  18. constructor(ctx, tableName) {
  19. super(ctx);
  20. this.tableName = tableName;
  21. this.dataId = '';
  22. this.stdType = '';
  23. }
  24. // 供子类继承(不同的标准库可能需要转换一下数据)
  25. convertData (data) {
  26. }
  27. /**
  28. * 获取数据
  29. *
  30. * @param {Number} listId - 项目节列表id
  31. * @param {Number} level - 小于此层级的全部不显示
  32. * @return {Array} - 返回对应数据
  33. */
  34. async getData(listId, level = 2) {
  35. this.initSqlBuilder();
  36. this.sqlBuilder.setAndWhere('list_id', {
  37. operate: '=',
  38. value: listId,
  39. });
  40. if (level >= 0) {
  41. this.sqlBuilder.setAndWhere('level', {
  42. operate: '<=',
  43. value: level,
  44. });
  45. }
  46. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  47. const list = await this.db.query(sql, sqlParam);
  48. return list;
  49. }
  50. /**
  51. * 实例中具体的dataId使用字段不相同,统一赋值到source下
  52. * @param {Object|Array} data
  53. */
  54. setSourceData(data) {
  55. if (!data) { return; }
  56. const datas = data instanceof Array ? data : [data];
  57. for (const d of datas) {
  58. if (d) {
  59. d.source = this.stdType + '-' + d.list_id + '-' + d[this.dataId];
  60. }
  61. }
  62. }
  63. /**
  64. * 根据dataId和listId获取指定标准节点
  65. * @param {Number} listId - 标准库id
  66. * @param {Number} dataId - dataId(chapter_id|bill_id)
  67. * @returns {Promise<*>}
  68. */
  69. async getDataByDataId(listId, dataId) {
  70. this.initSqlBuilder();
  71. this.sqlBuilder.setAndWhere('list_id', {
  72. value: listId,
  73. operate: '='
  74. });
  75. this.sqlBuilder.setAndWhere(this.dataId, {
  76. value: dataId,
  77. operate: '='
  78. });
  79. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  80. const node = await this.db.queryOne(sql, sqlParam);
  81. this.setSourceData(node);
  82. this.convertData(node);
  83. return node;
  84. }
  85. /**
  86. * 根据full_path获取数据 full_path Like ‘1.2.3%’(传参full_path = '1.2.3%')
  87. * @param {Number} listId - 标准库id
  88. * @param {String} full_path - 路径
  89. * @return {Promise<void>}
  90. */
  91. async getDataByFullPath(listId, full_path) {
  92. this.initSqlBuilder();
  93. this.sqlBuilder.setAndWhere('list_id', {
  94. value: listId,
  95. operate: '=',
  96. });
  97. this.sqlBuilder.setAndWhere('full_path', {
  98. value: this.db.escape(full_path),
  99. operate: 'Like',
  100. });
  101. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  102. const resultData = await this.db.query(sql, sqlParam);
  103. this.setSourceData(resultData);
  104. this.convertData(resultData);
  105. return resultData;
  106. }
  107. /**
  108. * 根据full_path检索自己及所有父项
  109. * @param {Number} listId - 标准库id
  110. * @param {Array|String} fullPath - 节点完整路径
  111. * @returns {Promise<*>}
  112. * @private
  113. */
  114. async getFullLevelDataByFullPath(listId, fullPath) {
  115. const explodePath = this.ctx.helper.explodePath(fullPath);
  116. this.initSqlBuilder();
  117. this.sqlBuilder.setAndWhere('list_id', {
  118. value: listId,
  119. operate: '=',
  120. });
  121. this.sqlBuilder.setAndWhere('full_path', {
  122. value: explodePath,
  123. operate: 'in'
  124. });
  125. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  126. const data = await this.db.query(sql, sqlParam);
  127. this.setSourceData(data);
  128. this.convertData(data);
  129. return data;
  130. };
  131. }
  132. module.exports = StandardLib;