sub_proj_progress.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 budgetStd = subProj.std_id ? await this.ctx.service.budgetStd.getDataById(subProj.std_id) : null;
  47. const template_id = budgetStd ? budgetStd.progress_template_id.split(',')[0] : '';
  48. const insertData = [];
  49. if (template_id) {
  50. const templateData = await this.ctx.service.tenderNodeTemplate.getData(template_id);
  51. for (const tmp of templateData) {
  52. const bills = {
  53. code: tmp.code || '',
  54. name: tmp.name || '',
  55. tree_id: tmp.template_id,
  56. tree_pid: tmp.pid,
  57. tree_level: tmp.level,
  58. tree_order: tmp.order,
  59. tree_full_path: tmp.full_path,
  60. tree_is_leaf: tmp.is_leaf,
  61. };
  62. this._getDefaultData(bills, subProj.id);
  63. insertData.push(bills);
  64. }
  65. } else {
  66. for (const b of defaultData) {
  67. const bills = JSON.parse(JSON.stringify(b));
  68. this._getDefaultData(bills, subProj.id);
  69. insertData.push(bills);
  70. }
  71. }
  72. const operate = await this.db.insert(this.tableName, insertData);
  73. return operate.affectedRows === insertData.length;
  74. }
  75. async getData(subProj) {
  76. let result = await this.getAllDataByCondition({ where: { spid: subProj.id } });
  77. if (result.length === 0) {
  78. await this.init(subProj);
  79. result = await this.getAllDataByCondition({ where: { spid: subProj.id } });
  80. }
  81. return result;
  82. }
  83. async addChild(spid, select, count) {
  84. const maxId = await this._getMaxLid(spid);
  85. const children = await this.getChildrenByParentId(spid, select[this.setting.kid]);
  86. const newDatas = [];
  87. for (let i = 1; i < count + 1; i++) {
  88. const newData = {};
  89. newData[this.setting.kid] = maxId + i;
  90. newData[this.setting.pid] = select[this.setting.kid];
  91. newData[this.setting.level] = select[this.setting.level] + 1;
  92. newData[this.setting.order] = children.length + i;
  93. newData[this.setting.fullPath] = select[this.setting.fullPath] + '-' + newData[this.setting.kid];
  94. newData[this.setting.isLeaf] = true;
  95. this._getDefaultData(newData, spid);
  96. newDatas.push(newData);
  97. }
  98. this.transaction = await this.db.beginTransaction();
  99. try {
  100. const result = await this.transaction.insert(this.tableName, newDatas);
  101. if (children.length === 0) await this.transaction.update(this.tableName, { id: select.id, tree_is_leaf: 0 });
  102. await this.transaction.commit();
  103. } catch(err) {
  104. this.transaction.rollback();
  105. throw err;
  106. }
  107. // 查询应返回的结果
  108. const resultData = {};
  109. resultData.create = await this.getNextsData(spid, select[this.setting.kid], children.length);
  110. if (children.length === 0) resultData.update = await this.getDataByKid(spid, select[this.setting.kid]);
  111. return resultData;
  112. }
  113. async addNextSibling(spid, select, count) {
  114. const maxId = await this._getMaxLid(spid);
  115. const newDatas = [];
  116. for (let i = 1; i < count + 1; i++) {
  117. const newData = {};
  118. newData[this.setting.kid] = maxId + i;
  119. newData[this.setting.pid] = select ? select[this.setting.pid] : this.rootId;
  120. newData[this.setting.level] = select ? select[this.setting.level] : 1;
  121. newData[this.setting.order] = select ? select[this.setting.order] + i : i;
  122. newData[this.setting.fullPath] = newData[this.setting.level] > 1
  123. ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid])
  124. : newData[this.setting.kid] + '';
  125. newData[this.setting.isLeaf] = true;
  126. this._getDefaultData(newData, spid);
  127. newDatas.push(newData);
  128. }
  129. this.transaction = await this.db.beginTransaction();
  130. try {
  131. if (select) await this._updateChildrenOrder(spid, select[this.setting.pid], select[this.setting.order] + 1, count);
  132. const insertResult = await this.transaction.insert(this.tableName, newDatas);
  133. if (insertResult.affectedRows !== count) throw '新增节点数据错误';
  134. await this.transaction.commit();
  135. } catch (err) {
  136. await this.transaction.rollback();
  137. this.transaction = null;
  138. throw err;
  139. }
  140. if (select) {
  141. const createData = await this.getChildBetween(spid, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1);
  142. const updateData = await this.getNextsData(spid, select[this.setting.pid], select[this.setting.order] + count);
  143. return {create: createData, update: updateData};
  144. } else {
  145. const createData = await this.getChildBetween(spid, -1, 0, count + 1);
  146. return {create: createData};
  147. }
  148. }
  149. async addProgressNode(spid, targetId, count) {
  150. if (!spid) return null;
  151. const select = targetId ? await this.getDataByKid(spid, targetId) : null;
  152. if (targetId && !select) throw '新增节点数据错误';
  153. if (select[this.setting.level] === 1) {
  154. return await this.addChild(spid, select, count);
  155. } else {
  156. return await this.addNextSibling(spid, select, count);
  157. }
  158. }
  159. async _deleteRelaData(spid, deleteData) {
  160. if (!this.transaction) throw '删除相关数据错误';
  161. await this.transaction.update(this.ctx.service.subProjFile.tableName, {is_deleted: 1},
  162. { where: { spid, type: 'progress', rela_id: deleteData.map(x => { return x.id; })} });
  163. }
  164. async updateInfos(spid, data) {
  165. if (!spid) throw '数据错误';
  166. const datas = Array.isArray(data) ? data : [data];
  167. const orgDatas = await this.getAllDataByCondition({ where: { id: datas.map(x => { return x.id; })} });
  168. const updateDatas = [];
  169. for (const d of datas) {
  170. const node = orgDatas.find(x => { return x.id === d.id; });
  171. if (!node || node.spid !== spid) throw '提交数据错误';
  172. d.update_user_id = this.ctx.session.sessionUser.accountId;
  173. updateDatas.push(this._filterUpdateInvalidField(node.id, d));
  174. }
  175. await this.db.updateRows(this.tableName, updateDatas);
  176. const resultData = await this.getDataById(this._.map(datas, 'id'));
  177. return { update: resultData };
  178. }
  179. }
  180. return SubProjProgress;
  181. }