stage_rela.js 30 KB

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