rpt_gather_memory.js 19 KB

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