stage_pos.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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, updateBills = null;
  142. const result = {pos: [], ledger: []};
  143. const datas = data instanceof Array ? data : [data], calcStageBills = [];
  144. if (datas[0].sgfh_qty !== undefined || datas[0].sjcl_qty !== undefined || datas[0].qtcl_qty !== undefined
  145. || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined) {
  146. bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  147. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  148. result.ledger.push(bills.id);
  149. }
  150. if (datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].postil !== undefined) {
  151. result.stageUpdate = true;
  152. }
  153. const insertPos = [], insertPosStage = [];
  154. for (const d of datas) {
  155. const p = {
  156. id: this.uuid.v4(), tid: this.ctx.tender.id, lid: d.lid, name: d.name, porder: d.porder, position: d.position,
  157. add_stage: this.ctx.stage.id,
  158. add_times: this.ctx.stage.curTimes,
  159. add_user: this.ctx.session.sessionUser.accountId,
  160. };
  161. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  162. if (d.sgfh_qty!== undefined) p.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  163. if (d.sjcl_qty!== undefined) p.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  164. if (d.qtcl_qty!== undefined) p.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  165. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  166. if (!updateBills) updateBills = {id: bills.id, sgfh_qty: bills.sgfh_qty, sjcl_qty: bills.sjcl_qty, qtcl_qty: bills.qtcl_qty};
  167. }
  168. insertPos.push(p);
  169. result.pos.push(p.id);
  170. if (d.contract_qty!== undefined || d.qc_qty!== undefined || d.postil!== undefined) {
  171. const ps = {
  172. pid: p.id,
  173. lid: d.lid,
  174. tid: this.ctx.tender.id,
  175. sid: this.ctx.stage.id,
  176. said: this.ctx.session.sessionUser.accountId,
  177. times: this.ctx.stage.curTimes,
  178. order: this.ctx.stage.curOrder,
  179. };
  180. if (d.contract_qty !== undefined) ps.contract_qty = this.round(d.contract_qty, precision.value);
  181. if (d.qc_qty!== undefined) ps.qc_qty = this.round(d.qc_qty, precision.value);
  182. if (d.postil!== undefined) ps.postil = d.postil;
  183. insertPosStage.push(ps);
  184. if ((d.contract_qty || d.qc_qty) && calcStageBills.indexOf(ps.lid) === -1) {
  185. calcStageBills.push(ps.lid);
  186. }
  187. }
  188. }
  189. if (updateBills) {
  190. for (const d of insertPos) {
  191. if (d.sgfh_qty) {
  192. d.sgfh_qty = this.ctx.helper.round(d.sgfh_qty, precision.value);
  193. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  194. }
  195. if (d.sjcl_qty) {
  196. d.sjcl_qty = this.ctx.helper.round(d.sjcl_qty, precision.value);
  197. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  198. }
  199. if (d.qtcl_qty) {
  200. d.qtcl_qty = this.ctx.helper.round(d.qtcl_qty, precision.value);
  201. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  202. }
  203. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  204. }
  205. updateBills.quantity = this.ctx.helper.sum([updateBills.sgfh_qty, updateBills.qtcl_qty, updateBills.sjcl_qty]);
  206. const info = this.ctx.tender.info;
  207. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  208. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  209. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  210. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  211. }
  212. const transaction = await this.db.beginTransaction();
  213. try {
  214. if (insertPos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, insertPos);
  215. if (updateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  216. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  217. for (const lid of calcStageBills) {
  218. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  219. }
  220. await transaction.commit();
  221. return result;
  222. } catch(err) {
  223. await transaction.rollback();
  224. throw err;
  225. }
  226. }
  227. /**
  228. * 更新部位明细数据(仅供updateStageData调用)
  229. *
  230. * @param transaction - 事务
  231. * @param data - 更新数据(允许一次性提交多条)
  232. * @returns {Promise<{ledger: Array, pos: Array}>}
  233. * @private
  234. */
  235. async _updateStagePosData(data) {
  236. let bills, precision, updateBills = null;
  237. const datas = data instanceof Array ? data : [data];
  238. const orgPos = await this.ctx.service.pos.getPosDataByIds(this._.map(datas, 'pid'));
  239. const result = {ledger: [], pos: [], stageUpdate: true};
  240. const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
  241. {pid: this._.map(datas, 'pid')});
  242. if (datas[0].sgfh_qty !== undefined || datas[0].qtcl_qty !== undefined || datas[0].sjcl_qty !== undefined
  243. || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined) {
  244. bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  245. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  246. result.ledger.push(bills.id);
  247. }
  248. const updatePos = [], updatePosStage = [], insertPosStage = [];
  249. for (const d of datas) {
  250. if (d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
  251. || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  252. const op = this._.find(orgPos, {id: d.pid});
  253. if (op.add_stage !== this.ctx.stage.id) throw '不可修改数据';
  254. const p = {id: op.id};
  255. if (d.name !== undefined) p.name = d.name;
  256. if (d.position !== undefined) p.position = d.position;
  257. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  258. p.sgfh_qty = d.sgfh_qty !== undefined ? d.sgfh_qty : op.sgfh_qty;
  259. p.sjcl_qty = d.sjcl_qty !== undefined ? d.sjcl_qty : op.sjcl_qty;
  260. p.qtcl_qty = d.qtcl_qty !== undefined ? d.qtcl_qty : op.qtcl_qty;
  261. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  262. if (!updateBills) updateBills = {id: bills.id};
  263. }
  264. if (d.drawing_code !== undefined) p.drawing_code = d.drawing_code;
  265. updatePos.push(p);
  266. }
  267. if (d.contract_qty !== undefined || d.qc_qty !== undefined || d.postil !== undefined) {
  268. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  269. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  270. const sp = {id: osp.id};
  271. if (d.contract_qty !== undefined) {
  272. sp.contract_qty = this.ctx.helper.round(d.contract_qty, precision.value);
  273. }
  274. if (d.qc_qty !== undefined) {
  275. sp.qc_qty = this.ctx.helper.round(d.qc_qty, precision.value);
  276. }
  277. if (d.postil !== undefined) {
  278. sp.postil = d.postil;
  279. }
  280. updatePosStage.push(sp);
  281. } else {
  282. const sp = {
  283. pid: d.pid, lid: d.lid,
  284. tid: this.ctx.tender.id, sid: this.ctx.stage.id,
  285. said: this.ctx.session.sessionUser.accountId,
  286. times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
  287. };
  288. if (d.contract_qty !== undefined || osp) {
  289. sp.contract_qty = d.contract_qty === undefined && osp
  290. ? osp.contract_qty
  291. : this.ctx.helper.round(d.contract_qty, precision.value);
  292. }
  293. if (d.qc_qty || osp) {
  294. sp.qc_qty = d.qc_qty === undefined && osp
  295. ? osp.qc_qty
  296. : this.ctx.helper.round(d.qc_qty, precision.value);
  297. }
  298. if (d.postil || osp) {
  299. sp.postil = d.postil === undefined && osp ? osp.postil : d.postil;
  300. }
  301. insertPosStage.push(sp);
  302. }
  303. }
  304. result.pos.push(d.pid);
  305. }
  306. if (updateBills) {
  307. const billsPos = await this.ctx.service.pos.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  308. for (const bp of billsPos) {
  309. const newPos = updatePos.find(function (x) { return x.id === bp.id });
  310. const calcData = newPos ? newPos : bp;
  311. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  312. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  313. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  314. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  315. }
  316. const info = this.ctx.tender.info;
  317. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  318. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  319. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  320. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  321. }
  322. const transaction = await this.db.beginTransaction();
  323. try {
  324. if (updatePos.length > 0) {
  325. await transaction.updateRows(this.ctx.service.pos.tableName, updatePos);
  326. }
  327. if (updateBills) {
  328. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  329. }
  330. if (updatePosStage.length > 0) {
  331. await transaction.updateRows(this.tableName, updatePosStage);
  332. }
  333. if (insertPosStage.length > 0) {
  334. await transaction.insert(this.tableName, insertPosStage);
  335. }
  336. for (const lid of result.ledger) {
  337. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  338. }
  339. await transaction.commit();
  340. return result;
  341. } catch (err) {
  342. await transaction.rollback();
  343. throw err;
  344. }
  345. }
  346. /**
  347. * 删除部位明细数据(仅供updateStageData调用)
  348. *
  349. * @param transaction - 事务
  350. * @param data - 删除的部位明细(允许一次提醒多条,也允许跨清单(但前端操作不允许))
  351. * @returns {Promise<{}>}
  352. * @private
  353. */
  354. async _deleteStagePosData(data) {
  355. const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
  356. const result = { pos: data, isDeletePos: true};
  357. if (pos instanceof Array) {
  358. for (const p of pos) {
  359. if (p.add_stage !== this.ctx.stage.id /*|| p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId*/) {
  360. throw '不可删除该数据';
  361. }
  362. if (p.lid !== pos[0].lid) {
  363. throw '提交数据错误';
  364. }
  365. }
  366. } else if (pos.add_stage !== this.ctx.stage.id /*|| pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId*/) {
  367. throw '不可删除该数据';
  368. }
  369. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  370. const billsPos = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: bills.tender_id, lid: bills.id} });
  371. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  372. for (const bp of billsPos) {
  373. if (data.indexOf(bp.id) >= 0) continue;
  374. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  375. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  376. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  377. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  378. }
  379. const info = this.ctx.tender.info;
  380. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  381. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  382. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  383. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  384. const transaction = await this.db.beginTransaction();
  385. try {
  386. // 删除部位明细
  387. await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
  388. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  389. // 删除部位明细计量数据
  390. await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
  391. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, bills.id, transaction);
  392. await transaction.commit();
  393. // 获取需要更新的数据
  394. result.ledger = [bills.id];
  395. result.stageUpdate = true;
  396. return result;
  397. } catch (err) {
  398. await transaction.rollback();
  399. throw err;
  400. }
  401. }
  402. /**
  403. * 根据前端提交数据,更新并计算
  404. *
  405. * @param data
  406. * @returns {Promise<{ledger: {}, pos: {}}>}
  407. */
  408. async updateStageData(data) {
  409. if ((data.updateType === 'add' || data.upateType === 'delete') && this.ctx.tender.measure_type === measureType.tz) {
  410. throw '台账模式下,不可在计量中新增或删除部位明细,如需操作,请进行台账修订';
  411. }
  412. let refreshData;
  413. if (data.updateType === 'add') {
  414. refreshData = await this._addStagePosData(data.updateData);
  415. } else if (data.updateType === 'update') {
  416. refreshData = await this._updateStagePosData(data.updateData);
  417. } else if (data.updateType === 'delete') {
  418. if (!data.updateData || data.updateData.length === 0) {
  419. throw '提交数据错误';
  420. }
  421. refreshData = await this._deleteStagePosData(data.updateData);
  422. } else {
  423. throw '提交数据错误';
  424. }
  425. try {
  426. const result = {ledger: {}, pos: {}};
  427. if (refreshData.ledger && refreshData.ledger.length > 0) {
  428. result.ledger.bills = await this.ctx.service.ledger.getDataByIds(refreshData.ledger);
  429. if (refreshData.stageUpdate) {
  430. result.ledger.curStageData = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
  431. }
  432. }
  433. if (refreshData.pos) {
  434. if (refreshData.isDeletePos) {
  435. result.pos.pos = refreshData.pos;
  436. } else if (refreshData.pos.length > 0) {
  437. result.pos.pos = await this.ctx.service.pos.getPosDataWithAddStageOrder({id: refreshData.pos});
  438. if (refreshData.stageUpdate) {
  439. result.pos.curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
  440. }
  441. }
  442. }
  443. return result;
  444. } catch(err) {
  445. throw '获取数据异常,请刷新页面。';
  446. }
  447. }
  448. async updateChangeQuantity(transaction, pos, qty) {
  449. let orgPos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  450. if (orgPos.length > 1) {
  451. throw '数据错误';
  452. } else {
  453. orgPos = orgPos[0];
  454. }
  455. if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
  456. await transaction.update(this.tableName, {id: orgPos.id, qc_qty: qty});
  457. } else {
  458. await transaction.insert(this.tableName, {
  459. tid: this.ctx.tender.id,
  460. sid: this.ctx.stage.id,
  461. lid: pos.lid,
  462. pid: pos.id,
  463. said: this.ctx.session.sessionUser.accountId,
  464. times: this.ctx.stage.curTimes,
  465. order: this.ctx.stage.curOrder,
  466. contract_qty: orgPos ? orgPos.contract_qty : 0,
  467. qc_qty: qty,
  468. });
  469. }
  470. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
  471. }
  472. /**
  473. * 统计清单下部位明细合计
  474. * @param {Number} tid - 标段id
  475. * @param {Number} sid - 期id
  476. * @param {Number} lid - 清单节点id
  477. * @param transaction - 事务(不为空则在事务中查询,反之在数据库中查询)
  478. * @returns {Promise<*>}
  479. */
  480. async getPosGather(tid, sid, lid, transaction) {
  481. const calcQtySql = 'SELECT SUM(`contract_qty`) As `contract_qty`, SUM(`qc_qty`) As `qc_qty` FROM (' +
  482. ' SELECT `contract_qty`, `qc_qty` FROM ' + this.ctx.service.stagePos.tableName + ' As Pos ' +
  483. ' INNER JOIN (' +
  484. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` ' +
  485. ' FROM ' + this.ctx.service.stagePos.tableName +
  486. ' WHERE `tid` = ? And sid = ? And `lid` = ? ' +
  487. ' GROUP BY `pid`' +
  488. ' ) As MaxFilter ' +
  489. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid ' +
  490. ' WHERE Pos.tid = ? And Pos.sid = ? And Pos.lid = ?' +
  491. ' ) As Gather';
  492. const param = [tid, sid, lid];
  493. const sqlParam = param.concat(param);
  494. if (transaction) {
  495. return await transaction.queryOne(calcQtySql, sqlParam);
  496. } else {
  497. return await this.db.queryOne(calcQtySql, sqlParam);
  498. }
  499. }
  500. /**
  501. * 多期清单数据整合 (材料调差调用)
  502. * @param {Number} tid - 标段id
  503. * @param {String} stage_id_list - 期id列表
  504. * @returns {Promise<void>}
  505. */
  506. async getStagesData(tid, stage_id_list) {
  507. let stage_id_listSql = '';
  508. stage_id_list = stage_id_list.indexOf(',') !== -1 ? stage_id_list.split(',') : stage_id_list;
  509. if (stage_id_list) {
  510. if (stage_id_list instanceof Array) {
  511. stage_id_listSql = stage_id_list.length > 0 ? ' And sid in (' + this.ctx.helper.getInArrStrSqlFilter(stage_id_list) + ')' : '';
  512. } else {
  513. stage_id_listSql = ' And sid in (' + this.db.escape(stage_id_list) + ')';
  514. }
  515. }
  516. const sql = 'SELECT `id`, `tid`, `sid`, `pid`, `lid`, SUM(`contract_qty`) as `contract_qty`, SUM(`qc_qty`) as `qc_qty`' +
  517. ' FROM ' + this.tableName + ' WHERE `tid` = ? ' +
  518. stage_id_listSql + 'GROUP BY `pid`';
  519. const sqlParam = [tid];
  520. const result = await this.db.query(sql, sqlParam);
  521. return result;
  522. }
  523. /**
  524. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  525. * @param {Number} tid - 标段id
  526. * @param {String} stage_id_list - 期id列表
  527. * @param {String} lid - 台账id
  528. * @param {String} pid - 部位id
  529. * @returns {Promise<void>}
  530. */
  531. async getGatherQtyByMaterial(tid, stage_id_list, lid, pid) {
  532. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  533. let gather_qty = 0;
  534. for (const sid of stage_id_list) {
  535. const sql = 'SELECT Pos.contract_qty, Pos.qc_qty FROM ' +
  536. ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND sid = ?) As Pos ' +
  537. ' INNER JOIN ( ' +
  538. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
  539. ' WHERE `tid` = ? AND sid = ?' +
  540. ' GROUP BY `pid`' +
  541. ' ) As MaxFilter ' +
  542. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid' +
  543. ' WHERE Pos.lid = ? AND Pos.pid = ?';
  544. const sqlParam = [tid, sid, tid, sid, lid, pid];
  545. const result = await this.db.queryOne(sql, sqlParam);
  546. if (result) {
  547. gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  548. }
  549. }
  550. return gather_qty !== 0 ? gather_qty : null;
  551. }
  552. }
  553. return StagePos;
  554. };