stage_bills.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. 'use strict';
  2. /**
  3. * 期计量 - 部位明细计量
  4. *
  5. * @author Mai
  6. * @date 2018/12/8
  7. * @version
  8. */
  9. const calcFields = ['contract_qty', 'qc_qty'];
  10. const auditConst = require('../const/audit');
  11. const timesLen = auditConst.stage.timesLen;
  12. const SumLoad = require('../lib/sum_load');
  13. module.exports = app => {
  14. class StageBills extends app.BaseService {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局变量
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.depart = 10;
  24. this.tableName = 'stage_bills';
  25. }
  26. /**
  27. * 查询期计量最后审核人数据
  28. * @param {Number} tid - 标段id
  29. * @param {Number} sid - 期id
  30. * @param {Number|Array} lid - 台账节点id(可以为空)
  31. * @return {Promise<*>}
  32. */
  33. async getLastestStageData(tid, sid, lid) {
  34. let lidSql = '',
  35. result;
  36. if (lid) {
  37. if (lid instanceof Array) {
  38. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  39. } else {
  40. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  41. }
  42. }
  43. const sql = 'SELECT Bills.* FROM ' + this.departTableName(tid) + ' As Bills ' +
  44. ' INNER JOIN ( ' +
  45. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.departTableName(tid) +
  46. ' WHERE tid = ? And sid = ?' + lidSql +
  47. ' GROUP BY `lid`' +
  48. ' ) As MaxFilter ' +
  49. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  50. const sqlParam = [tid, sid];
  51. if (!lid) {
  52. return await this.db.query(sql, sqlParam);
  53. } else if (lid instanceof Array) {
  54. return await this.db.query(sql, sqlParam);
  55. }
  56. return await this.db.queryOne(sql, sqlParam);
  57. }
  58. /**
  59. * 查询 某期 某轮审批 某人数据
  60. * @param {Number} tid - 标段id
  61. * @param {Number} sid - 期id
  62. * @param {Number} times - 第几轮
  63. * @param {Number} order - 流程
  64. * @param {Number|Array} lid - 台账节点id(可以为空)
  65. * @return {Promise<*>}
  66. */
  67. async getAuditorStageData(tid, sid, times, order, lid) {
  68. const lidSql = lid ? ' And Bills.lid in (?)' : '';
  69. const sql = 'SELECT Bills.* FROM ' + this.departTableName(tid) + ' As Bills ' +
  70. ' INNER JOIN ( ' +
  71. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `tid`, `sid` From ' + this.departTableName(tid) +
  72. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) And tid = ? And sid = ?' + lidSql +
  73. ' GROUP BY `lid`' +
  74. ' ) As MaxFilter ' +
  75. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid' +
  76. ' AND Bills.sid = MaxFilter.sid';
  77. const sqlParam = [times, times, order, tid, sid];
  78. if (!lid) {
  79. return await this.db.query(sql, sqlParam);
  80. } else if (lid instanceof Array) {
  81. sqlParam.push(lid.join(', '));
  82. return await this.db.query(sql, sqlParam);
  83. }
  84. sqlParam.push(lid);
  85. return await this.db.queryOne(sql, sqlParam);
  86. }
  87. async getLastestStageData2(tid, sid, lid) {
  88. let lidSql = '',
  89. result;
  90. if (lid) {
  91. if (lid instanceof Array) {
  92. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  93. } else {
  94. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  95. }
  96. }
  97. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  98. ' INNER JOIN ( ' +
  99. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  100. ' WHERE tid = ? And sid = ?' + lidSql +
  101. ' GROUP BY `lid`' +
  102. ' ) As MaxFilter ' +
  103. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  104. const sqlParam = [tid, sid];
  105. if (!lid) {
  106. return await this.db.query(sql, sqlParam);
  107. } else if (lid instanceof Array) {
  108. return await this.db.query(sql, sqlParam);
  109. }
  110. return await this.db.queryOne(sql, sqlParam);
  111. }
  112. async getStageUsedBills(tid, sid) {
  113. const sql = 'SELECT Bills.lid, ((not IsNull(Bills.contract_qty) and Bills.contract_qty <> 0) or (not IsNull(Bills.qc_qty) and Bills.qc_qty <> 0)) As used' +
  114. ' FROM ' + this.tableName + ' As Bills ' +
  115. ' INNER JOIN ( ' +
  116. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  117. ' WHERE tid = ? And sid = ?' +
  118. ' GROUP BY `lid`' +
  119. ' ) As MaxFilter ' +
  120. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  121. const sqlParam = [tid, sid];
  122. const stageBills = await this.db.query(sql, sqlParam);
  123. return this._.map(this._.filter(stageBills, 'used'), 'lid');
  124. }
  125. /**
  126. * 获取截止本期数据
  127. * @param {Number} tid - 标段id
  128. * @param {Number} sorder - 截止期序号
  129. * @param {String|Array[String]} lid - 台账id
  130. * @returns {Promise<*>}
  131. */
  132. async getEndStageData(tid, sorder, lid) {
  133. let lidSql = '';
  134. if (lid) {
  135. if (lid instanceof Array) {
  136. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  137. } else {
  138. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  139. }
  140. }
  141. const sql = 'SELECT Bills.tid, Bills.lid,' +
  142. ' Sum(Bills.contract_qty) As contract_qty, Sum(Bills.contract_tp) As contract_tp,' +
  143. ' Sum(Bills.qc_qty) As qc_qty, Sum(Bills.qc_tp) As qc_tp FROM ' + this.tableName + ' As Bills ' +
  144. ' INNER JOIN ( ' +
  145. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  146. ' WHERE tid = ? ' + lidSql +
  147. ' GROUP BY `lid`, `sid`' +
  148. ' ) As MaxFilter ' +
  149. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  150. ' INNER JOIN ' + this.ctx.service.stage.tableName + ' As Stage' +
  151. ' ON Bills.sid = Stage.id' +
  152. ' WHERE Stage.order <= ?' +
  153. ' GROUP BY `lid`';
  154. const sqlParam = [tid, sorder];
  155. if (!lid) {
  156. return await this.db.query(sql, sqlParam);
  157. } else if (lid instanceof Array) {
  158. return await this.db.query(sql, sqlParam);
  159. }
  160. return await this.db.queryOne(sql, sqlParam);
  161. }
  162. async getStageBills(tid, sid, lid) {
  163. const sql = 'SELECT Stage.*, Ledger.unit_price FROM ?? As Stage, ?? As Ledger ' +
  164. ' Where Stage.tid = ?, Stage.sid = ?, Stage.lid = ?, Stage.lid = Ledger.id ' +
  165. ' Order Stage.time DESC, Stage.order DESC ';
  166. const sqlParam = [this.tableName, this.ctx.service.ledger.tableName];
  167. sqlParam.push(this.db.escape(tid));
  168. sqlParam.push(this.db.escape(sid));
  169. sqlParam.push(this.db.escape(lid));
  170. return await this.db.queryOne(sql, sqlParam);
  171. }
  172. _calcStageBillsData(data, orgData, ledgerData) {
  173. const info = this.ctx.tender.info;
  174. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  175. if (data.contract_qty !== undefined) {
  176. data.contract_qty = this.round(data.contract_qty, precision.value);
  177. data.contract_tp = this.ctx.helper.mul(data.contract_qty, ledgerData.unit_price, info.decimal.tp);
  178. }
  179. if (data.qc_qty !== undefined) {
  180. data.qc_qty = this.round(data.qc_qty, precision.value);
  181. data.qc_tp = this.ctx.helper.mul(data.qc_qty, ledgerData.unit_price, info.decimal.tp);
  182. }
  183. if (ledgerData.is_tp && data.contract_tp !== undefined) {
  184. data.contract_tp = this.ctx.helper.round(data.contract_tp, info.decimal.tp);
  185. }
  186. }
  187. async _insertStageBillsData(transaction, insertData, orgData, ledgerData) {
  188. const info = this.ctx.tender.info;
  189. const d = {
  190. tid: this.ctx.tender.id,
  191. lid: ledgerData.id,
  192. sid: this.ctx.stage.id,
  193. times: this.ctx.stage.curTimes,
  194. order: this.ctx.stage.curOrder,
  195. said: this.ctx.session.sessionUser.accountId,
  196. };
  197. if (orgData) {
  198. d.contract_qty = orgData.contract_qty;
  199. d.contract_tp = orgData.contract_tp;
  200. d.qc_qty = orgData.qc_qty;
  201. d.qc_tp = orgData.qc_tp;
  202. d.postil = orgData.postil;
  203. }
  204. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  205. if (insertData.contract_qty !== undefined) {
  206. d.contract_qty = this.round(insertData.contract_qty, precision.value);
  207. d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerData.unit_price, info.decimal.tp);
  208. }
  209. if (insertData.contract_expr !== undefined) d.contract_expr = insertData.contract_expr;
  210. if (insertData.qc_qty !== undefined) {
  211. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  212. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerData.unit_price, info.decimal.tp);
  213. }
  214. if (insertData.postil) {
  215. d.postil = insertData.postil;
  216. }
  217. if (ledgerData.is_tp && insertData.contract_tp !== undefined) {
  218. d.contract_tp = this.ctx.helper.round(insertData.contract_tp, info.decimal.tp);
  219. }
  220. await transaction.insert(this.tableName, d);
  221. }
  222. /**
  223. * 前端提交数据
  224. * @param {Object|Array} data - 提交的数据
  225. * @return {Promise<void>}
  226. */
  227. async updateStageData(data) {
  228. const datas = data instanceof Array ? data : [data];
  229. const transaction = await this.db.beginTransaction();
  230. try {
  231. for (const d of datas) {
  232. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  233. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  234. if (!stageBills || stageBills.times !== this.ctx.stage.curTimes || stageBills.order !== this.ctx.stage.curOrder) {
  235. await this._insertStageBillsData(transaction, d, stageBills, ledgerBills);
  236. } else {
  237. d.id = stageBills.id;
  238. this._calcStageBillsData(d, stageBills, ledgerBills);
  239. await transaction.update(this.tableName, d);
  240. }
  241. }
  242. await transaction.commit();
  243. } catch (err) {
  244. await transaction.rollback();
  245. throw err;
  246. }
  247. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  248. }
  249. /**
  250. * 根据
  251. * @param transaction
  252. * @param ledgerBills
  253. * @param stageBills
  254. * @param data
  255. * @return {Promise<void>}
  256. * @private
  257. */
  258. async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
  259. if (stageBills) {
  260. if ((data.contract_qty === undefined || stageBills.contract_qty !== data.contract_qty) ||
  261. (data.qc_qty === undefined || stageBills.qc_qty !== data.qc_qty)) {
  262. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  263. data.id = stageBills.id;
  264. this._calcStageBillsData(data, stageBills, ledgerBills);
  265. await transaction.update(this.tableName, data);
  266. } else {
  267. await this._insertStageBillsData(transaction, data, stageBills, ledgerBills);
  268. }
  269. }
  270. } else {
  271. await this._insertStageBillsData(transaction, data, stageBills, ledgerBills);
  272. }
  273. }
  274. /**
  275. * 重算 本期计量 数量 (根据部位明细)
  276. * @param {Number} tid - 标段id
  277. * @param {Number} id - 需要计算的节点的id
  278. * @param {Number} lid - 台账id
  279. * @param {Object} transaction - 操作所属事务
  280. * @return {Promise<void>}
  281. */
  282. async calc(tid, sid, lid, transaction) {
  283. const info = this.ctx.tender.info;
  284. const stageBills = await this.getLastestStageData(tid, sid, lid);
  285. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  286. if (!ledgerBills) {
  287. throw '提交数据错误';
  288. }
  289. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  290. if (!posGather) { return; }
  291. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  292. // 计算
  293. if (posGather.contract_qty !== undefined) {
  294. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  295. posGather.contract_tp = this.ctx.helper.mul(posGather.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  296. }
  297. if (posGather.qc_qty !== undefined) {
  298. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  299. posGather.qc_tp = this.ctx.helper.mul(posGather.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  300. }
  301. if (stageBills) {
  302. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  303. return;
  304. }
  305. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  306. posGather.id = stageBills.id;
  307. await transaction.update(this.tableName, posGather);
  308. } else {
  309. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  310. }
  311. } else {
  312. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  313. }
  314. }
  315. async updateStageBillsCalcType(data) {
  316. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, data.id);
  317. const updateData = { contract_qty: null, contract_tp: null };
  318. const transaction = await this.db.beginTransaction();
  319. try {
  320. await transaction.update(this.ctx.service.ledger.tableName, data);
  321. if (stageBills) {
  322. if (stageBills.times !== this.ctx.stage.curTimes || stageBills.order !== this.ctx.stage.curOrder) {
  323. const ledgerBills = await this.ctx.service.ledger.getDataById(data.id);
  324. await this._insertStageBillsData(transaction, updateData, stageBills, ledgerBills);
  325. } else {
  326. updateData.id = stageBills.id;
  327. await transaction.update(this.tableName, updateData);
  328. }
  329. }
  330. await transaction.commit();
  331. } catch (err) {
  332. await transaction.rollback();
  333. throw err;
  334. }
  335. const bills = await this.ctx.service.ledger.getDataById(data.id);
  336. const curStageData = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [data.id]);
  337. return { bills: [bills], curStageData };
  338. }
  339. async getSumTotalPrice(stage) {
  340. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.departTableName(stage.tid) + ' As Bills ' +
  341. ' INNER JOIN ( ' +
  342. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `sid` From ' + this.departTableName(stage.tid) +
  343. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) AND `sid` = ?' +
  344. ' GROUP BY `lid`' +
  345. ' ) As MaxFilter ' +
  346. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.sid = MaxFilter.sid';
  347. const sqlParam = [stage.curTimes, stage.curTimes, stage.curOrder, stage.id];
  348. const result = await this.db.queryOne(sql, sqlParam);
  349. return result;
  350. }
  351. async getSumTotalPriceFilter(stage, operate, filter) {
  352. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp`' +
  353. ' FROM ' + this.departTableName(stage.tid) + ' As Bills ' +
  354. ' INNER JOIN ( ' +
  355. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.departTableName(stage.tid) +
  356. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) AND `sid` = ?' +
  357. ' GROUP BY `lid`' +
  358. ' ) As MaxFilter ' +
  359. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid ' +
  360. ' INNER JOIN ' + this.ctx.service.ledger.departTableName(stage.tid) + ' As Ledger ON Bills.lid = Ledger.id' +
  361. ' WHERE Bills.sid = ? And Ledger.b_code ' + operate + ' ?';
  362. const sqlParam = [stage.times, stage.curTimes, stage.curOrder, stage.id, stage.id, filter];
  363. const result = await this.db.queryOne(sql, sqlParam);
  364. return result;
  365. }
  366. async getSumTotalPriceGcl(stage, regText) {
  367. if (regText) {
  368. return await this.getSumTotalPriceFilter(stage, 'REGEXP', regText);
  369. }
  370. return await this.getSumTotalPriceFilter(stage, '<>', this.db.escape(''));
  371. }
  372. async getSumTotalPriceNotGcl(stage) {
  373. return await this.getSumTotalPriceFilter(stage, '=', this.db.escape(''));
  374. }
  375. /**
  376. * 多期清单数据整合 (材料调差调用)
  377. * @param {Number} tid - 标段id
  378. * @param {String} stage_id_list - 期id列表
  379. * @return {Promise<void>}
  380. */
  381. async getStagesData(tid, stage_id_list) {
  382. const whereSql = this.ctx.helper.whereSql({tid: tid, sid: stage_id_list.split(',')});
  383. const sql = 'SELECT Bills.lid, Bills.tid, Bills.sid,' +
  384. ' Sum(Bills.contract_qty) As contract_qty, Sum(Bills.contract_tp) As contract_tp,' +
  385. ' Sum(Bills.qc_qty) As qc_qty, Sum(Bills.qc_tp) As qc_tp' +
  386. ' FROM ' + this.tableName + ' As Bills ' +
  387. ' INNER JOIN ( ' +
  388. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `tid`, `lid`, `sid` From ' + this.tableName + whereSql + ' GROUP BY `lid`, `sid`' +
  389. ' ) As MaxFilter ' +
  390. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.tid = MaxFilter.tid And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  391. ' GROUP BY Bills.lid';
  392. const result = await this.db.query(sql);
  393. return result;
  394. }
  395. /**
  396. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  397. * @param {Number} tid - 标段id
  398. * @param {String} stage_id_list - 期id列表
  399. * @param {String} lid - 台账id
  400. * @param {String} xid - 项目节id
  401. * @return {Promise<void>}
  402. */
  403. async getGatherQtyByMaterial(tid, stage_id_list, lid) {
  404. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  405. let gather_qty = 0;
  406. for (const sid of stage_id_list) {
  407. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  408. ' INNER JOIN ( ' +
  409. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  410. ' WHERE tid = ? And sid = ?' +
  411. ' GROUP BY `lid`' +
  412. ' ) As MaxFilter ' +
  413. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  414. ' WHERE Bills.lid = ?';
  415. const sqlParam = [tid, sid, lid];
  416. const result = await this.db.queryOne(sql, sqlParam);
  417. if (result) {
  418. gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  419. }
  420. }
  421. return gather_qty !== 0 ? gather_qty : null;
  422. }
  423. async getSumTotalPriceByMaterial(stage_list) {
  424. let contract_tp = 0;
  425. let qc_tp = 0;
  426. for (const stage of stage_list) {
  427. const result = await this.getSumTotalPrice(stage);
  428. if (result) {
  429. contract_tp = this.ctx.helper.add(contract_tp, result.contract_tp);
  430. qc_tp = this.ctx.helper.add(qc_tp, result.qc_tp);
  431. }
  432. }
  433. return { contract_tp, qc_tp };
  434. }
  435. async getSumTotalPriceGclByMaterial(stage_list, regText) {
  436. let contract_tp = 0;
  437. let qc_tp = 0;
  438. for (const stage of stage_list) {
  439. const result = await this.getSumTotalPriceGcl(stage, regText);
  440. if (result) {
  441. contract_tp = this.ctx.helper.add(contract_tp, result.contract_tp);
  442. qc_tp = this.ctx.helper.add(qc_tp, result.qc_tp);
  443. }
  444. }
  445. return { contract_tp, qc_tp };
  446. }
  447. async sumLoad(lid, tender) {
  448. const conn = await this.db.beginTransaction();
  449. try {
  450. const maxId = await this._getMaxLid(this.ctx.tender.id);
  451. const select = await this.getDataById(lid);
  452. const sumLoad = new SumLoad(this.ctx);
  453. const loadTree = await sumLoad.stageGatherGcl(select, maxId, tenders);
  454. const result = loadTree.getUpdateData();
  455. this._cacheMaxLid(this.ctx.tender.id, loadTree.keyNodeId);
  456. await this.ctx.service.sumLoadHistory.saveReviseHistory(this.ctx.tender.id, rid, lid, tenders, result.errors);
  457. if (result.update.length > 0) await conn.updateRows(this.tableName, result.update);
  458. if (result.create.length > 0)await conn.insert(this.tableName, result.create);
  459. await conn.commit();
  460. return result;
  461. } catch (err) {
  462. await conn.rollback();
  463. throw (err.stack ? err : '导入工程量数据出错');
  464. }
  465. }
  466. }
  467. return StageBills;
  468. };