stage_pos.js 15 KB

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