revise_bills.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.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, count) {
  42. if (!rid) return null;
  43. return await this.addNodeBatch(tid, lid, {crid: rid}, count);
  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.getDataByKid(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, porder: p.porder,
  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. id: select.id,
  109. is_leaf: false,
  110. unit_price: null,
  111. quantity: null,
  112. total_price: null,
  113. deal_qty: null,
  114. deal_tp: null,
  115. });
  116. }
  117. // 数据库创建新增节点数据
  118. await this.transaction.insert(this.tableName, bills);
  119. if (pos.length > 0) {
  120. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  121. }
  122. this._cacheMaxLid(tid, maxId + data.length);
  123. await this.transaction.commit();
  124. } catch (err) {
  125. await this.transaction.rollback();
  126. throw err;
  127. }
  128. // 查询应返回的结果
  129. result.ledger.create = await this.getDataById(newIds);
  130. if (!lastChild) {
  131. result.ledger.update = await this.getDataById(select.id);
  132. }
  133. result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
  134. return result;
  135. }
  136. /**
  137. * 批量插入后项
  138. * @param {Number} tid - 台账id
  139. * @param {uuid} rid - 修订id
  140. * @param {Number} lid - 节点id
  141. * @param {Object} data - 批量插入数据
  142. * @return {Promise<void>}
  143. */
  144. async batchInsertNext(tid, rid, lid, data) {
  145. const result = { ledger: {}, pos: null };
  146. if (!tid || !rid || !lid) return result;
  147. const select = await this.getDataByKid(tid, lid);
  148. if (!select) {
  149. throw '位置数据错误';
  150. }
  151. const parentData = await this.getDataByKid(tid, select.ledger_pid);
  152. if (!parentData) {
  153. throw '位置数据错误';
  154. }
  155. // 计算id和order
  156. const maxId = await this._getMaxLid(tid);
  157. const order = select.order;
  158. // 整理数据
  159. const bills = [], pos = [], newIds = [];
  160. for (let i = 0, iLen = data.length; i < iLen; i++) {
  161. // 合并新增数据
  162. const qd = {
  163. crid: rid,
  164. id: this.uuid.v4(),
  165. tender_id: tid,
  166. ledger_id: maxId + i + 1,
  167. ledger_pid: parentData.ledger_id,
  168. is_leaf: true,
  169. order: order + i + 1,
  170. level: parentData.level + 1,
  171. b_code: data[i].b_code,
  172. name: data[i].name,
  173. unit: data[i].unit,
  174. unit_price: data[i].price,
  175. };
  176. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  177. qd.sgfh_qty = 0;
  178. for (const p of data[i].pos) {
  179. const inP = {
  180. id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
  181. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  182. name: p.name, drawing_code: p.drawing_code, porder: p.porder,
  183. };
  184. if (p.quantity) {
  185. inP.sgfh_qty = this.round(p.quantity, precision.value);
  186. inP.quantity = inP.sgfh_qty;
  187. }
  188. qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
  189. pos.push(inP);
  190. }
  191. qd.full_path = parentData.full_path + '-' + qd.ledger_id;
  192. qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
  193. qd.quantity = qd.sgfh_qty;
  194. qd.total_price = qd.sgfh_tp;
  195. bills.push(qd);
  196. newIds.push(qd.id);
  197. }
  198. this.transaction = await this.db.beginTransaction();
  199. try {
  200. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  201. await this._updateChildrenOrder(tid, select.ledger_pid, select.order+1);
  202. // 数据库创建新增节点数据
  203. await this.transaction.insert(this.tableName, bills);
  204. if (pos.length > 0) {
  205. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  206. }
  207. this._cacheMaxLid(tid, maxId + data.length);
  208. await this.transaction.commit();
  209. } catch (err) {
  210. await this.transaction.rollback();
  211. throw err;
  212. }
  213. // 查询应返回的结果
  214. result.ledger.create = await this.getDataById(newIds);
  215. result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length);
  216. result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
  217. return result;
  218. }
  219. /**
  220. *
  221. * @param node
  222. * @param transaction
  223. * @returns {Promise<void>}
  224. * @private
  225. */
  226. async _calcNode(node, transaction) {
  227. const info = this.ctx.tender.info;
  228. const precision = this.ctx.helper.findPrecision(info.precision, node.unit);
  229. 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` = ?';
  230. const data = await transaction.queryOne(calcQtySql, [this.ctx.service.revisePos.tableName, node.id]);
  231. data.id = node.id;
  232. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  233. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  234. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  235. data.quantity = this.round(data.quantity, precision.value);
  236. data.sgfh_tp = this.ctx.helper.mul(data.sgfh_qty, node.unit_price, info.decimal.tp);
  237. data.sjcl_tp = this.ctx.helper.mul(data.sjcl_qty, node.unit_price, info.decimal.tp);
  238. data.qtcl_tp = this.ctx.helper.mul(data.qtcl_qty, node.unit_price, info.decimal.tp);
  239. data.total_price = this.ctx.helper.mul(data.quantity, node.unit_price, info.decimal.tp);
  240. const result = await transaction.update(this.tableName, data);
  241. }
  242. /**
  243. *
  244. * @param {Number} tid - 标段id
  245. * @param {Number} id - 需要计算的节点的id
  246. * @param {Object} transaction - 操作所属事务,没有则创建
  247. * @return {Promise<void>}
  248. */
  249. async calc(tid, id, transaction) {
  250. const node = await transaction.get(this.tableName, {id: id});
  251. if (!node) {
  252. throw '数据错误';
  253. }
  254. await this._calcNode(node, transaction);
  255. }
  256. }
  257. return ReviseBills;
  258. };