revise_bills.js 12 KB

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