rpt_gather_memory.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. }
  109. _checkSpecialTender(tender, special) {
  110. if (special) {
  111. for (const spec of special) {
  112. if (tender[spec.key] === true) return spec.key;
  113. }
  114. }
  115. return '';
  116. }
  117. async _gatherStageData(index, tender, stage, hasPre) {
  118. const helper = this.ctx.helper;
  119. const billsTree = new Ledger.billsTree(this.ctx, {
  120. id: 'ledger_id',
  121. pid: 'ledger_pid',
  122. order: 'order',
  123. level: 'level',
  124. rootId: -1,
  125. keys: ['id', 'tender_id', 'ledger_id'],
  126. stageId: 'id',
  127. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  128. calc: function (node) {
  129. if (node.children && node.children.length === 0) {
  130. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  131. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  132. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  133. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  134. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  135. }
  136. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  137. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  138. node.end_contract_tp = helper.add(node.pre_contract_tp, node.contract_tp);
  139. node.end_qc_tp = helper.add(node.pre_qc_tp, node.qc_tp);
  140. node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
  141. }
  142. });
  143. const billsData = await this.ctx.service.ledger.getData(tender.id);
  144. if (stage) {
  145. await this.ctx.service.stage.doCheckStage(stage);
  146. if (stage.readOnly) {
  147. const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
  148. stage.id, stage.curTimes, stage.curOrder);
  149. this.ctx.helper.assignRelaData(billsData, [
  150. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  151. ]);
  152. } else {
  153. const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
  154. this.ctx.helper.assignRelaData(billsData, [
  155. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  156. ]);
  157. }
  158. if (hasPre) {
  159. const preStage = stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];
  160. this.ctx.helper.assignRelaData(billsData, [
  161. {data: preStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'}
  162. ]);
  163. }
  164. }
  165. billsTree.loadDatas(billsData);
  166. billsTree.calculateAll();
  167. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  168. gatherUtils.gatherStage(tender, gatherNode, sourceNode, 't_' + index + '_', helper);
  169. });
  170. }
  171. async _gatherMonthData(sTender, index, month, hasPre) {
  172. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  173. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
  174. await this._gatherStageData(index, tender, stage, hasPre);
  175. }
  176. async _gatherZoneData(sTender, index, zone) {
  177. const helper = this.ctx.helper;
  178. /**
  179. * 汇总并合并 相关数据
  180. * @param {Array} index - 主数据
  181. * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
  182. */
  183. const sumAssignRelaData = function (index, rela) {
  184. const loadFields = function (datas, fields, prefix, relaId) {
  185. for (const d of datas) {
  186. const key = indexPre + d[relaId];
  187. const m = index[key];
  188. if (m) {
  189. for (const f of fields) {
  190. if (d[f] !== undefined) {
  191. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  192. }
  193. }
  194. }
  195. }
  196. };
  197. for (const r of rela) {
  198. loadFields(r.data, r.fields, r.prefix, r.relaId);
  199. }
  200. };
  201. const billsTree = new Ledger.billsTree(this.ctx, {
  202. id: 'ledger_id',
  203. pid: 'ledger_pid',
  204. order: 'order',
  205. level: 'level',
  206. rootId: -1,
  207. keys: ['id', 'tender_id', 'ledger_id'],
  208. stageId: 'id',
  209. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
  210. calc: function (node) {
  211. if (node.children && node.children.length === 0) {
  212. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  213. }
  214. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  215. }
  216. });
  217. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  218. const billsData = await this.ctx.service.ledger.getData(tender.id);
  219. let billsIndexData = {};
  220. for (const bd of billsData) {
  221. billsIndexData[indexPre + bd.id] = bd;
  222. }
  223. const times = zone.split(' - ');
  224. if (times.length !== 2) throw '选择的汇总周期无效';
  225. const beginTime = moment(times[0], 'YYYY-MM').date();
  226. const endTime = moment(times[1], 'YYYY-MM').date();
  227. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
  228. for (const stage of stages) {
  229. const sTime = moment(stage.s_time, 'YYYY-MM').date();
  230. if (sTime >= beginTime && sTime <= endTime) {
  231. await this.ctx.service.stage.doCheckStage(stage);
  232. if (stage.readOnly) {
  233. const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
  234. stage.id, stage.curTimes, stage.curOrder);
  235. sumAssignRelaData(billsIndexData, [
  236. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  237. ]);
  238. } else {
  239. const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
  240. sumAssignRelaData(billsIndexData, [
  241. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  242. ]);
  243. }
  244. }
  245. }
  246. billsTree.loadDatas(billsData);
  247. billsTree.calculateAll();
  248. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  249. gatherUtils.gatherZone(gatherNode, sourceNode, 't_' + index + '_', helper);
  250. });
  251. }
  252. async _gatherFinalData(sTender, index, hasPre) {
  253. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  254. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  255. where: { tid: tender.id },
  256. orders: [['order', 'desc']],
  257. });
  258. if (stages.length !== 0) {
  259. const lastStage = stages[0];
  260. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  261. stages.splice(0, 1);
  262. }
  263. }
  264. await this._gatherStageData(index, tender, stages[0], hasPre);
  265. }
  266. async _gatherCheckedFinalData(sTender, index, hasPre) {
  267. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  268. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  269. where: { tid: tender.id },
  270. orders: [['order', 'desc']],
  271. });
  272. if (stages.length !== 0) {
  273. const lastStage = stages[0];
  274. if (lastStage.status !== auditConst.stage.status.checked) {
  275. stages.splice(0, 1);
  276. }
  277. }
  278. await this._gatherStageData(index, tender, stages[0], hasPre);
  279. }
  280. async _gatherLegerData(sTender, index) {
  281. const helper = this.ctx.helper;
  282. const billsTree = new Ledger.billsTree(this.ctx, {
  283. id: 'ledger_id',
  284. pid: 'ledger_pid',
  285. order: 'order',
  286. level: 'level',
  287. rootId: -1,
  288. keys: ['id', 'tender_id', 'ledger_id'],
  289. stageId: 'id',
  290. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  291. });
  292. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  293. const billsData = await this.ctx.service.ledger.getData(tender.id);
  294. billsTree.loadDatas(billsData);
  295. billsTree.calculateAll();
  296. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  297. gatherUtils.gatherLedger(tender, gatherNode, sourceNode, 't_' + index + '_', helper);
  298. })
  299. }
  300. async _gatherSpecialData(sTender, sKey) {
  301. const helper = this.ctx.helper;
  302. const billsTree = new Ledger.billsTree(this.ctx, {
  303. id: 'ledger_id',
  304. pid: 'ledger_pid',
  305. order: 'order',
  306. level: 'level',
  307. rootId: -1,
  308. keys: ['id', 'tender_id', 'ledger_id'],
  309. stageId: 'id',
  310. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  311. });
  312. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  313. const billsData = await this.ctx.service.ledger.getData(tender.id);
  314. billsTree.loadDatas(billsData);
  315. billsTree.calculateAll();
  316. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  317. gatherUtils.gatherSpecial(gatherNode, sourceNode, 'ts_' + sKey + '_', helper);
  318. })
  319. }
  320. async getGatherStageBills(memFieldKeys, gsDefine, gsCustom) {
  321. if (!gsDefine || !gsDefine.enable) return [];
  322. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  323. const gsSetting = JSON.parse(gsDefine.setting);
  324. let commonIndex = 0;
  325. for (const tender of gsCustom.tenders) {
  326. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  327. if (specialKey === '') {
  328. switch (gsSetting.type) {
  329. case 'month':
  330. await this._gatherMonthData(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  331. break;
  332. case 'zone':
  333. await this._gatherZoneData(tender, commonIndex, gsCustom.zone);
  334. break;
  335. case 'final':
  336. await this._gatherFinalData(tender, commonIndex, gsSetting.hasPre);
  337. break;
  338. case 'checked-final':
  339. await this._gatherCheckedFinalData(tender, commonIndex, gsSetting.hasPre);
  340. break;
  341. case 'ledger':
  342. await this._gatherLegerData(tender, commonIndex);
  343. break;
  344. }
  345. commonIndex++;
  346. } else {
  347. await this._gatherSpecialData(tender, specialKey);
  348. }
  349. }
  350. this.resultTree.generateSortNodes();
  351. return this.resultTree.getDefaultDatas();
  352. }
  353. }
  354. return RptGatherMemory;
  355. };