stage_pos.js 14 KB

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