stage_change.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.*' +
  55. ' oc.code As c_code, oc.new_code As c_new_code' +
  56. ' FROM ' + this.tableName + ' As c ' +
  57. ' INNER JOIN ( ' +
  58. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
  59. ' WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?)) And lid = ? And pid = ?' +
  60. ' GROUP By `lid`, `pid`' +
  61. ' ) As m ' +
  62. ' 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`'
  63. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  64. ' ON c.cid = oc.cid';
  65. const sqlParam = [tid, sid, times, times, order, lid, pid ? pid : -1];
  66. return await this.db.query(sql, sqlParam);
  67. }
  68. async getLastestAllStageData(tid, sid) {
  69. const sql = 'SELECT c.*,' +
  70. ' oc.code As c_code, oc.new_code As c_new_code' +
  71. ' FROM ' + this.tableName + ' As c ' +
  72. ' INNER JOIN ( ' +
  73. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
  74. ' WHERE tid = ? And sid = ?' +
  75. ' GROUP By `lid`, `pid`' +
  76. ' ) As m ' +
  77. ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.`sid` = m.`sid`' +
  78. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  79. ' ON c.cid = oc.cid';
  80. const sqlParam = [tid, sid];
  81. return await this.db.query(sql, sqlParam);
  82. }
  83. async getAuditorAllStageData(tid, sid, times, order) {
  84. const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
  85. ' INNER JOIN ( ' +
  86. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
  87. ' WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?))' +
  88. ' GROUP By `lid`, `pid`' +
  89. ' ) As m ' +
  90. ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.`sid` = m.`sid`';
  91. const sqlParam = [tid, sid, times, times, order];
  92. return await this.db.query(sql, sqlParam);
  93. }
  94. /**
  95. * 台账,调用变更令
  96. *
  97. * @param {Object} bills - 台账节点数据
  98. * @param {Array} changes - 调用的变更令
  99. * @returns {Promise<void>}
  100. */
  101. async billsChange(bills, changes) {
  102. const self = this;
  103. function getNewChange(cid, cbid, times, order, qty) {
  104. return {
  105. tid: self.ctx.tender.id,
  106. sid: self.ctx.stage.id,
  107. lid: bills.id,
  108. pid: -1,
  109. cid: cid,
  110. cbid: cbid,
  111. stimes: times,
  112. sorder: order,
  113. qty: qty
  114. }
  115. }
  116. const ledgerBills = await this.ctx.service.ledger.getDataById(bills.id);
  117. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  118. throw '提交数据错误';
  119. }
  120. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  121. // 获取原变更令
  122. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1);
  123. // 获取更新数据
  124. const updateChanges = [], newChanges = [];
  125. let billsQty = 0;
  126. for (const oc of oldChanges) {
  127. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  128. if (!nc || nc.qty !== oc.qty) {
  129. const qty = nc ? this.round(nc.qty, precision.value) : null;
  130. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
  131. billsQty = this.ctx.helper.add(billsQty, change.qty);
  132. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  133. change.id = oc.id;
  134. updateChanges.push(change);
  135. } else {
  136. newChanges.push(change);
  137. }
  138. } else {
  139. billsQty = this.ctx.helper.add(billsQty, oc.qty);
  140. }
  141. }
  142. for (const c of changes) {
  143. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  144. if (!nc) {
  145. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
  146. billsQty = this.ctx.helper.add(billsQty, change.qty);
  147. newChanges.push(change);
  148. }
  149. }
  150. // 更新数据
  151. const transaction = await this.db.beginTransaction();
  152. try {
  153. if (newChanges.length > 0) {
  154. await transaction.insert(this.tableName, newChanges);
  155. }
  156. for (const c of updateChanges) {
  157. await transaction.update(this.tableName, c);
  158. }
  159. const stageBills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id);
  160. await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, { qc_qty: billsQty });
  161. await transaction.commit();
  162. } catch (err) {
  163. await transaction.rollback();
  164. throw err;
  165. }
  166. const result = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
  167. return { bills: {curStageData: result} };
  168. }
  169. /**
  170. * 部位明细,调用变更令
  171. *
  172. * @param {Object} pos - 部位明细数据
  173. * @param {Array} changes - 调用的变更令
  174. * @returns {Promise<{}>}
  175. */
  176. async posChange(pos, changes) {
  177. const self = this;
  178. function getNewChange(cid, cbid, times, order, qty) {
  179. return {
  180. tid: self.ctx.tender.id,
  181. sid: self.ctx.stage.id,
  182. lid: pos.lid,
  183. pid: pos.id,
  184. cid: cid,
  185. cbid: cbid,
  186. stimes: times,
  187. sorder: order,
  188. qty: qty
  189. }
  190. }
  191. const ledgerBills = await this.ctx.service.ledger.getDataById(pos.lid);
  192. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  193. throw '提交数据错误';
  194. }
  195. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  196. // 获取原变更令
  197. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.lid, pos.id);
  198. const updateChanges = [], newChanges = [];
  199. let posQty = 0;
  200. for (const oc of oldChanges) {
  201. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  202. if (!nc || nc.qty !== oc.qty) {
  203. const qty = nc ? this.round(nc.qty, precision.value) : null;
  204. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
  205. posQty = this.ctx.helper.add(posQty, change.qty);
  206. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  207. change.id = oc.id;
  208. updateChanges.push(change);
  209. } else {
  210. newChanges.push(change);
  211. }
  212. } else {
  213. posQty = this.ctx.helper.add(posQty, oc.qty);
  214. }
  215. }
  216. for (const c of changes) {
  217. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  218. if (!nc) {
  219. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
  220. posQty = this.ctx.helper.add(posQty, change.qty);
  221. newChanges.push(change);
  222. }
  223. }
  224. // 更新数据
  225. const transaction = await this.db.beginTransaction();
  226. try {
  227. if (newChanges.length > 0) {
  228. await transaction.insert(this.tableName, newChanges);
  229. }
  230. for (const c of updateChanges) {
  231. await transaction.update(this.tableName, c);
  232. }
  233. await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty);
  234. await transaction.commit();
  235. } catch (err) {
  236. await transaction.rollback();
  237. throw err;
  238. }
  239. // 获取返回数据
  240. try {
  241. const data = { bills: {}, pos: {} };
  242. data.bills.curStageData = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
  243. data.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  244. return data;
  245. } catch(err) {
  246. throw '获取数据错误,请刷新页面';
  247. }
  248. }
  249. /**
  250. * 获取 变更令 - 变更清单 使用情况
  251. * @param {Number} sid - 查询期id
  252. * @param {uuid} cid - 变更令id
  253. * @returns {Promise<void>}
  254. */
  255. async getUsedData(tid, cid) {
  256. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  257. let filter;
  258. if (lastStage.id === this.ctx.stage.id) {
  259. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))',
  260. [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
  261. } else {
  262. if (lastStage.status === audit.stage.status.uncheck) {
  263. filter = 'And s.order < ' + lastStage.order;
  264. } else if (lastStage.status === audit.stage.status.checked) {
  265. filter = '';
  266. } else if (lastStage.status === audit.stage.status.checkNo) {
  267. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ?))',
  268. [lastStage.order, lastStage.order, lastStage.times])
  269. } else {
  270. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
  271. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))',
  272. [lastStage.order, lastStage.order, lastStage.times, curAuditor.order - 1]);
  273. }
  274. }
  275. const sql = 'SELECT c.lid, c.pid, SUM(c.qty) as used_qty,' +
  276. ' cb.tid, cb.cid, cb.id, cb.code, cb.name, cb.unit, cb.unit_price, cb.detail, cb.samount' +
  277. ' FROM ' + this.ctx.service.changeAuditList.tableName + ' As cb' +
  278. ' LEFT JOIN ' + this.tableName + ' As c ON cb.id = c.cbid ' +
  279. ' INNER JOIN (' +
  280. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid`, sChange.`sid`, `cid` ' +
  281. ' FROM ' + this.tableName + ' As sChange' +
  282. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s ON sChange.sid = s.id' +
  283. ' WHERE sChange.tid = ? AND cid = ?' + filter +
  284. ' GROUP By `lid`, `pid`, `cbid`, sChange.`sid`' +
  285. ' ) As m' +
  286. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.flow And c.`cbid` = m.`cbid` AND c.`sid` = m.`sid` And c.`cid` = m.`cid`' +
  287. ' WHERE cb.cid = ?' +
  288. ' GROUP By c.`cbid`';
  289. const sqlParam = [tid, cid, cid];
  290. return await this.db.query(sql, sqlParam);
  291. }
  292. /**
  293. * 获取 变更令 - 变更清单 当期使用情况
  294. * @param {Number} sid - 查询期id
  295. * @param {uuid} cid - 变更令id
  296. * @returns {Promise<*>}
  297. */
  298. async getStageUsedData(sid, cid) {
  299. const sql = 'SELECT c.*, ' +
  300. ' 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`,' +
  301. ' 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`, ' +
  302. ' l.drawing_code As `l_drawing_code`, ' +
  303. ' p.name As `p_name`, p.drawing_code As `p_drawing_code`, p.`quantity` As `p_qty`' +
  304. ' FROM ' + this.tableName + ' As c ' +
  305. ' INNER JOIN ( ' +
  306. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid` From ' + this.tableName +
  307. ' WHERE sid = ? And cid = ?' +
  308. ' GROUP By `lid`, `pid`, `cbid`' +
  309. ' ) As m ' +
  310. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.flow And c.lid = m.lid And c.pid = m.pid And c.cbid = m.cbid' +
  311. ' LEFT JOIN ' + this.ctx.service.ledger.tableName + ' As l ON c.lid = l.id' +
  312. ' LEFT JOIN ' + this.ctx.service.pos.tableName + ' As p ON c.pid = p.id';
  313. const sqlParam = [sid, cid];
  314. return await this.db.query(sql, sqlParam);
  315. }
  316. /**
  317. * 获取 本期 使用的变更令
  318. * @param sid
  319. * @returns {Promise<void>}
  320. */
  321. async getStageUsedChangeId(sid) {
  322. const sql = 'SELECT c.`cid`, sum(qty) As qty FROM ' + this.tableName + ' As c' +
  323. ' INNER JOIN (' +
  324. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid` From ' + this.tableName +
  325. ' WHERE sid = ?' +
  326. ' GROUP By `lid`, `pid`, `cbid`' +
  327. ' ) As m' +
  328. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.flow And c.lid = m.lid And c.pid = m.pid And c.cbid = m.cbid' +
  329. ' GROUP BY c.`cid`';
  330. const sqlParam = [sid];
  331. const result = await this.db.query(sql, sqlParam);
  332. return this._.map(this._.filter(result, 'qty'), 'cid');
  333. }
  334. }
  335. return StageChange;
  336. };