standard_lib.js 3.8 KB

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