rpt_gather_memory.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. const auditConst = require('../const/audit');
  11. const moment = require('moment');
  12. const indexPre = 'id_';
  13. const gatherUtils = {
  14. gatherStage: function (tender, gatherNode, sourceNode, prefix, helper) {
  15. gatherNode[prefix + 'id'] = tender.id;
  16. gatherNode[prefix + 'name'] = tender.name;
  17. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  18. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  19. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  20. gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);
  21. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  22. gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);
  23. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  24. gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);
  25. gatherNode[prefix + "pre_contract_qty"] = helper.add(gatherNode[prefix + "pre_contract_qty"], sourceNode.pre_contract_qty);
  26. gatherNode[prefix + "pre_contract_tp"] = helper.add(gatherNode[prefix + "pre_contract_tp"], sourceNode.pre_contract_tp);
  27. gatherNode[prefix + "pre_qc_qty"] = helper.add(gatherNode[prefix + "pre_qc_qty"], sourceNode.pre_qc_qty);
  28. gatherNode[prefix + "pre_qc_tp"] = helper.add(gatherNode[prefix + "pre_qc_tp"], sourceNode.pre_qc_tp);
  29. gatherNode[prefix + "pre_gather_qty"] = helper.add(gatherNode[prefix + "pre_gather_qty"], sourceNode.pre_gather_qty);
  30. gatherNode[prefix + "pre_gather_tp"] = helper.add(gatherNode[prefix + "pre_gather_tp"], sourceNode.pre_gather_tp);
  31. gatherNode[prefix + "end_contract_qty"] = helper.add(gatherNode[prefix + "end_contract_qty"], sourceNode.end_contract_qty);
  32. gatherNode[prefix + "end_contract_tp"] = helper.add(gatherNode[prefix + "end_contract_tp"], sourceNode.end_contract_tp);
  33. gatherNode[prefix + "end_qc_qty"] = helper.add(gatherNode[prefix + "end_qc_qty"], sourceNode.end_qc_qty);
  34. gatherNode[prefix + "end_qc_tp"] = helper.add(gatherNode[prefix + "end_qc_tp"], sourceNode.end_qc_tp);
  35. gatherNode[prefix + "end_gather_qty"] = helper.add(gatherNode[prefix + "end_gather_qty"], sourceNode.end_gather_qty);
  36. gatherNode[prefix + "end_gather_tp"] = helper.add(gatherNode[prefix + "end_gather_tp"], sourceNode.end_gather_tp);
  37. gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);
  38. gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);
  39. gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['s_' + "contract_qty"], sourceNode.contract_qty);
  40. gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['s_' + "contract_tp"], sourceNode.contract_tp);
  41. gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['s_' + "qc_qty"], sourceNode.qc_qty);
  42. gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['s_' + "qc_tp"], sourceNode.qc_tp);
  43. gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['s_' + "gather_qty"], sourceNode.gather_qty);
  44. gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['s_' + "gather_tp"], sourceNode.gather_tp);
  45. gatherNode['s_' + "pre_contract_qty"] = helper.add(gatherNode['s_' + "pre_contract_qty"], sourceNode.pre_contract_qty);
  46. gatherNode['s_' + "pre_contract_tp"] = helper.add(gatherNode['s_' + "pre_contract_tp"], sourceNode.pre_contract_tp);
  47. gatherNode['s_' + "pre_qc_qty"] = helper.add(gatherNode['s_' + "pre_qc_qty"], sourceNode.pre_qc_qty);
  48. gatherNode['s_' + "pre_qc_tp"] = helper.add(gatherNode['s_' + "pre_qc_tp"], sourceNode.pre_qc_tp);
  49. gatherNode['s_' + "pre_gather_qty"] = helper.add(gatherNode['s_' + "pre_gather_qty"], sourceNode.pre_gather_qty);
  50. gatherNode['s_' + "pre_gather_tp"] = helper.add(gatherNode['s_' + "pre_gather_tp"], sourceNode.pre_gather_tp);
  51. gatherNode['s_' + "end_contract_qty"] = helper.add(gatherNode['s_' + "end_contract_qty"], sourceNode.end_contract_qty);
  52. gatherNode['s_' + "end_contract_tp"] = helper.add(gatherNode['s_' + "end_contract_tp"], sourceNode.end_contract_tp);
  53. gatherNode['s_' + "end_qc_qty"] = helper.add(gatherNode['s_' + "end_qc_qty"], sourceNode.end_qc_qty);
  54. gatherNode['s_' + "end_qc_tp"] = helper.add(gatherNode['s_' + "end_qc_tp"], sourceNode.end_qc_tp);
  55. gatherNode['s_' + "end_gather_qty"] = helper.add(gatherNode['s_' + "end_gather_qty"], sourceNode.end_gather_qty);
  56. gatherNode['s_' + "end_gather_tp"] = helper.add(gatherNode['s_' + "end_gather_tp"], sourceNode.end_gather_tp);
  57. },
  58. gatherZone: function (tender, gatherNode, sourceNode, prefix, helper) {
  59. gatherNode[prefix + 'id'] = tender.id;
  60. gatherNode[prefix + 'name'] = tender.name;
  61. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  62. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  63. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  64. gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);
  65. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  66. gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);
  67. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  68. gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);
  69. gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);
  70. gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);
  71. gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['s_' + "contract_qty"], sourceNode.contract_qty);
  72. gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['s_' + "contract_tp"], sourceNode.contract_tp);
  73. gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['s_' + "qc_qty"], sourceNode.qc_qty);
  74. gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['s_' + "qc_tp"], sourceNode.qc_tp);
  75. gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['s_' + "gather_qty"], sourceNode.gather_qty);
  76. gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['s_' + "gather_tp"], sourceNode.gather_tp);
  77. },
  78. gatherLedger: function (tender, gatherNode, sourceNode, prefix, helper) {
  79. gatherNode[prefix + 'id'] = tender.id;
  80. gatherNode[prefix + 'name'] = tender.name;
  81. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  82. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  83. gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);
  84. gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);
  85. },
  86. gatherSpecial: function (gatherNode, sourceNode, prefix, helper) {
  87. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  88. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  89. },
  90. };
  91. module.exports = app => {
  92. class RptGatherMemory extends app.BaseService {
  93. /**
  94. * 构造函数
  95. *
  96. * @param {Object} ctx - egg全局context
  97. * @return {void}
  98. */
  99. constructor(ctx) {
  100. super(ctx);
  101. this.resultTree = new Ledger.gatherTree(ctx, {
  102. id: 'id',
  103. pid: 'pid',
  104. order: 'order',
  105. level: 'level',
  106. rootId: -1
  107. });
  108. this.resultTenderInfo = [];
  109. }
  110. _checkSpecialTender(tender, special) {
  111. if (special) {
  112. for (const spec of special) {
  113. if (tender[spec.key] === true) return spec.key;
  114. }
  115. }
  116. return '';
  117. }
  118. /**
  119. * 台账数据
  120. */
  121. async _gatherStageData(index, tender, stage, hasPre) {
  122. const helper = this.ctx.helper;
  123. const billsTree = new Ledger.billsTree(this.ctx, {
  124. id: 'ledger_id',
  125. pid: 'ledger_pid',
  126. order: 'order',
  127. level: 'level',
  128. rootId: -1,
  129. keys: ['id', 'tender_id', 'ledger_id'],
  130. stageId: 'id',
  131. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  132. calc: function (node) {
  133. if (node.children && node.children.length === 0) {
  134. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  135. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  136. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  137. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  138. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  139. }
  140. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  141. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  142. node.end_contract_tp = helper.add(node.pre_contract_tp, node.contract_tp);
  143. node.end_qc_tp = helper.add(node.pre_qc_tp, node.qc_tp);
  144. node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
  145. }
  146. });
  147. const billsData = await this.ctx.service.ledger.getData(tender.id);
  148. if (stage) {
  149. await this.ctx.service.stage.doCheckStage(stage);
  150. if (stage.readOnly) {
  151. const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
  152. stage.id, stage.curTimes, stage.curOrder);
  153. this.ctx.helper.assignRelaData(billsData, [
  154. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  155. ]);
  156. } else {
  157. const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
  158. this.ctx.helper.assignRelaData(billsData, [
  159. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  160. ]);
  161. }
  162. if (hasPre) {
  163. const preStage = stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];
  164. this.ctx.helper.assignRelaData(billsData, [
  165. {data: preStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'}
  166. ]);
  167. }
  168. }
  169. billsTree.loadDatas(billsData);
  170. billsTree.calculateAll();
  171. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  172. gatherUtils.gatherStage(tender, gatherNode, sourceNode, 't_' + index + '_', helper);
  173. });
  174. }
  175. async _gatherMonthData(sTender, index, month, hasPre) {
  176. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  177. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
  178. await this._gatherStageData(index, tender, stage, hasPre);
  179. }
  180. async _gatherZoneData(sTender, index, zone) {
  181. const helper = this.ctx.helper;
  182. /**
  183. * 汇总并合并 相关数据
  184. * @param {Array} index - 主数据
  185. * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
  186. */
  187. const sumAssignRelaData = function (index, rela) {
  188. const loadFields = function (datas, fields, prefix, relaId) {
  189. for (const d of datas) {
  190. const key = indexPre + d[relaId];
  191. const m = index[key];
  192. if (m) {
  193. for (const f of fields) {
  194. if (d[f] !== undefined) {
  195. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  196. }
  197. }
  198. }
  199. }
  200. };
  201. for (const r of rela) {
  202. loadFields(r.data, r.fields, r.prefix, r.relaId);
  203. }
  204. };
  205. const billsTree = new Ledger.billsTree(this.ctx, {
  206. id: 'ledger_id',
  207. pid: 'ledger_pid',
  208. order: 'order',
  209. level: 'level',
  210. rootId: -1,
  211. keys: ['id', 'tender_id', 'ledger_id'],
  212. stageId: 'id',
  213. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
  214. calc: function (node) {
  215. if (node.children && node.children.length === 0) {
  216. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  217. }
  218. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  219. }
  220. });
  221. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  222. const billsData = await this.ctx.service.ledger.getData(tender.id);
  223. let billsIndexData = {};
  224. for (const bd of billsData) {
  225. billsIndexData[indexPre + bd.id] = bd;
  226. }
  227. const times = zone.split(' - ');
  228. if (times.length !== 2) throw '选择的汇总周期无效';
  229. const beginTime = moment(times[0], 'YYYY-MM').date();
  230. const endTime = moment(times[1], 'YYYY-MM').date();
  231. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
  232. for (const stage of stages) {
  233. const sTime = moment(stage.s_time, 'YYYY-MM').date();
  234. if (sTime >= beginTime && sTime <= endTime) {
  235. await this.ctx.service.stage.doCheckStage(stage);
  236. if (stage.readOnly) {
  237. const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
  238. stage.id, stage.curTimes, stage.curOrder);
  239. sumAssignRelaData(billsIndexData, [
  240. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  241. ]);
  242. } else {
  243. const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
  244. sumAssignRelaData(billsIndexData, [
  245. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  246. ]);
  247. }
  248. }
  249. }
  250. billsTree.loadDatas(billsData);
  251. billsTree.calculateAll();
  252. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  253. gatherUtils.gatherZone(gatherNode, sourceNode, 't_' + index + '_', helper);
  254. });
  255. }
  256. async _gatherFinalData(sTender, index, hasPre) {
  257. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  258. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  259. where: { tid: tender.id },
  260. orders: [['order', 'desc']],
  261. });
  262. if (stages.length !== 0) {
  263. const lastStage = stages[0];
  264. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  265. stages.splice(0, 1);
  266. }
  267. }
  268. await this._gatherStageData(index, tender, stages[0], hasPre);
  269. }
  270. async _gatherCheckedFinalData(sTender, index, hasPre) {
  271. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  272. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  273. where: { tid: tender.id },
  274. orders: [['order', 'desc']],
  275. });
  276. if (stages.length !== 0) {
  277. const lastStage = stages[0];
  278. if (lastStage.status !== auditConst.stage.status.checked) {
  279. stages.splice(0, 1);
  280. }
  281. }
  282. await this._gatherStageData(index, tender, stages[0], hasPre);
  283. }
  284. async _gatherLedgerData(sTender, index) {
  285. const helper = this.ctx.helper;
  286. const billsTree = new Ledger.billsTree(this.ctx, {
  287. id: 'ledger_id',
  288. pid: 'ledger_pid',
  289. order: 'order',
  290. level: 'level',
  291. rootId: -1,
  292. keys: ['id', 'tender_id', 'ledger_id'],
  293. stageId: 'id',
  294. calcFields: ['deal_tp', 'total_price'],
  295. });
  296. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  297. const billsData = await this.ctx.service.ledger.getData(tender.id);
  298. billsTree.loadDatas(billsData);
  299. billsTree.calculateAll();
  300. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  301. gatherUtils.gatherLedger(tender, gatherNode, sourceNode, 't_' + index + '_', helper);
  302. })
  303. }
  304. async _gatherSpecialData(sTender, sKey) {
  305. const helper = this.ctx.helper;
  306. const billsTree = new Ledger.billsTree(this.ctx, {
  307. id: 'ledger_id',
  308. pid: 'ledger_pid',
  309. order: 'order',
  310. level: 'level',
  311. rootId: -1,
  312. keys: ['id', 'tender_id', 'ledger_id'],
  313. stageId: 'id',
  314. calcFields: ['deal_tp', 'total_price'],
  315. });
  316. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  317. const billsData = await this.ctx.service.ledger.getData(tender.id);
  318. billsTree.loadDatas(billsData);
  319. billsTree.calculateAll();
  320. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  321. gatherUtils.gatherSpecial(gatherNode, sourceNode, 'ts_' + sKey + '_', helper);
  322. })
  323. }
  324. async getGatherStageBills(memFieldKeys, gsDefine, gsCustom) {
  325. if (!gsDefine || !gsDefine.enable) return [];
  326. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  327. const gsSetting = JSON.parse(gsDefine.setting);
  328. let commonIndex = 0;
  329. for (const tender of gsCustom.tenders) {
  330. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  331. if (specialKey === '') {
  332. switch (gsSetting.type) {
  333. case 'month':
  334. await this._gatherMonthData(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  335. break;
  336. case 'zone':
  337. await this._gatherZoneData(tender, commonIndex, gsCustom.zone);
  338. break;
  339. case 'final':
  340. await this._gatherFinalData(tender, commonIndex, gsSetting.hasPre);
  341. break;
  342. case 'checked-final':
  343. await this._gatherCheckedFinalData(tender, commonIndex, gsSetting.hasPre);
  344. break;
  345. case 'ledger':
  346. await this._gatherLedgerData(tender, commonIndex);
  347. break;
  348. }
  349. commonIndex++;
  350. } else {
  351. await this._gatherSpecialData(tender, specialKey);
  352. }
  353. }
  354. this.resultTree.generateSortNodes();
  355. return this.resultTree.getDefaultDatas();
  356. }
  357. /**
  358. * 标段信息
  359. */
  360. async _getBaseTenderInfo(tender) {
  361. const info = {
  362. id: tender.id,
  363. name: tender.name,
  364. deal_tp: tender.deal_tp,
  365. tp: tender.total_price
  366. };
  367. if (tender.ledger_status === auditConst.ledger.status.uncheck || tender.ledger_status === auditConst.ledger.status.checkNo) {
  368. const sum = await this.ctx.service.ledger.addUp({tender_id: tender.id/*, is_leaf: true*/});
  369. info.tp = sum.total_price;
  370. info.deal_tp = sum.deal_tp;
  371. }
  372. return info;
  373. }
  374. async _getStageTenderInfo(stage, info) {
  375. if (stage) {
  376. const helper = this.ctx.helper;
  377. await this.ctx.service.stage.doCheckStage(stage);
  378. await this.ctx.service.stage.checkStageGatherData(stage);
  379. info.pre_contract_tp = stage.pre_contract_tp;
  380. info.pre_qc_tp = stage.pre_qc_tp;
  381. info.pre_gather_tp = helper.add(info.pre_contract_tp, info.pre_qc_tp);
  382. info.contract_tp = stage.contract_tp;
  383. info.qc_tp = stage.qc_tp;
  384. info.gather_tp = helper.add(info.contract_tp, info.qc_tp);
  385. info.end_contract_tp = helper.add(info.pre_contract_tp, info.contract_tp);
  386. info.end_qc_tp = helper.add(info.pre_qc_tp, info.qc_tp);
  387. info.end_gather_tp = helper.add(info.pre_gather_tp, info.gather_tp);
  388. info.yf = stage.yf;
  389. info.pre_yf = stage.pre_yf;
  390. info.end_yf = helper.add(stage.yf, stage.pre_yf);
  391. }
  392. }
  393. async _gatherMonthTenderInfo(sTender, index, month, hasPre) {
  394. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  395. const info = this._getBaseTenderInfo(tender);
  396. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
  397. this._getStageTenderInfo(stage, info);
  398. this.resultTenderInfo.push(info);
  399. }
  400. async _gatherZoneTenderInfo(sTender, index, zone) {
  401. const helper = this.ctx.helper;
  402. /**
  403. * 汇总并合并 相关数据
  404. * @param {Array} index - 主数据
  405. * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
  406. */
  407. const sumAssignRelaData = function (index, rela) {
  408. const loadFields = function (datas, fields, prefix, relaId) {
  409. for (const d of datas) {
  410. const key = indexPre + d[relaId];
  411. const m = index[key];
  412. if (m) {
  413. for (const f of fields) {
  414. if (d[f] !== undefined) {
  415. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  416. }
  417. }
  418. }
  419. }
  420. };
  421. for (const r of rela) {
  422. loadFields(r.data, r.fields, r.prefix, r.relaId);
  423. }
  424. };
  425. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  426. const info = this._getBaseTenderInfo(tender);
  427. const times = zone.split(' - ');
  428. if (times.length !== 2) throw '选择的汇总周期无效';
  429. const beginTime = moment(times[0], 'YYYY-MM').date();
  430. const endTime = moment(times[1], 'YYYY-MM').date();
  431. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
  432. for (const stage of stages) {
  433. const sTime = moment(stage.s_time, 'YYYY-MM').date();
  434. if (sTime >= beginTime && sTime <= endTime) {
  435. await this.ctx.service.stage.doCheckStage(stage);
  436. await this.ctx.service.stage.checkStageGatherData(stage);
  437. info.contract_tp = helper.add(info.contract_tp, stage.contract_tp);
  438. info.qc_tp = helper.add(info.qc_tp, stage.qc_tp);
  439. info.yf = helper.add(info.yf, stage.yf);
  440. }
  441. }
  442. info.gather_tp = helper.add(info.contract_tp, info.qc_tp);
  443. this.resultTenderInfo.push(info);
  444. }
  445. async _gatherFinalTenderInfo(sTender, index, hasPre) {
  446. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  447. const info = this._getBaseTenderInfo(tender);
  448. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  449. where: { tid: tender.id },
  450. orders: [['order', 'desc']],
  451. });
  452. if (stages.length !== 0) {
  453. const lastStage = stages[0];
  454. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  455. stages.splice(0, 1);
  456. }
  457. }
  458. await this._getStageTenderInfo(stages[0], info);
  459. this.resultTenderInfo.push(info);
  460. }
  461. async _gatherCheckedFinalTenderInfo(sTender, index, hasPre) {
  462. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  463. const info = this._getBaseTenderInfo(tender);
  464. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  465. where: { tid: tender.id },
  466. orders: [['order', 'desc']],
  467. });
  468. if (stages.length !== 0) {
  469. const lastStage = stages[0];
  470. if (lastStage.status !== auditConst.stage.status.checked) {
  471. stages.splice(0, 1);
  472. }
  473. }
  474. await this._getStageTenderInfo(stages[0], info);
  475. this.resultTenderInfo.push(info);
  476. }
  477. async _gatherLedgerTenderInfo(sTender, index) {
  478. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  479. const info = this._getBaseTenderInfo(tender);
  480. this.resultTenderInfo.push(info);
  481. }
  482. async _gatherSpecialTenderInfo(sTender, sKey) {
  483. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  484. const info = this._getBaseTenderInfo(tender);
  485. info.spec = sKey;
  486. this.resultTenderInfo.push(info);
  487. }
  488. async getGatherTenderInfo(memFieldKeys, gsDefine, gsCustom) {
  489. if (!gsDefine || !gsDefine.enable) return [];
  490. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  491. this.resultTenderInfo = [];
  492. const gsSetting = JSON.parse(gsDefine.setting);
  493. let commonIndex = 0;
  494. for (const tender of gsCustom.tenders) {
  495. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  496. if (specialKey === '') {
  497. switch (gsSetting.type) {
  498. case 'month':
  499. await this._gatherMonthTenderInfo(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  500. break;
  501. case 'zone':
  502. await this._gatherZoneTenderInfo(tender, commonIndex, gsCustom.zone);
  503. break;
  504. case 'final':
  505. await this._gatherFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
  506. break;
  507. case 'checked-final':
  508. await this._gatherCheckedFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
  509. break;
  510. case 'ledger':
  511. await this._gatherLedgerTenderInfo(tender, commonIndex);
  512. break;
  513. }
  514. commonIndex++;
  515. } else {
  516. await this._gatherSpecialTenderInfo(tender, specialKey);
  517. }
  518. }
  519. }
  520. }
  521. return RptGatherMemory;
  522. };