stage_rela.js 30 KB

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