material_exponent_node.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. 'use strict';
  2. /**
  3. * 调差表(多期独立单价使用) 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const materialConst = require('../const/material');
  10. module.exports = app => {
  11. class MaterialExponentNode extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'material_exponent_node';
  21. }
  22. async updateNode(data) {
  23. if (!this.ctx.tender || !this.ctx.material) {
  24. throw '数据错误';
  25. }
  26. if (!data.baseNodes || !data.addNodes || !data.delNodes) {
  27. throw '参数错误';
  28. }
  29. // 判断t_type是否为费用,且存在quantity,m_spread值
  30. const transaction = await this.db.beginTransaction();
  31. try {
  32. if (data.remove_node && data.remove_node.length > 0 && this.ctx.material.pre_exponent_node) {
  33. // 移除pre_exponent_node值
  34. const newPreExponentNode = this.ctx.material.pre_exponent_node ? this.ctx.material.pre_exponent_node.split(',').filter(item => !data.remove_node.includes(item)).join(',') : '';
  35. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, pre_exponent_node: newPreExponentNode });
  36. this.ctx.material.pre_exponent_node = newPreExponentNode;
  37. }
  38. // 清空nodes并把总金额插回到exponent_stage或者material表里
  39. if (data.exponent_node.length === 0 && this.ctx.material.pre_exponent_node) throw '上期存在已调用分项,无法清空';
  40. const exponentList = await transaction.select(this.ctx.service.materialExponent.tableName, { where: { tid: this.ctx.tender.id } });
  41. if (data.exponent_node.length > 0) {
  42. // 之前不存在已调用分项且本次有新增分项
  43. const oldMsShardList = !this.ctx.material.exponent_node && this.ctx.material.is_stage_self ? await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id } }) : [];
  44. if (!this.ctx.material.exponent_node) {
  45. await transaction.delete(this.ctx.service.materialExponentShard.tableName, { mid: this.ctx.material.id });
  46. const insertBaseDatas = [];
  47. for (const base of data.baseNodes) {
  48. base.tid = this.ctx.tender.id;
  49. base.mid = this.ctx.material.id;
  50. base.ex_calc = JSON.stringify(base.ex_calc);
  51. insertBaseDatas.push(base);
  52. }
  53. if (insertBaseDatas.length > 0) await transaction.insert(this.tableName, insertBaseDatas);
  54. } else {
  55. const updateBaseNodes = [];
  56. const oldBaseDatas = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id, node: '-1' } });
  57. for (const base of data.baseNodes) {
  58. const old = this._.find(oldBaseDatas, { id: base.id });
  59. if (old) {
  60. old.ex_calc = JSON.stringify(base.ex_calc);
  61. old.contract_tp = base.contract_tp;
  62. old.qc_tp = base.qc_tp;
  63. old.gather_tp = base.gather_tp;
  64. updateBaseNodes.push(old);
  65. }
  66. }
  67. if (updateBaseNodes.length > 0) await transaction.updateRows(this.tableName, updateBaseNodes);
  68. }
  69. const oldDatas = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } });
  70. const insertDatas = [];
  71. for (const add of data.addNodes) {
  72. add.tid = this.ctx.tender.id;
  73. add.mid = this.ctx.material.id;
  74. add.ex_calc = JSON.stringify(add.ex_calc);
  75. insertDatas.push(add);
  76. }
  77. if (insertDatas.length > 0) {
  78. await transaction.insert(this.tableName, insertDatas);
  79. const newNodeDatas = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } });
  80. const insertExponentShardDatas = [];
  81. if (this.ctx.material.is_stage_self) {
  82. const msList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  83. for (const node of newNodeDatas) {
  84. const ms = this._.find(msList, { id: node.ms_id });
  85. for (const ex of exponentList) {
  86. const exponentShardOld = this._.find(oldMsShardList, { ms_id: ms.id, me_id: ex.id });
  87. const sourceData = !this.ctx.material.exponent_node && exponentShardOld ? exponentShardOld : {};
  88. insertExponentShardDatas.push({
  89. tid: this.ctx.tender.id,
  90. mid: this.ctx.material.id,
  91. ms_id: ms.id,
  92. mn_id: node.id,
  93. me_id: ex.id,
  94. type: ex.type,
  95. m_price: sourceData.m_price || 0,
  96. effective_price: sourceData.effective_price !== null && sourceData.effective_price !== undefined ? sourceData.effective_price : (sourceData.m_price || 0),
  97. remark: ex.remark,
  98. });
  99. }
  100. }
  101. } else {
  102. for (const node of newNodeDatas) {
  103. for (const ex of exponentList) {
  104. const sourceData = !this.ctx.material.exponent_node ? ex : {};
  105. insertExponentShardDatas.push({
  106. tid: this.ctx.tender.id,
  107. mid: this.ctx.material.id,
  108. ms_id: null,
  109. mn_id: node.id,
  110. me_id: ex.id,
  111. type: ex.type,
  112. m_price: sourceData.m_price || 0,
  113. effective_price: sourceData.effective_price !== null && sourceData.effective_price !== undefined ? sourceData.effective_price : (sourceData.m_price || 0),
  114. remark: ex.remark,
  115. });
  116. }
  117. }
  118. }
  119. if (insertExponentShardDatas.length > 0) await transaction.insert(this.ctx.service.materialExponentShard.tableName, insertExponentShardDatas);
  120. }
  121. const delDatas = [];
  122. for (const del of data.delNodes) {
  123. const olds = this._.filter(oldDatas, { node: del });
  124. for (const old of olds) {
  125. delDatas.push(old.id);
  126. }
  127. }
  128. if (delDatas.length > 0) {
  129. await transaction.delete(this.tableName, { id: delDatas });
  130. await transaction.delete(this.ctx.service.materialExponentShard.tableName, { me_id: delDatas });
  131. }
  132. // 重算所有并更新到material表、material_stage表、material_exponent_node表
  133. await this.ctx.service.materialExponentShard.calcMaterialAllExTpByNode(transaction, this.ctx.material.id);
  134. await this.ctx.service.materialExponent.calcTpByMaterialExponentNode(transaction, this.ctx.material.id);
  135. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, exponent_node: data.exponent_node.join(',') });
  136. } else {
  137. // 清空所有节点并重复赋值给期或者总材料节点
  138. await transaction.delete(this.tableName, { mid: this.ctx.material.id });
  139. await transaction.delete(this.ctx.service.materialExponentShard.tableName, { mid: this.ctx.material.id });
  140. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, exponent_node: '' });
  141. if (this.ctx.material.is_stage_self) {
  142. const msList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  143. for (const ms of msList) {
  144. const insertExponentShardDatas = [];
  145. for (const ex of exponentList) {
  146. insertExponentShardDatas.push({
  147. tid: this.ctx.tender.id,
  148. mid: this.ctx.material.id,
  149. ms_id: ms.id,
  150. mn_id: null,
  151. me_id: ex.id,
  152. type: ex.type,
  153. m_price: 0,
  154. effective_price: 0,
  155. remark: ex.remark,
  156. });
  157. }
  158. if (insertExponentShardDatas.length > 0) await transaction.insert(this.ctx.service.materialExponentShard.tableName, insertExponentShardDatas);
  159. const baseNode = this._.find(data.baseNodes, { ms_id: ms.id });
  160. if (!baseNode) throw '标段(不含新增分项)数据不存在';
  161. await this.ctx.service.materialExponentShard.calcMaterialExTp(transaction, baseNode.ex_calc, this.ctx.material.id, ms.id);
  162. }
  163. await this.ctx.service.materialExponent.calcTpByMaterialStageExponent(transaction, this.ctx.material.id);
  164. } else {
  165. const baseNode = data.baseNodes[0];
  166. if (!baseNode) throw '标段(不含新增分项)数据不存在';
  167. await this.ctx.service.materialExponent.calcMaterialExTp(transaction, baseNode.ex_calc);
  168. }
  169. }
  170. await transaction.commit();
  171. return true;
  172. } catch (err) {
  173. await transaction.rollback();
  174. throw err;
  175. }
  176. }
  177. async calcStageExTpByNode(transaction, mid, ms_id) {
  178. const mnList = await transaction.select(this.tableName, { where: { ms_id } });
  179. let stage_ex_tp = 0;
  180. let stage_ex_tax_tp = 0;
  181. for (const mn of mnList) {
  182. stage_ex_tp = this.ctx.helper.add(stage_ex_tp, mn.ex_tp ? mn.ex_tp : 0);
  183. stage_ex_tax_tp = this.ctx.helper.add(stage_ex_tax_tp, mn.ex_tax_tp ? mn.ex_tax_tp : 0);
  184. }
  185. await transaction.update(this.ctx.service.materialStage.tableName, {
  186. id: ms_id,
  187. ex_tp: stage_ex_tp,
  188. ex_tax_tp: stage_ex_tax_tp,
  189. });
  190. }
  191. async getPreNodeData(mid, tid) {
  192. const materials = await this.ctx.service.material.getAllDataByCondition({
  193. where: { tid },
  194. order: ['order'],
  195. });
  196. const preMaterial = materials.length > 1 ? materials[materials.length - 2] : null;
  197. // if (!preMaterial) throw '上期不存在';
  198. const preNodes = preMaterial ? await this.getAllDataByCondition({ where: { mid: preMaterial.id } }) : [];
  199. return preNodes;
  200. }
  201. }
  202. return MaterialExponentNode;
  203. };