revise_bills.js 11 KB

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