stage_rela.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const changeConst = require('../const/change');
  11. const ledgerModel = require('../lib/ledger');
  12. const StageIm = require('../lib/stage_im');
  13. class srCache {
  14. constructor(ctx) {
  15. this.ctx = ctx;
  16. }
  17. async _getCacheOrgTp() {
  18. const bg = await this.ctx.service.stageChange.getQualityTotalPrice(this.stage);
  19. const gcl100 = await this.ctx.service.stageBills.getSumTotalPriceGcl(this.stage, '^[^0-9]*1[0-9]{2}(-|$)');
  20. return {
  21. contract_tp: this.stage.contract_tp, qc_tp: this.stage.qc_tp,
  22. gather_tp: this.ctx.helper.add(this.stage.contract_tp, this.stage.qc_tp),
  23. gather_tp_100: this.ctx.helper.add(gcl100.contract_tp, gcl100.qc_tp),
  24. bg_common: bg.common, bg_more: bg.more, bg_great: bg.great,
  25. };
  26. }
  27. async _loadBillsData() {
  28. const decimal = this.ctx.tender.info.decimal;
  29. const ledger = await this.ctx.service.ledger.getData(this.stage.tid);
  30. const curBillsData = await this.ctx.service.stageBills.getLastestStageData(this.stage.tid, this.stage.id);
  31. const preStageBills = this.preRelaStage
  32. ? this.ctx.service.stageRelaBillsFinal.getAllDataByCondition({ where: { sid: this.preRelaStage.sid, rela_tid: this.preRelaStage.rela_tid } })
  33. : [];
  34. this.ctx.helper.assignRelaData(ledger, [
  35. { data: curBillsData, fields: ['contract_qty', 'qc_qty', 'postil'], prefix: '', relaId: 'lid' },
  36. { data: preStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  37. ]);
  38. for (const l of ledger) {
  39. const db = this.dealBills.find(x => {
  40. return x.code === l.b_code && x.name === l.name && x.unit === l.unit;
  41. });
  42. l.unit_price = db ? db.unit_price : 0;
  43. l.contract_tp = this.ctx.helper.mul(l.unit_price, l.contract_qty, decimal.tp);
  44. l.qc_tp = this.ctx.helper.mul(l.unit_price, l.qc_qty, decimal.tp);
  45. l.gather_tp = this.ctx.helper.add(l.contract_tp, l.qc_tp);
  46. l.pre_gather_tp = this.ctx.helper.add(l.pre_contract_qty, l.pre_contract_tp);
  47. l.end_contract_qty = this.ctx.helper.add(l.pre_contract_qty, l.contract_qty);
  48. l.end_contract_tp = this.ctx.helper.add(l.pre_contract_tp, l.contract_tp);
  49. l.end_qc_qty = this.ctx.helper.add(l.pre_qc_qty, l.qc_qty);
  50. l.end_qc_tp = this.ctx.helper.add(l.pre_qc_tp, l.qc_tp);
  51. l.end_gather_qty = this.ctx.helper.add(l.pre_gather_qty, l.gather_qty);
  52. l.end_gather_tp = this.ctx.helper.add(l.pre_gather_tp, l.gather_tp);
  53. }
  54. this.billsTree = new ledgerModel.billsTree(this.ctx, {
  55. id: 'ledger_id',
  56. pid: 'ledger_pid',
  57. order: 'order',
  58. level: 'level',
  59. rootId: -1,
  60. keys: ['id', 'tender_id', 'ledger_id'],
  61. stageId: 'id',
  62. calcFields: ['deal_tp', 'total_price',
  63. 'contract_tp', 'qc_tp', 'gather_tp',
  64. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp',
  65. 'end_contract_tp', 'end_qc_tp', 'end_gather_tp'],
  66. });
  67. this.billsTree.loadDatas(ledger);
  68. }
  69. async _loadPosData() {
  70. const pos = await this.ctx.service.pos.getPosData({tid: this.stage.tid});
  71. const curPosData = await this.ctx.service.stagePos.getLastestStageData(this.stage.tid, this.stage.id);
  72. const preStagePos = this.preRelaStage
  73. ? this.ctx.service.stageRelaPosFinal.getAllDataByCondition({ where: { sid: this.preRelaStage.sid, rela_tid: this.preRelaStage.rela_tid } })
  74. : [];
  75. this.ctx.helper.assignRelaData(pos, [
  76. { data: curPosData, fields: ['contract_qty', 'qc_qty', 'postil'], prefix: '', relaId: 'pid' },
  77. { data: preStagePos, fields: ['contract_qty', 'qc_qty', 'postil'], prefix: 'pre_', relaId: 'pid' },
  78. ]);
  79. for (const p of pos) {
  80. p.gather_qty = this.ctx.helper.add(p.contract_qty, p.qc_qty);
  81. p.pre_gather_qty = this.ctx.helper.add(p.pre_contract_qty, p.qc_qty);
  82. p.end_contract_qty = this.ctx.helper.add(p.contract_qty, p.pre_contract_qty);
  83. p.end_qc_qty = this.ctx.helper.add(p.qc_qty, p.pre_qc_qty);
  84. p.end_gather_qty = this.ctx.helper.add(p.gather_qty, p.pre_gather_qty);
  85. }
  86. this.pos = new ledgerModel.pos({ id: 'id', ledgerId: 'lid' });
  87. this.pos.loadDatas(pos);
  88. }
  89. async _getCacheTp(gcl, gcl100) {
  90. const helper = this.ctx.helper;
  91. const result = {
  92. contract_tp: gcl.contract_tp, qc_tp: gcl.qc_tp,
  93. gather_tp: this.ctx.helper.add(gcl.contract_tp, gcl.qc_tp),
  94. gather_tp_100: this.ctx.helper.add(gcl100.contract_tp, gcl100.qc_tp),
  95. };
  96. this.changes = await this.ctx.service.stageChange.getLastestAllStageData(this.stage.tid, this.stage.id);
  97. const bqData = [];
  98. for (const d of this.changes) {
  99. if (!d.qty) continue;
  100. let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
  101. if (!bd) {
  102. const bills = dealBills.find(x => {
  103. return x.code === d.code && x.name === d.name && x.unit === d.unit;
  104. });
  105. if (!bills) continue;
  106. bd = { lid: d.lid, quality: d.quality, unit_price: bills.unit_price };
  107. bqData.push(bd);
  108. }
  109. const tp = this.ctx.helper.mul(d.qty, bd.unit_price, tender.info.decimal.tp);
  110. bd.tp = this.ctx.helper.add(bd.tp, tp);
  111. }
  112. result.bg_common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
  113. result.bg_more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
  114. result.bg_great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
  115. return result;
  116. }
  117. _getCacheBills() {
  118. this.stageBills = [];
  119. this.stageBillsFinal = [];
  120. const gcl = {}, gcl100 = {}, gcl100reg = /^[^0-9]*1[0-9]{2}(-|$)/;
  121. for (const t of this.billsTree.nodes) {
  122. if (t.children && t.children.length > 0) continue;
  123. if (!t.b_code) continue;
  124. gcl.contract_tp = this.ctx.helper.add(gcl.contract_tp, t.contract_tp);
  125. gcl.qc_tp = this.ctx.helper.add(gcl.qc_tp, t.qc_tp);
  126. if (gcl100reg.test(t.b_code)) {
  127. gcl100.contract_tp = this.ctx.helper.add(gcl100.contract_tp, t.contract_tp);
  128. gcl100.qc_tp = this.ctx.helper.add(gcl100.qc_tp, t.qc_tp);
  129. }
  130. if (t.contract_qty || t.contract_tp || t.qc_qty || t.qc_tp) {
  131. this.stageBills.push({
  132. tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  133. rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
  134. lid: t.id, unit_price: t.unit_price,
  135. contract_qty: t.contract_qty, contract_tp: t.contract_tp, contract_expr: t.contract_expr,
  136. qc_qty: t.qc_qty, qc_tp: t.qc_tp,
  137. postil: t.postil,
  138. });
  139. }
  140. if (t.end_contract_qty || t.end_contract_tp || t.end_qc_qty || t.end_qc_tp) {
  141. this.stageBillsFinal.push({
  142. tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  143. rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
  144. lid: t.id,
  145. contract_qty: t.end_contract_qty, contract_tp: t.end_contract_tp,
  146. qc_qty: t.end_qc_qty, qc_tp: t.end_qc_tp,
  147. })
  148. }
  149. }
  150. return [gcl, gcl100];
  151. }
  152. _getCachePos() {
  153. this.stagePos = [];
  154. this.stagePosFinal = [];
  155. for (const t of this.pos.datas) {
  156. if (t.contract_qty || t.qc_qty) {
  157. this.stagePos.push({
  158. tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  159. rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
  160. pid: t.pid, lid: t.id,
  161. contract_qty: t.contract_qty, contract_expr: t.contract_expr, qc_qty: t.qc_qty, postil: t.postil,
  162. });
  163. }
  164. if (t.end_contract_qty || t.end_qc_qty) {
  165. this.stagePosFinal.push({
  166. tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  167. rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
  168. pid: t.pid, lid: t.lid,
  169. contract_qty: t.end_contract_qty, qc_qty: t.end_qc_qty
  170. })
  171. }
  172. }
  173. }
  174. async _getCacheStageIm() {
  175. const stageIm = new StageIm(this.ctx);
  176. this.details = await this.ctx.service.stageDetail.getLastestStageData(this.stage.tid, this.stage.id);
  177. stageIm.buildRelaStageIm(this.stage, this.billsTree, this.pos, this.details, this.changes);
  178. this.stageIm = [];
  179. this.stageImBills = [];
  180. for (const i of stageIm.ImData) {
  181. this.stageIm.push({
  182. tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  183. rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
  184. lid: i.lid, pid: i.pid, im_id: i.id,
  185. code: i.code, name: i.name, unit: i.unit, unit_price: i.unit_price,
  186. peg: i.peg, drawing_code: i.drawing_code, bw: i.bw, xm: i.xm,
  187. position: i.position, jldy: i.jldy,
  188. dwgc: i.dwgc, fbgc: i.fbgc, fxgc: i.fxgc,
  189. doc_code: i.doc_code, im_code: i.im_code,
  190. calc_memo: i.calc_memo, calc_memo_remark: i.calc_memo_remark, calc_img: i.calc_img,
  191. bgl_code: i.bgl_code, bgl_drawing_code: i.bgl_drawing_code,
  192. jl: i.jl, contract_jl: i.contract_jl, qc_jl: i.qc_jl,
  193. pre_jl: i.jl, pre_contract_jl: i.pre_contract_jl, pre_qc_jl: i.pre_qc_jl,
  194. end_jl: i.end_jl, end_contract_jl: i.end_contract_jl, end_qc_jl: i.end_qc_jl,
  195. tp: i.tp, contract_tp: i.contract_tp, qc_tp: i.qc_tp,
  196. pre_tp: i.pre_tp, pre_contract_tp: i.pre_contract_tp, pre_qc_tp: i.pre_qc_tp,
  197. end_tp: i.end_tp, end_contract_tp: i.end_contract_tp, end_qc_tp: i.end_qc_tp,
  198. quantity: i.quantity, total_price: i.total_price,
  199. })
  200. }
  201. for (const i of stageIm.ImBillsData) {
  202. this.stageImBills.push({
  203. tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  204. rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
  205. im_id: i.imid, bid: i.bid, im_code: i.im_code,
  206. b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price,
  207. jl: i.jl, contract_jl: i.contract_jl, qc_jl: i.qc_jl,
  208. pre_jl: i.jl, pre_contract_jl: i.pre_contract_jl, pre_qc_jl: i.pre_qc_jl,
  209. end_jl: i.end_jl, end_contract_jl: i.end_contract_jl, end_qc_jl: i.end_qc_jl,
  210. tp: i.tp, contract_tp: i.contract_tp, qc_tp: i.qc_tp,
  211. pre_tp: i.pre_tp, pre_contract_tp: i.pre_contract_tp, pre_qc_tp: i.pre_qc_tp,
  212. end_tp: i.end_tp, end_contract_tp: i.end_contract_tp, end_qc_tp: i.end_qc_tp,
  213. quantity: i.quantity, total_price: i.total_price
  214. })
  215. }
  216. }
  217. async calculate(stage) {
  218. this.stage = stage;
  219. this.dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.stage.tid } });
  220. this.preRelaStage = await this.ctx.service.stageRela.getPreRelaStage(
  221. this.ctx.tender.id, this.ctx.stage.order, stage.tid);
  222. this.cache_org_tp = await this._getCacheOrgTp();
  223. await this._loadBillsData();
  224. await this._loadPosData();
  225. const [gcl, gcl100] = this._getCacheBills();
  226. this._getCachePos();
  227. this.cache_tp = await this._getCacheTp(gcl, gcl100);
  228. this._getCacheStageIm();
  229. }
  230. };
  231. module.exports = app => {
  232. class StageRela extends app.BaseService {
  233. /**
  234. * 构造函数
  235. *
  236. * @param {Object} ctx - egg全局变量
  237. * @return {void}
  238. */
  239. constructor(ctx) {
  240. super(ctx);
  241. this.tableName = 'stage_rela';
  242. }
  243. _analysisStageRela(data) {
  244. const datas = data instanceof Array ? data : [data];
  245. for (const d of datas) {
  246. d.cache_tp = d.cache_tp ? JSON.parse(d.cache_tp) : {};
  247. d.cache_org_tp = d.cache_org_tp ? JSON.parse(d.cache_org_tp) : {};
  248. }
  249. }
  250. async getStageRela(sid) {
  251. const result = await this.getAllDataByCondition({
  252. where: { sid },
  253. orders: [['add_time', 'asc']],
  254. });
  255. this._analysisStageRela(result);
  256. return result;
  257. }
  258. async getHistoryStageRela(tid, sorder) {
  259. const sql = 'SELECT * FROM ' + this.tableName + ' WHERE tid = ? And sorder < ? ORDER BY add_time desc';
  260. const result = await this.db.query(sql, [tid, sorder]);
  261. this._analysisStageRela(result);
  262. return result;
  263. }
  264. async getPreRelaStage(tid, sorder, rela_tid) {
  265. const sql = 'SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND rela_tid = ? And sorder < ? ORDER BY sorder';
  266. return await this.db.queryOne(sql, [tid, rela_tid, sorder]);
  267. }
  268. // async _getCacheOrgTp(stage) {
  269. // const bg = await this.ctx.service.stageChange.getQualityTotalPrice(stage);
  270. // const gcl100 = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
  271. // return {
  272. // contract_tp: stage.contract_tp, qc_tp: stage.qc_tp,
  273. // gather_tp: this.ctx.helper.add(stage.contract_tp, stage.qc_tp),
  274. // gather_tp_100: this.ctx.helper.add(gcl100.contract_tp, gcl100.qc_tp),
  275. // bg_common: bg.common, bg_more: bg.more, bg_great: bg.great,
  276. // };
  277. // }
  278. //
  279. // async _calculateRelaStage(stage, preRelaStage) {
  280. // const result = {}, helper = this.ctx.helper, stageBills = [];
  281. // const ledger = await this.ctx.service.ledger.getData(stage.tid);
  282. // const curBillsData = await this.ctx.service.stageBills.getLastestStageData(stage.tid, stage.id);
  283. // helper.assignRelaData(ledger, [
  284. // { data: curBillsData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid' },
  285. // ]);
  286. // const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.stage.tid } });
  287. //
  288. // const gcl = {}, gcl100 = {}, gcl100reg = /^[^0-9]*1[0-9]{2}(-|$)/;
  289. // for (const t of ledger) {
  290. // const db = dealBills.find(x => {
  291. // return x.code === t.b_code && x.name === t.name && x.unit === t.unit;
  292. // });
  293. // t.unit_price = db ? db.unit_price : 0;
  294. // if (t.contract_qty) t.contract_tp = helper.mul(t.unit_price, t.contract_qty, this.ctx.tender.info.decimal.tp);
  295. // if (t.qc_qty) t.qc_tp = helper.mul(t.unit_price, t.qc_qty, this.ctx.tender.info.decimal.tp);
  296. // gcl.contract_tp = helper.add(gcl.contract_tp, t.contract_tp);
  297. // gcl.qc_tp = helper.add(gcl.qc_tp, t.qc_tp);
  298. // if (gcl100reg.test(t.b_code)) {
  299. // gcl100.contract_tp = helper.add(gcl100.contract_tp, t.contract_tp);
  300. // gcl100.qc_tp = helper.add(gcl100.qc_tp, t.qc_tp);
  301. // }
  302. // if (t.contract_qty || t.contract_tp || t.qc_qty || t.qc_tp) {
  303. // stageBills.push({
  304. // tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  305. // rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
  306. // lid: t.id, unit_price: t.unit_price,
  307. // contract_qty: t.contract_qty, contract_tp: t.contract_tp, contract_expr: t.contract_expr,
  308. // qc_qty: t.qc_qty, qc_tp: t.qc_tp,
  309. // postil: t.postil,
  310. // });
  311. // }
  312. // }
  313. // result.contract_tp = gcl.contract_tp;
  314. // result.qc_tp = gcl.qc_tp;
  315. // result.gather_tp = helper.add(gcl.contract_tp, gcl.qc_tp);
  316. // result.gather_tp_100 = helper.add(gcl100.contract_tp, gcl100.qc_tp);
  317. //
  318. // const sql = 'SELECT sc.*, c.quality, cb.code, cb.name, cb.unit FROM ' + this.ctx.service.stageChange.tableName + ' sc' +
  319. // ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON sc.cid = c.cid' +
  320. // ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' cb ON sc.cbid = cb.id' +
  321. // ' WHERE sid = ?';
  322. // const data = await this.db.query(sql, [stage.id]);
  323. // const bqData = [];
  324. // for (const d of data) {
  325. // if (!d.qty) continue;
  326. // let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
  327. // if (!bd) {
  328. // const bills = dealBills.find(x => {
  329. // return x.code === d.code && x.name === d.name && x.unit === d.unit;
  330. // });
  331. // if (!bills) continue;
  332. // bd = { lid: d.lid, quality: d.quality, unit_price: bills.unit_price };
  333. // bqData.push(bd);
  334. // }
  335. // const tp = this.ctx.helper.mul(d.qty, bd.unit_price, tender.info.decimal.tp);
  336. // bd.tp = this.ctx.helper.add(bd.tp, tp);
  337. // }
  338. // result.bg_common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
  339. // result.bg_more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
  340. // result.bg_great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
  341. //
  342. // const stageBillsFinal = [];
  343. // const preStageBills = preRelaStage
  344. // ? this.ctx.service.stageRelaBillsFinal.getAllDataByCondition({ where: { sid: preRelaStage.sid, rela_tid: preRelaStage.rela_tid } })
  345. // : [];
  346. // for (const b of stageBills) {
  347. // const p = preStageBills.find(x => { return x.lid === b.lid });
  348. // stageBillsFinal.push({
  349. // tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  350. // rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
  351. // lid: b.lid,
  352. // contract_qty: p ? helper.add(p.contract_qty, b.contract_qty) : b.contract_qty,
  353. // contract_tp: p ? helper.add(p.contract_tp, b.contract_tp) : b.contract_tp,
  354. // qc_qty: p ? helper.add(p.qc_qty, b.qc_qty) : b.qc_qty,
  355. // qc_tp: p ? helper.add(p.qc_tp, b.qc_tp) : b.qc_tp,
  356. // });
  357. // if (p) preStageBills.splice(preStageBills.indexOf(p), 1);
  358. // }
  359. // for (const p of preStageBills) {
  360. // stageBillsFinal.push({
  361. // tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  362. // rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
  363. // lid: p.lid,
  364. // contract_qty: p.contract_qty, contract_tp: p.contract_tp,
  365. // qc_qty: p.qc_qty, qc_tp: p.qc_tp,
  366. // });
  367. // }
  368. // return [result, stageBills, stageBillsFinal];
  369. // }
  370. //
  371. // async _getRelaCachePos(stage, preRelaStage) {
  372. // const helper = this.ctx.helper;
  373. // const curPosData = await this.ctx.service.stagePos.getLastestStageData(stage.tid, stage.id);
  374. // const preStagePos = preRelaStage
  375. // ? this.ctx.service.stageRelaPosFinal.getAllDataByCondition({ where: { sid: preRelaStage.sid, rela_tid: preRelaStage.rela_tid } })
  376. // : [];
  377. // const stagePos = [], stagePosFinal = [];
  378. // for (const b of curPosData) {
  379. // if (!stagePos.contract_qty || !stagePos.qc_tp || !stagePos.postil) continue;
  380. // const p = preStagePos.find(x => { return x.pid === b.pid });
  381. // stagePos.push({
  382. // tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  383. // rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
  384. // lid: b.lid, pid: b.pid,
  385. // contract_qty: b.contract_qty, contract_expr: b.contract_expr, qc_qty: b.qc_qty, postil: b.postil,
  386. // });
  387. // stagePosFinal.push({
  388. // tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  389. // rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
  390. // lid: b.lid, pid: b.pid,
  391. // contract_qty: p ? helper.add(p.contract_qty, b.contract_qty) : b.contract_qty,
  392. // qc_qty: p ? helper.add(p.qc_qty, b.qc_qty) : b.qc_qty,
  393. // });
  394. // if (p) stagePosFinal.splice(stagePosFinal.indexOf(p), 1);
  395. // }
  396. // for (const p of stagePosFinal) {
  397. // stagePosFinal.push({
  398. // tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  399. // rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
  400. // lid: p.lid, pid: p.pid,
  401. // contract_qty: p.contract_qty, contract_tp: p.contract_tp,
  402. // qc_qty: p.qc_qty, qc_tp: p.qc_tp,
  403. // });
  404. // }
  405. // return [stagePos, stagePosFinal];
  406. // }
  407. //
  408. // async calculateRelaStage(relaStage) {
  409. // const preRelaStage = await this.getPreRelaStage(this.ctx.tender.id, this.ctx.stage.order, relaStage.tid);
  410. // const cache_org_tp = await this._getCacheOrgTp(relaStage);
  411. // const [cache_tp, stageBills, stageBillsFinal] = await this._calculateRelaStage(relaStage, preRelaStage);
  412. // const [stagePos, stagePosFinal] = await this._getRelaCachePos(relaStage, preRelaStage);
  413. // return { cache_tp, cache_org_tp, stageBills, stageBillsFinal, stagePos, stagePosFinal }
  414. // }
  415. async addStageRela(relaStage) {
  416. const addTime = new Date();
  417. const tender = await this.ctx.service.tender.getDataById(relaStage.tid);
  418. if (!tender) throw '关联标段不存在';
  419. const conn = await this.db.beginTransaction();
  420. try {
  421. const calcModel = new srCache(this.ctx);
  422. await calcModel.calculate(relaStage);
  423. const data = {
  424. tid: this.ctx.tender.id,
  425. sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
  426. rela_tid: relaStage.tid, rela_sid: relaStage.id, rela_sorder: relaStage.order, rela_tname: tender.name,
  427. add_time: addTime, update_time: addTime,
  428. cache_tp: JSON.stringify(calcModel.cache_tp), cache_org_tp: JSON.stringify(calcModel.cache_org_tp),
  429. };
  430. await conn.insert(this.tableName, data);
  431. if (calcModel.stageBills.length > 0) await conn.insert(this.ctx.service.stageRelaBills.tableName, calcModel.stageBills);
  432. if (calcModel.stageBillsFinal.length > 0) await conn.insert(this.ctx.service.stageRelaBillsFinal.tableName, calcModel.stageBillsFinal);
  433. if (calcModel.stagePos.length > 0) await conn.insert(this.ctx.service.stageRelaPos.tableName, calcModel.stagePos);
  434. if (calcModel.stagePosFinal.length > 0) await conn.insert(this.ctx.service.stageRelaPosFinal.tableName, calcModel.stagePosFinal);
  435. if (calcModel.stageIm.length > 0) await conn.insert(this.ctx.service.stageRelaIm.tableName, calcModel.stageIm);
  436. if (calcModel.stageImBills.length > 0) await conn.insert(this.ctx.service.stageRelaImBills.tableName, calcModel.stageImBills);
  437. await conn.update(this.ctx.service.stage.tableName, { id: this.ctx.stage.id, check_calc: 1 });
  438. await conn.commit();
  439. } catch (err) {
  440. await conn.rollback();
  441. throw err;
  442. }
  443. }
  444. async deleteStageRela(id) {
  445. const rela = await this.getDataById(id);
  446. if (!rela) throw '关联标段不存在';
  447. const conn = await this.db.beginTransaction();
  448. try {
  449. await conn.delete(this.tableName, { id: rela.id });
  450. await conn.delete(this.ctx.service.stageRelaBills.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
  451. await conn.delete(this.ctx.service.stageRelaBillsFinal.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
  452. await conn.update(this.ctx.service.stage.tableName, { id: this.ctx.stage.id, check_calc: 1 });
  453. await conn.commit();
  454. } catch (err) {
  455. await conn.rollback();
  456. throw err;
  457. }
  458. }
  459. async updateStageRela(id) {
  460. const rela = await this.getDataById(id);
  461. const relaStage = await this.ctx.service.stage.getDataById(rela.rela_sid);
  462. const conn = await this.db.beginTransaction();
  463. try {
  464. const calcModel = new srCache(this.ctx);
  465. await calcModel.calculate(relaStage);
  466. await conn.update(this.tableName, {
  467. id: rela.id, update_time: new Date(),
  468. cache_tp: JSON.stringify(calcModel.cache_tp), cache_org_tp: JSON.stringify(calcModel.cache_org_tp)
  469. });
  470. await conn.delete(this.ctx.service.stageRelaBills.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
  471. if (calcModel.stageBills.length > 0) await conn.insert(this.ctx.service.stageRelaBills.tableName, calcModel.stageBills);
  472. await conn.delete(this.ctx.service.stageRelaBillsFinal.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
  473. if (calcModel.stageBillsFinal.length > 0) await conn.insert(this.ctx.service.stageRelaBillsFinal.tableName, calcModel.stageBillsFinal);
  474. await conn.delete(this.ctx.service.stageRelaPos.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
  475. if (calcModel.stagePos.length > 0) await conn.insert(this.ctx.service.stageRelaPos.tableName, calcModel.stagePos);
  476. await conn.delete(this.ctx.service.stageRelaPosFinal.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
  477. if (calcModel.stagePosFinal.length > 0) await conn.insert(this.ctx.service.stageRelaPosFinal.tableName, calcModel.stagePosFinal);
  478. await conn.delete(this.ctx.service.stageRelaIm.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
  479. if (calcModel.stageIm.length > 0) await conn.insert(this.ctx.service.stageRelaIm.tableName, calcModel.stageIm);
  480. await conn.delete(this.ctx.service.stageRelaImBills.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
  481. if (calcModel.stageImBills.length > 0) await conn.insert(this.ctx.service.stageRelaImBills.tableName, calcModel.stageImBills);
  482. await conn.update(this.ctx.service.stage.tableName, { id: this.ctx.stage.id, check_calc: 1 });
  483. await conn.commit();
  484. } catch (err) {
  485. await conn.rollback();
  486. throw err;
  487. }
  488. }
  489. async getSumCacheTp (sid) {
  490. const result = {};
  491. const relas = await this.getAllDataByCondition({ where: { sid } });
  492. if (relas.length === 0) return result;
  493. this._analysisStageRela(relas);
  494. for (const r of relas) {
  495. result.contract_tp = this.ctx.helper.add(result.contract_tp, r.cache_tp.contract_tp);
  496. result.qc_tp = this.ctx.helper.add(result.qc_tp, r.cache_tp.qc_tp);
  497. result.gather_tp = this.ctx.helper.add(result.gather_tp, r.cache_tp.gather_tp);
  498. result.gather_tp_100 = this.ctx.helper.add(result.gather_tp_100, r.cache_tp.gather_tp_100);
  499. result.bg_common = this.ctx.helper.add(result.bg_common, r.cache_tp.bg_common);
  500. result.bg_more = this.ctx.helper.add(result.bg_more, r.cache_tp.bg_more);
  501. result.bg_great = this.ctx.helper.add(result.bg_more, r.cache_tp.bg_more);
  502. }
  503. return result;
  504. }
  505. }
  506. return StageRela;
  507. };