Browse Source

test batch insert

TonyKang 8 years ago
parent
commit
2b2382d6ca

+ 1 - 1
modules/rationRepository/controllers/rationRepositoryController.js

@@ -1,7 +1,7 @@
 /**
  * Created by Tony on 2017/4/20.
  */
-var rationRepository = require("../models/rationRepository");
+var rationRepository = require("../models/repositoryMap");
 
 var callback = function(req,res,err,data){
     if(data){

+ 18 - 14
modules/rationRepository/models/rationChapterTree.js

@@ -4,30 +4,34 @@
 
 var mongoose = require("mongoose");
 var dbm = require("../../../config/db/db_manager");
+var chapterTreeDb = dbm.getCfgConnection("rationRepository")
 var async = require("async");
 var Schema = mongoose.Schema;
 
 var rationChapterTreeSchema = mongoose.Schema({//章节树  //生成唯一id改为sectionID  改成string
+    rationRepId: Number,
     sectionId:Number,
     parentId:Number,
     nextSiblingId:Number,
     name:String
 });
-
+var rationChapterTreeModel = chapterTreeDb.model("rationChapterTrees",rationChapterTreeSchema, "rationChapterTrees")
+var repositoryMap = require('./repositoryMap');
 
 var rationChapterTreeDAO = function(){};
 
-rationChapterTreeDAO.prototype.getRationChapterTrees = function(repositoryDbName,callback){
-    var db = dbm.getCfgConnection(repositoryDbName)
-    var rationChapterTreeModel = db.model("rationChapterTrees",rationChapterTreeSchema, "rationChapterTrees")
-    rationChapterTreeModel.find({},function(err,data){
-        if(data.length) callback(false,data);
-        else  if(err) callback("获取定额树错误!",false)
-        else callback(false,false);
+rationChapterTreeDAO.prototype.getRationChapterTrees = function(repositoryName,callback){
+    repositoryMap.getRealLibName(repositoryName, function(err, rst){
+        rationChapterTreeModel.find({"rationRepId": rst[0].ID},function(err,data){
+            if(data.length) callback(false,data);
+            else  if(err) callback("获取定额树错误!",false)
+            else callback(false,false);
+        })
     })
 }
-rationChapterTreeDAO.prototype.tempRationChapterTreeInsert = function(repositoryDbName,rationTempTree,callback){
-    var db = dbm.getCfgConnection(repositoryDbName)
+
+rationChapterTreeDAO.prototype.tempRationChapterTreeInsert = function(repositoryName,rationTempTree,callback){
+    var db = dbm.getCfgConnection(repositoryName)
     var rationChapterTreeModel = db.model("rationChapterTrees",rationChapterTreeSchema, "rationChapterTrees")
     rationChapterTreeModel.collection.insert(rationTempTree,function(err,data){
         if(err) callback("插入定额模板错误",false)
@@ -35,8 +39,8 @@ rationChapterTreeDAO.prototype.tempRationChapterTreeInsert = function(repository
     })
 }
 
-rationChapterTreeDAO.prototype.sectionUpsert = function(repositoryDbName,section,callback){
-    var db = dbm.getCfgConnection(repositoryDbName);
+rationChapterTreeDAO.prototype.sectionUpsert = function(repositoryName,section,callback){
+    var db = dbm.getCfgConnection(repositoryName);
     var rationChapterTreeModel = db.model("rationChapterTrees",rationChapterTreeSchema, "rationChapterTrees");
     rationChapterTreeModel.find({"sectionId": section.sectionId},function(err,data){
         if(data.length){
@@ -57,8 +61,8 @@ rationChapterTreeDAO.prototype.sectionUpsert = function(repositoryDbName,section
     })
 }
 //待  ration模块完成
-rationChapterTreeDAO.prototype.deleteSection= function(repositoryDbName,sectionId,callback){
-    var db = dbm.getCfgConnection(repositoryDbName);
+rationChapterTreeDAO.prototype.deleteSection= function(repositoryName,sectionId,callback){
+    var db = dbm.getCfgConnection(repositoryName);
     var rationChapterTreeModel = db.model("rationItems",rationChapterTreeSchema);
     rationChapterTreeModel.find({"sectionId": sectionId},[],function(err,data){
 

+ 17 - 18
modules/rationRepository/models/rationRepository.js

@@ -1,22 +1,23 @@
 /**
- * Created by Tony on 2017/4/19.
- * 定额库分类,如重庆、广东等各个省的建筑定额,未来考虑与公路合并
+ * Created by Tony on 2017/4/24.
+ * 重新构造,不适宜生成多个定额库db,还是得统一在一个表
  */
 var mongoose = require('mongoose');
 var dbm = require("../../../config/db/db_manager");
-var stringUtil = require('../../../public/stringUtil');
-var rationLibdb = dbm.getCfgConnection("rationLibMap");
+//var stringUtil = require('../../../public/stringUtil');
+var rationLibdb = dbm.getCfgConnection("rationRepository");
 var Schema = mongoose.Schema;
-var RationLibSchema = new Schema({
+var RepositoryMapSchema = new Schema({
+    "ID": Number,
     "dispName" : String,
-    "dbName": String,
     "appType" : String, //如:"建筑" / "公路"
     "localeType": String, //如 各个省份 / 部颁
     "descr" : String,
     "deleted": Boolean
 });
+var counter = require('../../../public/counter/counter');
 
-var rationRepository = rationLibdb.model("rationRepository", RationLibSchema, "rationRepository");
+var rationRepository = rationLibdb.model("repositoryMap", RepositoryMapSchema, "repositoryMap");
 
 function createNewLibModel(rationLibObj){
     var rst = {};
@@ -24,7 +25,6 @@ function createNewLibModel(rationLibObj){
     rst.appType = rationLibObj.appType?rationLibObj.appType:'construct';
     rst.localeType = rationLibObj.localeType?rationLibObj.localeType:'';
     rst.descr = rationLibObj.descr;
-    rst.dbName = 'r_r_' + stringUtil.getPinYinFullChars(rationLibObj.dispName);
     rst.deleted = false;
     return rst;
 }
@@ -42,16 +42,15 @@ rationRepositoryDao.prototype.getRealLibName = function(dispName,callback){
 };
 
 rationRepositoryDao.prototype.addRationRepository = function( rationLibObj,callback){
-    rationRepository.find({"dispName": rationLibObj.dispName},function(err,data){
-        if(data.length == 0)
-            new rationRepository(createNewLibModel(rationLibObj)).save(function(err) {
-                if (err) callback("Error", null)
-                else
-                    callback(false, "ok");
-            })
-        else
-            callback("定额库重名!",null)
-    })
+    counter.counterDAO.getIDAfterCount(counter.moduleName.rationMap, 1, function(err, result){
+        var rMap = createNewLibModel(rationLibObj);
+        rMap.ID = result.value.sequence_value;
+        new rationRepository(rMap).save(function(err) {
+            if (err) callback("Error", null)
+            else
+                callback(false, "ok");
+        });
+    });
 };
 
 rationRepositoryDao.prototype.getDisplayRationLibs = function(callback) {

+ 2 - 0
public/counter/counter.js

@@ -21,6 +21,8 @@ const COUNTER_MODULE_NAME = {
     user: 'users',
     bills: 'bills',
     ration: 'rations',
+    rationMap: 'rationMaps',
+    rationTree: 'rationChapterTrees',
     report: 'rptTemplates',
     fee: 'fees',
     template_bills: 'temp_bills'

+ 37 - 0
test/unit/public/testBatchInsertDocs.js

@@ -0,0 +1,37 @@
+/**
+ * Created by Tony on 2017/4/24.
+ */
+
+var test = require('tape');
+var mongoose = require('mongoose');
+var dbm = require("../../../config/db/db_manager");
+var testdb = dbm.getLocalConnection("Demo");
+var Schema = mongoose.Schema;
+//testBatchInsert
+var BatchInsertSchema = new Schema({
+    "ID": Number,
+    "dispName" : String
+});
+
+var testModel = testdb.model("testBatchInsert", BatchInsertSchema, "testBatchInsert");
+
+test('test batch insert docs', function(t){
+    var arr = [];
+    arr.push(new testModel({ID: 1, dispName: "name1"}));
+    arr.push(new testModel({ID: 2, dispName: "name2"}));
+    arr.push(new testModel({ID: 3, dispName: "name3"}));
+    testModel.collection.insert(arr, null, function(err, docs){
+        if (err) {
+            t.pass('pass with error');
+        } else {
+            t.pass('pass with: ' + docs.insertedCount + ' records were inserted!');
+        }
+        t.end();
+    })
+});
+
+test('finish', function (t) {
+    mongoose.disconnect();
+    t.pass('closing db connection');
+    t.end();
+});

+ 4 - 19
web/rationLibEditor/js/dinge.js

@@ -21,29 +21,14 @@ $(document).ready(function(){
 });
 function initParam(){
     var rationLibName = getQueryString("repository");//获取定额库参数
-    if(rationLibName)
-        getRealLib(rationLibName);
-    else{
+    if(rationLibName) {
+        params.realLibName = LibName;
+        getRationTree();
+    } else{
         params = JSON.parse(getQueryString("params"));
         getRationTree();
     }
 }
-function getRealLib(LibName){
-    $.ajax({
-        type:"POST",
-        url:"api/getRealLibName",
-        data:{"rationName":LibName},
-        async:false,
-        dataType:"json",
-        cache:false,
-        timeout:1000,
-        success:function(result){
-            params.realLibName = result.data[0].dbName
-            getRationTree();
-        },
-        error:function(){}
-    })
-}
 //---------------------------------------------------初始化章节树界面
 function  getRationTree(){
     $.ajax({