revise_bills.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class ReviseBills extends app.BaseTreeService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx, {
  19. mid: 'tender_id',
  20. kid: 'ledger_id',
  21. pid: 'ledger_pid',
  22. order: 'order',
  23. level: 'level',
  24. isLeaf: 'is_leaf',
  25. fullPath: 'full_path',
  26. keyPre: 'revise_bills_maxLid:'
  27. });
  28. this.tableName = 'revise_bills';
  29. }
  30. /**
  31. * 新增节点
  32. * @param {Number} tid - 台账id
  33. * @param {uuid} rid - 修订id
  34. * @param {Number} lid - 清单节点id
  35. * @returns {Promise<void>}
  36. */
  37. async addReviseNode(tid, rid, lid) {
  38. if (!rid) return null;
  39. return await this.addNode(tid, lid, {crid: rid});
  40. }
  41. /**
  42. * 批量插入子项
  43. * @param {uuid} rid - 修订id
  44. * @param {Number} lid - 节点id
  45. * @param {Object} data - 批量插入数据
  46. * @return {Promise<void>}
  47. */
  48. async batchInsertChild(tid, rid, lid, data) {
  49. const result = { ledger: {}, pos: null };
  50. if (!tid || !rid || !lid) return result;
  51. const select = await this.getDataByLid(tid, lid);
  52. if (!select) {
  53. throw '位置数据错误';
  54. }
  55. // 计算id和order
  56. const maxId = await this._getMaxLid(tid);
  57. const lastChild = await this.getLastChildData(tid, lid);
  58. const order = lastChild ? lastChild.order : 0;
  59. // 整理数据
  60. const bills = [], pos = [], newIds = [];
  61. for (let i = 0, iLen = data.length; i < iLen; i++) {
  62. // 合并新增数据
  63. const qd = {
  64. crid: rid,
  65. id: this.uuid.v4(),
  66. tender_id: tid,
  67. ledger_id: maxId + i + 1,
  68. ledger_pid: select.ledger_id,
  69. is_leaf: true,
  70. order: order + i + 1,
  71. level: select.level + 1,
  72. b_code: data[i].b_code,
  73. name: data[i].name,
  74. unit: data[i].unit,
  75. unit_price: data[i].price,
  76. };
  77. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  78. qd.sgfh_qty = 0;
  79. for (const p of data[i].pos) {
  80. const inP = {
  81. id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
  82. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  83. name: p.name, drawing_code: p.drawing_code,
  84. };
  85. if (p.quantity) {
  86. inP.sgfh_qty = this.round(p.quantity, precision.value);
  87. inP.quantity = inP.sgfh_qty;
  88. }
  89. qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
  90. pos.push(inP);
  91. }
  92. qd.full_path = select.full_path + '-' + qd.ledger_id;
  93. qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
  94. qd.quantity = qd.sgfh_qty;
  95. qd.total_price = qd.sgfh_tp;
  96. bills.push(qd);
  97. newIds.push(qd.id);
  98. }
  99. this.transaction = await this.db.beginTransaction();
  100. try {
  101. // 更新父项isLeaf
  102. if (!lastChild) {
  103. await this.transaction.update(this.tableName, {
  104. is_leaf: false,
  105. unit_price: null,
  106. quantity: null,
  107. total_price: null,
  108. deal_qty: null,
  109. deal_tp: null,
  110. }, {tender_id: rid, id: select.id});
  111. }
  112. // 数据库创建新增节点数据
  113. await this.transaction.insert(this.tableName, bills);
  114. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  115. this._cacheMaxLid(tid, maxId + data.length);
  116. await this.transaction.commit();
  117. } catch (err) {
  118. await this.transaction.rollback();
  119. throw err;
  120. }
  121. // 查询应返回的结果
  122. result.ledger.create = await this.getDataById(newIds);
  123. if (!lastChild) {
  124. result.ledger.update = await this.getDataByLid(select.id);
  125. }
  126. result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
  127. return result;
  128. }
  129. /**
  130. * 批量插入后项
  131. * @param {Number} tid - 台账id
  132. * @param {uuid} rid - 修订id
  133. * @param {Number} lid - 节点id
  134. * @param {Object} data - 批量插入数据
  135. * @return {Promise<void>}
  136. */
  137. async batchInsertNext(tid, rid, lid, data) {
  138. const result = { ledger: {}, pos: null };
  139. if (!tid || !rid || !lid) return result;
  140. const select = await this.getDataByLid(tid, lid);
  141. if (!select) {
  142. throw '位置数据错误';
  143. }
  144. const parentData = await this.getDataByLid(tid, select.ledger_pid);
  145. if (!parentData) {
  146. throw '位置数据错误';
  147. }
  148. // 计算id和order
  149. const maxId = await this._getMaxLid(tid);
  150. const order = select.order;
  151. // 整理数据
  152. const bills = [], pos = [], newIds = [];
  153. for (let i = 0, iLen = data.length; i < iLen; i++) {
  154. // 合并新增数据
  155. const qd = {
  156. crid: rid,
  157. id: this.uuid.v4(),
  158. tender_id: tid,
  159. ledger_id: maxId + i + 1,
  160. ledger_pid: parentData.ledger_id,
  161. is_leaf: true,
  162. order: order + i + 1,
  163. level: parentData.level + 1,
  164. b_code: data[i].b_code,
  165. name: data[i].name,
  166. unit: data[i].unit,
  167. unit_price: data[i].price,
  168. };
  169. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  170. qd.sgfh_qty = 0;
  171. for (const p of data[i].pos) {
  172. const inP = {
  173. id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
  174. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  175. name: p.name, drawing_code: p.drawing_code,
  176. };
  177. if (p.quantity) {
  178. inP.sgfh_qty = this.round(p.quantity, precision.value);
  179. inP.quantity = inP.sgfh_qty;
  180. }
  181. qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
  182. pos.push(inP);
  183. }
  184. qd.full_path = parentData.full_path + '-' + qd.ledger_id;
  185. qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
  186. qd.quantity = qd.sgfh_qty;
  187. qd.total_price = qd.sgfh_tp;
  188. bills.push(qd);
  189. newIds.push(qd.id);
  190. }
  191. this.transaction = await this.db.beginTransaction();
  192. try {
  193. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  194. await this._updateChildrenOrder(tid, select.ledger_pid, select.order+1);
  195. // 数据库创建新增节点数据
  196. await this.transaction.insert(this.tableName, bills);
  197. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  198. this._cacheMaxLid(tid, maxId + data.length);
  199. await this.transaction.commit();
  200. } catch (err) {
  201. await this.transaction.rollback();
  202. throw err;
  203. }
  204. // 查询应返回的结果
  205. result.ledger.create = await this.getDataById(newIds);
  206. result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length);
  207. result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
  208. return result;
  209. }
  210. }
  211. return ReviseBills;
  212. };