stage_change.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. module.exports = app => {
  12. class StageChange 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_change';
  22. }
  23. /**
  24. * 查询 调用的变更令 最新数据
  25. * @param {Number} tid - 标段id
  26. * @param {Number} sid - 期id
  27. * @param {Number} lid - 台账节点id
  28. * @param {Number} pid - 部位明细id
  29. * @returns {Promise<*>}
  30. */
  31. async getLastestStageData(tid, sid, lid, pid) {
  32. const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
  33. ' INNER JOIN ( ' +
  34. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
  35. ' WHERE tid = ? And sid = ? And lid = ? And pid = ?' +
  36. ' GROUP By `lid`, `pid`' +
  37. ' ) As m ' +
  38. ' 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`';
  39. const sqlParam = [tid, sid, lid, pid ? pid : -1];
  40. return await this.db.query(sql, sqlParam);
  41. }
  42. /**
  43. * 查询 调用的变更令 某轮 某人的数据
  44. * @param {Number} tid - 标段id
  45. * @param {Number} sid - 期id
  46. * @param {Number} times - 第几轮
  47. * @param {Number} order - 第几人
  48. * @param {Number} lid - 台账节点id
  49. * @param {Number} pid - 部位明细id
  50. * @returns {Promise<*>}
  51. */
  52. async getAuditorStageData(tid, sid, times, order, lid, pid) {
  53. const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
  54. ' INNER JOIN ( ' +
  55. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
  56. ' WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?)) And lid = ? And pid = ?' +
  57. ' GROUP By `lid`, `pid`' +
  58. ' ) As m ' +
  59. ' 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`';
  60. const sqlParam = [tid, sid, times, times, order, lid, pid ? pid : -1];
  61. return await this.db.query(sql, sqlParam);
  62. }
  63. /**
  64. * 台账,调用变更令
  65. *
  66. * @param {Object} bills - 台账节点数据
  67. * @param {Array} changes - 调用的变更令
  68. * @returns {Promise<void>}
  69. */
  70. async billsChange(bills, changes) {
  71. const self = this;
  72. function getNewChange(cid, cbid, times, order, qty) {
  73. return {
  74. tid: self.ctx.tender.id,
  75. sid: self.ctx.stage.id,
  76. lid: bills.id,
  77. pid: -1,
  78. cid: cid,
  79. cbid: cbid,
  80. stimes: times,
  81. sorder: order,
  82. qty: qty
  83. }
  84. }
  85. const ledgerBills = await this.ctx.service.ledger.getDataById(bills.id);
  86. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  87. throw '提交数据错误';
  88. }
  89. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  90. // 获取原变更令
  91. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1);
  92. // 获取更新数据
  93. const updateChanges = [], newChanges = [];
  94. let billsQty = 0;
  95. for (const oc of oldChanges) {
  96. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  97. if (!nc || nc.qty !== oc.qty) {
  98. const qty = nc ? this.round(nc.qty, precision.value) : null;
  99. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
  100. billsQty = this.ctx.helper.add(billsQty, change.qty);
  101. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  102. change.id = oc.id;
  103. updateChanges.push(change);
  104. } else {
  105. newChanges.push(change);
  106. }
  107. } else {
  108. billsQty = this.ctx.helper.add(billsQty, oc.qty);
  109. }
  110. }
  111. for (const c of changes) {
  112. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  113. if (!nc) {
  114. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
  115. billsQty = this.ctx.helper.add(billsQty, change.qty);
  116. newChanges.push(change);
  117. }
  118. }
  119. // 更新数据
  120. const transaction = await this.db.beginTransaction();
  121. try {
  122. if (newChanges.length > 0) {
  123. await transaction.insert(this.tableName, newChanges);
  124. }
  125. for (const c of updateChanges) {
  126. await transaction.update(this.tableName, c);
  127. }
  128. const stageBills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id);
  129. await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, { qc_qty: billsQty });
  130. await transaction.commit();
  131. } catch (err) {
  132. await transaction.rollback();
  133. throw err;
  134. }
  135. const result = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
  136. return { bills: {curStageData: result} };
  137. }
  138. /**
  139. * 部位明细,调用变更令
  140. *
  141. * @param {Object} pos - 部位明细数据
  142. * @param {Array} changes - 调用的变更令
  143. * @returns {Promise<{}>}
  144. */
  145. async posChange(pos, changes) {
  146. console.log(pos);
  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. console.log(posQty);
  204. await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty);
  205. await transaction.commit();
  206. } catch (err) {
  207. await transaction.rollback();
  208. throw err;
  209. }
  210. // 获取返回数据
  211. try {
  212. const data = { bills: {}, pos: {} };
  213. data.bills.curStageData = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
  214. data.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.id]);
  215. return data;
  216. } catch(err) {
  217. throw '获取数据错误,请刷新页面';
  218. }
  219. }
  220. /**
  221. * 获取 变更令 - 变更清单 使用情况
  222. * @param {Number} sid - 查询期id
  223. * @param {uuid} cid - 变更令id
  224. * @returns {Promise<void>}
  225. */
  226. async getUsedData(tid, cid) {
  227. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  228. let filter;
  229. if (lastStage.id === this.ctx.stage.id) {
  230. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))',
  231. [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
  232. } else {
  233. if (lastStage.status === audit.stage.status.uncheck) {
  234. filter = 'And s.order < ' + lastStage.order;
  235. } else if (lastStage.status === audit.stage.status.checked) {
  236. filter = '';
  237. } else if (lastStage.status === audit.stage.status.checkNo) {
  238. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ?))',
  239. [lastStage.order, lastStage.order, lastStage.times])
  240. } else {
  241. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
  242. filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))',
  243. [lastStage.order, lastStage.order, lastStage.times, curAuditor.order - 1]);
  244. }
  245. }
  246. const sql = 'SELECT c.lid, c.pid, SUM(c.qty) as used_qty,' +
  247. ' cb.tid, cb.cid, cb.id, cb.code, cb.name, cb.unit, cb.unit_price, cb.detail, cb.samount' +
  248. ' FROM ' + this.ctx.service.changeAuditList.tableName + ' As cb' +
  249. ' LEFT JOIN ' + this.tableName + ' As c ON cb.id = c.cbid ' +
  250. ' INNER JOIN (' +
  251. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid`, `cid` ' +
  252. ' FROM ' + this.tableName + ' As sChange' +
  253. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s ON sChange.sid = s.id' +
  254. ' WHERE sChange.tid = ? AND cid = ?' + filter +
  255. ' GROUP By `lid`, `pid`, `cbid`, sChange.`sid`' +
  256. ' ) As m' +
  257. ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.`cbid` = m.`cbid` AND c.`sid` = m.`sid` And c.`cid` = m.`cid`' +
  258. ' WHERE cb.cid = ?' +
  259. ' GROUP By c.`cbid`';
  260. const sqlParam = [tid, cid, cid];
  261. return await this.db.query(sql, sqlParam);
  262. }
  263. /**
  264. * 获取 变更令 - 变更清单 当期使用情况
  265. * @param {Number} sid - 查询期id
  266. * @param {uuid} cid - 变更令id
  267. * @returns {Promise<*>}
  268. */
  269. async getStageUsedData(sid, cid) {
  270. const sql = 'SELECT c.*, ' +
  271. ' 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`,' +
  272. ' 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`, ' +
  273. ' l.drawing_code As `l_drawing_code`, ' +
  274. ' p.name As `p_name`, p.drawing_code As `p_drawing_code`, p.`quantity` As `p_qty`' +
  275. ' FROM ' + this.tableName + ' As c ' +
  276. ' INNER JOIN ( ' +
  277. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid` From ' + this.tableName +
  278. ' WHERE sid = ? And cid = ?' +
  279. ' GROUP By `lid`, `pid`, `cbid`' +
  280. ' ) As m ' +
  281. ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid And c.cbid = m.cbid' +
  282. ' LEFT JOIN ' + this.ctx.service.ledger.tableName + ' As l ON c.lid = l.id' +
  283. ' LEFT JOIN ' + this.ctx.service.pos.tableName + ' As p ON c.pid = p.id';
  284. const sqlParam = [sid, cid];
  285. return await this.db.query(sql, sqlParam);
  286. }
  287. /**
  288. * 获取 本期 使用的变更令
  289. * @param sid
  290. * @returns {Promise<void>}
  291. */
  292. async getStageUsedChangeId(sid) {
  293. const sql = 'SELECT c.`cid` FROM ' + this.tableName + ' As c' +
  294. ' INNER JOIN (' +
  295. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid` From ' + this.tableName +
  296. ' WHERE sid = ? And not ISNULL(qty)' +
  297. ' GROUP By `lid`, `pid`, `cbid`' +
  298. ' ) As m' +
  299. ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid And c.cbid = m.cbid' +
  300. ' GROUP BY c.`cid`';
  301. const sqlParam = [sid];
  302. const result = await this.db.query(sql, sqlParam);
  303. return this._.map(result, 'cid');
  304. }
  305. }
  306. return StageChange;
  307. };