stage_pos.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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.lid === "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, Pos.contract_expr 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, Pos.contract_expr 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. _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`, `contract_expr`' +
  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`, `contract_expr`' +
  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 || datas[0].real_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. if (d.sgfh_expr !== undefined) p.sgfh_expr = d.sgfh_expr;
  166. if (d.sjcl_expr !== undefined) p.sjcl_expr = d.sjcl_expr;
  167. if (d.qtcl_expr !== undefined) p.qtcl_expr = d.qtcl_expr;
  168. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  169. if (!updateBills)
  170. updateBills = {id: bills.id, sgfh_qty: bills.sgfh_qty, sjcl_qty: bills.sjcl_qty, qtcl_qty: bills.qtcl_qty};
  171. }
  172. if (d.real_qty!== undefined) p.real_qty = this.round(d.real_qty, precision.value);
  173. insertPos.push(p);
  174. result.pos.push(p.id);
  175. if (d.contract_qty!== undefined || d.qc_qty!== undefined || d.postil!== undefined) {
  176. const ps = {
  177. pid: p.id,
  178. lid: d.lid,
  179. tid: this.ctx.tender.id,
  180. sid: this.ctx.stage.id,
  181. said: this.ctx.session.sessionUser.accountId,
  182. times: this.ctx.stage.curTimes,
  183. order: this.ctx.stage.curOrder,
  184. };
  185. if (d.contract_qty !== undefined) ps.contract_qty = this.round(d.contract_qty, precision.value);
  186. if (d.contract_expr !== undefined) p.contract_expr = d.contract_expr;
  187. if (d.qc_qty!== undefined) ps.qc_qty = this.round(d.qc_qty, precision.value);
  188. if (d.postil!== undefined) ps.postil = d.postil;
  189. insertPosStage.push(ps);
  190. if ((d.contract_qty || d.qc_qty) && calcStageBills.indexOf(ps.lid) === -1) {
  191. calcStageBills.push(ps.lid);
  192. }
  193. }
  194. }
  195. if (updateBills) {
  196. for (const d of insertPos) {
  197. if (d.sgfh_qty) {
  198. d.sgfh_qty = this.ctx.helper.round(d.sgfh_qty, precision.value);
  199. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  200. }
  201. if (d.sjcl_qty) {
  202. d.sjcl_qty = this.ctx.helper.round(d.sjcl_qty, precision.value);
  203. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  204. }
  205. if (d.qtcl_qty) {
  206. d.qtcl_qty = this.ctx.helper.round(d.qtcl_qty, precision.value);
  207. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  208. }
  209. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  210. }
  211. updateBills.quantity = this.ctx.helper.sum([updateBills.sgfh_qty, updateBills.qtcl_qty, updateBills.sjcl_qty]);
  212. const info = this.ctx.tender.info;
  213. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  214. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  215. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  216. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  217. }
  218. const transaction = await this.db.beginTransaction();
  219. try {
  220. if (insertPos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, insertPos);
  221. if (updateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  222. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  223. for (const lid of calcStageBills) {
  224. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  225. }
  226. await transaction.commit();
  227. return result;
  228. } catch(err) {
  229. await transaction.rollback();
  230. throw err;
  231. }
  232. }
  233. /**
  234. * 更新部位明细数据(仅供updateStageData调用)
  235. *
  236. * @param transaction - 事务
  237. * @param data - 更新数据(允许一次性提交多条)
  238. * @returns {Promise<{ledger: Array, pos: Array}>}
  239. * @private
  240. */
  241. async _updateStagePosData(data) {
  242. let bills, precision, updateBills = null;
  243. const datas = data instanceof Array ? data : [data];
  244. const orgPos = await this.ctx.service.pos.getPosDataByIds(this._.map(datas, 'pid'));
  245. const result = {ledger: [], pos: [], stageUpdate: true};
  246. const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
  247. {pid: this._.map(datas, 'pid')});
  248. if (datas[0].sgfh_qty !== undefined || datas[0].qtcl_qty !== undefined || datas[0].sjcl_qty !== undefined
  249. || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].real_qty !== undefined) {
  250. bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  251. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  252. result.ledger.push(bills.id);
  253. }
  254. const updatePos = [], updatePosStage = [], insertPosStage = [];
  255. for (const d of datas) {
  256. if (d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
  257. || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined
  258. || d.real_qty !== undefined
  259. ) {
  260. const op = this._.find(orgPos, {id: d.pid});
  261. if (op.add_stage !== this.ctx.stage.id && (
  262. d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
  263. || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined
  264. )) throw '不可修改数据';
  265. const p = {id: op.id};
  266. if (d.name !== undefined) p.name = d.name;
  267. if (d.position !== undefined) p.position = d.position;
  268. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  269. p.sgfh_qty = d.sgfh_qty !== undefined ? d.sgfh_qty : op.sgfh_qty;
  270. p.sjcl_qty = d.sjcl_qty !== undefined ? d.sjcl_qty : op.sjcl_qty;
  271. p.qtcl_qty = d.qtcl_qty !== undefined ? d.qtcl_qty : op.qtcl_qty;
  272. if (d.sgfh_expr !== undefined) p.sgfh_expr = d.sgfh_expr;
  273. if (d.sjcl_expr !== undefined) p.sjcl_expr = d.sjcl_expr;
  274. if (d.qtcl_expr !== undefined) p.qtcl_expr = d.qtcl_expr;
  275. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  276. if (!updateBills) updateBills = {id: bills.id};
  277. }
  278. if (d.drawing_code !== undefined) p.drawing_code = d.drawing_code;
  279. if (d.real_qty !== undefined) p.real_qty = this.ctx.helper.round(d.real_qty, precision.value);
  280. updatePos.push(p);
  281. }
  282. if (d.contract_qty !== undefined || d.qc_qty !== undefined || d.postil !== undefined) {
  283. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  284. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  285. const sp = {id: osp.id};
  286. if (d.contract_qty !== undefined) {
  287. sp.contract_qty = this.ctx.helper.round(d.contract_qty, precision.value);
  288. }
  289. if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
  290. if (d.qc_qty !== undefined) {
  291. sp.qc_qty = this.ctx.helper.round(d.qc_qty, precision.value);
  292. }
  293. if (d.postil !== undefined) {
  294. sp.postil = d.postil;
  295. }
  296. updatePosStage.push(sp);
  297. } else {
  298. const sp = {
  299. pid: d.pid, lid: d.lid,
  300. tid: this.ctx.tender.id, sid: this.ctx.stage.id,
  301. said: this.ctx.session.sessionUser.accountId,
  302. times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
  303. };
  304. if (d.contract_qty !== undefined || osp) {
  305. sp.contract_qty = d.contract_qty === undefined && osp
  306. ? osp.contract_qty
  307. : this.ctx.helper.round(d.contract_qty, precision.value);
  308. }
  309. if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
  310. if (d.qc_qty || osp) {
  311. sp.qc_qty = d.qc_qty === undefined && osp
  312. ? osp.qc_qty
  313. : this.ctx.helper.round(d.qc_qty, precision.value);
  314. }
  315. if (d.postil || osp) {
  316. sp.postil = d.postil === undefined && osp ? osp.postil : d.postil;
  317. }
  318. insertPosStage.push(sp);
  319. }
  320. }
  321. result.pos.push(d.pid);
  322. }
  323. if (updateBills) {
  324. const billsPos = await this.ctx.service.pos.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  325. for (const bp of billsPos) {
  326. const newPos = updatePos.find(function (x) { return x.id === bp.id });
  327. const calcData = newPos ? newPos : bp;
  328. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  329. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  330. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  331. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  332. }
  333. const info = this.ctx.tender.info;
  334. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  335. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  336. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  337. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  338. }
  339. const transaction = await this.db.beginTransaction();
  340. try {
  341. if (updatePos.length > 0) {
  342. await transaction.updateRows(this.ctx.service.pos.tableName, updatePos);
  343. }
  344. if (updateBills) {
  345. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  346. }
  347. if (updatePosStage.length > 0) {
  348. await transaction.updateRows(this.tableName, updatePosStage);
  349. }
  350. if (insertPosStage.length > 0) {
  351. await transaction.insert(this.tableName, insertPosStage);
  352. }
  353. for (const lid of result.ledger) {
  354. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  355. }
  356. await transaction.commit();
  357. return result;
  358. } catch (err) {
  359. await transaction.rollback();
  360. throw err;
  361. }
  362. }
  363. /**
  364. * 更新部位明细数据(仅供updateStageData调用)
  365. *
  366. * @param transaction - 事务
  367. * @param data - 更新数据(允许一次性提交多条)
  368. * @returns {Promise<{ledger: Array, pos: Array}>}
  369. * @private
  370. */
  371. async _batchUpdateStagePosData(data) {
  372. const datas = data instanceof Array ? data : [data];
  373. const result = {
  374. ledger: this._.uniq(this._.map(datas, 'lid')),
  375. pos: this._.map(datas, 'pid'),
  376. stageUpdate: true
  377. };
  378. const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
  379. {pid: this._.map(datas, 'pid')});
  380. const bills = await this.ctx.service.ledger.getAllDataByCondition({where: {id: result.ledger}});
  381. for (const b of bills) {
  382. b.precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, b.unit);
  383. }
  384. const updatePosStage = [], insertPosStage = [];
  385. for (const d of datas) {
  386. const b = this._.find(bills, {id: d.lid});
  387. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  388. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  389. const sp = {id: osp.id, pid: osp.pid};
  390. if (d.contract_qty !== undefined) {
  391. sp.contract_qty = this.ctx.helper.round(d.contract_qty, b.precision.value);
  392. }
  393. sp.contract_expr = d.contract_expr;
  394. updatePosStage.push(sp);
  395. } else {
  396. const sp = {
  397. pid: d.pid, lid: d.lid,
  398. tid: this.ctx.tender.id, sid: this.ctx.stage.id,
  399. said: this.ctx.session.sessionUser.accountId,
  400. times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
  401. };
  402. if (d.contract_qty !== undefined || osp) {
  403. sp.contract_qty = d.contract_qty === undefined && osp
  404. ? osp.contract_qty
  405. : this.ctx.helper.round(d.contract_qty, b.precision.value);
  406. }
  407. insertPosStage.push(sp);
  408. }
  409. result.pos.push(d.pid);
  410. }
  411. const updateBillsStage = [], insertBillsStage = [], info = this.ctx.tender.info;
  412. for (const b of bills) {
  413. const stageBills = await this.ctx.service.stageBills.getLastestStageData(b.tender_id, this.ctx.stage.id, b.id);
  414. const posStage = await this.getLastestStageData2(b.tender_id, this.ctx.stage.id, {lid: b.id});
  415. let contract_qty = 0;
  416. const newPosRange = insertPosStage.filter(x => {return x.lid === b.id});
  417. for (const nps of newPosRange) {
  418. contract_qty = this.ctx.helper.add(contract_qty, nps.contract_qty);
  419. }
  420. for (const ps of posStage) {
  421. const ips = this._.find(insertPosStage, {pid: ps.pid});
  422. if (!ips) {
  423. const ups = this._.find(updatePosStage, {id: ps.id});
  424. contract_qty = this.ctx.helper.add(contract_qty, ups ? ups.contract_qty : ps.contract_qty);
  425. }
  426. }
  427. if (stageBills && stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  428. if (contract_qty === stageBills.contract_qty) continue;
  429. updateBillsStage.push({
  430. id: stageBills.id,
  431. contract_qty: contract_qty,
  432. contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
  433. });
  434. } else {
  435. insertBillsStage.push({
  436. tid: this.ctx.tender.id,
  437. lid: b.id,
  438. sid: this.ctx.stage.id,
  439. times: this.ctx.stage.curTimes,
  440. order: this.ctx.stage.curOrder,
  441. said: this.ctx.session.sessionUser.accountId,
  442. contract_qty: contract_qty,
  443. contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
  444. })
  445. }
  446. }
  447. const transaction = await this.db.beginTransaction();
  448. try {
  449. if (updatePosStage.length > 0) {
  450. await transaction.updateRows(this.tableName, updatePosStage);
  451. }
  452. if (insertPosStage.length > 0) {
  453. await transaction.insert(this.tableName, insertPosStage);
  454. }
  455. if (updateBillsStage.length > 0) {
  456. await transaction.updateRows(this.ctx.service.stageBills.tableName, updateBillsStage);
  457. }
  458. if (insertBillsStage.length > 0) {
  459. await transaction.insert(this.ctx.service.stageBills.tableName, insertBillsStage);
  460. }
  461. await transaction.commit();
  462. return result;
  463. } catch (err) {
  464. await transaction.rollback();
  465. throw err;
  466. }
  467. }
  468. /**
  469. * 删除部位明细数据(仅供updateStageData调用)
  470. *
  471. * @param transaction - 事务
  472. * @param data - 删除的部位明细(允许一次提醒多条,也允许跨清单(但前端操作不允许))
  473. * @returns {Promise<{}>}
  474. * @private
  475. */
  476. async _deleteStagePosData(data) {
  477. const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
  478. const result = { pos: data, isDeletePos: true};
  479. if (pos instanceof Array) {
  480. for (const p of pos) {
  481. if (p.add_stage !== this.ctx.stage.id /*|| p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId*/) {
  482. throw '不可删除该数据';
  483. }
  484. if (p.lid !== pos[0].lid) {
  485. throw '提交数据错误';
  486. }
  487. }
  488. } else if (pos.add_stage !== this.ctx.stage.id /*|| pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId*/) {
  489. throw '不可删除该数据';
  490. }
  491. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  492. const billsPos = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: bills.tender_id, lid: bills.id} });
  493. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  494. for (const bp of billsPos) {
  495. if (data.indexOf(bp.id) >= 0) continue;
  496. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  497. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  498. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  499. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  500. }
  501. const info = this.ctx.tender.info;
  502. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  503. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  504. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  505. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  506. const transaction = await this.db.beginTransaction();
  507. try {
  508. // 删除部位明细
  509. await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
  510. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  511. // 删除部位明细计量数据
  512. await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
  513. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, bills.id, transaction);
  514. await transaction.commit();
  515. // 获取需要更新的数据
  516. result.ledger = [bills.id];
  517. result.stageUpdate = true;
  518. return result;
  519. } catch (err) {
  520. await transaction.rollback();
  521. throw err;
  522. }
  523. }
  524. /**
  525. * 根据前端提交数据,更新并计算
  526. *
  527. * @param data
  528. * @returns {Promise<{ledger: {}, pos: {}}>}
  529. */
  530. async updateStageData(data) {
  531. if ((data.updateType === 'add' || data.upateType === 'delete') && this.ctx.tender.measure_type === measureType.tz) {
  532. throw '台账模式下,不可在计量中新增或删除部位明细,如需操作,请进行台账修订';
  533. }
  534. let refreshData;
  535. if (data.updateType === 'add') {
  536. refreshData = await this._addStagePosData(data.updateData);
  537. } else if (data.updateType === 'update') {
  538. refreshData = await this._updateStagePosData(data.updateData);
  539. } else if (data.updateType === 'batchUpdate') {
  540. refreshData = await this._batchUpdateStagePosData(data.updateData);
  541. } else if (data.updateType === 'delete') {
  542. if (!data.updateData || data.updateData.length === 0) {
  543. throw '提交数据错误';
  544. }
  545. refreshData = await this._deleteStagePosData(data.updateData);
  546. } else {
  547. throw '提交数据错误';
  548. }
  549. try {
  550. const result = {ledger: {}, pos: {}};
  551. if (refreshData.ledger && refreshData.ledger.length > 0) {
  552. result.ledger.bills = await this.ctx.service.ledger.getDataByIds(refreshData.ledger);
  553. if (refreshData.stageUpdate) {
  554. result.ledger.curStageData = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
  555. }
  556. }
  557. if (refreshData.pos) {
  558. if (refreshData.isDeletePos) {
  559. result.pos.pos = refreshData.pos;
  560. } else if (refreshData.pos.length > 0) {
  561. result.pos.pos = await this.ctx.service.pos.getPosDataWithAddStageOrder({id: refreshData.pos});
  562. if (refreshData.stageUpdate) {
  563. result.pos.curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
  564. }
  565. }
  566. }
  567. return result;
  568. } catch(err) {
  569. throw '获取数据异常,请刷新页面。';
  570. }
  571. }
  572. async updateChangeQuantity(transaction, pos, qty) {
  573. let orgPos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  574. if (orgPos.length > 1) {
  575. throw '数据错误';
  576. } else {
  577. orgPos = orgPos[0];
  578. }
  579. if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
  580. await transaction.update(this.tableName, {id: orgPos.id, qc_qty: qty});
  581. } else {
  582. await transaction.insert(this.tableName, {
  583. tid: this.ctx.tender.id,
  584. sid: this.ctx.stage.id,
  585. lid: pos.lid,
  586. pid: pos.id,
  587. said: this.ctx.session.sessionUser.accountId,
  588. times: this.ctx.stage.curTimes,
  589. order: this.ctx.stage.curOrder,
  590. contract_qty: orgPos ? orgPos.contract_qty : 0,
  591. qc_qty: qty,
  592. });
  593. }
  594. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
  595. }
  596. /**
  597. * 统计清单下部位明细合计
  598. * @param {Number} tid - 标段id
  599. * @param {Number} sid - 期id
  600. * @param {Number} lid - 清单节点id
  601. * @param transaction - 事务(不为空则在事务中查询,反之在数据库中查询)
  602. * @returns {Promise<*>}
  603. */
  604. async getPosGather(tid, sid, lid, transaction) {
  605. const calcQtySql = 'SELECT SUM(`contract_qty`) As `contract_qty`, SUM(`qc_qty`) As `qc_qty` FROM (' +
  606. ' SELECT `contract_qty`, `qc_qty` FROM ' + this.ctx.service.stagePos.tableName + ' As Pos ' +
  607. ' INNER JOIN (' +
  608. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` ' +
  609. ' FROM ' + this.ctx.service.stagePos.tableName +
  610. ' WHERE `tid` = ? And sid = ? And `lid` = ? ' +
  611. ' GROUP BY `pid`' +
  612. ' ) As MaxFilter ' +
  613. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid ' +
  614. ' WHERE Pos.tid = ? And Pos.sid = ? And Pos.lid = ?' +
  615. ' ) As Gather';
  616. const param = [tid, sid, lid];
  617. const sqlParam = param.concat(param);
  618. if (transaction) {
  619. return await transaction.queryOne(calcQtySql, sqlParam);
  620. } else {
  621. return await this.db.queryOne(calcQtySql, sqlParam);
  622. }
  623. }
  624. /**
  625. * 多期清单数据整合 (材料调差调用)
  626. * @param {Number} tid - 标段id
  627. * @param {String} stage_id_list - 期id列表
  628. * @returns {Promise<void>}
  629. */
  630. async getStagesData(tid, stage_id_list) {
  631. const sids = stage_id_list.split(',');
  632. const result = [];
  633. for (const sid of sids) {
  634. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, postil, `times`, `order`, `contract_expr`' +
  635. ' FROM ' + this.tableName +
  636. ' WHERE tid = ? And sid = ? ';
  637. const sqlParam = [tid, sid];
  638. const stagePos = await this.db.query(sql, sqlParam);
  639. const stagePosFilter = this._filterLastestData(stagePos);
  640. for (const sp of stagePosFilter) {
  641. const rsp = result.find(function (x) { return x.pid === sp.pid});
  642. if (rsp) {
  643. rsp.contract_qty = this.ctx.helper.add(rsp.contract_qty, sp.contract_qty);
  644. rsp.qc_qty = this.ctx.helper.add(rsp.qc_qty, sp.qc_qty);
  645. } else {
  646. result.push({
  647. id: sp.id, tid: sp.tid, lid: sp.lid, pid: sp.pid,
  648. contract_qty: sp.contract_qty, qc_qty: sp.qc_qty,
  649. });
  650. }
  651. }
  652. }
  653. return result;
  654. }
  655. /**
  656. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  657. * @param {Number} tid - 标段id
  658. * @param {String} stage_id_list - 期id列表
  659. * @param {String} lid - 台账id
  660. * @param {String} pid - 部位id
  661. * @returns {Promise<void>}
  662. */
  663. async getGatherQtyByMaterial(tid, stage_id_list, lid, pid) {
  664. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  665. let gather_qty = 0;
  666. for (const sid of stage_id_list) {
  667. const sql = 'SELECT Pos.contract_qty, Pos.qc_qty FROM ' +
  668. ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND sid = ?) As Pos ' +
  669. ' INNER JOIN ( ' +
  670. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
  671. ' WHERE `tid` = ? AND sid = ?' +
  672. ' GROUP BY `pid`' +
  673. ' ) As MaxFilter ' +
  674. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid' +
  675. ' WHERE Pos.lid = ? AND Pos.pid = ?';
  676. const sqlParam = [tid, sid, tid, sid, lid, pid];
  677. const result = await this.db.queryOne(sql, sqlParam);
  678. if (result) {
  679. gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  680. }
  681. }
  682. return gather_qty !== 0 ? gather_qty : null;
  683. }
  684. }
  685. return StagePos;
  686. };