stage_pos.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const measureType = require('../const/tender').measureType;
  10. const timesLen = require('../const/audit').stage.timesLen;
  11. module.exports = app => {
  12. class StagePos extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'stage_pos';
  22. this.qtyFields = ['contract_qty', 'qc_qty']
  23. }
  24. _getPosFilterSql(where, asTable = '') {
  25. let whereSql = '';
  26. if (!where) return whereSql;
  27. if (where.pid) {
  28. if (where.pid instanceof Array) {
  29. whereSql += ' And ' + asTable + 'pid in (' + this.ctx.helper.getInArrStrSqlFilter(where.pid) + ')';
  30. } else if (typeof where.pid === "string") {
  31. whereSql += ' And ' + asTable + 'pid = ' + this.db.escape(where.pid);
  32. }
  33. }
  34. if (where.lid) {
  35. if (where.lid instanceof Array) {
  36. whereSql += ' And ' + asTable + 'lid in (' + this.ctx.helper.getInArrStrSqlFilter(where.lid) + ')';
  37. } else if (typeof where.pid === "string") {
  38. whereSql += ' And ' + asTable + 'lid = ' + this.db.escape(where.lid);
  39. }
  40. }
  41. return whereSql;
  42. }
  43. /**
  44. * 查询期计量最后审核人数据
  45. * @param {Number} tid - 标段id
  46. * @param {Number} sid - 期id
  47. * @param {Number|Array} pid - 部位明细id(可以为空)
  48. * @returns {Promise<*>}
  49. */
  50. async getLastestStageData(tid, sid, where) {
  51. const filterSql = this._getPosFilterSql(where);
  52. const sql = 'SELECT Pos.id, Pos.tid, Pos.sid, Pos.lid, Pos.pid, Pos.contract_qty, Pos.qc_qty, Pos.postil, Pos.times, Pos.order FROM ' +
  53. ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
  54. ' INNER JOIN ( ' +
  55. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
  56. ' WHERE `tid` = ? And sid = ?' + filterSql +
  57. ' GROUP BY `pid`' +
  58. ' ) As MaxFilter ' +
  59. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
  60. const sqlParam = [tid, sid, tid, sid];
  61. return await this.db.query(sql, sqlParam);
  62. }
  63. /**
  64. * 查询 某期 某轮审批 某审核人数据
  65. * @param {Number} tid - 标段id
  66. * @param {Number} sid - 期id
  67. * @param {Number} times - 期第几轮审批
  68. * @param {Number} order - 审核人顺序
  69. * @param {Number|Array|Null} pid - 部位明细id - 为空则查询全部
  70. * @returns {Promise<*>}
  71. */
  72. async getAuditorStageData(tid, sid, times, order, where) {
  73. const filterSql = this._getPosFilterSql(where);
  74. const sql = 'SELECT Pos.id, Pos.tid, Pos.sid, Pos.pid, Pos.lid, Pos.contract_qty, Pos.qc_qty, Pos.postil, Pos.times, Pos.order FROM ' +
  75. ' (SELECT * FROM '+ this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
  76. ' INNER JOIN ( ' +
  77. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid`, `sid` From ' + this.tableName +
  78. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) And tid = ? And sid = ?' + filterSql +
  79. ' GROUP BY `pid`' +
  80. ' ) As MaxFilter ' +
  81. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
  82. const sqlParam = [tid, sid, times, times, order, tid, sid];
  83. return await this.db.query(sql, sqlParam);
  84. }
  85. async _filterLastestData(stagePos) {
  86. const stagePosIndex = {};
  87. for (const sp of stagePos) {
  88. const key = 'sp-' + sp.pid;
  89. const spi = stagePosIndex[key];
  90. if (spi) {
  91. if ((spi.times * timesLen + spi.order) < (sp.times * timesLen + sp.order)) stagePosIndex[key] = sp;
  92. } else {
  93. stagePosIndex[key] = sp;
  94. }
  95. }
  96. const result = [];
  97. for (const prop in stagePosIndex) {
  98. result.push(stagePosIndex[prop]);
  99. }
  100. return result;
  101. }
  102. async getLastestStageData2(tid, sid, where) {
  103. const filterSql = this._getPosFilterSql(where);
  104. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, postil, `times`, `order`' +
  105. ' FROM ' + this.tableName +
  106. ' WHERE tid = ? And sid = ? ' + filterSql;
  107. const sqlParam = [tid, sid];
  108. const stagePos = await this.db.query(sql, sqlParam);
  109. return this._filterLastestData(stagePos);
  110. }
  111. async getAuditorStageData2(tid, sid, times, order, where) {
  112. const filterSql = this._getPosFilterSql(where);
  113. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, postil, `times`, `order`' +
  114. ' FROM ' + this.tableName +
  115. ' WHERE tid = ? And sid = ? And (`times` < ? OR (`times` = ? AND `order` <= ?)) ' + filterSql;
  116. const sqlParam = [tid, sid, times, times, order];
  117. const stagePos = await this.db.query(sql, sqlParam);
  118. return this._filterLastestData(stagePos);
  119. }
  120. async getStageUsedPos(tid, sid, where) {
  121. const self = this;
  122. const stagePos = await this.getLastestStageData2(tid, sid, where);
  123. const pids = this._.map(stagePos, function (sp) {
  124. if (self.ctx.helper.checkZero(sp.contract_qty) || self.ctx.helper.checkZero(sp.qc_qty)) {
  125. return sp.pid;
  126. } else {
  127. return -1;
  128. }
  129. });
  130. return this._.pull(pids, -1);
  131. }
  132. /**
  133. * 新增部位明细数据(仅供updateStageData调用)
  134. *
  135. * @param transaction - 事务
  136. * @param data - 新增数据
  137. * @returns {Promise<{}>}
  138. * @private
  139. */
  140. async _addStagePosData(data) {
  141. let bills , precision;
  142. const result = {pos: [], ledger: []};
  143. const datas = data instanceof Array ? data : [data], calcBills = [], calcStageBills = [];
  144. const transaction = await this.db.beginTransaction();
  145. try {
  146. for (const d of datas) {
  147. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  148. if (!bills || bills.id !== data.lid) {
  149. bills = await this.ctx.service.ledger.getDataById(d.lid);
  150. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  151. }
  152. }
  153. // 在主表pos中新增数据
  154. const p = {
  155. id: this.uuid.v4(), tid: this.ctx.tender.id, lid: d.lid, name: d.name, porder: d.porder,
  156. add_stage: this.ctx.stage.id,
  157. add_times: this.ctx.stage.curTimes,
  158. add_user: this.ctx.session.sessionUser.accountId,
  159. };
  160. if (d.sgfh_qty) p.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  161. if (d.sjcl_qty) p.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  162. if (d.qtcl_qty) p.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  163. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  164. const addRst = await transaction.insert(this.ctx.service.pos.tableName, p);
  165. result.pos.push(p.id);
  166. // 如果存在复核数据,更新计算主表清单
  167. if (p.sgfh_qty || p.sjcl_qty || p.qtcl_qty) {
  168. calcBills.push(p.lid);
  169. result.ledger.push(p.lid);
  170. }
  171. // 如果存在本期计算数据,更新计算清单本期计量数据
  172. if (d.contract_qty || d.qc_qty || d.postil) {
  173. const ps = {
  174. pid: p.id,
  175. lid: d.lid,
  176. tid: this.ctx.tender.id,
  177. sid: this.ctx.stage.id,
  178. said: this.ctx.session.sessionUser.accountId,
  179. times: this.ctx.stage.curTimes,
  180. order: this.ctx.stage.curOrder,
  181. };
  182. if (d.contract_qty) ps.contract_qty = this.round(d.contract_qty, precision.value);
  183. if (d.qc_qty) ps.qc_qty = this.round(d.qc_qty, precision.value);
  184. if (d.postil) ps.postil = d.postil;
  185. await transaction.insert(this.tableName, ps);
  186. if ((d.contract_qty || d.qc_qty) && calcStageBills.indexOf(ps.lid) === -1) {
  187. calcStageBills.push(ps.lid);
  188. }
  189. result.stageUpdate = true;
  190. }
  191. }
  192. for (const lid of calcBills) {
  193. await this.ctx.service.ledger.calc(this.ctx.tender.id, lid, transaction);
  194. }
  195. for (const lid of calcStageBills) {
  196. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  197. }
  198. await transaction.commit();
  199. return result;
  200. } catch(err) {
  201. await transaction.rollback();
  202. throw err;
  203. }
  204. }
  205. /**
  206. * 更新部位明细数据(仅供updateStageData调用)
  207. *
  208. * @param transaction - 事务
  209. * @param data - 更新数据(允许一次性提交多条)
  210. * @returns {Promise<{ledger: Array, pos: Array}>}
  211. * @private
  212. */
  213. async _updateStagePosData(data) {
  214. let bills, precision;
  215. const result = {ledger: [], pos: [], stageUpdate: true}, ledgerCalc = [];
  216. const datas = data instanceof Array ? data : [data];
  217. const orgPos = await this.ctx.service.pos.getPosDataByIds(this._.map(datas, 'pid'));
  218. const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: this._.map(datas, 'pid')});
  219. const transaction = await this.db.beginTransaction();
  220. try {
  221. for (const d of datas) {
  222. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined || d.contract_qty !== undefined || d.qc_qty !== undefined) {
  223. if (!bills || bills.id !== data.lid) {
  224. bills = await this.ctx.service.ledger.getDataById(d.lid);
  225. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  226. }
  227. }
  228. if (d.name !== undefined || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined || d.drawing_code !== undefined) {
  229. const op = this._.find(orgPos, {id: d.pid});
  230. if (op.add_stage !== this.ctx.stage.id) throw '不可修改数据';
  231. const p = {id: d.pid};
  232. if (d.name !== undefined) p.name = d.name;
  233. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  234. p.sgfh_qty = d.sgfh_qty !== undefined ? d.sgfh_qty : op.sgfh_qty;
  235. p.sjcl_qty = d.sjcl_qty !== undefined ? d.sjcl_qty : op.sjcl_qty;
  236. p.qtcl_qty = d.qtcl_qty !== undefined ? d.qtcl_qty : op.qtcl_qty;
  237. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  238. if (ledgerCalc.indexOf(op.lid) === -1) {
  239. ledgerCalc.push(op.lid);
  240. }
  241. }
  242. if (d.drawing_code !== undefined) p.drawing_code = d.drawing_code;
  243. await transaction.update(this.ctx.service.pos.tableName, p);
  244. }
  245. if (d.contract_qty !== undefined || d.qc_qty !== undefined || d.postil !== undefined) {
  246. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  247. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  248. const sp = {};
  249. if (d.contract_qty !== undefined) {
  250. sp.contract_qty = this.ctx.helper.round(d.contract_qty, precision.value);
  251. }
  252. if (d.qc_qty !== undefined) {
  253. sp.qc_qty = this.ctx.helper.round(d.qc_qty, precision.value);
  254. }
  255. if (d.postil !== undefined) {
  256. sp.postil = d.postil;
  257. }
  258. await transaction.update(this.tableName, sp, {where: {id: osp.id}});
  259. } else {
  260. const sp = {
  261. pid: d.pid, lid: d.lid,
  262. tid: this.ctx.tender.id, sid: this.ctx.stage.id,
  263. said: this.ctx.session.sessionUser.accountId,
  264. times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
  265. };
  266. if (d.contract_qty !== undefined || osp) {
  267. sp.contract_qty = d.contract_qty === undefined && osp
  268. ? osp.contract_qty
  269. : this.ctx.helper.round(d.contract_qty, precision.value);
  270. }
  271. if (d.qc_qty || osp) {
  272. sp.qc_qty = d.qc_qty === undefined && osp
  273. ? osp.qc_qty
  274. : this.ctx.helper.round(d.qc_qty, precision.value);
  275. }
  276. if (d.postil || osp) {
  277. sp.postil = d.postil === undefined && osp ? osp.postil : d.postil;
  278. }
  279. await transaction.insert(this.tableName, sp);
  280. }
  281. }
  282. result.pos.push(d.pid);
  283. if ((d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined ||
  284. d.contract_qty === undefined || d.qc_qty === undefined) && (result.ledger.indexOf(d.lid) === -1)) {
  285. result.ledger.push(d.lid);
  286. }
  287. }
  288. for (const lid of ledgerCalc) {
  289. await this.ctx.service.ledger.calc(this.ctx.tender.id, lid, transaction);
  290. }
  291. for (const lid of result.ledger) {
  292. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  293. }
  294. await transaction.commit();
  295. return result;
  296. } catch (err) {
  297. await transaction.rollback();
  298. throw err;
  299. }
  300. }
  301. /**
  302. * 删除部位明细数据(仅供updateStageData调用)
  303. *
  304. * @param transaction - 事务
  305. * @param data - 删除的部位明细(允许一次提醒多条,也允许跨清单(但前端操作不允许))
  306. * @returns {Promise<{}>}
  307. * @private
  308. */
  309. async _deleteStagePosData(data) {
  310. const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
  311. const result = { pos: data, isDeletePos: true};
  312. if (pos instanceof Array) {
  313. for (const p of pos) {
  314. if (p.add_stage !== this.ctx.stage.id /*|| p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId*/) {
  315. throw '不可删除该数据';
  316. }
  317. }
  318. } else if (pos.add_stage !== this.ctx.stage.id /*|| pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId*/) {
  319. throw '不可删除该数据';
  320. }
  321. const ledgerIds = this._.map(pos, 'lid');
  322. const transaction = await this.db.beginTransaction();
  323. try {
  324. // 删除部位明细
  325. await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
  326. for (const lid of ledgerIds) {
  327. await this.ctx.service.ledger.calc(this.ctx.tender.id, lid, transaction);
  328. }
  329. // 删除部位明细计量数据
  330. await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
  331. for (const lid of ledgerIds) {
  332. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  333. }
  334. await transaction.commit();
  335. // 获取需要更新的数据
  336. result.ledger = ledgerIds;
  337. result.stageUpdate = true;
  338. return result;
  339. } catch (err) {
  340. await transaction.rollback();
  341. throw err;
  342. }
  343. }
  344. /**
  345. * 根据前端提交数据,更新并计算
  346. *
  347. * @param data
  348. * @returns {Promise<{ledger: {}, pos: {}}>}
  349. */
  350. async updateStageData(data) {
  351. if ((data.updateType === 'add' || data.upateType === 'delete') && this.ctx.tender.measure_type === measureType.tz) {
  352. throw '台账模式下,不可在计量中新增或删除部位明细,如需操作,请进行台账修订';
  353. }
  354. let refreshData;
  355. if (data.updateType === 'add') {
  356. refreshData = await this._addStagePosData(data.updateData);
  357. } else if (data.updateType === 'update') {
  358. refreshData = await this._updateStagePosData(data.updateData);
  359. } else if (data.updateType === 'delete') {
  360. if (!data.updateData || data.updateData.length === 0) {
  361. throw '提交数据错误';
  362. }
  363. refreshData = await this._deleteStagePosData(data.updateData);
  364. } else {
  365. throw '提交数据错误';
  366. }
  367. try {
  368. const result = {ledger: {}, pos: {}};
  369. if (refreshData.ledger && refreshData.ledger.length > 0) {
  370. result.ledger.bills = await this.ctx.service.ledger.getDataByIds(refreshData.ledger);
  371. if (refreshData.stageUpdate) {
  372. result.ledger.curStageData = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
  373. }
  374. }
  375. if (refreshData.pos) {
  376. if (refreshData.isDeletePos) {
  377. result.pos.pos = refreshData.pos;
  378. } else if (refreshData.pos.length > 0) {
  379. result.pos.pos = await this.ctx.service.pos.getPosDataWithAddStageOrder({id: refreshData.pos});
  380. if (refreshData.stageUpdate) {
  381. result.pos.curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
  382. }
  383. }
  384. }
  385. return result;
  386. } catch(err) {
  387. throw '获取数据异常,请刷新页面。';
  388. }
  389. }
  390. async updateChangeQuantity(transaction, pos, qty) {
  391. let orgPos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  392. if (orgPos.length > 1) {
  393. throw '数据错误';
  394. } else {
  395. orgPos = orgPos[0];
  396. }
  397. if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
  398. await transaction.update(this.tableName, {id: orgPos.id, qc_qty: qty});
  399. } else {
  400. await transaction.insert(this.tableName, {
  401. tid: this.ctx.tender.id,
  402. sid: this.ctx.stage.id,
  403. lid: pos.lid,
  404. pid: pos.id,
  405. said: this.ctx.session.sessionUser.accountId,
  406. times: this.ctx.stage.curTimes,
  407. order: this.ctx.stage.curOrder,
  408. contract_qty: orgPos ? orgPos.contract_qty : 0,
  409. qc_qty: qty,
  410. });
  411. }
  412. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
  413. }
  414. /**
  415. * 统计清单下部位明细合计
  416. * @param {Number} tid - 标段id
  417. * @param {Number} sid - 期id
  418. * @param {Number} lid - 清单节点id
  419. * @param transaction - 事务(不为空则在事务中查询,反之在数据库中查询)
  420. * @returns {Promise<*>}
  421. */
  422. async getPosGather(tid, sid, lid, transaction) {
  423. const calcQtySql = 'SELECT SUM(`contract_qty`) As `contract_qty`, SUM(`qc_qty`) As `qc_qty` FROM (' +
  424. ' SELECT `contract_qty`, `qc_qty` FROM ' + this.ctx.service.stagePos.tableName + ' As Pos ' +
  425. ' INNER JOIN (' +
  426. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` ' +
  427. ' FROM ' + this.ctx.service.stagePos.tableName +
  428. ' WHERE `tid` = ? And sid = ? And `lid` = ? ' +
  429. ' GROUP BY `pid`' +
  430. ' ) As MaxFilter ' +
  431. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid ' +
  432. ' WHERE Pos.tid = ? And Pos.sid = ? And Pos.lid = ?' +
  433. ' ) As Gather';
  434. const param = [tid, sid, lid];
  435. const sqlParam = param.concat(param);
  436. if (transaction) {
  437. return await transaction.queryOne(calcQtySql, sqlParam);
  438. } else {
  439. return await this.db.queryOne(calcQtySql, sqlParam);
  440. }
  441. }
  442. /**
  443. * 多期清单数据整合 (材料调差调用)
  444. * @param {Number} tid - 标段id
  445. * @param {String} stage_id_list - 期id列表
  446. * @returns {Promise<void>}
  447. */
  448. async getStagesData(tid, stage_id_list) {
  449. let stage_id_listSql = '';
  450. stage_id_list = stage_id_list.indexOf(',') !== -1 ? stage_id_list.split(',') : stage_id_list;
  451. if (stage_id_list) {
  452. if (stage_id_list instanceof Array) {
  453. stage_id_listSql = stage_id_list.length > 0 ? ' And sid in (' + this.ctx.helper.getInArrStrSqlFilter(stage_id_list) + ')' : '';
  454. } else {
  455. stage_id_listSql = ' And sid in (' + this.db.escape(stage_id_list) + ')';
  456. }
  457. }
  458. const sql = 'SELECT `id`, `tid`, `sid`, `pid`, `lid`, SUM(`contract_qty`) as `contract_qty`, SUM(`qc_qty`) as `qc_qty`' +
  459. ' FROM ' + this.tableName + ' WHERE `tid` = ? ' +
  460. stage_id_listSql + 'GROUP BY `pid`';
  461. const sqlParam = [tid];
  462. const result = await this.db.query(sql, sqlParam);
  463. return result;
  464. }
  465. /**
  466. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  467. * @param {Number} tid - 标段id
  468. * @param {String} stage_id_list - 期id列表
  469. * @param {String} lid - 台账id
  470. * @param {String} pid - 部位id
  471. * @returns {Promise<void>}
  472. */
  473. async getGatherQtyByMaterial(tid, stage_id_list, lid, pid) {
  474. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  475. let gather_qty = 0;
  476. for (const sid of stage_id_list) {
  477. const sql = 'SELECT Pos.contract_qty, Pos.qc_qty FROM ' +
  478. ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND sid = ?) As Pos ' +
  479. ' INNER JOIN ( ' +
  480. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
  481. ' WHERE `tid` = ? AND sid = ?' +
  482. ' GROUP BY `pid`' +
  483. ' ) As MaxFilter ' +
  484. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid' +
  485. ' WHERE Pos.lid = ? AND Pos.pid = ?';
  486. const sqlParam = [tid, sid, tid, sid, lid, pid];
  487. const result = await this.db.queryOne(sql, sqlParam);
  488. if (result) {
  489. gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  490. }
  491. }
  492. return gather_qty !== 0 ? gather_qty : null;
  493. }
  494. }
  495. return StagePos;
  496. };