category.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/9/26
  7. * @version
  8. */
  9. const settingConst = require('../const/setting.js');
  10. module.exports = app => {
  11. class Category extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'category';
  21. }
  22. /**
  23. * 新增分类
  24. *
  25. * @param {Number} pid - 项目id
  26. * @param {String} name - 分类名称
  27. * @param {Number} type - 分类类型
  28. * @returns {Promise<{pid: *, name: *, type: *}>}
  29. */
  30. async addCategory(pid, name, type) {
  31. const count = await this.count({pid: pid, name: name});
  32. if (count > 0) {
  33. throw '存在同名类别';
  34. }
  35. const category = {
  36. pid: pid,
  37. name: name,
  38. type: type,
  39. };
  40. const result = await this.db.insert(this.tableName, category);
  41. if (result.affectedRows !== 1) {
  42. throw '提交数据失败';
  43. } else {
  44. category.id = result.insertId;
  45. }
  46. category.value = category.value && category.value !== '' ? JSON.parse(category.value) : [];
  47. return category;
  48. }
  49. /**
  50. * 大于level的分类level - 1
  51. *
  52. * @param {Number} pid - 项目id
  53. * @param {Number} level - level
  54. * @returns {Promise<*>}
  55. * @private
  56. */
  57. async _upLevelCategory(pid, level) {
  58. this.initSqlBuilder();
  59. this.sqlBuilder.setAndWhere('pid', {
  60. value: pid,
  61. operate: '='
  62. });
  63. this.sqlBuilder.setAndWhere('level', {
  64. value: level,
  65. operate: '>',
  66. });
  67. this.sqlBuilder.setUpdateData('level', {
  68. value: 1,
  69. selfOperate: '-',
  70. });
  71. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  72. const data = await this.transaction.query(sql, sqlParam);
  73. return data;
  74. }
  75. /**
  76. * 删除分类
  77. * @param {Number} pid - 项目id
  78. * @param {Number} cid - 分类id
  79. * @returns {Promise<void>}
  80. */
  81. async deleteCategory(pid, cid) {
  82. const category = await this.getDataById(cid);
  83. if (category.pid !== pid) {
  84. throw '提交数据错误';
  85. }
  86. this.transaction = await this.db.beginTransaction();
  87. try {
  88. // 调整已用分类排序
  89. if (category.level >= 1) {
  90. await this._upLevelCategory(pid, category.level);
  91. }
  92. // 删除标段分类数据
  93. const tenders = await this.ctx.service.tender.getList('', null, 1);
  94. for (const t of tenders) {
  95. if (t.category && t.category.length > 0) {
  96. this._.remove(t.category, function (c) {
  97. return c.cid === cid;
  98. });
  99. }
  100. await this.transaction.update(this.ctx.service.tender.tableName, {
  101. id: t.id,
  102. category: JSON.stringify(t.category),
  103. });
  104. }
  105. // 删除分类数据
  106. await this.transaction.delete(this.tableName, {id: cid});
  107. // 删除分类值
  108. await this.transaction.delete(this.ctx.service.categoryValue.tableName, {cid: cid});
  109. await this.transaction.commit();
  110. } catch(err) {
  111. await this.transaction.rollback();
  112. throw err;
  113. }
  114. }
  115. /**
  116. * 获取分类数据
  117. *
  118. * @param {Number} id - 分类id
  119. * @returns {Promise<*>}
  120. */
  121. async getCategory(id) {
  122. const data = await this.getDataByCondition({id: id});
  123. data.value = await this.ctx.service.categoryValue.getAllDataByCondition({ where: { cid: id }, orders: [['sort', 'asc'], ['id', 'asc']] });
  124. return data;
  125. }
  126. /**
  127. * 获取项目下全部分类数据
  128. *
  129. * @param {Number} pid - 标段id
  130. * @returns {Promise<*>}
  131. */
  132. async getAllCategory(pid) {
  133. const data = await this.getAllDataByCondition({ where: { pid } });
  134. const values = await this.ctx.service.categoryValue.getAllDataByCondition({
  135. where: { pid },
  136. orders: [['sort', 'asc'], ['id', 'asc']],
  137. });
  138. // values 按名称排序
  139. // values.sort((a, b) => a.value.localeCompare(b.value, 'zh-Hans-CN', { sensitivity: 'accent' }));
  140. for (const d of data) {
  141. d.value = values.filter(function (v) {
  142. return v.cid === d.id;
  143. });
  144. }
  145. return data;
  146. }
  147. /**
  148. * 重置分类已用排序
  149. *
  150. * @param {Array} data - 分类已用排序数据(TODO未做验证)
  151. * @returns {Promise<void>}
  152. */
  153. async resetCategoryLevel(data) {
  154. this.transaction = await this.db.beginTransaction();
  155. try {
  156. for (const d of data) {
  157. this.transaction.update(this.tableName, {id: d.id, level: d.level});
  158. }
  159. this.transaction.commit();
  160. } catch (err) {
  161. await this.transaction.rollback();
  162. throw err;
  163. }
  164. }
  165. }
  166. return Category;
  167. };