stage_change.js 17 KB

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