Jelajahi Sumber

code tuning for ES6 style

TonyKang 8 tahun lalu
induk
melakukan
dfb5abeb04

+ 32 - 0
modules/reports/models/cfg_common.js

@@ -0,0 +1,32 @@
+/**
+ * Created by Tony on 2017/6/6.
+ */
+
+class cfgCommon{
+    constructor(modelObj) {
+        this.modelObj = modelObj;
+    }
+    get(id, callback){
+        this.modelObj.find({ID: id}, '-_id', function(err, templates){
+            if(templates.length){
+                callback(false, templates[0]);
+            }
+            else{
+                callback('no record was found!');
+            }
+        })
+    };
+    getAll(id, callback){
+        this.modelObj.find({}, '-_id', function(err, templates){
+            if(templates.length){
+                callback(false, templates);
+            }
+            else{
+                callback('no record was found!');
+            }
+        })
+    };
+};
+
+//module.exports = new cfgCommon(null);
+module.exports = cfgCommon;

+ 7 - 26
modules/reports/models/cfg_control.js

@@ -13,32 +13,13 @@ let CtrlSchema = new Schema({
     "Vertical" : String,
     "Wrap" : String
 });
-
 let Control = smartcostdb.model("com_ctrls", CtrlSchema, "com_ctrls");
+let cfgCommonClass = require('./cfg_common');
 
-let CtrlDAO = function(){};
-
-//根据id
-CtrlDAO.prototype.get = function(id, callback){
-    Control.find({ID: id}, '-_id', function(err, templates){
-        if(templates.length){
-            callback(false, templates[0]);
-        }
-        else{
-            callback('no record was found!');
-        }
-    })
-}
-
-CtrlDAO.prototype.getAll = function(id, callback){
-    Control.find({}, '-_id', function(err, templates){
-        if(templates.length){
-            callback(false, templates);
-        }
-        else{
-            callback('no record was found!');
-        }
-    })
-}
+class CtrlDAO extends cfgCommonClass{
+    constructor(){
+        super(Control);
+    };
+};
 
-module.exports = new CtrlDAO();
+module.exports = new CtrlDAO();

+ 7 - 26
modules/reports/models/cfg_font.js

@@ -16,32 +16,13 @@ let FontSchema = new Schema({
     "FontStrikeOut" : String,
     "FontAngle" : String
 });
-
 let Font = smartcostdb.model("com_fonts", FontSchema, "com_fonts");
+let cfgCommonClass = require('./cfg_common');
 
-let FontDAO = function(){};
-
-//根据id
-FontDAO.prototype.get = function(id, callback){
-    Font.find({ID: id}, '-_id', function(err, templates){
-        if(templates.length){
-            callback(false, templates[0]);
-        }
-        else{
-            callback('no record was found!');
-        }
-    })
-}
-
-FontDAO.prototype.getAll = function(id, callback){
-    Font.find({}, '-_id', function(err, templates){
-        if(templates.length){
-            callback(false, templates);
-        }
-        else{
-            callback('no record was found!');
-        }
-    })
-}
+class FontDAO extends cfgCommonClass{
+    constructor(){
+        super(Font);
+    };
+};
 
-module.exports = new FontDAO();
+module.exports = new FontDAO();

+ 7 - 25
modules/reports/models/cfg_style.js

@@ -9,32 +9,14 @@ let StyleSchema = new Schema({
     "ID" : String,
     "border_style" : Array
 });
-
 let Style = smartcostdb.model("com_styles", StyleSchema, "com_styles");
+let cfgCommonClass = require('./cfg_common');
 
-let StyleDAO = function(){};
-
-//根据id
-StyleDAO.prototype.get = function(id, callback){
-    Style.find({ID: id}, '-_id', function(err, templates){
-        if(templates.length){
-            callback(false, templates[0]);
-        }
-        else{
-            callback('no record was found!');
-        }
-    })
-}
-
-StyleDAO.prototype.getAll = function(id, callback){
-    Style.find({}, '-_id', function(err, templates){
-        if(templates.length){
-            callback(false, templates);
-        }
-        else{
-            callback('no record was found!');
-        }
-    })
-}
+class StyleDAO extends cfgCommonClass{
+    constructor(){
+        super(Style);
+    };
+};
 
 module.exports = new StyleDAO();
+

+ 15 - 16
modules/reports/models/rpt_template.js

@@ -20,22 +20,21 @@ let RptTemplateSchema = new Schema({
 
 let Template = smartcostdb.model("rpt_templates", RptTemplateSchema, "rpt_templates");
 
-let RplTplDAO = function(){};
-
-RplTplDAO.prototype.get = function(grpId, id, callback){
-    Template.find({GROUP_KEY: grpId, ID_KEY: id}, '-_id', function(err, templates){
-        if(templates.length){
-            callback(false, templates[0]);
-        }
-        else{
-            callback('查找不到报表模板!');
-        }
-    })
-};
-
-RplTplDAO.prototype.getPromise = function(grpId, id){
-    let rst = Template.findOne({GROUP_KEY: grpId, ID_KEY: id}, '-_id').exec() ;
-    return rst;
+class RplTplDAO{
+    get(grpId, id, callback){
+        Template.find({GROUP_KEY: grpId, ID_KEY: id}, '-_id', function(err, templates){
+            if(templates.length){
+                callback(false, templates[0]);
+            }
+            else{
+                callback('查找不到报表模板!');
+            }
+        })
+    };
+    getPromise(grpId, id){
+        let rst = Template.findOne({GROUP_KEY: grpId, ID_KEY: id}, '-_id').exec() ;
+        return rst;
+    }
 }
 
 module.exports = new RplTplDAO();

+ 15 - 17
modules/reports/models/rpt_tpl_data.js

@@ -14,23 +14,21 @@ let RptTemplateDataSchema = new Schema({
 
 let TemplateData = smartcostdb.model("temp_tpl_data", RptTemplateDataSchema, "temp_tpl_data");
 
-let RplTplDataDAO = function(){};
-
-//根据id获取临时数据
-RplTplDataDAO.prototype.get = function(tpl_id, callback){
-    TemplateData.find({"Data_Key": tpl_id}, function(err, templates){
-        if(templates.length){
-            callback(false, templates[0]);
-        }
-        else{
-            callback('查找不到模板临时数据!');
-        }
-    })
+class RplTplDataDAO{
+    //根据id获取临时数据
+    get(tpl_id, callback){
+        TemplateData.find({"Data_Key": tpl_id}, function(err, templates){
+            if(templates.length){
+                callback(false, templates[0]);
+            }
+            else{
+                callback('查找不到模板临时数据!');
+            }
+        })
+    };
+    getPromise(tpl_id, callback){
+        return TemplateData.findOne({"Data_Key": tpl_id}).exec();
+    }
 };
 
-RplTplDataDAO.prototype.getPromise = function(tpl_id, callback){
-    return TemplateData.findOne({"Data_Key": tpl_id}).exec();
-}
-
-
 module.exports = new RplTplDataDAO();

+ 69 - 71
modules/reports/models/tpl_tree_node.js

@@ -23,78 +23,76 @@ var TreeNodeSchema = new Schema({
 var TreeNodeModel = db.model("rpt_tpl_tree", TreeNodeSchema, "rpt_tpl_tree");
 var counter = require('../../../public/counter/counter');
 
-var RplTplTreeDAO = function(){};
-
-RplTplTreeDAO.prototype.getTplTreeNodes = function(grpType, userId, tplType, callback) {
-    var filter = {"grpType": grpType, "$or": [{"isDeleted": null}, {"isDeleted": false} ]};
-    if (userId) {
-        filter.userId = userId;
-    }
-    if ((tplType && tplType !== rptTplDef.TplType.ALL)) {
-        filter.tplType = tplType;
-    }
-    TreeNodeModel.find(filter, '-_id', function(err, data){
-        if (err) {
-            callback(true, null);
-        } else callback(false,data);
-    });
-};
-
-RplTplTreeDAO.prototype.updateTreeNodes = function(nodes, callback) {
-    var functions = [];
-    for (var i=0; i < nodes.length; i++) {
-        functions.push((function(doc) {
-            return function(cb) {
-                TreeNodeModel.update({ID: doc.ID}, doc, cb);
-            };
-        })(nodes[i]));
-    }
-    async.parallel(functions, function(err, results) {
-        callback(err, results);
-    });
-};
-
-RplTplTreeDAO.prototype.removeNodes = function(nodeIds, preNodeId, preNodeNextId, callback){
-    var functions = [];
-    if (preNodeId != -1) {
-        functions.push((function(nodeId, nextId) {
-            return function(cb) {
-                TreeNodeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
-            };
-        })(preNodeId, preNodeNextId));
-    }
-    for (var i=0; i < nodeIds.length; i++) {
-        functions.push((function(nodeId) {
-            return function(cb) {
-                TreeNodeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
-            };
-        })(nodeIds[i]));
-    }
-    async.parallel(functions, function(err, results) {
-        callback(err, results);
-    });
-}
-
-RplTplTreeDAO.prototype.createTplTreeNode = function(nodeData, lastNodeId, callback) {
-    counter.counterDAO.getIDAfterCount(counter.moduleName.report, 1, function(err, result){
-        nodeData.ID = result.value.sequence_value;
-        var node = new TreeNodeModel(nodeData);
-        node.save(function (err, result) {
+//var RplTplTreeDAO = function(){};
+class RplTplTreeDAO{
+    getTplTreeNodes(grpType, userId, tplType, callback) {
+        var filter = {"grpType": grpType, "$or": [{"isDeleted": null}, {"isDeleted": false} ]};
+        if (userId) {
+            filter.userId = userId;
+        }
+        if ((tplType && tplType !== rptTplDef.TplType.ALL)) {
+            filter.tplType = tplType;
+        }
+        TreeNodeModel.find(filter, '-_id', function(err, data){
             if (err) {
-                callback("树节点错误!", false);
-            } else {
-                if (lastNodeId > 0) {
-                    TreeNodeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
-                        if (err) {
-                            callback("树节点错误!", false);
-                        } else {
-                            callback(false, result);
-                        }
-                    });
-                } else callback(false, result);
-            }
+                callback(true, null);
+            } else callback(false,data);
         });
-    });
-};
+    };
+    updateTreeNodes(nodes, callback) {
+        var functions = [];
+        for (let node of nodes) {
+            functions.push((function(doc) {
+                return function(cb) {
+                    TreeNodeModel.update({ID: doc.ID}, doc, cb);
+                };
+            })(node));
+        }
+        async.parallel(functions, function(err, results) {
+            callback(err, results);
+        });
+    };
+    removeNodes(nodeIds, preNodeId, preNodeNextId, callback) {
+        var functions = [];
+        if (preNodeId != -1) {
+            functions.push((function(nodeId, nextId) {
+                return function(cb) {
+                    TreeNodeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
+                };
+            })(preNodeId, preNodeNextId));
+        }
+        for (let nId of nodeIds) {
+            functions.push((function(nodeId) {
+                return function(cb) {
+                    TreeNodeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
+                };
+            })(nId));
+        }
+        async.parallel(functions, function(err, results) {
+            callback(err, results);
+        });
+    };
+    createTplTreeNode(nodeData, lastNodeId, callback) {
+        counter.counterDAO.getIDAfterCount(counter.moduleName.report, 1, function(err, result){
+            nodeData.ID = result.value.sequence_value;
+            var node = new TreeNodeModel(nodeData);
+            node.save(function (err, result) {
+                if (err) {
+                    callback("树节点错误!", false);
+                } else {
+                    if (lastNodeId > 0) {
+                        TreeNodeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
+                            if (err) {
+                                callback("树节点错误!", false);
+                            } else {
+                                callback(false, result);
+                            }
+                        });
+                    } else callback(false, result);
+                }
+            });
+        });
+    };
+}
 
 module.exports = new RplTplTreeDAO();

+ 0 - 1
modules/reports/routes/rpt_tpl_router.js

@@ -2,7 +2,6 @@ let express = require("express");
 let rptTplRouter = express.Router();
 let reportTplController = require('./../controllers/rpt_tpl_controller');
 
-//rptTplRouter.post('/getReport', reportTplController.getReportAllPages);
 rptTplRouter.post('/createTplTreeNode', reportTplController.createTplTreeNode);
 rptTplRouter.post('/getRptTplTree', reportTplController.getRptTplTree);
 rptTplRouter.post('/updateTptTplNodes', reportTplController.updateTreeNodes);

+ 5 - 1
web/maintain/ration_repository/js/section_tree.js

@@ -168,13 +168,17 @@ var zTreeOprObj = {
         sObj.after(addStr);
         var btn = $("#addBtn_"+treeNode.tId);
         if (btn) btn.bind("click", function(){
-            var rawNode = {ParentID: treeNode.ID, NextSiblingID: -1, name: "新增子节点"}, lastNodeId = -1;
+            var rawNode = {ParentID: treeNode.ID, NextSiblingID: -1, name: "新增子节点"}, lastNodeId = -1, lastNode = null;
             if (treeNode.items.length > 0) {
                 lastNodeId = treeNode.items[treeNode.items.length - 1].ID;
+                lastNode = treeNode.items[treeNode.items.length - 1];
             }
             zTreeOprObj.addNewNode(rawNode, lastNodeId, function(err, rst){
                 if (!(err)) {
                     var newNodes = [], isSilent = false;
+                    if (lastNode) {
+                        lastNode.NextSiblingID = rst.ID;
+                    }
                     newNodes.push({ rationRepId: rst.data.rationRepId, ID: rst.data.ID, ParentID: rst.data.ParentID, NextSiblingID:-1, name:"新增子节点",isParent:false, items:[]});
                     treeNode.isParent = true;
                     if (me.treeObj) {

+ 4 - 2
web/maintain/report/js/rpt_tpl_main.js

@@ -23,7 +23,7 @@ let zTreeOprObj = {
     },
     refreshNodes: function() {
         let me = this;
-        private_setupIsParent = function(node){
+        let private_setupIsParent = function(node){
             if (node.nodeType == RT.NodeType.NODE) {
                 node.isParent = true;
             } else {
@@ -159,6 +159,7 @@ let zTreeOprObj = {
                         lastNode = treeNode.items[i];
                         rawNode.NextSiblingID = treeNode.items[i].NextSiblingID;
                         insertIdx = i + 1;
+                        break;
                     } else {
                         rawNode.NextSiblingID = treeNode.items[i].ID;
                         insertIdx = i;
@@ -171,6 +172,7 @@ let zTreeOprObj = {
                     lastNode.NextSiblingID = rst.ID;
                 }
                 rawNode.ID = rst.ID;
+                rawNode.NextSiblingID = rst.NextSiblingID;
                 rawNode.isParent = true;
                 rawNode.items = [];
                 newNodes.push(rawNode);
@@ -191,7 +193,7 @@ let zTreeOprObj = {
         if (preNode) {
             preNodeId = preNode.ID;
         }
-        private_fetchAllSubItems = function(pItem){
+        let private_fetchAllSubItems = function(pItem){
             nodeIds.push(pItem.ID);
             if (pItem.items && pItem.items.length > 0) {
                 for (let i = 0; i < pItem.items.length; i++) {