Browse Source

调整全局参数,从配置文件加载等

MaiXinRong 7 years ago
parent
commit
60191d0ce3

+ 9 - 0
app.js

@@ -8,10 +8,19 @@
  * @version
  */
 
+// 基类
 const BaseController = require('./app/base/base_controller');
 const BaseService = require('./app/base/base_service');
+// 常量
+const libConst = require('./app/const/lib');
+const nodeConst = require('./app/const/template_node');
+const paramConst = require('./app/const/template_param');
 module.exports = app => {
     // app内定义基类,方便继承
     app.BaseController = BaseController;
     app.BaseService = BaseService;
+    // app内定义所有所需常量
+    app.libConst = libConst;
+    app.nodeConst = nodeConst;
+    app.paramConst = paramConst;
 }

BIN
app/const/global_params.xls


+ 1 - 1
app/const/template_node.js

@@ -20,4 +20,4 @@ matchTypeStr[matchType.name] = '匹配分项名称';
 module.exports = {
     matchType,
     matchTypeStr
-}
+};

+ 46 - 145
app/const/template_param.js

@@ -7,6 +7,13 @@
  * @date 2018/4/24
  * @version
  */
+const excel = require('node-xlsx');
+
+// 参数可选代号
+const paramCodeArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad', 'ae', 'af',
+    'ag', 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as', 'at', 'au',
+    'av', 'aw', 'ax', 'ay', 'az'];
 
 // 参数绑定类别
 const matchType = {
@@ -43,152 +50,45 @@ matchNumStr[matchNum.dgn_quantity2] = '设计数量2';
 matchNumStr[matchNum.quantity] = '清单数量';
 
 const globalParamNodeId = 0;
+const defaultGlobalParamsFile = 'app/const/global_params.xls';
 // 默认全局参数
-const defaultGlobalParams = [
-    {
-        template_id: 1,
-        node_id: 0,
-        param_id: 1,
-        code: 'g_a',
-        name: '公路基本造价',
-        match_type: matchType.fixed_id,
-        match_key: '10',
-        match_num: matchNum.total_price,
-    },{
-        template_id: 1,
-        node_id: 0,
-        param_id: 2,
-        code: 'g_b',
-        name: '建安费',
-        match_type: matchType.fixed_id,
-        match_key: '1',
-        match_num: matchNum.total_price,
-    },{
-        template_id: 1,
-        node_id: 0,
-        param_id: 3,
-        code: 'g_c',
-        name: '工程建设其他费用',
-        match_type: matchType.fixed_id,
-        match_key: '3',
-        match_num: matchNum.total_price,
-    },{
-        template_id: 1,
-        node_id: 0,
-        param_id: 4,
-        code: 'g_d',
-        name: '路线总长度',
-        match_type: matchType.fixed_id,
-        match_key: '1',
-        matchNum: matchNum.dgn_quantity1,
-    },{
-        template_id: 1,
-        node_id: 0,
-        param_id: 5,
-        code: 'g_e',
-        name: '建筑总面积',
-        match_type: matchType.non_match,
-    },{
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_f',
-        name: '路基长度',
-        match_type: matchType.code,
-        match_key: '1-2',
-        match_num: matchNum.dgn_quantity1,
-    },  {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_g',
-        name: '桥梁建筑面积',
-        match_type: matchType.non_match,
-    },{
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_h',
-        name: '桥梁长度(含互通主线)',
-        match_type: matchType.code,
-        match_key: '1-4',
-        match_num: matchNum.dgn_quantity1,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_i',
-        name: '路面铺装体积',
-        match_type: matchType.non_match,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_j',
-        name: '桥梁建筑面积',
-        match_type: matchType.non_match,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_k',
-        name: '桥梁用筋重量',
-        match_type: matchType.non_match,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_l',
-        name: '桥梁混凝土体积',
-        match_type: matchType.non_match,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_m',
-        name: '交叉工程总占地面积',
-        match_type: matchType.non_match,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_n',
-        name: '隧道长度',
-        match_type: matchType.code,
-        match_key: '1-6',
-        match_num: matchNum.dgn_quantity1,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_o',
-        name: '隧道建筑面积',
-        match_type: matchType.non_match,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_p',
-        name: '房建总占地面积',
-        match_type: matchType.code,
-        match_key: '1-9',
-        match_num: matchNum.dgn_quantity1,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_q',
-        name: '房建处数',
-        match_type: matchType.non_match,
-    }, {
-        template_id: 1,
-        node_id: 0,
-        param_id: 6,
-        code: 'g_r',
-        name: '计划工期',
-        match_type: matchType.non_match,
-    },
-];
+const loadingGlobalParams = function () {
+    const params = [];
+    const sheets = excel.parse(defaultGlobalParamsFile);
+    if (sheets && sheets.length > 0 && sheets[0] !== undefined && sheets[0].data !== undefined) {
+        for (const row of sheets[0].data) {
+            if (!row[0] || row[0] === '') { continue; }
+            const param = {
+                template_id: 1,
+                node_id: globalParamNodeId,
+                param_id: params.length + 1,
+                name: row[0],
+            };
+            param.code = 'g_' + paramCodeArr[param.param_id];
+            if (row[1] && row[1] === 'fixed') {
+                param.match_type = matchType.fixed_id;
+            } else if (row[2] && row[2] !== '') {
+                param.match_type = matchType.code;
+            } else {
+                param.match_type = matchType.non_match;
+            }
+            param.match_key = row[2];
+            if (row[3] && row[3] === '合价') {
+                param.match_num = matchNum.total_price;
+            } else if (row[3] === '数量1') {
+                param.match_num = matchNum.dgn_quantity1;
+            } else if (row[3] === '数量2') {
+                param.match_num = matchNum.dgn_quantity2;
+            } else if (row[3] === '数量') {
+                param.match_num = matchNum.quantity;
+            }
+            params.push(param);
+        }
+    }
+    return params;
+};
+const defaultGlobalParams = loadingGlobalParams();
+
 // 默认节点参数
 const defaultNodeParams = [
     {
@@ -244,6 +144,7 @@ const defaultNodeParams = [
 ];
 
 module.exports = {
+    paramCodeArr,
     matchType,
     matchTypeStr,
     validMatchType,

+ 4 - 2
app/controller/lib_controller.js

@@ -11,9 +11,11 @@ const fs = require('fs');
 const path = require('path');
 const awaitWriteStream = require('await-stream-ready').write;
 const sendToWormhole = require('stream-wormhole');
-const libConst = require('../const/lib');
-const paramConst = require('../const/template_param');
 module.exports = app => {
+
+    const libConst = app.libConst;
+    const paramConst = app.paramConst;
+
     class LibController extends app.BaseController {
 
         /**

+ 4 - 2
app/controller/template_controller.js

@@ -12,9 +12,11 @@
 const path = require('path');
 const excel = require('node-xlsx');
 const sendToWormhole = require('stream-wormhole');
-const paramConst = require('../const/template_param');
-const nodeConst = require('../const/template_node');
 module.exports = app => {
+
+    const paramConst = app.paramConst;
+    const nodeConst = app.nodeConst;
+
     class TemplateController extends app.BaseController {
         /**
          * 指标模板页面

+ 3 - 2
app/service/match.js

@@ -9,10 +9,11 @@
  */
 
 const Service = require('egg').Service;
-const paramConst = require('../const/template_param');
-const nodeConst = require('../const/template_node');
 module.exports = app => {
 
+    const paramConst = app.paramConst;
+    const nodeConst = app.nodeConst;
+
     class Match extends Service {
 
         /**

+ 5 - 8
app/service/template_node.js

@@ -7,14 +7,11 @@
  * @date 2018/4/19
  * @version
  */
-
-const paramCode = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
-    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad', 'ae', 'af',
-    'ag', 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as', 'at', 'au',
-    'av', 'aw', 'ax', 'ay', 'az'];
-const paramConst = require('../const/template_param');
-const nodeConst = require('../const/template_node');
 module.exports = app => {
+
+    const paramConst = app.paramConst;
+    const nodeConst = app.nodeConst;
+
     class TemplateNode extends app.BaseService {
 
         /**
@@ -90,7 +87,7 @@ module.exports = app => {
                         template_id: templateId,
                         node_id: nodeId,
                         param_id: nodeParams.length + 1,
-                        code: paramCode[nodeParams.length],
+                        code: paramConst.paramCodeArr[nodeParams.length],
                         name: paramName,
                         match_type: paramConst.matchType.non_match,
                     };

+ 1 - 2
app/service/template_param.js

@@ -8,7 +8,6 @@
  * @version
  */
 
-const paramConst = require('../const/template_param');
 module.exports = app => {
     class TemplateParam extends app.BaseService {
         /**
@@ -49,7 +48,7 @@ module.exports = app => {
                 if (data.match_key && !this.ctx.helper.validMatchCode(data.match_key)) {
                     throw '用于绑定的分项编号有误'
                 }
-                data.match_type = paramConst.matchType.code;
+                data.match_type = this.ctx.app.paramConst.matchType.code;
                 await this.db.update(this.tableName, data, {where: condition});
             } catch (err) {
                 console.log(err);

+ 3 - 1
app/service/tender_param.js

@@ -8,8 +8,10 @@
  * @version
  */
 
-const paramConst = require('../const/template_param');
 module.exports = app => {
+
+    const paramConst = app.paramConst;
+
     class TenderParam extends app.BaseService {
         /**
          * 构造函数

+ 18 - 0
test/app/const/template_param.test.js

@@ -0,0 +1,18 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+const { app, assert } = require('egg-mock/bootstrap');
+
+describe('test/app/const/template_param.test.js', () => {
+    it('defaultGlobalParams', function* () {
+        const ctx = app.mockContext();
+        assert(app.paramConst.defaultGlobalParams.length === 39);
+    });
+
+});

+ 0 - 1
test/app/service/template_node.test.js

@@ -11,7 +11,6 @@
 const { app, assert } = require('egg-mock/bootstrap');
 // excel解析
 const excel = require('node-xlsx');
-const paramConst = require('../../../app/const/template_param');
 
 describe('test/app/service/template_node.test.js', () => {
     it('_parseSheetData test xls File', function () {