stage_pos.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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.depart = 20;
  22. this.tableName = 'stage_pos';
  23. this.qtyFields = ['contract_qty', 'qc_qty']
  24. }
  25. _getPosFilterSql(where, asTable = '') {
  26. let whereSql = '';
  27. if (!where) return whereSql;
  28. if (where.pid) {
  29. if (where.pid instanceof Array) {
  30. whereSql += ' And ' + asTable + 'pid in (' + this.ctx.helper.getInArrStrSqlFilter(where.pid) + ')';
  31. } else if (typeof where.pid === "string") {
  32. whereSql += ' And ' + asTable + 'pid = ' + this.db.escape(where.pid);
  33. }
  34. }
  35. if (where.lid) {
  36. if (where.lid instanceof Array) {
  37. whereSql += ' And ' + asTable + 'lid in (' + this.ctx.helper.getInArrStrSqlFilter(where.lid) + ')';
  38. } else if (typeof where.lid === "string") {
  39. whereSql += ' And ' + asTable + 'lid = ' + this.db.escape(where.lid);
  40. }
  41. }
  42. return whereSql;
  43. }
  44. /**
  45. * 查询期计量最后审核人数据
  46. * @param {Number} tid - 标段id
  47. * @param {Number} sid - 期id
  48. * @param {Number|Array} pid - 部位明细id(可以为空)
  49. * @returns {Promise<*>}
  50. */
  51. // async getLastestStageData(tid, sid, where) {
  52. // const filterSql = this._getPosFilterSql(where);
  53. // 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 ' +
  54. // ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
  55. // ' INNER JOIN ( ' +
  56. // ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
  57. // ' WHERE `tid` = ? And sid = ?' + filterSql +
  58. // ' GROUP BY `pid`' +
  59. // ' ) As MaxFilter ' +
  60. // ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
  61. // const sqlParam = [tid, sid, tid, sid];
  62. // return await this.db.query(sql, sqlParam);
  63. // }
  64. /**
  65. * 查询 某期 某轮审批 某审核人数据
  66. * @param {Number} tid - 标段id
  67. * @param {Number} sid - 期id
  68. * @param {Number} times - 期第几轮审批
  69. * @param {Number} order - 审核人顺序
  70. * @param {Number|Array|Null} pid - 部位明细id - 为空则查询全部
  71. * @returns {Promise<*>}
  72. */
  73. // async getAuditorStageData(tid, sid, times, order, where) {
  74. // const filterSql = this._getPosFilterSql(where);
  75. // 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 ' +
  76. // ' (SELECT * FROM '+ this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
  77. // ' INNER JOIN ( ' +
  78. // ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid`, `sid` From ' + this.tableName +
  79. // ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) And tid = ? And sid = ?' + filterSql +
  80. // ' GROUP BY `pid`' +
  81. // ' ) As MaxFilter ' +
  82. // ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
  83. // const sqlParam = [tid, sid, times, times, order, tid, sid];
  84. // return await this.db.query(sql, sqlParam);
  85. // }
  86. _filterLastestData(stagePos) {
  87. const stagePosIndex = {};
  88. for (const sp of stagePos) {
  89. const key = 'sp-' + sp.pid;
  90. const spi = stagePosIndex[key];
  91. if (spi) {
  92. if ((spi.times * timesLen + spi.order) < (sp.times * timesLen + sp.order)) stagePosIndex[key] = sp;
  93. } else {
  94. stagePosIndex[key] = sp;
  95. }
  96. }
  97. const result = [];
  98. for (const prop in stagePosIndex) {
  99. result.push(stagePosIndex[prop]);
  100. }
  101. return result;
  102. }
  103. async getLastestStageData2(tid, sid, where) {
  104. const filterSql = this._getPosFilterSql(where);
  105. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, postil, `times`, `order`, `contract_expr`' +
  106. ' FROM ' + this.departTableName(tid) +
  107. ' WHERE tid = ? And sid = ? ' + filterSql;
  108. const sqlParam = [tid, sid];
  109. const stagePos = await this.db.query(sql, sqlParam);
  110. return this._filterLastestData(stagePos);
  111. }
  112. async getAuditorStageData2(tid, sid, times, order, where) {
  113. const filterSql = this._getPosFilterSql(where);
  114. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, postil, `times`, `order`, `contract_expr`' +
  115. ' FROM ' + this.departTableName(tid) +
  116. ' WHERE tid = ? And sid = ? And (`times` < ? OR (`times` = ? AND `order` <= ?)) ' + filterSql;
  117. const sqlParam = [tid, sid, times, times, order];
  118. const stagePos = await this.db.query(sql, sqlParam);
  119. return this._filterLastestData(stagePos);
  120. }
  121. async getStageUsedPos(tid, sid, where) {
  122. const self = this;
  123. const stagePos = await this.getLastestStageData2(tid, sid, where);
  124. const pids = this._.map(stagePos, function (sp) {
  125. if (!self.ctx.helper.checkZero(sp.contract_qty) || !self.ctx.helper.checkZero(sp.qc_qty)) {
  126. return sp.pid;
  127. } else {
  128. return -1;
  129. }
  130. });
  131. return this._.pull(pids, -1);
  132. }
  133. /**
  134. * 新增部位明细数据(仅供updateStageData调用)
  135. *
  136. * @param transaction - 事务
  137. * @param data - 新增数据
  138. * @returns {Promise<{}>}
  139. * @private
  140. */
  141. async _addStagePosData(data) {
  142. let bills , precision, updateBills = null;
  143. const result = {pos: [], ledger: []};
  144. const datas = data instanceof Array ? data : [data], calcStageBills = [];
  145. if (datas[0].sgfh_qty !== undefined || datas[0].sjcl_qty !== undefined || datas[0].qtcl_qty !== undefined
  146. || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].real_qty !== undefined) {
  147. bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  148. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  149. result.ledger.push(bills.id);
  150. }
  151. if (datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].postil !== undefined) {
  152. result.stageUpdate = true;
  153. }
  154. const insertPos = [], insertPosStage = [];
  155. for (const d of datas) {
  156. const p = {
  157. id: this.uuid.v4(), tid: this.ctx.tender.id, lid: d.lid, name: d.name, porder: d.porder, position: d.position,
  158. add_stage: this.ctx.stage.id, add_stage_order: this.ctx.stage.order,
  159. add_times: this.ctx.stage.curTimes,
  160. add_user: this.ctx.session.sessionUser.accountId,
  161. };
  162. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  163. if (d.sgfh_qty!== undefined) p.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  164. if (d.sjcl_qty!== undefined) p.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  165. if (d.qtcl_qty!== undefined) p.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  166. if (d.sgfh_expr !== undefined) p.sgfh_expr = d.sgfh_expr;
  167. if (d.sjcl_expr !== undefined) p.sjcl_expr = d.sjcl_expr;
  168. if (d.qtcl_expr !== undefined) p.qtcl_expr = d.qtcl_expr;
  169. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  170. if (!updateBills)
  171. updateBills = {id: bills.id, sgfh_qty: bills.sgfh_qty, sjcl_qty: bills.sjcl_qty, qtcl_qty: bills.qtcl_qty};
  172. }
  173. if (d.real_qty!== undefined) p.real_qty = this.round(d.real_qty, precision.value);
  174. insertPos.push(p);
  175. result.pos.push(p.id);
  176. if (d.contract_qty!== undefined || d.qc_qty!== undefined || d.postil!== undefined) {
  177. const ps = {
  178. pid: p.id,
  179. lid: d.lid,
  180. tid: this.ctx.tender.id,
  181. sid: this.ctx.stage.id,
  182. said: this.ctx.session.sessionUser.accountId,
  183. times: this.ctx.stage.curTimes,
  184. order: this.ctx.stage.curOrder,
  185. };
  186. if (d.contract_qty !== undefined) ps.contract_qty = this.round(d.contract_qty, precision.value);
  187. if (d.contract_expr !== undefined) p.contract_expr = d.contract_expr;
  188. if (d.qc_qty!== undefined) ps.qc_qty = this.round(d.qc_qty, precision.value);
  189. if (d.postil!== undefined) ps.postil = d.postil;
  190. insertPosStage.push(ps);
  191. if ((d.contract_qty || d.qc_qty) && calcStageBills.indexOf(ps.lid) === -1) {
  192. calcStageBills.push(ps.lid);
  193. }
  194. }
  195. }
  196. if (updateBills) {
  197. for (const d of insertPos) {
  198. if (d.sgfh_qty) {
  199. d.sgfh_qty = this.ctx.helper.round(d.sgfh_qty, precision.value);
  200. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  201. }
  202. if (d.sjcl_qty) {
  203. d.sjcl_qty = this.ctx.helper.round(d.sjcl_qty, precision.value);
  204. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  205. }
  206. if (d.qtcl_qty) {
  207. d.qtcl_qty = this.ctx.helper.round(d.qtcl_qty, precision.value);
  208. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  209. }
  210. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  211. }
  212. updateBills.quantity = this.ctx.helper.sum([updateBills.sgfh_qty, updateBills.qtcl_qty, updateBills.sjcl_qty]);
  213. const info = this.ctx.tender.info;
  214. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  215. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  216. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  217. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  218. }
  219. const transaction = await this.db.beginTransaction();
  220. try {
  221. if (insertPos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, insertPos);
  222. if (updateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  223. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  224. for (const lid of calcStageBills) {
  225. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  226. }
  227. await transaction.commit();
  228. return result;
  229. } catch(err) {
  230. await transaction.rollback();
  231. throw err;
  232. }
  233. }
  234. /**
  235. * 更新部位明细数据(仅供updateStageData调用)
  236. *
  237. * @param transaction - 事务
  238. * @param data - 更新数据(允许一次性提交多条)
  239. * @returns {Promise<{ledger: Array, pos: Array}>}
  240. * @private
  241. */
  242. async _updateStagePosData(data) {
  243. let bills, precision, updateBills = null;
  244. const datas = data instanceof Array ? data : [data];
  245. const orgPos = await this.ctx.service.pos.getPosDataByIds(this.ctx.tender.id, this._.map(datas, 'pid'));
  246. const result = {ledger: [], pos: [], stageUpdate: true};
  247. const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
  248. {pid: this._.map(datas, 'pid')});
  249. if (datas[0].sgfh_qty !== undefined || datas[0].qtcl_qty !== undefined || datas[0].sjcl_qty !== undefined
  250. || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].real_qty !== undefined) {
  251. bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  252. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  253. result.ledger.push(bills.id);
  254. }
  255. const updatePos = [], updatePosStage = [], insertPosStage = [];
  256. for (const d of datas) {
  257. if (d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
  258. || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined
  259. || d.real_qty !== undefined
  260. || d.ex_memo1 !== undefined || d.ex_memo2 !== undefined || d.ex_memo3 !== undefined
  261. ) {
  262. const op = this._.find(orgPos, {id: d.pid});
  263. if (op.add_stage !== this.ctx.stage.id && (
  264. d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
  265. || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined
  266. )) throw '不可修改数据';
  267. const p = {id: op.id};
  268. if (d.name !== undefined) p.name = d.name;
  269. if (d.position !== undefined) p.position = d.position;
  270. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  271. p.sgfh_qty = d.sgfh_qty !== undefined ? d.sgfh_qty : op.sgfh_qty;
  272. p.sjcl_qty = d.sjcl_qty !== undefined ? d.sjcl_qty : op.sjcl_qty;
  273. p.qtcl_qty = d.qtcl_qty !== undefined ? d.qtcl_qty : op.qtcl_qty;
  274. if (d.sgfh_expr !== undefined) p.sgfh_expr = d.sgfh_expr;
  275. if (d.sjcl_expr !== undefined) p.sjcl_expr = d.sjcl_expr;
  276. if (d.qtcl_expr !== undefined) p.qtcl_expr = d.qtcl_expr;
  277. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  278. if (!updateBills) updateBills = {id: bills.id};
  279. }
  280. if (d.drawing_code !== undefined) p.drawing_code = d.drawing_code;
  281. if (d.real_qty !== undefined) p.real_qty = this.ctx.helper.round(d.real_qty, precision.value);
  282. if (d.ex_memo1 !== undefined) p.ex_memo1 = d.ex_memo1;
  283. if (d.ex_memo2 !== undefined) p.ex_memo2 = d.ex_memo2;
  284. if (d.ex_memo3 !== undefined) p.ex_memo3 = d.ex_memo3;
  285. updatePos.push(p);
  286. }
  287. if (d.contract_qty !== undefined || d.qc_qty !== undefined || d.postil !== undefined) {
  288. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  289. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  290. const sp = {id: osp.id};
  291. if (d.contract_qty !== undefined) {
  292. sp.contract_qty = this.ctx.helper.round(d.contract_qty, precision.value);
  293. }
  294. if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
  295. if (d.qc_qty !== undefined) {
  296. sp.qc_qty = this.ctx.helper.round(d.qc_qty, precision.value);
  297. }
  298. if (d.postil !== undefined) {
  299. sp.postil = d.postil;
  300. }
  301. updatePosStage.push(sp);
  302. } else {
  303. const sp = {
  304. pid: d.pid, lid: d.lid,
  305. tid: this.ctx.tender.id, sid: this.ctx.stage.id,
  306. said: this.ctx.session.sessionUser.accountId,
  307. times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
  308. };
  309. if (d.contract_qty !== undefined || osp) {
  310. sp.contract_qty = d.contract_qty === undefined && osp
  311. ? osp.contract_qty
  312. : this.ctx.helper.round(d.contract_qty, precision.value);
  313. }
  314. if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
  315. if (d.qc_qty || osp) {
  316. sp.qc_qty = d.qc_qty === undefined && osp
  317. ? osp.qc_qty
  318. : this.ctx.helper.round(d.qc_qty, precision.value);
  319. }
  320. if (d.postil || osp) {
  321. sp.postil = d.postil === undefined && osp ? osp.postil : d.postil;
  322. }
  323. insertPosStage.push(sp);
  324. }
  325. }
  326. result.pos.push(d.pid);
  327. }
  328. if (updateBills) {
  329. const billsPos = await this.ctx.service.pos.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  330. for (const bp of billsPos) {
  331. const newPos = updatePos.find(function (x) { return x.id === bp.id });
  332. const calcData = newPos ? newPos : bp;
  333. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  334. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  335. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  336. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  337. }
  338. const info = this.ctx.tender.info;
  339. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  340. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  341. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  342. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  343. }
  344. const transaction = await this.db.beginTransaction();
  345. try {
  346. if (updatePos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, updatePos);
  347. if (updateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  348. if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
  349. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  350. if (bills) await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, bills.id, transaction);
  351. await transaction.commit();
  352. return result;
  353. } catch (err) {
  354. await transaction.rollback();
  355. throw err;
  356. }
  357. }
  358. /**
  359. * 更新部位明细数据(仅供updateStageData调用)
  360. *
  361. * @param transaction - 事务
  362. * @param data - 更新数据(允许一次性提交多条)
  363. * @returns {Promise<{ledger: Array, pos: Array}>}
  364. * @private
  365. */
  366. async _batchUpdateStagePosData(data) {
  367. const datas = data instanceof Array ? data : [data];
  368. const result = {
  369. ledger: this._.uniq(this._.map(datas, 'lid')),
  370. pos: this._.map(datas, 'pid'),
  371. stageUpdate: true
  372. };
  373. const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
  374. {pid: this._.map(datas, 'pid')});
  375. const bills = await this.ctx.service.ledger.getAllDataByCondition({where: {id: result.ledger}});
  376. for (const b of bills) {
  377. b.precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, b.unit);
  378. }
  379. const updatePosStage = [], insertPosStage = [];
  380. for (const d of datas) {
  381. const b = this._.find(bills, {id: d.lid});
  382. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  383. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  384. const sp = {id: osp.id, pid: osp.pid};
  385. if (d.contract_qty !== undefined) {
  386. sp.contract_qty = this.ctx.helper.round(d.contract_qty, b.precision.value);
  387. }
  388. sp.contract_expr = d.contract_expr;
  389. updatePosStage.push(sp);
  390. } else {
  391. const sp = {
  392. pid: d.pid, lid: d.lid,
  393. tid: this.ctx.tender.id, sid: this.ctx.stage.id,
  394. said: this.ctx.session.sessionUser.accountId,
  395. times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
  396. };
  397. if (d.contract_qty !== undefined || osp) {
  398. sp.contract_qty = d.contract_qty === undefined && osp
  399. ? osp.contract_qty
  400. : this.ctx.helper.round(d.contract_qty, b.precision.value);
  401. sp.contract_expr = d.contract_expr;
  402. }
  403. insertPosStage.push(sp);
  404. }
  405. result.pos.push(d.pid);
  406. }
  407. const updateBillsStage = [], insertBillsStage = [], info = this.ctx.tender.info;
  408. for (const b of bills) {
  409. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(b.tender_id, this.ctx.stage.id, b.id);
  410. const posStage = await this.getLastestStageData2(b.tender_id, this.ctx.stage.id, {lid: b.id});
  411. let contract_qty = 0;
  412. const newPosRange = insertPosStage.filter(x => {return x.lid === b.id});
  413. for (const nps of newPosRange) {
  414. contract_qty = this.ctx.helper.add(contract_qty, nps.contract_qty);
  415. }
  416. for (const ps of posStage) {
  417. const ips = this._.find(insertPosStage, {pid: ps.pid});
  418. if (!ips) {
  419. const ups = this._.find(updatePosStage, {id: ps.id});
  420. contract_qty = this.ctx.helper.add(contract_qty, ups ? ups.contract_qty : ps.contract_qty);
  421. }
  422. }
  423. if (stageBills && stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  424. if (contract_qty === stageBills.contract_qty) continue;
  425. updateBillsStage.push({
  426. id: stageBills.id,
  427. contract_qty: contract_qty,
  428. contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
  429. });
  430. } else {
  431. insertBillsStage.push({
  432. tid: this.ctx.tender.id,
  433. lid: b.id,
  434. sid: this.ctx.stage.id,
  435. times: this.ctx.stage.curTimes,
  436. order: this.ctx.stage.curOrder,
  437. said: this.ctx.session.sessionUser.accountId,
  438. contract_qty: contract_qty,
  439. contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
  440. })
  441. }
  442. }
  443. const transaction = await this.db.beginTransaction();
  444. try {
  445. if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
  446. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  447. if (updateBillsStage.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateBillsStage);
  448. if (insertBillsStage.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertBillsStage);
  449. await transaction.commit();
  450. return result;
  451. } catch (err) {
  452. await transaction.rollback();
  453. throw err;
  454. }
  455. }
  456. /**
  457. * 删除部位明细数据(仅供updateStageData调用)
  458. *
  459. * @param transaction - 事务
  460. * @param data - 删除的部位明细(允许一次提醒多条,也允许跨清单(但前端操作不允许))
  461. * @returns {Promise<{}>}
  462. * @private
  463. */
  464. async _deleteStagePosData(data) {
  465. const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
  466. const result = { pos: data, isDeletePos: true};
  467. if (pos instanceof Array) {
  468. for (const p of pos) {
  469. if (p.add_stage !== this.ctx.stage.id /*|| p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId*/) {
  470. throw '不可删除该数据';
  471. }
  472. if (p.lid !== pos[0].lid) {
  473. throw '提交数据错误';
  474. }
  475. }
  476. } else if (pos.add_stage !== this.ctx.stage.id /*|| pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId*/) {
  477. throw '不可删除该数据';
  478. }
  479. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  480. const billsPos = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: bills.tender_id, lid: bills.id} });
  481. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  482. for (const bp of billsPos) {
  483. if (data.indexOf(bp.id) >= 0) continue;
  484. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  485. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  486. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  487. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  488. }
  489. const info = this.ctx.tender.info;
  490. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  491. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  492. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  493. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  494. const transaction = await this.db.beginTransaction();
  495. try {
  496. // 删除部位明细
  497. await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
  498. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  499. // 删除部位明细计量数据
  500. await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
  501. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, bills.id, transaction);
  502. await transaction.commit();
  503. // 获取需要更新的数据
  504. result.ledger = [bills.id];
  505. result.stageUpdate = true;
  506. return result;
  507. } catch (err) {
  508. await transaction.rollback();
  509. throw err;
  510. }
  511. }
  512. /**
  513. * 根据前端提交数据,更新并计算
  514. *
  515. * @param data
  516. * @returns {Promise<{ledger: {}, pos: {}}>}
  517. */
  518. async updateStageData(data) {
  519. if ((data.updateType === 'add' || data.upateType === 'delete') && this.ctx.tender.measure_type === measureType.tz) {
  520. throw '台账模式下,不可在计量中新增或删除部位明细,如需操作,请进行台账修订';
  521. }
  522. let refreshData;
  523. if (data.updateType === 'add') {
  524. refreshData = await this._addStagePosData(data.updateData);
  525. } else if (data.updateType === 'update') {
  526. refreshData = await this._updateStagePosData(data.updateData);
  527. } else if (data.updateType === 'batchUpdate') {
  528. refreshData = await this._batchUpdateStagePosData(data.updateData);
  529. } else if (data.updateType === 'delete') {
  530. if (!data.updateData || data.updateData.length === 0) {
  531. throw '提交数据错误';
  532. }
  533. refreshData = await this._deleteStagePosData(data.updateData);
  534. } else {
  535. throw '提交数据错误';
  536. }
  537. try {
  538. const result = {ledger: {}, pos: {}};
  539. if (refreshData.ledger && refreshData.ledger.length > 0) {
  540. result.ledger.bills = await this.ctx.service.ledger.getDataByIds(refreshData.ledger);
  541. if (refreshData.stageUpdate) {
  542. result.ledger.curStageData = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
  543. }
  544. }
  545. if (refreshData.pos) {
  546. if (refreshData.isDeletePos) {
  547. result.pos.pos = refreshData.pos;
  548. } else if (refreshData.pos.length > 0) {
  549. result.pos.pos = await this.ctx.service.pos.getPosDataWithAddStageOrder({tid: this.ctx.tender.id, id: refreshData.pos});
  550. if (refreshData.stageUpdate) {
  551. result.pos.curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
  552. }
  553. }
  554. }
  555. return result;
  556. } catch(err) {
  557. throw '获取数据异常,请刷新页面。';
  558. }
  559. }
  560. async updateChangeQuantity(transaction, pos, qty) {
  561. let orgPos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  562. if (orgPos.length > 1) {
  563. throw '数据错误';
  564. } else {
  565. orgPos = orgPos[0];
  566. }
  567. if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
  568. await transaction.update(this.tableName, {id: orgPos.id, qc_qty: qty});
  569. } else {
  570. await transaction.insert(this.tableName, {
  571. tid: this.ctx.tender.id,
  572. sid: this.ctx.stage.id,
  573. lid: pos.lid,
  574. pid: pos.id,
  575. said: this.ctx.session.sessionUser.accountId,
  576. times: this.ctx.stage.curTimes,
  577. order: this.ctx.stage.curOrder,
  578. contract_qty: orgPos ? orgPos.contract_qty : 0,
  579. qc_qty: qty,
  580. });
  581. }
  582. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
  583. }
  584. /**
  585. * 统计清单下部位明细合计
  586. * @param {Number} tid - 标段id
  587. * @param {Number} sid - 期id
  588. * @param {Number} lid - 清单节点id
  589. * @param transaction - 事务(不为空则在事务中查询,反之在数据库中查询)
  590. * @returns {Promise<*>}
  591. */
  592. async getPosGather(tid, sid, lid, transaction) {
  593. const calcQtySql = 'SELECT SUM(`contract_qty`) As `contract_qty`, SUM(`qc_qty`) As `qc_qty` FROM (' +
  594. ' SELECT `contract_qty`, `qc_qty` FROM ' + this.ctx.service.stagePos.tableName + ' As Pos ' +
  595. ' INNER JOIN (' +
  596. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` ' +
  597. ' FROM ' + this.ctx.service.stagePos.tableName +
  598. ' WHERE `tid` = ? And sid = ? And `lid` = ? ' +
  599. ' GROUP BY `pid`' +
  600. ' ) As MaxFilter ' +
  601. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid ' +
  602. ' WHERE Pos.tid = ? And Pos.sid = ? And Pos.lid = ?' +
  603. ' ) As Gather';
  604. const param = [tid, sid, lid];
  605. const sqlParam = param.concat(param);
  606. if (transaction) {
  607. return await transaction.queryOne(calcQtySql, sqlParam);
  608. } else {
  609. return await this.db.queryOne(calcQtySql, sqlParam);
  610. }
  611. }
  612. /**
  613. * 多期清单数据整合 (材料调差调用)
  614. * @param {Number} tid - 标段id
  615. * @param {String} stage_id_list - 期id列表
  616. * @param {String} comefrom - 来源(部分不调用计量不获取)
  617. * @returns {Promise<void>}
  618. */
  619. async getStagesData(tid, stage_id_list, comefrom = '') {
  620. const sids = stage_id_list.split(',');
  621. const result = [];
  622. for (const sid of sids) {
  623. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, postil, `times`, `order`, `contract_expr`' +
  624. ' FROM ' + this.tableName +
  625. ' WHERE tid = ? And sid = ? ';
  626. const sqlParam = [tid, sid];
  627. const stagePos = await this.db.query(sql, sqlParam);
  628. const stagePosFilter = this._filterLastestData(stagePos);
  629. for (const sp of stagePosFilter) {
  630. const rsp = result.find(function (x) { return x.pid === sp.pid});
  631. if (rsp) {
  632. rsp.contract_qty = this.ctx.helper.add(rsp.contract_qty, sp.contract_qty);
  633. rsp.qc_qty = this.ctx.helper.add(rsp.qc_qty, sp.qc_qty);
  634. } else if (!comefrom || (comefrom === 'list' && (sp.contract_qty || sp.qc_qty))) {
  635. result.push({
  636. id: sp.id, tid: sp.tid, lid: sp.lid, pid: sp.pid,
  637. contract_qty: sp.contract_qty, qc_qty: sp.qc_qty,
  638. });
  639. }
  640. }
  641. }
  642. return result;
  643. }
  644. /**
  645. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  646. * @param {Number} tid - 标段id
  647. * @param {String} stage_id_list - 期id列表
  648. * @param {String} lid - 台账id
  649. * @param {String} pid - 部位id
  650. * @returns {Promise<void>}
  651. */
  652. async getGatherQtyByMaterial(tid, stage_id_list, lid, pid) {
  653. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  654. let gather_qty = 0;
  655. for (const sid of stage_id_list) {
  656. const sql = 'SELECT Pos.contract_qty, Pos.qc_qty FROM ' +
  657. ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND sid = ?) As Pos ' +
  658. ' INNER JOIN ( ' +
  659. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
  660. ' WHERE `tid` = ? AND sid = ?' +
  661. ' GROUP BY `pid`' +
  662. ' ) As MaxFilter ' +
  663. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid' +
  664. ' WHERE Pos.lid = ? AND Pos.pid = ?';
  665. const sqlParam = [tid, sid, tid, sid, lid, pid];
  666. const result = await this.db.queryOne(sql, sqlParam);
  667. if (result) {
  668. gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  669. }
  670. }
  671. return gather_qty !== 0 ? gather_qty : null;
  672. }
  673. }
  674. return StagePos;
  675. };