Преглед изворни кода

feat: 更新weapp标段控制器,优化标段详情接口逻辑,新增预付款相关中间件和接口

lanjianrong пре 3 месеци
родитељ
комит
999b590037

+ 72 - 38
app/controller/weapp_tender_controller.js

@@ -1,7 +1,7 @@
 'use strict';
 const auditConst = require('../const/audit');
 const measureType = require('../const/tender').measureType;
-
+const advanceConst = require('../const/advance');
 
 module.exports = app => {
     class WeappTenderController extends app.BaseController {
@@ -34,17 +34,13 @@ module.exports = app => {
 
         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 tender = ctx.tender.data;
+
+                let bCalcTp = ctx.tender.data.user_id === ctx.session.sessionUser.accountId && (
+                    ctx.tender.ledger_status === auditConst.ledger.status.checkNo || ctx.tender.ledger_status === auditConst.ledger.status.uncheck);
+                const stages = await ctx.service.stage.getValidStages(ctx.tender.id);
                 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);
+                const [change_tp, change_p_tp, change_n_tp, change_valuation_tp, change_unvaluation_tp] = await ctx.service.change.getChangeTp(ctx.tender.id);
                 tender.change_tp = change_tp;
                 tender.change_p_tp = change_p_tp;
                 tender.change_n_tp = change_n_tp;
@@ -56,11 +52,11 @@ module.exports = app => {
                         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*/ });
+                        const sum = await this.ctx.service.ledger.addUp({ tender_id: ctx.tender.id/* , 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.sum = ctx.tender.info.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]);
@@ -70,7 +66,7 @@ module.exports = app => {
                     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.qc_ratio = ctx.helper.mul(ctx.helper.div(tender.end_qc_tp, ctx.tender.info.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);
@@ -80,36 +76,36 @@ module.exports = app => {
                     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*/ });
+                        const sum = await this.ctx.service.ledger.addUp({ tender_id: ctx.tender.id/* , 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);
-                }
+                // 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: {
@@ -124,6 +120,44 @@ module.exports = app => {
             }
 
         }
+
+        async advanceList(ctx) {
+            try {
+                const { advanceType } = ctx.query;
+                const typeCol = advanceConst.typeCol.find(x => x.type === Number(advanceType));
+                if (!typeCol) {
+                    ctx.body = { code: -1, msg: '预付款类型错误', data: null };
+                    return;
+                }
+                const { decimal } = ctx.tender.info;
+                const advancePayTotal = ctx.tender.info.deal_param[typeCol.key + 'Advance'];
+                const advances = await ctx.service.advance.getAdvanceList(ctx.tender.id, typeCol.type, decimal.pay ? decimal.payTp : decimal.tp, advancePayTotal);
+                ctx.body = { code: 0, msg: '', data: advances };
+            } catch (error) {
+                this.log(error);
+                ctx.body = { code: -1, msg: error.toString(), data: null };
+            }
+        }
+
+        async advanceDetail(ctx) {
+            try {
+                // const { advanceType } = ctx.query;
+                // const typeCol = advanceConst.typeCol.find(x => x.type === Number(advanceType));
+                // if (!typeCol) {
+                //     ctx.body = { code: -1, msg: '预付款类型错误', data: null };
+                //     return;
+                // }
+                // const tenderInfo = await this.service.tenderInfo.getTenderInfo(ctx.tender.id);
+                // const { decimal } = tenderInfo;
+                // this.decimal = decimal.pay ? decimal.payTp : decimal.tp;
+                // const advancePayTotal = ctx.tender.info.deal_param[typeCol.key + 'Advance'];
+                // const advances = await ctx.service.advance.getAdvanceList(ctx.tender.id, typeCol.type, this.decimal, advancePayTotal);
+                ctx.body = { code: 0, msg: '', data: {} };
+            } catch (error) {
+                this.log(error);
+                ctx.body = { code: -1, msg: error.toString(), data: null };
+            }
+        }
     }
 
     return WeappTenderController;

+ 1 - 1
app/middleware/weapp_auth.js

@@ -28,7 +28,7 @@ module.exports = (options, app) => {
             if (!projectAccount) {
                 throw '账号不存在或未启用';
             }
-            ctx.session.sessionUser = projectAccount;
+            ctx.session.sessionUser = { ...projectAccount, accountId: projectAccount.id };
             ctx.session.sessionProject = projectData;
         } catch (error) {
             if (error.name === 'TokenExpiredError') {

+ 39 - 0
app/middleware/weapp_tender_check.js

@@ -0,0 +1,39 @@
+'use strict';
+const auditConst = require('../const/audit').ledger;
+const messageType = require('../const/message_type');
+const buildStatus = require('../const/tender').buildStatus;
+
+module.exports = options => {
+    return async function weappTenderCheck(ctx, next) {
+        try {
+            const { tenderId } = ctx.query;
+            const accountId = ctx.session.sessionUser.accountId;
+            // 读取标段数据
+            const tender = { id: tenderId };
+            if (!tender.id) {
+                throw '当前未打开标段';
+            }
+            tender.data = await ctx.service.tender.getTender(tender.id);
+            if (!tender.data) {
+                throw '标段数据错误';
+            }
+            if (!tender.data.measure_type) {
+                throw '请先选择计量模式';
+            }
+            tender.info = await ctx.service.tenderInfo.getTenderInfo(tender.id);
+            if (!tender.data.ledger_status) {
+                tender.data.ledger_status = auditConst.status.uncheck;
+            }
+            if (tender.data.project_id !== ctx.session.sessionProject.id) {
+                throw '您无权查看该项目';
+            }
+            ctx.tender = tender;
+            await next();
+        } catch (err) {
+            console.log(err);
+
+            ctx.body = { code: -1, msg: err.toString(), data: null };
+            return;
+        }
+    };
+};

+ 4 - 1
app/router.js

@@ -66,6 +66,7 @@ module.exports = app => {
     const safeStageCheck = app.middlewares.safeStageCheck();
     // weapp中间件
     const weappAuth = app.middlewares.weappAuth();
+    const weappTenderCheck = app.middlewares.weappTenderCheck();
     // 登入登出相关
     app.get('/login', 'loginController.index');
     app.get('/login/:code', 'loginController.index');
@@ -1247,6 +1248,8 @@ module.exports = app => {
     app.post('/wx/weapp/attention/unfollow', weappAuth, 'weappAttentionController.unfollow');
     app.get('/wx/weapp/attention/list', weappAuth, 'weappAttentionController.followedList');
     app.get('/wx/weapp/attention/check', weappAuth, 'weappAttentionController.checkFollowing');
-    app.get('/wx/weapp/tender/detail', weappAuth, 'weappTenderController.detail');
+    app.get('/wx/weapp/tender/detail', weappAuth, weappTenderCheck, 'weappTenderController.detail');
     app.get('/wx/weapp/tender/list/manage', weappAuth, 'weappTenderController.listManage');
+    app.get('/wx/weapp/advance/list', weappAuth, weappTenderCheck, 'weappTenderController.advanceList');
+    app.get('/wx/weapp/advance/detail', weappAuth, weappTenderCheck, 'weappTenderController.advanceDetail');
 };