sub_proj_progress.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const readOnlyFields = ['id', 'spid', 'tree_id', 'tree_pid', 'tree_order', 'tree_level', 'tree_full_path', 'tree_is_leaf'];
  10. const defaultData = [
  11. { code: '1', name: '前期阶段', tree_id: 1, tree_pid: -1, tree_level: 1, tree_order: 1, tree_full_path: '1', tree_is_leaf: 1 },
  12. { code: '2', name: '实施阶段', tree_id: 2, tree_pid: -1, tree_level: 1, tree_order: 2, tree_full_path: '2', tree_is_leaf: 1 },
  13. { code: '3', name: '竣(交)工阶段', tree_id: 3, tree_pid: -1, tree_level: 1, tree_order: 3, tree_full_path: '3', tree_is_leaf: 1 },
  14. ];
  15. module.exports = app => {
  16. class SubProjProgress extends app.BaseTreeService {
  17. /**
  18. * 构造函数
  19. *
  20. * @param {Object} ctx - egg全局变量
  21. * @param {String} tableName - 表名
  22. * @return {void}
  23. */
  24. constructor(ctx) {
  25. super(ctx, {
  26. mid: 'spid',
  27. kid: 'tree_id',
  28. pid: 'tree_pid',
  29. order: 'tree_order',
  30. level: 'tree_level',
  31. isLeaf: 'tree_is_leaf',
  32. fullPath: 'tree_full_path',
  33. cacheKey: false,
  34. uuid: true,
  35. });
  36. this.tableName = 'sub_project_progress';
  37. }
  38. _getDefaultData(data, spid) {
  39. data.id = this.uuid.v4();
  40. data.spid = spid;
  41. data.add_user_id = this.ctx.session.sessionUser.accountId;
  42. data.update_user_id = this.ctx.session.sessionUser.accountId;
  43. }
  44. async init(subProj) {
  45. if (!subProj) throw '阶段进度数据错误';
  46. const insertData = [];
  47. for (const b of defaultData) {
  48. const bills = JSON.parse(JSON.stringify(b));
  49. this._getDefaultData(bills, subProj.id);
  50. insertData.push(bills);
  51. }
  52. const operate = await this.db.insert(this.tableName, insertData);
  53. return operate.affectedRows === insertData.length;
  54. }
  55. async getData(subProj) {
  56. let result = await this.getAllDataByCondition({ where: { spid: subProj.id } });
  57. if (result.length === 0) {
  58. await this.init(subProj);
  59. result = await this.getAllDataByCondition({ where: { spid: subProj.id } });
  60. }
  61. return result;
  62. }
  63. async addChild(spid, select, count) {
  64. const maxId = await this._getMaxLid(spid);
  65. const children = await this.getChildrenByParentId(spid, select[this.setting.kid]);
  66. const newDatas = [];
  67. for (let i = 1; i < count + 1; i++) {
  68. const newData = {};
  69. newData[this.setting.kid] = maxId + i;
  70. newData[this.setting.pid] = select[this.setting.kid];
  71. newData[this.setting.level] = select[this.setting.level] + 1;
  72. newData[this.setting.order] = children.length + i;
  73. newData[this.setting.fullPath] = select[this.setting.fullPath] + '-' + newData[this.setting.kid];
  74. newData[this.setting.isLeaf] = true;
  75. this._getDefaultData(newData, spid);
  76. newDatas.push(newData);
  77. }
  78. this.transaction = await this.db.beginTransaction();
  79. try {
  80. const result = await this.transaction.insert(this.tableName, newDatas);
  81. if (children.length === 0) await this.transaction.update(this.tableName, { id: select.id, tree_is_leaf: 0 });
  82. await this.transaction.commit();
  83. } catch(err) {
  84. this.transaction.rollback();
  85. throw err;
  86. }
  87. // 查询应返回的结果
  88. const resultData = {};
  89. resultData.create = await this.getNextsData(spid, select[this.setting.kid], children.length);
  90. if (children.length === 0) resultData.update = await this.getDataByKid(spid, select[this.setting.kid]);
  91. return resultData;
  92. }
  93. async addNextSibling(spid, select, count) {
  94. const maxId = await this._getMaxLid(spid);
  95. const newDatas = [];
  96. for (let i = 1; i < count + 1; i++) {
  97. const newData = {};
  98. newData[this.setting.kid] = maxId + i;
  99. newData[this.setting.pid] = select ? select[this.setting.pid] : this.rootId;
  100. newData[this.setting.level] = select ? select[this.setting.level] : 1;
  101. newData[this.setting.order] = select ? select[this.setting.order] + i : i;
  102. newData[this.setting.fullPath] = newData[this.setting.level] > 1
  103. ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid])
  104. : newData[this.setting.kid] + '';
  105. newData[this.setting.isLeaf] = true;
  106. this._getDefaultData(newData, spid);
  107. newDatas.push(newData);
  108. }
  109. this.transaction = await this.db.beginTransaction();
  110. try {
  111. if (select) await this._updateChildrenOrder(spid, select[this.setting.pid], select[this.setting.order] + 1, count);
  112. const insertResult = await this.transaction.insert(this.tableName, newDatas);
  113. if (insertResult.affectedRows !== count) throw '新增节点数据错误';
  114. await this.transaction.commit();
  115. } catch (err) {
  116. await this.transaction.rollback();
  117. this.transaction = null;
  118. throw err;
  119. }
  120. if (select) {
  121. const createData = await this.getChildBetween(spid, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1);
  122. const updateData = await this.getNextsData(spid, select[this.setting.pid], select[this.setting.order] + count);
  123. return {create: createData, update: updateData};
  124. } else {
  125. const createData = await this.getChildBetween(spid, -1, 0, count + 1);
  126. return {create: createData};
  127. }
  128. }
  129. async addProgressNode(spid, targetId, count) {
  130. if (!spid) return null;
  131. const select = targetId ? await this.getDataByKid(spid, targetId) : null;
  132. if (targetId && !select) throw '新增节点数据错误';
  133. if (select[this.setting.level] === 1) {
  134. return await this.addChild(spid, select, count);
  135. } else {
  136. return await this.addNextSibling(spid, select, count);
  137. }
  138. }
  139. async _deleteRelaData(spid, deleteData) {
  140. if (!this.transaction) throw '删除相关数据错误';
  141. await this.transaction.update(this.ctx.service.subProjFile.tableName, {is_deleted: 1},
  142. { where: { spid, type: 'progress', rela_id: deleteData.map(x => { return x.id; })} });
  143. }
  144. async updateInfos(spid, data) {
  145. if (!spid) throw '数据错误';
  146. const datas = Array.isArray(data) ? data : [data];
  147. const orgDatas = await this.getAllDataByCondition({ where: { id: datas.map(x => { return x.id; })} });
  148. const updateDatas = [];
  149. for (const d of datas) {
  150. const node = orgDatas.find(x => { return x.id === d.id; });
  151. if (!node || node.spid !== spid) throw '提交数据错误';
  152. d.update_user_id = this.ctx.session.sessionUser.accountId;
  153. updateDatas.push(this._filterUpdateInvalidField(node.id, d));
  154. }
  155. await this.db.updateRows(this.tableName, updateDatas);
  156. const resultData = await this.getDataById(this._.map(datas, 'id'));
  157. return { update: resultData };
  158. }
  159. }
  160. return SubProjProgress;
  161. }