stage_change.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. 'use strict';
  2. /**
  3. * 期 - 变更数据
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const defaultPid = -1; // 非pid
  10. const audit = require('../const/audit');
  11. const timesLen = 100;
  12. module.exports = app => {
  13. class StageChange extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'stage_change';
  23. }
  24. /**
  25. * 查询 调用的变更令 最新数据
  26. * @param {Number} tid - 标段id
  27. * @param {Number} sid - 期id
  28. * @param {Number} lid - 台账节点id
  29. * @param {Number} pid - 部位明细id
  30. * @returns {Promise<*>}
  31. */
  32. async getLastestStageData(tid, sid, lid, pid) {
  33. const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
  34. ' INNER JOIN ( ' +
  35. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
  36. ' WHERE tid = ? And sid = ? And lid = ? And pid = ?' +
  37. ' GROUP By `lid`, `pid`' +
  38. ' ) As m ' +
  39. ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid`';
  40. const sqlParam = [tid, sid, lid, pid ? pid : -1];
  41. return await this.db.query(sql, sqlParam);
  42. }
  43. /**
  44. * 查询 调用的变更令 某轮 某人的数据
  45. * @param {Number} tid - 标段id
  46. * @param {Number} sid - 期id
  47. * @param {Number} times - 第几轮
  48. * @param {Number} order - 第几人
  49. * @param {Number} lid - 台账节点id
  50. * @param {Number} pid - 部位明细id
  51. * @returns {Promise<*>}
  52. */
  53. async getAuditorStageData(tid, sid, times, order, lid, pid) {
  54. const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
  55. ' INNER JOIN ( ' +
  56. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
  57. ' WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?)) And lid = ? And pid = ?' +
  58. ' GROUP By `lid`, `pid`' +
  59. ' ) As m ' +
  60. ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid`';
  61. const sqlParam = [tid, sid, times, times, order, lid, pid ? pid : -1];
  62. return await this.db.query(sql, sqlParam);
  63. }
  64. /**
  65. * 台账,调用变更令
  66. *
  67. * @param {Object} bills - 台账节点数据
  68. * @param {Array} changes - 调用的变更令
  69. * @returns {Promise<void>}
  70. */
  71. async billsChange(bills, changes) {
  72. const self = this;
  73. function getNewChange(cid, cbid, times, order, qty) {
  74. return {
  75. tid: self.ctx.tender.id,
  76. sid: self.ctx.stage.id,
  77. lid: bills.id,
  78. pid: -1,
  79. cid: cid,
  80. cbid: cbid,
  81. stimes: times,
  82. sorder: order,
  83. qty: qty
  84. }
  85. }
  86. const ledgerBills = await this.ctx.service.ledger.getDataById(bills.id);
  87. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  88. throw '提交数据错误';
  89. }
  90. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  91. // 获取原变更令
  92. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1);
  93. // 获取更新数据
  94. const updateChanges = [], newChanges = [];
  95. let billsQty = 0;
  96. for (const oc of oldChanges) {
  97. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  98. if (!nc || nc.qty !== oc.qty) {
  99. const qty = nc ? this.round(nc.qty, precision.value) : null;
  100. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
  101. billsQty = this.ctx.helper.add(billsQty, change.qty);
  102. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  103. change.id = oc.id;
  104. updateChanges.push(change);
  105. } else {
  106. newChanges.push(change);
  107. }
  108. } else {
  109. billsQty = this.ctx.helper.add(billsQty, oc.qty);
  110. }
  111. }
  112. for (const c of changes) {
  113. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  114. if (!nc) {
  115. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
  116. billsQty = this.ctx.helper.add(billsQty, change.qty);
  117. newChanges.push(change);
  118. }
  119. }
  120. // 更新数据
  121. const transaction = await this.db.beginTransaction();
  122. try {
  123. if (newChanges.length > 0) {
  124. await transaction.insert(this.tableName, newChanges);
  125. }
  126. for (const c of updateChanges) {
  127. await transaction.update(this.tableName, c);
  128. }
  129. const stageBills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id);
  130. await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, { qc_qty: billsQty });
  131. await transaction.commit();
  132. } catch (err) {
  133. await transaction.rollback();
  134. throw err;
  135. }
  136. const result = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
  137. return { bills: {curStageData: result} };
  138. }
  139. /**
  140. * 部位明细,调用变更令
  141. *
  142. * @param {Object} pos - 部位明细数据
  143. * @param {Array} changes - 调用的变更令
  144. * @returns {Promise<{}>}
  145. */
  146. async posChange(pos, changes) {
  147. const self = this;
  148. function getNewChange(cid, cbid, times, order, qty) {
  149. return {
  150. tid: self.ctx.tender.id,
  151. sid: self.ctx.stage.id,
  152. lid: pos.lid,
  153. pid: pos.id,
  154. cid: cid,
  155. cbid: cbid,
  156. stimes: times,
  157. sorder: order,
  158. qty: qty
  159. }
  160. }
  161. const ledgerBills = await this.ctx.service.ledger.getDataById(pos.lid);
  162. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  163. throw '提交数据错误';
  164. }
  165. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  166. // 获取原变更令
  167. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.lid, pos.id);
  168. const updateChanges = [], newChanges = [];
  169. let posQty = 0;
  170. for (const oc of oldChanges) {
  171. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  172. if (!nc || nc.qty !== oc.qty) {
  173. const qty = nc ? this.round(nc.qty, precision.value) : null;
  174. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
  175. posQty = this.ctx.helper.add(posQty, change.qty);
  176. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  177. change.id = oc.id;
  178. updateChanges.push(change);
  179. } else {
  180. newChanges.push(change);
  181. }
  182. } else {
  183. posQty = this.ctx.helper.add(posQty, oc.qty);
  184. }
  185. }
  186. for (const c of changes) {
  187. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  188. if (!nc) {
  189. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
  190. posQty = this.ctx.helper.add(posQty, change.qty);
  191. newChanges.push(change);
  192. }
  193. }
  194. // 更新数据
  195. const transaction = await this.db.beginTransaction();
  196. try {
  197. if (newChanges.length > 0) {
  198. await transaction.insert(this.tableName, newChanges);
  199. }
  200. for (const c of updateChanges) {
  201. await transaction.update(this.tableName, c);
  202. }
  203. await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty);
  204. await transaction.commit();
  205. } catch (err) {
  206. await transaction.rollback();
  207. throw err;
  208. }
  209. // 获取返回数据
  210. try {
  211. const data = { bills: {}, pos: {} };
  212. data.bills.curStageData = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
  213. data.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  214. return data;
  215. } catch(err) {
  216. throw '获取数据错误,请刷新页面';
  217. }
  218. }
  219. /**
  220. * 获取 变更令 - 变更清单 使用情况
  221. * @param {Number} sid - 查询期id
  222. * @param {uuid} cid - 变更令id
  223. * @returns {Promise<void>}
  224. */
  225. async getUsedData(tid, cid) {
  226. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  227. let filter;
  228. if (lastStage.id === this.ctx.stage.id) {
  229. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))',
  230. [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
  231. } else {
  232. if (lastStage.status === audit.stage.status.uncheck) {
  233. filter = 'And s.order < ' + lastStage.order;
  234. } else if (lastStage.status === audit.stage.status.checked) {
  235. filter = '';
  236. } else if (lastStage.status === audit.stage.status.checkNo) {
  237. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ?))',
  238. [lastStage.order, lastStage.order, lastStage.times])
  239. } else {
  240. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
  241. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))',
  242. [lastStage.order, lastStage.order, lastStage.times, curAuditor.order - 1]);
  243. }
  244. }
  245. const sql = 'SELECT c.lid, c.pid, SUM(c.qty) as used_qty,' +
  246. ' cb.tid, cb.cid, cb.id, cb.code, cb.name, cb.unit, cb.unit_price, cb.detail, cb.samount' +
  247. ' FROM ' + this.ctx.service.changeAuditList.tableName + ' As cb' +
  248. ' LEFT JOIN ' + this.tableName + ' As c ON cb.id = c.cbid ' +
  249. ' INNER JOIN (' +
  250. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid`, sChange.`sid`, `cid` ' +
  251. ' FROM ' + this.tableName + ' As sChange' +
  252. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s ON sChange.sid = s.id' +
  253. ' WHERE sChange.tid = ? AND cid = ?' + filter +
  254. ' GROUP By `lid`, `pid`, `cbid`, sChange.`sid`' +
  255. ' ) As m' +
  256. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.flow And c.`cbid` = m.`cbid` AND c.`sid` = m.`sid` And c.`cid` = m.`cid`' +
  257. ' WHERE cb.cid = ?' +
  258. ' GROUP By c.`cbid`';
  259. const sqlParam = [tid, cid, cid];
  260. return await this.db.query(sql, sqlParam);
  261. }
  262. /**
  263. * 获取 变更令 - 变更清单 当期使用情况
  264. * @param {Number} sid - 查询期id
  265. * @param {uuid} cid - 变更令id
  266. * @returns {Promise<*>}
  267. */
  268. async getStageUsedData(sid, cid) {
  269. const sql = 'SELECT c.*, ' +
  270. ' l.ledger_id As `ledger_id`, l.b_code As `l_code`, l.name As `l_name`, l.unit As `l_unit`, l.unit_price As `l_up`,' +
  271. ' l.deal_qty As `l_deal_qty`, l.deal_tp As `l_deal_tp`, l.quantity As `l_qty`, l.total_price As `l_tp`, ' +
  272. ' l.drawing_code As `l_drawing_code`, ' +
  273. ' p.name As `p_name`, p.drawing_code As `p_drawing_code`, p.`quantity` As `p_qty`' +
  274. ' FROM ' + this.tableName + ' As c ' +
  275. ' INNER JOIN ( ' +
  276. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid` From ' + this.tableName +
  277. ' WHERE sid = ? And cid = ?' +
  278. ' GROUP By `lid`, `pid`, `cbid`' +
  279. ' ) As m ' +
  280. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.flow And c.lid = m.lid And c.pid = m.pid And c.cbid = m.cbid' +
  281. ' LEFT JOIN ' + this.ctx.service.ledger.tableName + ' As l ON c.lid = l.id' +
  282. ' LEFT JOIN ' + this.ctx.service.pos.tableName + ' As p ON c.pid = p.id';
  283. const sqlParam = [sid, cid];
  284. return await this.db.query(sql, sqlParam);
  285. }
  286. /**
  287. * 获取 本期 使用的变更令
  288. * @param sid
  289. * @returns {Promise<void>}
  290. */
  291. async getStageUsedChangeId(sid) {
  292. const sql = 'SELECT c.`cid` FROM ' + this.tableName + ' As c' +
  293. ' INNER JOIN (' +
  294. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid` From ' + this.tableName +
  295. ' WHERE sid = ?' +
  296. ' GROUP By `lid`, `pid`, `cbid`' +
  297. ' ) As m' +
  298. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.flow And c.lid = m.lid And c.pid = m.pid And c.cbid = m.cbid' +
  299. ' GROUP BY c.`cid`';
  300. const sqlParam = [sid];
  301. const result = await this.db.query(sql, sqlParam);
  302. return this._.map(this._.filter(result, 'qty'), 'cid');
  303. }
  304. }
  305. return StageChange;
  306. };