stage_bills.js 25 KB

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