Browse Source

指标模板,根据指标节点选择,加载指标

MaiXinRong 7 years ago
parent
commit
b65c6c65c7

+ 10 - 0
app/controller/template_controller.js

@@ -20,10 +20,20 @@ module.exports = app => {
          * @return {void}
          */
         async index (ctx) {
+            const id = ctx.queries.id ? ctx.queries.id[0] : 1;
             const node = await ctx.service.templateNode.getAllDataByCondition({template_id: 1});
+            for (const n of node) {
+                n.url = '/template?id=' + n.node_id;
+                n.target = '_self';
+            }
             const treeNode = ctx.helper.convertData(node, true, 'node_id', 'node_pid');
+            const condition = {template_id: 1, node_id: id};
+            const selectNode = await ctx.service.templateNode.getDataByCondition(condition);
+            const selectIndex = await ctx.service.templateIndex.getAllDataByCondition({ where: condition });
             const renderData = {
                 nodes: JSON.stringify(treeNode),
+                selectNode: selectNode,
+                selectIndex: selectIndex,
             }
             await this.layout('template/index.ejs', renderData, 'template/modal.ejs');
         }

+ 2 - 2
app/extend/helper.js

@@ -126,7 +126,7 @@ module.exports = {
      * @returns {boolean}
      */
     ValidTemplateNodeCode(code) {
-        const reg1 = /(^[a-z]+)([a-z0-9\-]*)/i;
+        const reg1 = /(^[a-z]+)([a-z0-9\-]*$)/i;
         const reg2 = /([a-z0-9]+$)/i;
         const reg3 = /([\-][0-9]+$)/i;
         return reg1.test(code) && reg2.test(code) && (!reg3.test(code));
@@ -137,7 +137,7 @@ module.exports = {
      * @returns {boolean}
      */
     ValidTemplateIndexCode(code) {
-        const reg1 = /(^[a-z]+)([a-z0-9\-]*)/i;
+        const reg1 = /(^[a-z]+)([a-z0-9\-]*$)/i;
         const reg2 = /([0-9]+$)/i;
         return reg1.test(code) && reg2.test(code);
     },

+ 6 - 2
app/public/css/main.css

@@ -40,8 +40,8 @@ body {
 	background-color: #999;
 	-webkit-border-radius: 6px;
 }
-.sjs-height-1,.sjs-height-2{
-  overflow: hidden;
+.sjs-height-1,.sjs-height-2,.sjs-height-3{
+  overflow: auto;
 }
 .sjs-bottom{
   height:400px;
@@ -405,6 +405,10 @@ body {
   height:500px;
   overflow: hidden
 }
+.modal-height-500-scroll{
+  height:500px;
+  overflow: auto;
+}
 .modal-lgx {
   max-width:1000px
 }

+ 37 - 0
app/service/template_index.js

@@ -0,0 +1,37 @@
+'use strict';
+
+/**
+ * 指标节点业务类
+ *
+ * @author Mai
+ * @date 2018/4/19
+ * @version
+ */
+
+module.exports = app => {
+    class TemplateIndex extends app.BaseService {
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局context
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'template_index';
+        }
+
+        async importData(datas, transaction) {
+            console.log(datas[241]);
+            console.log(datas[242]);
+            console.log(datas[243]);
+            await transaction.delete(this.tableName, {template_id: 1});
+            const insertResult = await transaction.insert(this.tableName, datas);
+            if (insertResult.affectedRows !== datas.length) {
+                throw '导入指标错误';
+            }
+        }
+    };
+
+    return TemplateIndex;
+};

+ 37 - 10
app/service/template_node.js

@@ -22,6 +22,14 @@ module.exports = app => {
             this.tableName = 'template_node';
         }
 
+        /**
+         * 查找父节点(根据编号),忽略大小写
+         * e.g. z1(z), z1-e(z1), z1-e-a(z1-e)
+         * @param code
+         * @param nodes
+         * @returns {*}
+         * @private
+         */
         _findParentId(code, nodes) {
             if (nodes.length === 0) { return -1; }
             const codeList = code.split('-');
@@ -29,18 +37,25 @@ module.exports = app => {
                 codeList.splice(codeList.length - 1);
                 const parentCode = codeList.join('-');
                 for (const node of nodes) {
-                    if (parentCode === node.code) {
+                    if (parentCode.toLowerCase() === node.code.toLowerCase()) {
                         return node.node_id;
                     }
                 }
             } else {
                 for (const node of nodes) {
-                    if (code.search(node.code) === 0) {
+                    if (code.toLowerCase().search(node.code.toLowerCase()) === 0) {
                         return node.node_id;
                     }
                 }
             }
         }
+        /**
+         * 解析一个Excel工作表内的全部 指标节点 和 指标
+         * @param {Object} excelSheet
+         * @param {Array} nodes - 解析后的指标节点
+         * @param {Array} indexes - 解析后的指标
+         * @private
+         */
         _parseSheetData(excelSheet, nodes, indexes) {
             for (const row of excelSheet.data) {
                 if (!row[0]) { continue; }
@@ -49,7 +64,7 @@ module.exports = app => {
                         const node = {
                             template_id: 1,
                             node_id: nodes.length + 1,
-                            node_pid: this._findParentId(row[0], nodes),
+                            node_pid: this._findParentId(row[0], nodes) || -1,
                             code: row[0],
                             name: row[1],
                         };
@@ -62,21 +77,29 @@ module.exports = app => {
                         unit1: row[2],
                         unit2: row[3],
                         node_id: nodes.length,
+                        index_id: indexes.length + 1,
+                        rule: row[9]
                     };
-                    if (row[5] === '√') {
-                        index.indexType = 1;
+                    if (row[4] === '√') {
+                        index.index_type = 1;
+                    } else if (row[5] === '√') {
+                        index.index_type = 2;
                     } else if (row[6] === '√') {
-                        index.indexType = 2;
+                        index.index_type = 3;
                     } else if (row[7] === '√') {
-                        index.indexType = 3;
-                    } else if (row[8] === '√') {
-                        index.indexType = 4;
+                        index.index_type = 4;
                     }
                     indexes.push(index);
                 }
             }
         }
 
+        /**
+         * 导入Excel数据
+         *
+         * @param {Array} excelSheets - Excel文件中的全部工作表
+         * @returns {Promise<boolean>}
+         */
         async importData(excelSheets) {
             let result = false;
             const limit = 30000;
@@ -90,12 +113,16 @@ module.exports = app => {
                 if (nodes.length > 0) {
                     await transaction.delete(this.tableName, {template_id: 1});
                     const insertResult = await transaction.insert(this.tableName, nodes);
-                    result = insertResult.affectedRows === nodes.length;
+                    if (insertResult.affectedRows !== nodes.length) {
+                        throw '导入指标节点错误';
+                    }
+                    await this.ctx.service.templateIndex.importData(indexes, transaction);
                 } else {
                     throw 'Excel文件中无标准的指标数据';
                 }
 
                 await transaction.commit();
+                result = true;
             } catch(err) {
                 await transaction.rollback();
                 throw err;

+ 19 - 11
app/view/template/index.ejs

@@ -13,12 +13,14 @@
 </div>
 <div class="panel-content">
     <div class="panel-title">
-        <div class="title-main"><h2>z2-e-a 边沟</h2></div>
+        <div class="title-main">
+            <h2>
+                <%= selectNode.code %> <%= selectNode.name %>
+                <a href="#add-index" data-toggle="modal" data-target="#add-index" class="btn btn-primary btn-sm pull-right">添加指标</a>
+            </h2>
+        </div>
     </div>
     <div class="content-wrap">
-        <div class="c-header m-0 p-0 d-flex flex-row-reverse">
-            <a href="#" class="btn btn-sm"><i class="fa fa-plus"></i> 添加指标</a>
-        </div>
         <div class="c-body">
             <div class="sjs-height-1">
                 <table class="table table-bordered">
@@ -29,14 +31,16 @@
                         <th>计算规则</th>
                         <th>设置规则</th>
                     </tr>
+                    <% for (const index of selectIndex) { %>
                     <tr>
-                        <td>z2-e-1</td>
-                        <td>排水工程公路公里造价</td>
-                        <td></td>
-                        <td>公路公里</td>
-                        <td>合价/路线总长度</td>
-                        <td>hj(合价)/lxzcd(路线总长度)<a href="#set-count" data-toggle="modal" data-target="#set-count"><i class="fa fa-cog"></i></a></td>
+                        <td><%= index.code %></td>
+                        <td><%= index.name %></td>
+                        <td><%= index.unit1 %></td>
+                        <td><%= index.unit2 %></td>
+                        <td><%= index.rule %></td>
+                        <td><%= index.rule %><a href="#set-count" data-toggle="modal" data-target="#set-count"><i class="fa fa-cog"></i></a></td>
                     </tr>
+                    <% } %>
                 </table>
             </div>
         </div>
@@ -44,6 +48,7 @@
 </div>
 <script type="text/javascript">
     const treeSetting = {
+        treeId: 'template',
         view: {showIcon: false},
         data: {
             key: {
@@ -51,6 +56,7 @@
             }
         }
     };
+    const selectNode = '<%- selectNode.node_id %>';
     const treeNode = '<%- nodes %>';
 
     $(document).ready(function(){
@@ -64,6 +70,8 @@
         }
         const treeNodeData = treeNode !== '' ? JSON.parse(treeNode) : [];
         loadText(treeNodeData);
-        $.fn.zTree.init($("#templateNode"), treeSetting, treeNodeData);
+        const treeObj = $.fn.zTree.init($("#templateNode"), treeSetting, treeNodeData);
+        const node = treeObj.getNodeByParam('node_id', selectNode);
+        treeObj.selectNode(node);
     });
 </script>

+ 31 - 20
app/view/template/modal.ejs

@@ -7,29 +7,40 @@
                 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
             </div>
             <div class="modal-body">
-                <h5 class="mt-3">hj(合价)/lxzcd(路线总长度)</h5>
+                <h5>当前规则:</h5>
+                <h5>
+                    <span class="badge badge-light" title="合价">合价 <!--<a href="#" class="text-danger" title="移除"><i class="fa fa-remove"></i></a>--></span>
+                    <span class="badge badge-light" title="/">/ <!--<a href="#" class="text-danger" title="移除"><i class="fa fa-remove"></i></a>--></span>
+                    <span class="badge badge-light" title="路线总长度">路线总长度 <a href="#" class="text-danger" title="移除"><i class="fa fa-remove"></i></a></span>
+                </h5>
                 <div class="form-group">
-                    <label>设置计算规则</label>
-                    <input class="form-control" value="hj/lxzcd" type="text">
+                    <select class="form-control">
+                        <option>全局参数</option>
+                        <option>本项目节参数</option>
+                        <option>计算式</option>
+                    </select>
                 </div>
-                <div class="row">
-                    <div class="col-6">
-                        <table class="table table-bordered">
-                            <legend>全局参数</legend>
-                            <tr><th>名称</th><th>代码</th><th>描述</th></tr>
-                            <tr><td>总造价</td><td>zzj</td><td><a href=""><i class="fa fa-info"></i></a></td></tr>
-                            <tr><td>路线总长度</td><td>lxzcd</td><td><a href=""><i class="fa fa-info"></i></a></td></tr>
-                            <tr><td>输入框</td><td>input</td><td><a href=""><i class="fa fa-info"></i></a></td></tr>
-                        </table>
-                    </div>
-                    <div class="col-6">
-                        <table class="table table-bordered">
-                            <legend>本项目节参数</legend>
-                            <tr><th>名称</th><th>代码</th><th>描述</th></tr>
-                            <tr><td>合价</td><td>hj</td><td><a href=""><i class="fa fa-info"></i></a></td></tr>
-                        </table>
-                    </div>
+                <!--全局参数-->
+                <div class="form-group">
+                    <select class="form-control">
+                        <option>总造价</option>
+                        <option>路线总长度</option>
+                        <option>计算式</option>
+                    </select>
+                </div>
+                <!--本项目节参数-->
+                <div class="form-group">
+                    <select class="form-control">
+                        <option>合价</option>
+                    </select>
+                </div>
+                <!--计算式-->
+                <div class="form-group">
+                    <select class="form-control">
+                        <option>/</option>
+                    </select>
                 </div>
+                <button class="btn btn-outline-primary">添加</button>
             </div>
             <div class="modal-footer">
                 <button class="btn btn-primary">确定</button>

+ 4 - 0
test/app/extend/helper.test.js

@@ -23,4 +23,8 @@ describe('test/app/extend/helper.test.js', () => {
         assert(ctx.helper.ValidTemplateCode('z1-e') === true);
         assert(ctx.helper.ValidTemplateCode('z1-e-1') === true);
     });
+    it('ValidTemplateIndexCode test', function () {
+        const ctx = app.mockContext();
+        assert(ctx.helper.ValidTemplateIndexCode('z2-d-d-1~z2-d-d-5   高填方路段处理   同z2-d-c-1~z2-d-c-5') === false);
+    });
 });