stage_pos.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.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.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. /**
  86. * 新增部位明细数据(仅供updateStageData调用)
  87. *
  88. * @param transaction - 事务
  89. * @param data - 新增数据
  90. * @returns {Promise<{}>}
  91. * @private
  92. */
  93. async _addStagePosData(transaction, data) {
  94. let bills , precision;
  95. const result = {pos: [], ledger: []};
  96. const datas = data instanceof Array ? data : [data], calcBills = [], calcStageBills = [];
  97. for (const d of datas) {
  98. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  99. if (!bills || bills.id !== data.lid) {
  100. bills = await this.ctx.service.ledger.getDataById(d.lid);
  101. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  102. }
  103. }
  104. // 在主表pos中新增数据
  105. const p = {
  106. id: this.uuid.v4(), tid: this.ctx.tender.id, lid: d.lid, name: d.name,
  107. add_stage: this.ctx.stage.id,
  108. add_times: this.ctx.stage.curTimes,
  109. add_user: this.ctx.session.sessionUser.accountId,
  110. };
  111. if (d.sgfh_qty) p.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  112. if (d.sjcl_qty) p.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  113. if (d.qtcl_qty) p.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  114. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  115. const addRst = await transaction.insert(this.ctx.service.pos.tableName, p);
  116. result.pos.push(p.id);
  117. // 如果存在复核数据,更新计算主表清单
  118. if (p.sgfh_qty || p.sjcl_qty || p.qtcl_qty) {
  119. calcBills.push(p.lid);
  120. result.ledger.push(p.lid);
  121. }
  122. // 如果存在本期计算数据,更新计算清单本期计量数据
  123. if (d.contract_qty || d.qc_qty || d.postil) {
  124. const ps = {
  125. pid: d.id,
  126. lid: d.lid,
  127. tid: this.ctx.tender.id,
  128. sid: this.ctx.stage.id,
  129. said: this.ctx.session.sessionUser.accountId,
  130. times: this.ctx.stage.curTimes,
  131. order: this.ctx.stage.curOrder,
  132. };
  133. if (d.contract_qty) ps.contract_qty = this.round(d.contract_qty, precision.value);
  134. if (d.qc_qty) ps.qc_qty = this.round(d.qc_qty, precision.value);
  135. if (d.postil) ps.postil = d.postil;
  136. await transaction.insert(ps);
  137. if (d.contract_qty || d.qc_qty) {
  138. calcStageBills.push(ps.lid);
  139. }
  140. result.stageUpdate = true;
  141. }
  142. }
  143. for (const lid of calcBills) {
  144. await this.ctx.service.ledger.calc(this.ctx.tender.id, lid, transaction);
  145. }
  146. for (const lid of calcStageBills) {
  147. await this.ctx.service.stageBills.calc(ctx.tender.id, ctx.stage.id, lid, transaction);
  148. }
  149. return result;
  150. }
  151. /**
  152. * 更新部位明细数据(仅供updateStageData调用)
  153. *
  154. * @param transaction - 事务
  155. * @param data - 更新数据(允许一次性提交多条)
  156. * @returns {Promise<{ledger: Array, pos: Array}>}
  157. * @private
  158. */
  159. async _updateStagePosData(transaction, data) {
  160. let bills, precision;
  161. const result = {ledger: [], pos: [], stageUpdate: true}, ledgerCalc = [];
  162. const datas = data instanceof Array ? data : [data];
  163. const orgPos = await this.ctx.service.pos.getPosDataByIds(this._.map(datas, 'pid'));
  164. const orgStagePos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: this._.map(datas, 'pid')});
  165. for (const d of datas) {
  166. if (d.sgfh_qty || d.qtcl_qty || d.sjcl_qty || d.contract_qty || d.qc_qty) {
  167. if (!bills || bills.id !== data.lid) {
  168. bills = await this.ctx.service.ledger.getDataById(d.lid);
  169. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  170. }
  171. }
  172. if (d.name !== undefined || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  173. const p = {id: d.pid};
  174. if (d.name !== undefined) p.name = d.name;
  175. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  176. const op = this._.find(orgPos, {id: d.pid});
  177. p.sgfh_qty = d.sgfh_qty !== undefined ? d.sgfh_qty : op.sgfh_qty;
  178. p.sjcl_qty = d.sjcl_qty !== undefined ? d.sjcl_qty : op.sjcl_qty;
  179. p.qtcl_qty = d.qtcl_qty !== undefined ? d.qtcl_qty : op.qtcl_qty;
  180. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  181. if (ledgerCalc.indexOf(op.lid) === -1) {
  182. ledgerCalc.push(op.lid);
  183. }
  184. }
  185. await transaction.update(this.ctx.service.pos.tableName, p);
  186. }
  187. if (d.contract_qty !== undefined || d.qc_qty !== undefined || d.postil !== undefined) {
  188. const sp = {pid: d.pid, lid: d.lid, contract_qty: d.contract_qty, qc_qty: d.qc_qty, postil: d.postil};
  189. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  190. if (precision) {
  191. this.ctx.helper.checkFieldPrecision(sp, this.qtyFields, precision.value);
  192. }
  193. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  194. await transaction.update(this.tableName, d, {where: {id: osp.id}});
  195. } else {
  196. sp.tid = this.ctx.tender.id;
  197. sp.sid = this.ctx.stage.id;
  198. sp.said = this.ctx.session.sessionUser.accountId;
  199. sp.times = this.ctx.stage.curTimes;
  200. sp.order = this.ctx.stage.curOrder;
  201. await transaction.insert(this.tableName, sp);
  202. }
  203. }
  204. result.pos.push(d.pid);
  205. if ((d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined ||
  206. d.contract_qty === undefined || d.qc_qty === undefined) && (result.ledger.indexOf(d.lid) === -1)) {
  207. result.ledger.push(d.lid);
  208. }
  209. }
  210. for (const lid of ledgerCalc) {
  211. await this.ctx.service.ledger.calc(this.ctx.tender.id, lid, transaction);
  212. }
  213. for (const lid of result.ledger) {
  214. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  215. }
  216. return result;
  217. }
  218. /**
  219. * 删除部位明细数据(仅供updateStageData调用)
  220. *
  221. * @param transaction - 事务
  222. * @param data - 删除的部位明细(允许一次提醒多条,也允许跨清单(但前端操作不允许))
  223. * @returns {Promise<{}>}
  224. * @private
  225. */
  226. async _deleteStagePosData(transaction, data) {
  227. const result = {};
  228. const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
  229. if (pos instanceof Array) {
  230. for (const p of pos) {
  231. if (p.add_stage !== this.ctx.stage.id || p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId) {
  232. throw '您无权删除该数据';
  233. }
  234. }
  235. } else if (pos.add_stage !== this.ctx.stage.id || pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId) {
  236. throw '您无权删除该数据';
  237. }
  238. const ledgerIds = this._.map(pos, 'lid');
  239. // 删除部位明细
  240. await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
  241. for (const lid of ledgerIds) {
  242. await this.ctx.service.ledger.calc(tid, lid, transaction);
  243. }
  244. // 删除部位明细计量数据
  245. await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
  246. for (const lid of ledgerIds) {
  247. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
  248. }
  249. // 获取需要更新的数据
  250. result.ledger = ledgerIds;
  251. result.stageUpdate = true;
  252. return result;
  253. }
  254. /**
  255. * 根据前端提交数据,更新并计算
  256. *
  257. * @param data
  258. * @returns {Promise<{ledger: {}, pos: {}}>}
  259. */
  260. async updateStageData(data) {
  261. let refreshData;
  262. const transaction = await this.db.beginTransaction();
  263. try {
  264. if ((data.updateType === 'add' || data.upateType === 'delete') && this.ctx.tender.measure_type === measureType.tz) {
  265. throw '台账模式下,不可在计量中新增或删除部位明细,如需操作,请进行台账修订';
  266. }
  267. if (data.updateType === 'add') {
  268. refreshData = await this._addStagePosData(transaction, data.updateData);
  269. } else if (data.updateType === 'update') {
  270. refreshData = await this._updateStagePosData(transaction, data.updateData);
  271. } else if (data.updateType === 'delete') {
  272. if (!data.updateData || data.updateData.length === 0) {
  273. throw '提交数据错误';
  274. }
  275. refreshData = await this._deleteStagePosData(transaction, data.updateData);
  276. } else {
  277. throw '提交数据错误';
  278. }
  279. await transaction.commit();
  280. } catch (err) {
  281. await transaction.rollback();
  282. throw err;
  283. }
  284. try {
  285. const result = {ledger: {}, pos: {}};
  286. if (refreshData.ledger && refreshData.ledger.length > 0) {
  287. result.ledger.bills = await this.ctx.service.ledger.getDataByIds(refreshData.ledger);
  288. if (refreshData.stageUpdate) {
  289. result.ledger.curStageData = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
  290. }
  291. }
  292. if (refreshData.pos && refreshData.pos.length > 0) {
  293. result.pos.pos = await this.ctx.service.pos.getPosData({id: refreshData.pos});
  294. if (refreshData.stageUpdate) {
  295. result.pos.curStageData = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
  296. }
  297. }
  298. return result;
  299. } catch(err) {
  300. throw '获取数据异常,请刷新页面。';
  301. }
  302. }
  303. async updateChangeQuantity(transaction, pos, qty) {
  304. let orgPos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  305. if (orgPos.length > 1) {
  306. throw '数据错误';
  307. } else {
  308. orgPos = orgPos[0];
  309. }
  310. if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
  311. await transaction.update(this.tableName, {id: orgPos.id, qc_qty: qty});
  312. } else {
  313. await transaction.insert(this.tableName, {
  314. tid: this.ctx.tender.id,
  315. sid: this.ctx.stage.id,
  316. lid: pos.lid,
  317. pid: pos.id,
  318. said: this.ctx.session.sessionUser.accountId,
  319. times: this.ctx.stage.curTimes,
  320. order: this.ctx.stage.curOrder,
  321. qc_qty: qty,
  322. });
  323. }
  324. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
  325. }
  326. /**
  327. * 统计清单下部位明细合计
  328. * @param {Number} tid - 标段id
  329. * @param {Number} sid - 期id
  330. * @param {Number} lid - 清单节点id
  331. * @param transaction - 事务(不为空则在事务中查询,反之在数据库中查询)
  332. * @returns {Promise<*>}
  333. */
  334. async getPosGather(tid, sid, lid, transaction) {
  335. const calcQtySql = 'SELECT SUM(`contract_qty`) As `contract_qty`, SUM(`qc_qty`) As `qc_qty` FROM (' +
  336. ' SELECT `contract_qty`, `qc_qty` FROM ' + this.ctx.service.stagePos.tableName + ' As Pos ' +
  337. ' INNER JOIN (' +
  338. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` ' +
  339. ' FROM ' + this.ctx.service.stagePos.tableName +
  340. ' WHERE `tid` = ? And sid = ? And `lid` = ? ' +
  341. ' GROUP BY `pid`' +
  342. ' ) As MaxFilter ' +
  343. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid ' +
  344. ' WHERE Pos.tid = ? And Pos.sid = ? And Pos.lid = ?' +
  345. ' ) As Gather';
  346. const param = [tid, sid, lid];
  347. const sqlParam = param.concat(param);
  348. if (transaction) {
  349. return await transaction.queryOne(calcQtySql, sqlParam);
  350. } else {
  351. return await this.db.queryOne(calcQtySql, sqlParam);
  352. }
  353. }
  354. }
  355. return StagePos;
  356. };