stage_rela.js 30 KB

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