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