stage_pos.js 20 KB

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