'use strict'; const auditConst = require('../const/audit'); const measureType = require('../const/tender').measureType; module.exports = app => { class WeappTenderController extends app.BaseController { async listManage(ctx) { try { const projectId = ctx.query.projectId; if (!projectId) { ctx.body = { err: 1, msg: '缺少projectId参数', data: null }; return; } const accountInfo = await this.ctx.service.projectAccount.getDataById(ctx.session.sessionUser.id); const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null; if (userPermission !== null && userPermission.tender !== undefined && userPermission.tender.indexOf('1') !== -1) { const subProject = await this.ctx.service.subProject.getDataById(projectId); const tenderList = await this.ctx.service.tender.getList('manage', userPermission, ctx.session.sessionUser.is_admin, '', subProject); const categoryData = await this.ctx.service.category.getAllCategory(subProject); const renderData = { categoryData, tenderList, }; ctx.body = { err: 0, msg: '', data: renderData }; } else { ctx.body = { err: 1, msg: '您没有管理权限', data: null }; } } catch (err) { this.log(err); ctx.body = { err: 1, msg: err.toString(), data: null }; } } async detail(ctx) { try { const { id: tenderId } = ctx.query; const tender = await this.service.tender.getTender(tenderId); const tenderInfo = await this.service.tenderInfo.getTenderInfo(tenderId); if (!tender || !tenderInfo) { throw '标段信息不存在'; } let bCalcTp = tender.user_id === ctx.session.sessionUser.accountId && ( tender.ledger_status === auditConst.ledger.status.checkNo || tender.ledger_status === auditConst.ledger.status.uncheck); const stages = await ctx.service.stage.getValidStages(tenderId); const lastStage = stages.length > 0 ? stages[0] : null; // await ctx.service.stage.getLastestStage(ctx.tender.id); const [change_tp, change_p_tp, change_n_tp, change_valuation_tp, change_unvaluation_tp] = await ctx.service.change.getChangeTp(tenderId); tender.change_tp = change_tp; tender.change_p_tp = change_p_tp; tender.change_n_tp = change_n_tp; tender.change_valuation_tp = change_valuation_tp; tender.change_unvaluation_tp = change_unvaluation_tp; if (lastStage) { await this.ctx.service.stage.checkStageGatherData(lastStage, ctx.session.sessionUser.is_admin); if ((!bCalcTp) && tender.measure_type === measureType.gcl.value) { bCalcTp = lastStage.status !== auditConst.stage.status.checked && !lastStage.readOnly; } if (bCalcTp) { const sum = await this.ctx.service.ledger.addUp({ tender_id: tenderId/* , is_leaf: true*/ }); tender.total_price = sum.total_price; tender.deal_tp = sum.deal_tp; } tender.sum = tenderInfo.calc_type === 'tp' ? ctx.helper.add(tender.total_price, tender.change_valuation_tp) : ctx.helper.add(tender.total_price, tender.change_tp); tender.gather_tp = ctx.helper.sum([lastStage.contract_tp, lastStage.qc_tp, lastStage.pc_tp]); tender.end_contract_tp = ctx.helper.sum([lastStage.contract_tp, lastStage.pre_contract_tp, lastStage.contract_pc_tp]); tender.end_qc_tp = ctx.helper.sum([lastStage.qc_tp, lastStage.pre_qc_tp, lastStage.qc_pc_tp]); tender.end_positive_qc_tp = ctx.helper.sum([lastStage.positive_qc_tp, lastStage.pre_positive_qc_tp, lastStage.positive_qc_pc_tp]); tender.end_negative_qc_tp = ctx.helper.sum([lastStage.negative_qc_tp, lastStage.pre_negative_qc_tp, lastStage.negative_qc_pc_tp]); tender.end_gather_tp = ctx.helper.add(tender.end_contract_tp, tender.end_qc_tp); tender.pre_gather_tp = ctx.helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp); tender.yf_tp = lastStage.yf_tp; tender.sf_tp = lastStage.sf_tp; tender.qc_ratio = ctx.helper.mul(ctx.helper.div(tender.end_qc_tp, tenderInfo.deal_param.contractPrice, 2), 100); tender.pre_ratio = ctx.helper.mul(ctx.helper.div(tender.pre_gather_tp, tender.sum, 2), 100); tender.cur_ratio = ctx.helper.mul(ctx.helper.div(tender.gather_tp, tender.sum, 2), 100); tender.other_tp = ctx.helper.sub(ctx.helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp); tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio); tender.end_yf_tp = ctx.helper.add(lastStage.yf_tp, lastStage.pre_yf_tp); tender.end_sf_tp = ctx.helper.add(lastStage.sf_tp, lastStage.pre_sf_tp); tender.undone_tp = ctx.helper.sub(ctx.helper.sub(ctx.helper.add(tender.total_price, change_tp), tender.end_contract_tp), tender.end_qc_tp); } else { if (bCalcTp) { const sum = await this.ctx.service.ledger.addUp({ tender_id: tenderId/* , is_leaf: true*/ }); tender.total_price = sum.total_price; tender.deal_tp = sum.deal_tp; } tender.sum = ctx.helper.add(tender.total_price, tender.change_tp); } const monthProgress = []; for (const s of stages) { if (s.s_time) { let progress = monthProgress.find(function(x) { return x.month === s.s_time; }); if (!progress) { progress = { month: s.s_time }; monthProgress.push(progress); } progress.tp = ctx.helper.add(ctx.helper.add(progress.tp, s.contract_tp), s.qc_tp); } } monthProgress.sort(function(x, y) { return Date.parse(x.month) - Date.parse(y.month); }); let sum = 0; for (const p of monthProgress) { p.ratio = ctx.helper.mul(ctx.helper.div(p.tp, tender.sum, 4), 100); sum = ctx.helper.add(sum, p.tp); p.end_tp = sum; p.end_ratio = ctx.helper.mul(ctx.helper.div(p.end_tp, tender.sum, 4), 100); } ctx.body = { code: 0, msg: '', data: { tender, // monthProgress, // stagesEcharts: JSON.parse(JSON.stringify(stages)).reverse(), }, }; } catch (error) { this.log(error); ctx.body = { code: -1, msg: error.toString(), data: null }; } } } return WeappTenderController; };