category.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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} subProject - 子项目
  26. * @param {String} name - 分类名称
  27. * @param {Number} type - 分类类型
  28. * @returns {Promise<{pid: *, name: *, type: *}>}
  29. */
  30. async addCategory(subProject, name, type) {
  31. const count = await this.count({spid: subProject.id, name: name});
  32. if (count > 0) {
  33. throw '存在同名类别';
  34. }
  35. const category = {
  36. pid: subProject.project_id,
  37. spid: subProject.id,
  38. name: name,
  39. type: type,
  40. };
  41. const result = await this.db.insert(this.tableName, category);
  42. if (result.affectedRows !== 1) {
  43. throw '提交数据失败';
  44. } else {
  45. category.id = result.insertId;
  46. }
  47. category.value = category.value && category.value !== '' ? JSON.parse(category.value) : [];
  48. return category;
  49. }
  50. /**
  51. * 大于level的分类level - 1
  52. *
  53. * @param {Number} pid - 项目id
  54. * @param {Number} level - level
  55. * @returns {Promise<*>}
  56. * @private
  57. */
  58. async _upLevelCategory(spid, level) {
  59. this.initSqlBuilder();
  60. this.sqlBuilder.setAndWhere('spid', {
  61. value: this.db.escape(spid),
  62. operate: '='
  63. });
  64. this.sqlBuilder.setAndWhere('level', {
  65. value: level,
  66. operate: '>',
  67. });
  68. this.sqlBuilder.setUpdateData('level', {
  69. value: 1,
  70. selfOperate: '-',
  71. });
  72. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  73. const data = await this.transaction.query(sql, sqlParam);
  74. return data;
  75. }
  76. /**
  77. * 删除分类
  78. * @param {Number} subProject - 子项目
  79. * @param {Number} cid - 分类id
  80. * @returns {Promise<void>}
  81. */
  82. async deleteCategory(subProject, cid) {
  83. const category = await this.getDataById(cid);
  84. if (category.spid !== subProject.id) {
  85. throw '提交数据错误';
  86. }
  87. this.transaction = await this.db.beginTransaction();
  88. try {
  89. // 调整已用分类排序
  90. if (category.level >= 1) {
  91. await this._upLevelCategory(subProject.spid, category.level);
  92. }
  93. // 删除标段分类数据
  94. const tenders = await this.ctx.service.tender.getList('', null, 1);
  95. for (const t of tenders) {
  96. if (t.category && t.category.length > 0) {
  97. this._.remove(t.category, function (c) {
  98. return c.cid === cid;
  99. });
  100. }
  101. await this.transaction.update(this.ctx.service.tender.tableName, {
  102. id: t.id,
  103. category: JSON.stringify(t.category),
  104. });
  105. }
  106. // 删除分类数据
  107. await this.transaction.delete(this.tableName, {id: cid});
  108. // 删除分类值
  109. await this.transaction.delete(this.ctx.service.categoryValue.tableName, {cid: cid});
  110. await this.transaction.commit();
  111. } catch(err) {
  112. await this.transaction.rollback();
  113. throw err;
  114. }
  115. }
  116. /**
  117. * 获取分类数据
  118. *
  119. * @param {Number} id - 分类id
  120. * @returns {Promise<*>}
  121. */
  122. async getCategory(id) {
  123. const data = await this.getDataByCondition({id: id});
  124. data.value = await this.ctx.service.categoryValue.getAllDataByCondition({ where: { cid: id }, orders: [['sort', 'asc'], ['id', 'asc']] });
  125. return data;
  126. }
  127. /**
  128. * 获取项目下全部分类数据
  129. *
  130. * @param {Number} subProject - 子项目
  131. * @returns {Promise<*>}
  132. */
  133. async getAllCategory(subProject) {
  134. const data = await this.getAllDataByCondition({ where: { spid: subProject.id } });
  135. const values = await this.ctx.service.categoryValue.getAllDataByCondition({
  136. where: { spid: subProject.id },
  137. orders: [['sort', 'asc'], ['id', 'asc']],
  138. });
  139. // values 按名称排序
  140. // values.sort((a, b) => a.value.localeCompare(b.value, 'zh-Hans-CN', { sensitivity: 'accent' }));
  141. for (const d of data) {
  142. d.value = values.filter(function (v) {
  143. return v.cid === d.id;
  144. });
  145. }
  146. return data;
  147. }
  148. /**
  149. * 获取项目下全部分类数据
  150. *
  151. * @param {Number} subProject - 子项目
  152. * @returns {Promise<*>}
  153. */
  154. async getOrgAllCategory(pid) {
  155. const data = await this.getAllDataByCondition({ where: { pid, spid: '' } });
  156. const values = await this.ctx.service.categoryValue.getAllDataByCondition({
  157. where: { pid, spid: '' },
  158. orders: [['sort', 'asc'], ['id', 'asc']],
  159. });
  160. // values 按名称排序
  161. // values.sort((a, b) => a.value.localeCompare(b.value, 'zh-Hans-CN', { sensitivity: 'accent' }));
  162. for (const d of data) {
  163. d.value = values.filter(function (v) {
  164. return v.cid === d.id;
  165. });
  166. }
  167. return data;
  168. }
  169. /**
  170. * 重置分类已用排序
  171. *
  172. * @param {Array} data - 分类已用排序数据(TODO未做验证)
  173. * @returns {Promise<void>}
  174. */
  175. async resetCategoryLevel(data) {
  176. this.transaction = await this.db.beginTransaction();
  177. try {
  178. for (const d of data) {
  179. this.transaction.update(this.tableName, {id: d.id, level: d.level});
  180. }
  181. await this.transaction.commit();
  182. } catch (err) {
  183. await this.transaction.rollback();
  184. throw err;
  185. }
  186. }
  187. /**
  188. * 获取项目下全部分类数据
  189. *
  190. * @param {Number} pid - 标段id
  191. * @returns {Promise<*>}
  192. */
  193. async getListByCategoryLevel(spid) {
  194. let data = await this.getAllDataByCondition({ where: { spid }, orders: [['level', 'asc']] });
  195. data = this._.filter(data, function(item) {
  196. return item.level !== 0;
  197. });
  198. const values = await this.ctx.service.categoryValue.getAllDataByCondition({
  199. where: { spid },
  200. orders: [['sort', 'asc'], ['id', 'asc']],
  201. });
  202. // values 按名称排序
  203. // values.sort((a, b) => a.value.localeCompare(b.value, 'zh-Hans-CN', { sensitivity: 'accent' }));
  204. for (const d of data) {
  205. d.value = values.filter(function (v) {
  206. return v.cid === d.id;
  207. });
  208. }
  209. return data;
  210. }
  211. }
  212. return Category;
  213. };