stage_pos.js 15 KB

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