revise_bills.js 8.9 KB

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