Browse Source

projCounter

MaiXinRong 8 years ago
parent
commit
e87090ea28

+ 36 - 0
modules/main/models/proj_counter.js

@@ -0,0 +1,36 @@
+/**
+ * Created by Mai on 2017/6/13.
+ */
+
+let baseModel = require('./base_model');
+
+class projCounter extends baseModel {
+    constructor (name) {
+        let db = require("../db/project_db");
+        let Schema = require("mongoose").Schema;
+        let projCounterSchema = new Schema({
+            projectID: Number,
+            bills: Number,
+            ration: Number
+        });
+        let projCounterModel = db.model(name, projCounterSchema);
+        super(projCounterModel);
+        this.collectionName = name;
+    };
+
+    getData (projectID, callback) {
+        this.model.findOne({"projectID": projectID}, '-_id', function (err, result) {
+            if (!err) {
+                callback(0, '', result);
+            } else {
+                callback(1, '查询数据失败。', null);
+            }
+        });
+    };
+
+    save (user_id, data, callback) {
+        this.model.update({"projectID": data.projectID}, data, callback);
+    }
+};
+
+module.exports = new projCounter('projCounter');

+ 1 - 0
modules/main/models/project.js

@@ -14,6 +14,7 @@ var moduleMap = {};
 moduleMap[projectConsts.BILLS] = billsData;
 moduleMap[projectConsts.RATION] = rationData;
 moduleMap[projectConsts.GLJ] = GLJData;
+moduleMap[projCounter.collectionName] = projCounter;
 
 var Project = function (){};
 

+ 21 - 3
web/building_saas/main/js/models/bills.js

@@ -87,11 +87,25 @@ var Bills = {
             this.tree.loadDatas(this.datas);
         };
 
+        bills.prototype.setMaxID = function (ID) {
+            this.tree.maxNodeID(ID);
+        };
+
         // 提交数据后的错误处理方法
         bills.prototype.doAfterUpdate = function(err, data){
             // to do
         };
 
+        bills.prototype.getCounterData = function (count) {
+            var updateData = {'projectID': this.project.ID()};
+            if (count) {
+                updateData[this.getSourceType()] = this.tree.maxNodeID() + count;
+            } else {
+                updateData[this.getSourceType()] = this.tree.maxNodeID() + 1;
+            }
+            return updateData;
+        };
+
         bills.prototype.insertBills = function (parentId, nextSiblingId) {
             var insertData = this.tree.getInsertData(parentId, nextSiblingId);
             var that = this;
@@ -100,8 +114,10 @@ var Bills = {
                 if (data.type === idTree.updateType.new) {
                     that.datas.push(data.data);
                 }
-            })
-            project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
+            });
+            this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
+                [ tools.coverseTreeUpdateData(insertData), this.getCounterData()]);
+            //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
 
             return this.tree.insert(parentId, nextSiblingId);
         };
@@ -119,7 +135,9 @@ var Bills = {
                     newData = data.data;
                 }
             });
-            project.pushNow('insertStdBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
+            this.project.pushNow('insertStdBills', [this.getSourceType(), this.project.projCounter()],
+                [ tools.coverseTreeUpdateData(insertData), this.getCounterData()]);
+            //project.pushNow('insertStdBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
 
             return this.tree.insertByData(newData, parentId, nextSiblingId);
         }

+ 14 - 1
web/building_saas/main/js/models/project.js

@@ -11,7 +11,8 @@ var PROJECT = {
             updateLock: 0,
             updateData: [],
             operation: '',
-            modules: {}
+            modules: {},
+            projCounter: 'projCounter'
         };
 
         var me = tools;
@@ -24,11 +25,19 @@ var PROJECT = {
             });
         };
         tools.doAfterLoad = function(result, callback){
+            var counter;
             result.forEach(function(item){
                 if (me.modules[item.moduleName]){
                     me.modules[item.moduleName].loadData(item.data);
+                } else if (item.moduleName === me.projCounter) {
+                    counter = item.data;
                 }
             });
+            for (module in counter) {
+                if (me.modules[module]) {
+                    me.modules[module].setMaxID(counter[module]);
+                }
+            }
             me._project.loadMainTree();
             //me.test(result[0].data[0]);
             callback(0);
@@ -63,6 +72,10 @@ var PROJECT = {
             return tools._ID;
         };
 
+        project.prototype.projCounter = function () {
+            return tools.projCounter;
+        }
+
         project.prototype.loadMainTree = function () {
             var that = this;
             var loadRationNode = function (rations, cacheNode) {

+ 7 - 3
web/building_saas/main/js/models/ration.js

@@ -50,6 +50,10 @@ var Ration = {
             });
         };
 
+        ration.prototype.setMaxID = function (ID) {
+            this.maxRationID(ID);
+        }
+
         // 提交数据后的错误处理方法
         ration.prototype.doAfterUpdate = function(err, data){
             // to do
@@ -98,9 +102,9 @@ var Ration = {
 
         ration.prototype.insertRation = function (billsID, preRation) {
             var br = this.getBillsSortRation(billsID);
-            /*this.project.pushNow('insertRation', [this.getSourceType(), 'proj_counter'],
-                [this.getInsertRationData(billsID, preRation), this.getCounterData()]);*/
-            this.project.pushNow('insertRation', [this.getSourceType()], [this.getInsertRationData(billsID, preRation)]);
+            this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],
+                [this.getInsertRationData(billsID, preRation), this.getCounterData()]);
+            //this.project.pushNow('insertRation', [this.getSourceType()], [this.getInsertRationData(billsID, preRation)]);
 
             var newRation = null;
             if (preRation) {