|  | @@ -12,45 +12,8 @@ const paramCode = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', '
 | 
	
		
			
				|  |  |      '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 defaultGlobalParams = [
 | 
	
		
			
				|  |  | -    {
 | 
	
		
			
				|  |  | -        template_id: 1,
 | 
	
		
			
				|  |  | -        node_id: 0,
 | 
	
		
			
				|  |  | -        param_id: 1,
 | 
	
		
			
				|  |  | -        code: 'g_a',
 | 
	
		
			
				|  |  | -        name: '公路基本造价',
 | 
	
		
			
				|  |  | -    },{
 | 
	
		
			
				|  |  | -        template_id: 1,
 | 
	
		
			
				|  |  | -        node_id: 0,
 | 
	
		
			
				|  |  | -        param_id: 2,
 | 
	
		
			
				|  |  | -        code: 'g_b',
 | 
	
		
			
				|  |  | -        name: '建安费',
 | 
	
		
			
				|  |  | -    },{
 | 
	
		
			
				|  |  | -        template_id: 1,
 | 
	
		
			
				|  |  | -        node_id: 0,
 | 
	
		
			
				|  |  | -        param_id: 3,
 | 
	
		
			
				|  |  | -        code: 'g_c',
 | 
	
		
			
				|  |  | -        name: '工程建设其他费用',
 | 
	
		
			
				|  |  | -    },{
 | 
	
		
			
				|  |  | -        template_id: 1,
 | 
	
		
			
				|  |  | -        node_id: 0,
 | 
	
		
			
				|  |  | -        param_id: 4,
 | 
	
		
			
				|  |  | -        code: 'g_d',
 | 
	
		
			
				|  |  | -        name: '路线总长度(主线长度)',
 | 
	
		
			
				|  |  | -    },{
 | 
	
		
			
				|  |  | -        template_id: 1,
 | 
	
		
			
				|  |  | -        node_id: 0,
 | 
	
		
			
				|  |  | -        param_id: 5,
 | 
	
		
			
				|  |  | -        code: 'g_e',
 | 
	
		
			
				|  |  | -        name: '建筑总面积{路线总长度(主线长度)×路基(或桥隧)宽度}',
 | 
	
		
			
				|  |  | -    },{
 | 
	
		
			
				|  |  | -        template_id: 1,
 | 
	
		
			
				|  |  | -        node_id: 0,
 | 
	
		
			
				|  |  | -        param_id: 6,
 | 
	
		
			
				|  |  | -        code: 'g_f',
 | 
	
		
			
				|  |  | -        name: '路基长度(指不含桥梁、隧道的路基长度(双幅平均计))',
 | 
	
		
			
				|  |  | -    },
 | 
	
		
			
				|  |  | -];
 | 
	
		
			
				|  |  | +const paramConst = require('../const/template_param');
 | 
	
		
			
				|  |  | +const nodeConst = require('../const/template_node');
 | 
	
		
			
				|  |  |  module.exports = app => {
 | 
	
		
			
				|  |  |      class TemplateNode extends app.BaseService {
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -66,6 +29,39 @@ module.exports = app => {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          /**
 | 
	
		
			
				|  |  | +         * 加载默认节点参数
 | 
	
		
			
				|  |  | +         * @param {Array} params - 参数数组
 | 
	
		
			
				|  |  | +         * @param {Array} defaultParams - 默认参数数组
 | 
	
		
			
				|  |  | +         * @param {Number} nodeId - 指标节点Id
 | 
	
		
			
				|  |  | +         * @private
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        _loadDefaultParam(params, defaultParams, nodeId) {
 | 
	
		
			
				|  |  | +            const newParams = JSON.parse(JSON.stringify(defaultParams));
 | 
	
		
			
				|  |  | +            for (const p of newParams) {
 | 
	
		
			
				|  |  | +                p.node_id = nodeId;
 | 
	
		
			
				|  |  | +                params.push(p);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 过滤指标节点的参数(含节点参数初始化)
 | 
	
		
			
				|  |  | +         * @param {Array} params - 全部参数数组
 | 
	
		
			
				|  |  | +         * @param {Number} nodeId - 指标节点Id
 | 
	
		
			
				|  |  | +         * @private
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        _filterNodeParams(params, nodeId) {
 | 
	
		
			
				|  |  | +            let nodeParams = params.filter(function (p) {
 | 
	
		
			
				|  |  | +                return p.node_id === nodeId;
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            if (nodeParams.length > 0) {
 | 
	
		
			
				|  |  | +                return nodeParams;
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                this._loadDefaultParam(params, paramConst.defaultNodeParams, nodeId);
 | 
	
		
			
				|  |  | +                return params.filter(function (p) {
 | 
	
		
			
				|  |  | +                    return p.node_id === nodeId;
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  |           * 从计算规则中解析出指标参数
 | 
	
		
			
				|  |  |           * @param {String} rule - 指标规则
 | 
	
		
			
				|  |  |           * @param {Number} nodeId - 指标节点id
 | 
	
	
		
			
				|  | @@ -77,12 +73,10 @@ module.exports = app => {
 | 
	
		
			
				|  |  |              if (rule === '') { return; }
 | 
	
		
			
				|  |  |              const self = this;
 | 
	
		
			
				|  |  |              const ruleParams = rule.split('/');
 | 
	
		
			
				|  |  | -            const nodeParams = params.filter(function (p) {
 | 
	
		
			
				|  |  | -                return p.node_id === nodeId;
 | 
	
		
			
				|  |  | -            });
 | 
	
		
			
				|  |  | +            const nodeParams = this._filterNodeParams(params, nodeId);
 | 
	
		
			
				|  |  |              const addParam = function (paramName) {
 | 
	
		
			
				|  |  |                  if (paramName === '') { return ''; }
 | 
	
		
			
				|  |  | -                let param = self.ctx.helper.findObj(defaultGlobalParams, 'name', paramName);
 | 
	
		
			
				|  |  | +                let param = self.ctx.helper.findObj(paramConst.defaultGlobalParams, 'name', paramName);
 | 
	
		
			
				|  |  |                  if (!param) {
 | 
	
		
			
				|  |  |                      param = self.ctx.helper.findObj(nodeParams, 'name', paramName);
 | 
	
		
			
				|  |  |                  }
 | 
	
	
		
			
				|  | @@ -93,6 +87,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                          param_id: nodeParams.length + 1,
 | 
	
		
			
				|  |  |                          code: paramCode[nodeParams.length],
 | 
	
		
			
				|  |  |                          name: paramName,
 | 
	
		
			
				|  |  | +                        match_type: paramConst.matchType.non_match,
 | 
	
		
			
				|  |  |                      };
 | 
	
		
			
				|  |  |                      nodeParams.push(newParam);
 | 
	
		
			
				|  |  |                      params.push(newParam);
 | 
	
	
		
			
				|  | @@ -100,7 +95,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  } else {
 | 
	
		
			
				|  |  |                      return param.code;
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +            };
 | 
	
		
			
				|  |  |              if (ruleParams.length > 1) {
 | 
	
		
			
				|  |  |                  const paramName1 = ruleParams[0];
 | 
	
		
			
				|  |  |                  const paramCode1 = addParam(paramName1);
 | 
	
	
		
			
				|  | @@ -209,7 +204,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                          throw '导入指标节点错误';
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                      await this.ctx.service.templateIndex.importData(indexes, transaction);
 | 
	
		
			
				|  |  | -                    await this.ctx.service.templateParam.importData(params.concat(defaultGlobalParams), transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.templateParam.importData(params.concat(paramConst.defaultGlobalParams), transaction);
 | 
	
		
			
				|  |  |                  } else {
 | 
	
		
			
				|  |  |                      throw 'Excel文件中无标准的指标数据';
 | 
	
		
			
				|  |  |                  }
 | 
	
	
		
			
				|  | @@ -218,11 +213,32 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  result = true;
 | 
	
		
			
				|  |  |              } catch(err) {
 | 
	
		
			
				|  |  |                  await transaction.rollback();
 | 
	
		
			
				|  |  | +                console.log(err);
 | 
	
		
			
				|  |  |                  throw err;
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              return result;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 保存指标节点绑定信息
 | 
	
		
			
				|  |  | +         *
 | 
	
		
			
				|  |  | +         * @param data
 | 
	
		
			
				|  |  | +         * @returns {Promise<*>}
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async updateNodeMatch(data, condition) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                if (this.ctx.helper.validMatchCode(data.match_key)) {
 | 
	
		
			
				|  |  | +                    data.match_type = nodeConst.matchType.code;
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    data.match_type = nodeConst.matchType.name;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                await this.db.update(this.tableName, data, {where: condition});
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                console.log(err);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return await this.getDataByCondition(condition);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      return TemplateNode;
 |