revise_bills.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. }, 'revisePos');
  29. this.depart = 10;
  30. this.tableName = 'revise_bills';
  31. }
  32. async _deleteRelaData (mid, deleteData) {
  33. await this.ctx.service.revisePos.deletePosData(this.transaction, mid, this._.map(deleteData, 'id'));
  34. }
  35. /**
  36. * 新增节点
  37. * @param {Number} tid - 台账id
  38. * @param {uuid} rid - 修订id
  39. * @param {Number} lid - 清单节点id
  40. * @returns {Promise<void>}
  41. */
  42. async addReviseNode(tid, rid, lid, count) {
  43. if (!rid) return null;
  44. return await this.addNodeBatch(tid, lid, {crid: rid}, count);
  45. }
  46. /**
  47. * 批量插入子项
  48. * @param {uuid} rid - 修订id
  49. * @param {Number} lid - 节点id
  50. * @param {Object} data - 批量插入数据
  51. * @return {Promise<void>}
  52. */
  53. async batchInsertChild(tid, rid, lid, data) {
  54. const result = { ledger: {}, pos: null };
  55. if (!tid || !rid || !lid) return result;
  56. const select = await this.getDataByKid(tid, lid);
  57. if (!select) {
  58. throw '位置数据错误';
  59. }
  60. // 计算id和order
  61. const maxId = await this._getMaxLid(tid);
  62. const lastChild = await this.getLastChildData(tid, lid);
  63. const order = lastChild ? lastChild.order : 0;
  64. // 整理数据
  65. const bills = [], pos = [], newIds = [];
  66. for (let i = 0, iLen = data.length; i < iLen; i++) {
  67. // 合并新增数据
  68. const qd = {
  69. crid: rid,
  70. id: this.uuid.v4(),
  71. tender_id: tid,
  72. ledger_id: maxId + i + 1,
  73. ledger_pid: select.ledger_id,
  74. is_leaf: true,
  75. order: order + i + 1,
  76. level: select.level + 1,
  77. b_code: data[i].b_code,
  78. name: data[i].name,
  79. unit: data[i].unit,
  80. unit_price: data[i].price,
  81. };
  82. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  83. qd.sgfh_qty = 0;
  84. for (const p of data[i].pos) {
  85. const inP = {
  86. id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
  87. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  88. name: p.name, drawing_code: p.drawing_code, porder: p.porder,
  89. };
  90. if (p.quantity) {
  91. inP.sgfh_qty = this.round(p.quantity, precision.value);
  92. inP.quantity = inP.sgfh_qty;
  93. }
  94. qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
  95. pos.push(inP);
  96. }
  97. qd.full_path = select.full_path + '-' + qd.ledger_id;
  98. qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
  99. qd.quantity = qd.sgfh_qty;
  100. qd.total_price = qd.sgfh_tp;
  101. bills.push(qd);
  102. newIds.push(qd.id);
  103. }
  104. this.transaction = await this.db.beginTransaction();
  105. try {
  106. // 更新父项isLeaf
  107. if (!lastChild) {
  108. await this.transaction.update(this.tableName, {
  109. id: select.id,
  110. is_leaf: false,
  111. unit_price: null,
  112. quantity: null,
  113. total_price: null,
  114. deal_qty: null,
  115. deal_tp: null,
  116. });
  117. }
  118. // 数据库创建新增节点数据
  119. await this.transaction.insert(this.tableName, bills);
  120. if (pos.length > 0) {
  121. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  122. }
  123. this._cacheMaxLid(tid, maxId + data.length);
  124. await this.transaction.commit();
  125. } catch (err) {
  126. await this.transaction.rollback();
  127. throw err;
  128. }
  129. // 查询应返回的结果
  130. result.ledger.create = await this.getDataById(newIds);
  131. if (!lastChild) {
  132. result.ledger.update = await this.getDataById(select.id);
  133. }
  134. result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
  135. return result;
  136. }
  137. /**
  138. * 批量插入后项
  139. * @param {Number} tid - 台账id
  140. * @param {uuid} rid - 修订id
  141. * @param {Number} lid - 节点id
  142. * @param {Object} data - 批量插入数据
  143. * @return {Promise<void>}
  144. */
  145. async batchInsertNext(tid, rid, lid, data) {
  146. const result = { ledger: {}, pos: null };
  147. if (!tid || !rid || !lid) return result;
  148. const select = await this.getDataByKid(tid, lid);
  149. if (!select) {
  150. throw '位置数据错误';
  151. }
  152. const parentData = await this.getDataByKid(tid, select.ledger_pid);
  153. if (!parentData) {
  154. throw '位置数据错误';
  155. }
  156. // 计算id和order
  157. const maxId = await this._getMaxLid(tid);
  158. const order = select.order;
  159. // 整理数据
  160. const bills = [], pos = [], newIds = [];
  161. for (let i = 0, iLen = data.length; i < iLen; i++) {
  162. // 合并新增数据
  163. const qd = {
  164. crid: rid,
  165. id: this.uuid.v4(),
  166. tender_id: tid,
  167. ledger_id: maxId + i + 1,
  168. ledger_pid: parentData.ledger_id,
  169. is_leaf: true,
  170. order: order + i + 1,
  171. level: parentData.level + 1,
  172. b_code: data[i].b_code,
  173. name: data[i].name,
  174. unit: data[i].unit,
  175. unit_price: data[i].price,
  176. };
  177. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  178. qd.sgfh_qty = 0;
  179. for (const p of data[i].pos) {
  180. const inP = {
  181. id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
  182. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  183. name: p.name, drawing_code: p.drawing_code, porder: p.porder,
  184. };
  185. if (p.quantity) {
  186. inP.sgfh_qty = this.round(p.quantity, precision.value);
  187. inP.quantity = inP.sgfh_qty;
  188. }
  189. qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
  190. pos.push(inP);
  191. }
  192. qd.full_path = parentData.full_path + '-' + qd.ledger_id;
  193. qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
  194. qd.quantity = qd.sgfh_qty;
  195. qd.total_price = qd.sgfh_tp;
  196. bills.push(qd);
  197. newIds.push(qd.id);
  198. }
  199. this.transaction = await this.db.beginTransaction();
  200. try {
  201. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  202. await this._updateChildrenOrder(tid, select.ledger_pid, select.order+1);
  203. // 数据库创建新增节点数据
  204. await this.transaction.insert(this.tableName, bills);
  205. if (pos.length > 0) {
  206. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  207. }
  208. this._cacheMaxLid(tid, maxId + data.length);
  209. await this.transaction.commit();
  210. } catch (err) {
  211. await this.transaction.rollback();
  212. throw err;
  213. }
  214. // 查询应返回的结果
  215. result.ledger.create = await this.getDataById(newIds);
  216. result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length);
  217. result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
  218. return result;
  219. }
  220. /**
  221. *
  222. * @param node
  223. * @param transaction
  224. * @returns {Promise<void>}
  225. * @private
  226. */
  227. async _calcNode(node, transaction) {
  228. const info = this.ctx.tender.info;
  229. const precision = this.ctx.helper.findPrecision(info.precision, node.unit);
  230. const calcQtySql = 'SELECT SUM(`sgfh_qty`) As `sgfh_qty`, SUM(`sjcl_qty`) As `sjcl_qty`, SUM(`qtcl_qty`) As `qtcl_qty`, SUM(`quantity`) As `quantity` FROM ?? WHERE `lid` = ?';
  231. const data = await transaction.queryOne(calcQtySql, [this.ctx.service.revisePos.tableName, node.id]);
  232. data.id = node.id;
  233. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  234. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  235. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  236. data.quantity = this.round(data.quantity, precision.value);
  237. data.sgfh_tp = this.ctx.helper.mul(data.sgfh_qty, node.unit_price, info.decimal.tp);
  238. data.sjcl_tp = this.ctx.helper.mul(data.sjcl_qty, node.unit_price, info.decimal.tp);
  239. data.qtcl_tp = this.ctx.helper.mul(data.qtcl_qty, node.unit_price, info.decimal.tp);
  240. data.total_price = this.ctx.helper.mul(data.quantity, node.unit_price, info.decimal.tp);
  241. const result = await transaction.update(this.tableName, data);
  242. }
  243. /**
  244. *
  245. * @param {Number} tid - 标段id
  246. * @param {Number} id - 需要计算的节点的id
  247. * @param {Object} transaction - 操作所属事务,没有则创建
  248. * @return {Promise<void>}
  249. */
  250. async calc(tid, id, transaction) {
  251. const node = await transaction.get(this.tableName, {id: id});
  252. if (!node) {
  253. throw '数据错误';
  254. }
  255. await this._calcNode(node, transaction);
  256. }
  257. }
  258. return ReviseBills;
  259. };