stage_pos.js 14 KB

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