spss_controller.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. 'use strict';
  2. /**
  3. * 标段对比 控制器
  4. *
  5. * @author Mai
  6. * @date 2020/2/21
  7. * @version
  8. */
  9. const measureType = require('../const/tender').measureType;
  10. const status = require('../const/audit').stage.status;
  11. const moment = require('moment');
  12. module.exports = app => {
  13. class SpssController extends app.BaseController {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局context
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. ctx.showProject = true;
  23. }
  24. async info(ctx) {
  25. try {
  26. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  27. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_info');
  28. const renderData = {
  29. categoryData,
  30. stashList,
  31. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherInfo)
  32. };
  33. await this.layout('spss/gather_info.ejs', renderData, 'spss/gather_info_modal.ejs');
  34. } catch (err) {
  35. ctx.helper.log(err);
  36. }
  37. }
  38. async gatherLedger(ctx) {
  39. try {
  40. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  41. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_ledger');
  42. const renderData = {
  43. categoryData,
  44. stashList,
  45. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherLedger)
  46. };
  47. await this.layout('spss/gather_ledger.ejs', renderData, 'spss/spss_modal.ejs');
  48. } catch (err) {
  49. ctx.helper.log(err);
  50. }
  51. }
  52. async gatherStage(ctx) {
  53. try {
  54. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  55. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_stage');
  56. const renderData = {
  57. categoryData,
  58. stashList,
  59. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherStage)
  60. };
  61. await this.layout('spss/gather_stage.ejs', renderData, 'spss/spss_modal.ejs');
  62. } catch (err) {
  63. ctx.helper.log(err);
  64. }
  65. }
  66. async gatherStageExtra(ctx) {
  67. try {
  68. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  69. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_stage_extra');
  70. const renderData = {
  71. categoryData,
  72. stashList,
  73. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherStageExtra)
  74. };
  75. await this.layout('spss/gather_stage_extra.ejs', renderData, 'spss/spss_modal.ejs');
  76. } catch (err) {
  77. ctx.helper.log(err);
  78. }
  79. }
  80. async gatherStagePay(ctx) {
  81. try {
  82. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  83. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_stage_pay');
  84. const renderData = {
  85. categoryData,
  86. stashList,
  87. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherStagePay)
  88. };
  89. await this.layout('spss/gather_stage_pay.ejs', renderData, 'spss/spss_modal.ejs');
  90. } catch(err) {
  91. ctx.log(err);
  92. }
  93. }
  94. async compareLedger(ctx) {
  95. try {
  96. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  97. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'compare_ledger');
  98. const renderData = {
  99. categoryData,
  100. stashList,
  101. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.compareLedger)
  102. };
  103. await this.layout('spss/compare_ledger.ejs', renderData, 'spss/spss_modal.ejs');
  104. } catch (err) {
  105. ctx.helper.log(err);
  106. }
  107. }
  108. async compareStage(ctx) {
  109. try {
  110. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  111. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'compare_stage');
  112. const renderData = {
  113. categoryData,
  114. stashList,
  115. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.compareStage),
  116. };
  117. await this.layout('spss/compare_stage.ejs', renderData, 'spss/spss_modal.ejs');
  118. } catch (err) {
  119. ctx.helper.log(err);
  120. }
  121. }
  122. async _loadStageTpData(info, tender, stage, loadPre = true) {
  123. if (!stage) return;
  124. if (stage.readOnly && stage.status === status.uncheck) return;
  125. if (loadPre) {
  126. info.pre_contract_tp = stage.pre_contract_tp;
  127. info.pre_qc_tp = stage.pre_qc_tp;
  128. info.pre_yf_tp = stage.pre_yf_tp;
  129. info.pre_sf_tp = stage.pre_sf_tp;
  130. }
  131. if (!stage.readOnly) await this.ctx.service.stage.checkStageGatherData(stage, this.ctx.session.sessionUser.is_admin);
  132. info.contract_tp = this.ctx.helper.add(stage.contract_tp, info.contract_tp);
  133. info.qc_tp = this.ctx.helper.add(stage.qc_tp, info.qc_tp);
  134. info.contract_pc_tp = this.ctx.helper.add(stage.contract_pc_tp, info.contract_pc_tp);
  135. info.qc_pc_tp = this.ctx.helper.add(stage.qc_pc_tp, info.qc_pc_tp);
  136. info.pc_tp = this.ctx.helper.add(stage.pc_tp, info.pc_tp);
  137. info.yf_tp = this.ctx.helper.add(stage.yf_tp, info.yf_tp);
  138. info.sf_tp = this.ctx.helper.add(stage.sf_tp, info.sf_tp);
  139. }
  140. async _loadStagesTpData(info, tender, stages, preStage, endStage) {
  141. if (preStage) {
  142. info.pre_contract_tp = this.ctx.helper.sum([preStage.contract_tp, preStage.pre_contract_tp, preStage.contract_pc_tp]);
  143. info.pre_contract_tp = this.ctx.helper.sum([preStage.qc_tp, preStage.pre_qc_tp, preStage.qc_pc_tp]);
  144. info.pre_yf_tp = this.ctx.helper.add(preStage.yf_tp, preStage.pre_yf_tp);
  145. info.pre_sf_tp = this.ctx.helper.add(preStage.sf_tp, preStage.pre_sf_tp);
  146. }
  147. for (const stage of stages) {
  148. await this.ctx.service.stage.doCheckStage(stage);
  149. await this._loadStageTpData(info, tender, stage, false);
  150. }
  151. }
  152. async _loadInfoData(tender, filter) {
  153. const info = { measure_type_str: '', total_price: tender.total_price };
  154. if (tender.measure_type === measureType.tz.value) info.measure_type_str = measureType.tz.title;
  155. if (tender.measure_type === measureType.gcl.value) info.measure_type_str = measureType.gcl.title;
  156. await this.ctx.service.tenderCache.loadTenderCache(tender, this.ctx.session.sessionUser.accountId);
  157. info.contract_price = tender.contract_price;
  158. info.advance_tp = tender.advance_tp;
  159. info.change_tp = tender.change_tp;
  160. info.progress = tender.progress;
  161. if (filter.type === 'stage') {
  162. await this._loadStageTpData(info, tender, filter.stage);
  163. } else {
  164. await this._loadStagesTpData(info, tender, filter.stages, filter.preStage, filter.endStage);
  165. }
  166. info.pre_gather_tp = this.ctx.helper.add(info.pre_contract_tp, info.pre_qc_tp);
  167. info.gather_tp = this.ctx.helper.sum([info.contract_tp, info.qc_tp, info.pc_tp]);
  168. info.end_contract_tp = this.ctx.helper.sum([info.contract_tp, info.contract_pc_tp, info.pre_contract_tp]);
  169. info.end_qc_tp = this.ctx.helper.sum([info.qc_tp, info.qc_pc_tp, info.pre_qc_tp]);
  170. info.end_gather_tp = this.ctx.helper.add(info.gather_tp, info.pre_gather_tp);
  171. info.end_sf_tp = this.ctx.helper.add(info.sf_tp, info.pre_sf_tp);
  172. info.end_yf_tp = this.ctx.helper.add(info.yf_tp, info.pre_yf_tp);
  173. return info;
  174. }
  175. async _loadLedgerData(tender) {
  176. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  177. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'full_path', 'is_leaf', 'order', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  178. where: { tender_id: tender.id },
  179. });
  180. const pos = await this.ctx.service.pos.getAllDataByCondition({
  181. columns: ['id', 'lid', 'name', 'quantity', 'porder'],
  182. where: { tid: tender.id },
  183. });
  184. return [bills, pos];
  185. }
  186. async _loadDealBillsData(tender) {
  187. }
  188. async _getValidStages(tenderId, sort = 'desc', checked = false) {
  189. const accountId = this.ctx.session.sessionUser.accountId;
  190. const condition = { tid: tenderId };
  191. if (checked) condition.status = status.checked;
  192. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: condition, orders: [['order', sort]] });
  193. return stages.filter(s => { return s.status !== status.uncheck || s.user_id === accountId; });
  194. }
  195. async _filterOrder(tender, order, checked) {
  196. const stages = await this._getValidStages(tender.id, 'desc', checked);
  197. const stage = this.ctx.helper._.find(stages, { order });
  198. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  199. return { type: 'stage', stage, filter: `第${order}期` };
  200. }
  201. async _filterMonth(tender, month, checked) {
  202. const stages = await this._getValidStages(tender.id, 'desc', checked);
  203. const stage = this.ctx.helper._.find(stages, { s_time: month });
  204. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  205. return { type: 'stage', stage, filter: month };
  206. }
  207. async _filterFinal(tender, checked) {
  208. const stages = await this._getValidStages(tender.id, 'desc', checked);
  209. const stage = stages[0];
  210. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  211. return { type: 'stage', stage, filter: `第${stage.order}期` };
  212. }
  213. async _filterCheckedFinal(tender, checked) {
  214. const stages = await this._getValidStages(tender.id, 'desc', checked);
  215. const checkedStages = stages.filter(x => { return x.status === status.checked; });
  216. const stage = checkedStages[0];
  217. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  218. return { type: 'stage', stage, filter: `第${stage.order}期` };
  219. }
  220. async _filterOrderZone(tender, zone, checked) {
  221. let [iBegin, iEnd] = zone.split(':');
  222. iBegin = this.ctx.helper._.toInteger(iBegin) || 0;
  223. iEnd = this.ctx.helper._.toInteger(iEnd) || 0;
  224. const stages = await this._getValidStages(tender.id, 'asc', checked), validStages = [];
  225. let preStage, endStage;
  226. for (const stage of stages) {
  227. if (stage.order < iBegin) {
  228. if (!preStage || preStage.order < stage.order) preStage = stage;
  229. } else if (stage.order > iEnd) {
  230. if (!endStage || endStage.order > stage.order) endStage = stage;
  231. } else {
  232. validStages.push(stage);
  233. }
  234. }
  235. return { type: 'stages', stages: validStages, preStage, endStage, filter: `第${iBegin}期 ~ 第${iEnd}期` };
  236. }
  237. async _filterTimeZone(tender, zone, checked) {
  238. const times = zone.split(' - ');
  239. if (times.length !== 2) throw '选择的汇总周期无效';
  240. const beginTime = moment(times[0], 'YYYY-MM');
  241. const endTime = moment(times[1], 'YYYY-MM');
  242. const stages = await this._getValidStages(tender.id, 'asc', checked), validStages = [];
  243. let preStage, endStage;
  244. for (const stage of stages) {
  245. const sTime = moment(stage.s_time, 'YYYY-MM');
  246. if (sTime.isBetween(beginTime, endTime, null, '[]')) {
  247. validStages.push(stage);
  248. } else if (sTime.isBefore(beginTime)) {
  249. if (!preStage || moment(preStage.s_time, 'YYYY-MM').isBefore(sTime)) preStage = stage;
  250. } else if (sTime.isAfter(endTime)) {
  251. if (!endStage || moment(endStage.s_time, 'YYYY-MM').isAfter(sTime)) endStage = stage;
  252. }
  253. }
  254. return { type: 'stages', stages: validStages, preStage, endStage, filter: zone };
  255. }
  256. async _filterStages(tender, stageInfo) {
  257. switch (stageInfo.type) {
  258. case 'stage':
  259. return await this._filterOrder(tender, stageInfo.stage, stageInfo.checked);
  260. case 'month':
  261. return await this._filterMonth(tender, stageInfo.month, stageInfo.checked);
  262. case 'final':
  263. return await this._filterFinal(tender, stageInfo.checked);
  264. case 'checked-final':
  265. return await this._filterCheckedFinal(tender, stageInfo.checked);
  266. case 'zone':
  267. return await this._filterTimeZone(tender, stageInfo.zone, stageInfo.checked);
  268. case 'stage-zone':
  269. return await this._filterOrderZone(tender, stageInfo.stage_zone, stageInfo.checked);
  270. default:
  271. return;
  272. }
  273. }
  274. async _loadStageData(tender, stage) {
  275. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  276. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'full_path', 'is_leaf', 'order', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  277. where: { tender_id: tender.id },
  278. });
  279. const pos = await this.ctx.service.pos.getAllDataByCondition({
  280. columns: ['id', 'lid', 'name', 'quantity', 'porder'],
  281. where: { tid: tender.id },
  282. });
  283. if (!stage) return [bills, pos];
  284. const curStage = stage.readOnly
  285. ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  286. : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
  287. const preStage = stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
  288. const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
  289. this.ctx.helper.assignRelaData(bills, [
  290. { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  291. { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  292. { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
  293. ]);
  294. const curStagePos = stage.readOnly
  295. ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  296. : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
  297. const preStagePos = stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
  298. this.ctx.helper.assignRelaData(pos, [
  299. { data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  300. { data: preStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
  301. ]);
  302. return [bills, pos];
  303. }
  304. async _loadStagesData(tender, stages, preStage, endStage) {
  305. const indexPre = 'id_', helper = this.ctx.helper;
  306. const sumAssignRelaData = function (index, rela) {
  307. const loadFields = function (datas, fields, prefix, relaId) {
  308. for (const d of datas) {
  309. const key = indexPre + d[relaId];
  310. const m = index[key];
  311. if (m) {
  312. for (const f of fields) {
  313. if (d[f] !== undefined) {
  314. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  315. }
  316. }
  317. }
  318. }
  319. };
  320. for (const r of rela) {
  321. loadFields(r.data, r.fields, r.prefix, r.relaId);
  322. }
  323. };
  324. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  325. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'full_path', 'is_leaf', 'order', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  326. where: { tender_id: tender.id },
  327. });
  328. const pos = await this.ctx.service.pos.getAllDataByCondition({
  329. columns: ['id', 'lid', 'name', 'quantity', 'porder'],
  330. where: { tid: tender.id },
  331. });
  332. let billsIndexData = {};
  333. for (const bd of bills) {
  334. billsIndexData[indexPre + bd.id] = bd;
  335. }
  336. let posIndexData = {};
  337. for (const p of pos) {
  338. posIndexData[indexPre + p.id] = p;
  339. }
  340. if (preStage) {
  341. const endStage = await this.ctx.service.stageBillsFinal.getFinalData(tender, preStage.order);
  342. sumAssignRelaData(billsIndexData, [
  343. { data: endStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'},
  344. ]);
  345. const endStagePos = await this.ctx.service.stagePosFinal.getFinalData(tender, preStage.order);
  346. sumAssignRelaData(posIndexData, [
  347. {data: endStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'},
  348. ]);
  349. }
  350. for (const stage of stages) {
  351. await this.ctx.service.stage.doCheckStage(stage);
  352. const curStage = stage.readOnly
  353. ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  354. : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
  355. const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
  356. sumAssignRelaData(billsIndexData, [
  357. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'},
  358. { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
  359. ]);
  360. const curStagePos = stage.readOnly
  361. ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  362. : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
  363. sumAssignRelaData(posIndexData, [
  364. {data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'},
  365. ]);
  366. }
  367. return [bills, pos];
  368. }
  369. async _loadStageLedgerData(tender, filter) {
  370. return filter.type === 'stage'
  371. ? await this._loadStageData(tender, filter.stage)
  372. : await this._loadStagesData(tender, filter.stages, filter.preStage, filter.endStage);
  373. }
  374. async _loadStagePayData(tender, stage) {
  375. if (!stage) return [];
  376. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  377. if (!stage.readOnly) {
  378. // 计算 本期金额
  379. const PayCalculator = require('../lib/pay_calc');
  380. const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
  381. await payCalculator.calculateAll(dealPay);
  382. }
  383. return dealPay;
  384. }
  385. async _loadStagesPayData(tender, stages, preStage, endStage) {
  386. // todo
  387. return [];
  388. }
  389. async _loadPayData(tender, filter) {
  390. return filter.type === 'stage'
  391. ? await this._loadStagePayData(tender, filter.stage)
  392. : await this._loadStagesPayData(tender, filter.stage, filter.preStage, filter.endStage);
  393. }
  394. async _loadStageJgclData(tender, stage) {
  395. if (!stage) return [];
  396. const data = await this.ctx.service.stageJgcl.getStageData(stage);
  397. const preData = stage.preCheckedStage ? await this.ctx.service.stageJgcl.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  398. for (const d of data) {
  399. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  400. if (pd) {
  401. d.pre_arrive_qty = pd.arrive_qty;
  402. d.pre_arrive_tp = pd.arrive_tp;
  403. d.pre_deduct_qty = pd.deduct_qty;
  404. d.pre_deduct_tp = pd.deduct_tp;
  405. }
  406. d.end_arrive_qty = this.ctx.helper.add(d.pre_arrive_qty, d.arrive_qty);
  407. d.end_arrive_tp = this.ctx.helper.add(d.pre_arrive_tp, d.arrive_tp);
  408. d.end_deduct_qty = this.ctx.helper.add(d.pre_deduct_qty, d.deduct_qty);
  409. d.end_deduct_tp = this.ctx.helper.add(d.pre_deduct_tp, d.deduct_tp);
  410. }
  411. return data;
  412. }
  413. async _loadStagesJgclData(tender, stages, preStage, endStage) {
  414. // todo
  415. return [];
  416. }
  417. async _loadJgclData(tender, filter) {
  418. return filter.type === 'stage'
  419. ? await this._loadStageJgclData(tender, filter.stage)
  420. : await this._loadStagesJgclData(tender, filter.stages, filter.preStage, filter.endStage);
  421. };
  422. async _loadStageYgclData(tender, stage) {
  423. if (!stage) return [];
  424. const data = await this.ctx.service.stageYjcl.getStageData(stage);
  425. for (const d of data) {
  426. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  427. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  428. }
  429. return data;
  430. }
  431. async _loadStagesYgclData(tender, stages, preStage, endStage) {
  432. // todo
  433. return [];
  434. }
  435. async _loadYjclData(tender, filter) {
  436. return filter.type === 'stage'
  437. ? await this._loadStageYgclData(tender, filter.stage)
  438. : await this._loadStagesYgclData(tender, filter.stages, filter.preStage, filter.endStage);
  439. };
  440. async _loadStageBonusData(tender, stage) {
  441. if (!stage) return [];
  442. const data = await this.ctx.service.stageBonus.getEndStageData(tender.id, stage.order);
  443. return data;
  444. }
  445. async _loadStagesBonusData(tender, stages, preStage, endStage) {
  446. // todo
  447. return [];
  448. }
  449. async _loadBonusData(tender, filter) {
  450. return filter.type === 'stage'
  451. ? await this._loadStageBonusData(tender, filter.stage)
  452. : await this._loadStagesBonusData(tender, filter.stages, filter.preStage, filter.endStage);
  453. };
  454. async _loadStageSafeProdData(tender, stage) {
  455. if (!stage) return [];
  456. const data = await this.ctx.service.stageSafeProd.getStageData(stage);
  457. const preData = stage.preCheckedStage ? await this.ctx.service.stageSafeProd.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  458. for (const d of data) {
  459. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  460. if (pd) {
  461. d.pre_qty = pd.qty;
  462. d.pre_tp = pd.tp;
  463. }
  464. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  465. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  466. }
  467. return data;
  468. }
  469. async _loadStagesSafeProdData(tender, stages, preStage, endStage) {
  470. // todo
  471. return [];
  472. }
  473. async _loadSafeProdData(tender, filter) {
  474. return filter.type === 'stage'
  475. ? await this._loadStageSafeProdData(tender, filter.stage)
  476. : await this._loadStagesSafeProdData(tender, filter.stages, filter.preStage, filter.endStage);
  477. };
  478. async _loadStageTempLandData(tender, stage) {
  479. if (!stage) return [];
  480. const data = await this.ctx.service.stageTempLand.getStageData(stage);
  481. const preData = stage.preCheckedStage ? await this.ctx.service.stageTempLand.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  482. for (const d of data) {
  483. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  484. if (pd) {
  485. d.pre_qty = pd.qty;
  486. d.pre_tp = pd.tp;
  487. }
  488. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  489. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  490. }
  491. return data;
  492. }
  493. async _loadStagesTempLandData(tender, stages, preStage, endStage) {
  494. // todo
  495. return [];
  496. }
  497. async _loadTempLandData(tender, filter) {
  498. return filter.type === 'stage'
  499. ? await this._loadStageTempLandData(tender, filter.stage)
  500. : await this._loadStagesTempLandData(tender, filter.stages, filter.preStage, filter.endStage);
  501. };
  502. async _loadStageOtherData(tender, stage) {
  503. if (!stage) return [];
  504. const data = await this.ctx.service.stageOther.getStageData(stage);
  505. const preData = stage.preCheckedStage ? await this.ctx.service.stageOther.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  506. for (const d of data) {
  507. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  508. if (pd) {
  509. d.pre_tp = pd.tp;
  510. }
  511. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  512. }
  513. return data;
  514. }
  515. async _loadStagesOtherData(tender, stages, preStage, endStage) {
  516. // todo
  517. return [];
  518. }
  519. async _loadOtherData(tender, filter) {
  520. return filter.type === 'stage'
  521. ? await this._loadStageOtherData(tender, filter.stage)
  522. : await this._loadStagesOtherData(tender, filter.stages, filter.preStage, filter.endStage);
  523. };
  524. async _loadTenderData(tid, filter, stageInfo) {
  525. this.ctx.tender = null;
  526. const tender = await this.ctx.service.tender.checkTender(tid);
  527. const result = { id: tender.id, name: tender.name, category: tender.category };
  528. let stageFilter = null;
  529. for (const f of filter) {
  530. switch (f) {
  531. case 'info':
  532. if (!stageFilter) {
  533. stageFilter = await this._filterStages(tender, stageInfo);
  534. result.stage_filter = stageFilter.filter;
  535. }
  536. result.info = await this._loadInfoData(tender, stageFilter);
  537. break;
  538. case 'ledger':
  539. if (filter.indexOf('stage') < 0) [result.bills, result.pos] = await this._loadLedgerData(tender);
  540. break;
  541. case 'stage':
  542. if (!stageFilter) {
  543. stageFilter = await this._filterStages(tender, stageInfo);
  544. result.stage_filter = stageFilter.filter;
  545. }
  546. [result.bills, result.pos] = await this._loadStageLedgerData(tender, stageFilter);
  547. break;
  548. case 'pay':
  549. if (!stageFilter) {
  550. stageFilter = await this._filterStages(tender, stageInfo);
  551. result.stage_filter = stageFilter.filter;
  552. }
  553. result.pay = await this._loadPayData(tender, stageFilter);
  554. break;
  555. case 'jgcl':
  556. if (!stageFilter) {
  557. stageFilter = await this._filterStages(tender, stageInfo);
  558. result.stage_filter = stageFilter.filter;
  559. }
  560. result.jgcl = await this._loadJgclData(tender, stageFilter);
  561. break;
  562. case 'yjcl':
  563. if (!stageFilter) {
  564. stageFilter = await this._filterStages(tender, stageInfo);
  565. result.stage_filter = stageFilter.filter;
  566. }
  567. result.yjcl = await this._loadYjclData(tender, stageFilter);
  568. break;
  569. case 'bonus':
  570. if (!stageFilter) {
  571. stageFilter = await this._filterStages(tender, stageInfo);
  572. result.stage_filter = stageFilter.filter;
  573. }
  574. result.bonus = await this._loadBonusData(tender, stageFilter);
  575. break;
  576. case 'safeProd':
  577. if (!stageFilter) {
  578. stageFilter = await this._filterStages(tender, stageInfo);
  579. result.stage_filter = stageFilter.filter;
  580. }
  581. result.safeProd = await this._loadSafeProdData(tender, stageFilter);
  582. break;
  583. case 'tempLand':
  584. if (!stageFilter) {
  585. stageFilter = await this._filterStages(tender, stageInfo);
  586. result.stage_filter = stageFilter.filter;
  587. }
  588. result.tempLand = await this._loadTempLandData(tender, stageFilter);
  589. break;
  590. case 'other':
  591. if (!stageFilter) {
  592. stageFilter = await this._filterStages(tender, stageInfo);
  593. result.stage_filter = stageFilter.filter;
  594. }
  595. result.other = await this._loadOtherData(tender, stageFilter);
  596. break;
  597. default:
  598. throw '查询的数据类型未定义';
  599. }
  600. }
  601. return result;
  602. }
  603. async load(ctx) {
  604. try {
  605. const data = JSON.parse(ctx.request.body.data);
  606. const filter = data.filter.split(';');
  607. const result = [];
  608. for (const t of data.tender) {
  609. result.push(await this._loadTenderData(t.tid, filter, t.stageInfo));
  610. }
  611. ctx.body = { err: 0, msg: '', data: result };
  612. } catch (err) {
  613. ctx.log(err);
  614. ctx.ajaxErrorBody(err, '加载数据错误');
  615. }
  616. }
  617. async _getTzData(tid, includePos = false) {
  618. const tender = await this.ctx.service.tender.getTender(tid);
  619. if (!tender || tender.project_id !== this.ctx.session.sessionProject.id) {
  620. throw '不存在该标段';
  621. }
  622. const bills = await this.ctx.service.ledger.getData(tid);
  623. const pos = tender.measure_type === measureType.tz.value || includePos
  624. ? await this.ctx.service.pos.getPosData({tid: tid})
  625. : [];
  626. return { id: tid, name: tender.name, bills: bills, pos: pos };
  627. }
  628. async _checkStage(tid, sorder) {
  629. const stage = await this.service.stage.getDataByCondition({ tid: tid, order: sorder });
  630. if (!stage) throw '期数据错误';
  631. await this.service.stage.doCheckStage(stage);
  632. return stage;
  633. }
  634. async _getStageData(tid, sorder) {
  635. const data = await this._getTzData(tid, true);
  636. const stage = await this._checkStage(tid, sorder);
  637. const bills = await this.ctx.service.stageBills.getAuditorStageData2(tid, stage.id, stage.curTimes, stage.curOrder);
  638. const pos = await this.ctx.service.stagePos.getAuditorStageData2(tid, stage.id, stage.curTimes, stage.curOrder);
  639. data.stage = {
  640. sid: stage.id, sorder: stage.order, curTimes: stage.curTimes, curOrder: stage.curOrder,
  641. bills: bills, pos: pos
  642. };
  643. return data;
  644. }
  645. /**
  646. * 检测台账 页面
  647. *
  648. * @param {Object} ctx - egg全局变量
  649. * @return {void}
  650. */
  651. async checkTz(ctx) {
  652. try {
  653. const renderData = {
  654. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.tools.checkTz)
  655. };
  656. await this.layout('spss/check_tz.ejs', renderData);
  657. } catch (err) {
  658. ctx.helper.log(err);
  659. }
  660. }
  661. /**
  662. * 获取 期计量 汇总 数据(Ajax)
  663. * @param ctx
  664. * @returns {Promise<void>}
  665. */
  666. async loadBaseData(ctx) {
  667. try {
  668. const data = JSON.parse(ctx.request.body.data);
  669. const responseData = {err: 0, msg: '', data: []};
  670. responseData.data = await this._getTzData(data.id);
  671. ctx.body = responseData;
  672. } catch (err) {
  673. ctx.helper.log(err);
  674. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  675. }
  676. }
  677. async stash(ctx) {
  678. try {
  679. const data = JSON.parse(ctx.request.body.data);
  680. const responseData = { err: 0, msg: '', data: [] };
  681. if (!data.type || !data.action) throw '参数错误';
  682. switch (data.action) {
  683. case 'load':
  684. responseData.data = await this.ctx.service.spssStash.getSpssStash(data.id);
  685. break;
  686. case 'list':
  687. responseData.data = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, data.type);
  688. break;
  689. case 'add':
  690. await this.ctx.service.spssStash.addSpssStash(data.type, data.select, data.result);
  691. responseData.data = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, data.type);
  692. break;
  693. default: throw '未知操作';
  694. }
  695. ctx.body = responseData;
  696. } catch (err) {
  697. ctx.log(err);
  698. ctx.ajaxErrorBody(err, '暂存数据错误');
  699. }
  700. }
  701. }
  702. return SpssController;
  703. };