stage_change.js 20 KB

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