standard_lib.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. this.convertData(list);
  49. return list;
  50. }
  51. /**
  52. * 实例中具体的dataId使用字段不相同,统一赋值到source下
  53. * @param {Object|Array} data
  54. */
  55. setSourceData(data) {
  56. if (!data) { return; }
  57. const datas = data instanceof Array ? data : [data];
  58. for (const d of datas) {
  59. if (d) {
  60. d.source = this.stdType + '-' + d.list_id + '-' + d[this.dataId];
  61. }
  62. }
  63. }
  64. /**
  65. * 根据dataId和listId获取指定标准节点
  66. * @param {Number} listId - 标准库id
  67. * @param {Number} dataId - dataId(chapter_id|bill_id)
  68. * @returns {Promise<*>}
  69. */
  70. async getDataByDataId(listId, dataId) {
  71. this.initSqlBuilder();
  72. this.sqlBuilder.setAndWhere('list_id', {
  73. value: listId,
  74. operate: '='
  75. });
  76. this.sqlBuilder.setAndWhere(this.dataId, {
  77. value: dataId,
  78. operate: '='
  79. });
  80. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  81. const node = await this.db.queryOne(sql, sqlParam);
  82. this.setSourceData(node);
  83. this.convertData(node);
  84. return node;
  85. }
  86. /**
  87. * 根据full_path获取数据 full_path Like ‘1.2.3%’(传参full_path = '1.2.3%')
  88. * @param {Number} listId - 标准库id
  89. * @param {String} full_path - 路径
  90. * @return {Promise<void>}
  91. */
  92. async getDataByFullPath(listId, full_path) {
  93. this.initSqlBuilder();
  94. this.sqlBuilder.setAndWhere('list_id', {
  95. value: listId,
  96. operate: '=',
  97. });
  98. this.sqlBuilder.setAndWhere('full_path', {
  99. value: this.db.escape(full_path),
  100. operate: 'Like',
  101. });
  102. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  103. const resultData = await this.db.query(sql, sqlParam);
  104. this.setSourceData(resultData);
  105. this.convertData(resultData);
  106. return resultData;
  107. }
  108. /**
  109. * 根据full_path检索自己及所有父项
  110. * @param {Number} listId - 标准库id
  111. * @param {Array|String} fullPath - 节点完整路径
  112. * @returns {Promise<*>}
  113. * @private
  114. */
  115. async getFullLevelDataByFullPath(listId, fullPath) {
  116. const explodePath = this.ctx.helper.explodePath(fullPath);
  117. this.initSqlBuilder();
  118. this.sqlBuilder.setAndWhere('list_id', {
  119. value: listId,
  120. operate: '=',
  121. });
  122. this.sqlBuilder.setAndWhere('full_path', {
  123. value: explodePath,
  124. operate: 'in'
  125. });
  126. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  127. const data = await this.db.query(sql, sqlParam);
  128. this.setSourceData(data);
  129. this.convertData(data);
  130. return data;
  131. };
  132. }
  133. module.exports = StandardLib;