Prechádzať zdrojové kódy

insert Ration (button & contextMenu & From Standard Ration Lib)

MaiXinRong 8 rokov pred
rodič
commit
7a137b96a2

+ 1 - 1
modules/main/models/bills.js

@@ -68,7 +68,7 @@ billsDAO.prototype.getProjectBills = function (projectId, callback) {
                 if (err) {
                     callback(1, '找不到模板', null);
                 } else {
-                    callback(0, '', template);
+                    callback(0, '', result);
                 }
             });
         return null;

+ 7 - 7
modules/main/models/ration.js

@@ -6,6 +6,7 @@ var db = require("../db/project_db");
 var subSchema = require("./bills_sub_schemas");
 var Schema = mongoose.Schema;
 var deleteSchema = require('../../../public/models/delete_schema');
+var async = require("async");
 var counter = require("../../../public/counter/counter.js");
 var consts = require('./project_consts');
 var projectConsts = consts.projectConst;
@@ -52,20 +53,19 @@ rationDAO.prototype.save = function(projectId, datas, callback){
     var functions = [];
     var data;
 
-    function saveOne(data) {
+    function saveOne(doc) {
         return function (cb) {
             switch (doc.updateType) {
                 case commonConsts.UT_UPDATE:
-                    ration.update({ID: doc.ID}, doc, cb);
+                    ration.update({projectID: doc.updateData.projectID, ID: doc.updateData.ID}, doc.updateData, cb);
                     break;
                 case commonConsts.UT_CREATE:
-                    ration.create(doc, cb);
+                    ration.create(doc.updateData, cb);
                     break;
                 case commonConsts.UT_DELETE:
-                /* 假删除
-                 var item = new ration(doc);
-                 item.remove(cb);
-                 */
+                    doc.updateData.deleteInfo = {deleted: true, deleteDateTime: new Date(), deleteBy: user_id};
+                    ration.update({projectID: doc.updateData.projectID, ID: doc.updateData.ID}, doc.updateData, cb);
+                    break;
             }
         }
     }

+ 13 - 0
modules/pm/routes/pm_route.js

@@ -14,8 +14,21 @@ pmRouter.use(function (req, res, next) {
     }
 });
 
+/*
+  req.body = {data: '{user_id}'}
+ */
 pmRouter.post('/getProjects', pmController.getProjects);
+
+/*
+ req.body = {data: '{user_id: user_id, updateData: [{updateType, updateData}]}'}
+ data.updateData.updateType: 1 of ['new', 'update', 'delete']
+ */
 pmRouter.post('/updateProjects', pmController.updateProjects);
+
+/*
+ req.body = {data: '{user_id: user_id, updateData: [{updateType, updateData}]}'}
+ data.updateData.updateType: 1 of ['update', 'copy']
+ */
 pmRouter.post('/copyProjects', pmController.copyProjects);
 pmRouter.post('/renameProject', pmController.rename);
 pmRouter.post('/beforeOpenProject', pmController.beforeOpenProject);

+ 10 - 0
modules/ration_repository/controllers/ration_controller.js

@@ -18,6 +18,16 @@ module.exports = {
             }
         });
     },
+    getRationItem: function (req, res) {
+        var sId = req.body.sectionId, code = req.body.code;
+        var rationData = {}
+        rationItem.getRationItem(sId, code).then(function (result) {
+            rationData = result._doc;
+            callback(req, res, null, '', rationData);
+        }).catch(function (err, message) {
+            callback(req, res, err, message, null);
+        })
+    },
     mixUpdateRationItems: function(req, res){
         var sectionId = req.body.sectionID,
             updateItems = JSON.parse(req.body.updateItems),

+ 16 - 0
modules/ration_repository/models/ration_item.js

@@ -78,6 +78,22 @@ rationItemDAO.prototype.getRationItemsByCode = function(code,callback){
     //
 };
 
+rationItemDAO.prototype.getRationItem = function (sectionId, code, callback) {
+    if (callback) {
+        rationItemModel.findOne({sectionId: sectionId, code: code}, '-_id').exec()
+            .then(function (result, err) {
+                if (err) {
+                    callback(1, '找不到定额“' + code +'”' , null);
+                } else {
+                    callback(0, '', result);
+                }
+            });
+        return null;
+    } else {
+        return rationItemModel.findOne({sectionId: sectionId, code: code}, '-_id').exec();
+    }
+}
+
 rationItemDAO.prototype.addRationItems = function(sectionId, items,callback){
     if (items && items.length > 0) {
         counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){

+ 1 - 0
modules/ration_repository/routes/ration_rep_routes.js

@@ -24,6 +24,7 @@ apiRouter.post("/updateNodes",rationChapterTreeController.updateNodes);
 apiRouter.post("/deleteNodes",rationChapterTreeController.deleteNodes);
 
 apiRouter.post("/getRationItems",rationController.getRationItemsBySection);
+apiRouter.post('/getRationItem', rationController.getRationItem);
 apiRouter.post("/mixUpdateRationItems",rationController.mixUpdateRationItems);
 
 apiRouter.post("/createNewGljTypeNode",repositoryGljController.createNewGljTypeNode);

+ 3 - 0
server.js

@@ -140,6 +140,9 @@ var billsLibRouter = require("./modules/bills_lib/routes/bills_lib_routes");
 app.use("/stdBillsEditor", billsLibRouter);
 //-----------------
 
+var rationRepository_Router = require("./modules/ration_repository/routes/ration_rep_routes");
+app.use("/rationRepository/api",rationRepository_Router);
+
 app.use(function(req, res, next) {
 	res.status(404).send('404 Error');
 });

+ 7 - 2
web/building_saas/main/html/main.html

@@ -648,7 +648,7 @@
                         return project.mainTree.selected ? project.mainTree.selected.sourceType !== project.Bills.getSourceType() : false;
                     },
                     callback: function (key, opt) {
-                        ProjectController.addBills(project, billsSpread);
+                        ProjectController.addBills(project, controller);
                     }
                 },
                 "insertRation": {
@@ -665,6 +665,9 @@
                         } else {
                             return true;
                         }
+                    },
+                    callback: function (key, opt) {
+                        ProjectController.addRation(project, controller);
                     }
                 },
                 "insertLJ": {
@@ -709,7 +712,9 @@
             var selected = controller.tree.selected;
 
             if (!selected || selected.sourceType === project.Bills.getSourceType()) {
-                ProjectController.addBills(project, billsSpread);
+                ProjectController.addBills(project, controller);
+            } else if (selected.sourceType === project.Ration.getSourceType()) {
+                ProjectController.addRation(project, controller);
             }
         });
         $('#delete').click(function () {

+ 45 - 11
web/building_saas/main/js/controllers/project_controller.js

@@ -3,11 +3,22 @@
  */
 
 ProjectController = {
-    addBills: function (project, mainSpread, std) {
-        if (!project || !mainSpread) { return null; }
+    /* sc: tree_sheet_controller */
+    syncDisplayNewNode: function (sc, newNode) {
+        TREE_SHEET_HELPER.massOperationSheet(sc.sheet, function () {
+            var sels = sc.sheet.getSelections();
+            sc.sheet.addRows(newNode.serialNo(), 1);
+            TREE_SHEET_HELPER.refreshTreeNodeData(sc.setting, sc.sheet, [newNode], false);
+            sc.setTreeSelected(newNode);
+            sc.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
+            sc.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
+        });
+    },
+    addBills: function (project, sheetController, std) {
+        if (!project || !sheetController) { return null; }
 
         var target = project.getParentTarget(project.mainTree.selected, 'sourceType', project.Bills.getSourceType());
-        var newSource = null, newNode = null, sels = billsSpread.getActiveSheet().getSelections();
+        var newSource = null, newNode = null;
 
         if (std) {
             newSource = project.Bills.insertStdBills(target ? target.source.getParentID() : project.Bills.tree.setting.rootId, target ? target.source.getNextSiblingID() : project.Bills.tree.setting.rootId, std.data);
@@ -21,13 +32,36 @@ ProjectController = {
             newNode.sourceType = project.Bills.getSourceType();
             newNode.data = newSource.data;
 
-            TREE_SHEET_HELPER.massOperationSheet(controller.sheet, function () {
-                controller.sheet.addRows(newNode.serialNo(), 1);
-                TREE_SHEET_HELPER.refreshTreeNodeData(controller.setting, controller.sheet, [newNode], false);
-                controller.setTreeSelected(newNode);
-                controller.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
-                controller.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center)
-            });
-        };
+            this.syncDisplayNewNode(sheetController, newNode);
+        }
+    },
+    addRation: function (project, sheetController, std) {
+        if (!project || !sheetController) { return null; }
+
+        var selected = project.mainTree.selected;
+        var newSource = null, newNode = null;
+
+        if (selected.sourceType === project.Bills.getSourceType() && selected.source.children.length === 0) {
+            if (std) {
+                newSource = project.Ration.insertStdRation(selected.source.getID(), null, std);
+            } else {
+                newSource = project.Ration.insertRation(selected.source.getID());
+            }
+            newNode = project.mainTree.insert(selected.getID(), selected.tree.rootID());
+        } else if (selected.sourceType === project.Ration.getSourceType()) {
+            if (std) {
+                newSource = project.Ration.insertStdRation(selected.source[project.masterField.ration], selected.source, std);
+            } else {
+                newSource = project.Ration.insertRation(selected.source[project.masterField.ration], selected.source);
+            }
+            newNode = project.mainTree.insert(selected.getParentID(), selected.getNextSiblingID());
+        }
+        if (newNode) {
+            newNode.source = newSource;
+            newNode.sourceType = project.Ration.getSourceType();
+            newNode.data = newSource;
+
+            this.syncDisplayNewNode(sheetController, newNode);
+        }
     }
 }

+ 17 - 11
web/building_saas/main/js/models/project.js

@@ -16,6 +16,7 @@ var PROJECT = {
 
         var me = tools;
         tools.doAfterUpdate = function(result){
+            if (!result) { return }
             result.forEach(function(item){
                 if (me.modules[item.moduleName]){
                     me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
@@ -65,15 +66,13 @@ var PROJECT = {
         project.prototype.loadMainTree = function () {
             var that = this;
             var loadRationNode = function (rations, cacheNode) {
-                var newNode;
-                rations.forEach(function (ration) {
-                    if (ration[that.masterField.ration] && ration[that.masterField.ration] === cacheNode.source.getID()) {
-                        newNode = that.mainTree.addNode(cacheNode);
-                        newNode.source = ration;
-                        newNode.sourceType = that.Ration.getSourceType();
-                        newNode.data = ration;
-                    }
-                });
+                var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
+                for (i = 0; i < br.length; i++) {
+                    newNode = that.mainTree.addNode(cacheNode);
+                    newNode.source = br[i];
+                    newNode.sourceType = that.Ration.getSourceType();
+                    newNode.data = br[i];
+                }
             };
             var loadIdTreeNode = function (nodes, parent) {
                 var newNode, i;
@@ -192,7 +191,7 @@ var PROJECT = {
         project.prototype.push = function(moduleName, data){
             if (tools.updateLock === 0){
                 throw "project can not push data before beginUpdate";
-            };
+            }
             var moduleData = {
                 moduleName: moduleName,
                 data: data
@@ -201,8 +200,15 @@ var PROJECT = {
         };
 
         project.prototype.pushNow = function (operation, moduleName, data) {
+            var that = this;
             this.beginUpdate(operation);
-            this.push(moduleName, data);
+            if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
+                moduleName.forEach(function (name, index) {
+                    that.push(moduleName[index], data[index]);
+                });
+            } else {
+                this.push(moduleName, data);
+            }
             this.endUpdate();
         }
 

+ 107 - 18
web/building_saas/main/js/models/ration.js

@@ -5,7 +5,8 @@
 var Ration = {
     createNew: function (project) {
         // 用户定义private方法
-        var tools = {};
+        var tools = {
+        };
 
         // 所有通过this访问的属性,都不应在此单元外部进行写入操作
         var ration = function (proj) {
@@ -17,27 +18,23 @@ var Ration = {
                 return sourceType;
             }
             proj.registerModule(ModuleNames.ration, this);
+
+            var maxRationID = 0;
+            this.getNewRationID = function () {
+                return maxRationID += 1;
+            };
+            this.maxRationID = function (maxID) {
+                if (arguments.length === 0) {
+                    return maxRationID;
+                } else {
+                    maxRationID = Math.max(maxID, maxRationID);
+                }
+            };
         };
 
-        // 从后台获取数据
-        /*ration.prototype.pullData = function (){
-            this.project.pullData(
-                '/ration/getData',
-                {projectID: this.project.ID},
-                function(result){
-                    if (result.error ===0){
-                        this.loadDatas(result.data);
-                    }
-                    else {
-                        // to do: 错误处理需要细化
-                        alert(result.message);
-                    }
-                },
-                function (){}//to do: 错误处理需要细化
-            )
-        };*/
         // prototype用于定义public方法
         ration.prototype.loadData = function (datas) {
+            var that = this;
             this.datas = datas;
             // generate Fees & Flags Index,For View & Calculate
             this.datas.forEach(function (data) {
@@ -49,6 +46,7 @@ var Ration = {
                 data.flags.forEach(function (flag) {
                     data.FlagsIndex[flag.fieldName] = flag;
                 });
+                that.maxRationID(data.ID);
             });
         };
 
@@ -57,6 +55,97 @@ var Ration = {
             // to do
         };
 
+        ration.prototype.getTempRationData = function (id, billsID, serialNo) {
+            var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
+            newData[project.masterField.ration] = billsID;
+            return newData;
+        };
+
+        ration.prototype.getBillsSortRation = function (billsID) {
+            var rations = this.datas.filter(function (data) {
+                return data[project.masterField.ration] === billsID;
+            });
+            rations.sort(function (x, y) {
+                return x.serialNo - y.serialNo;
+            });
+            return rations;
+        };
+
+        ration.prototype.getInsertRationData = function (billsID, preRation) {
+            var br = this.getBillsSortRation(billsID);
+            var updateData = [];
+            if (preRation) {
+                var preIndex = br.indexOf(preRation), i;
+                updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1)});
+                for (i = preIndex + 1; i < br.length; i++) {
+                    updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1)});
+                }
+            } else {
+                updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1)});
+            }
+            return updateData;
+        };
+
+        ration.prototype.getCounterData = function (count) {
+            var updateData = {'projectID': this.project.ID()};
+            if (count) {
+                updateData[this.getSourceType()] = this.maxRationID() + count;
+            } else {
+                updateData[this.getSourceType()] = this.maxRationID() + 1;
+            }
+            return updateData;
+        };
+
+        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)]);
+
+            var newRation = null;
+            if (preRation) {
+                var preIndex = br.indexOf(preRation), i;
+                newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
+                this.datas.push(newRation);
+                for (i = preIndex + 1; i < br.length; i++) {
+                    br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
+                }
+            } else {
+                newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
+                this.datas.push(newRation);
+            }
+            return newRation;
+        };
+
+        ration.prototype.insertStdRation = function (billsID, preRation, std) {
+            var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation);
+            updateData.forEach(function (data) {
+                if (data.updateType === 'ut_create') {
+                    data.updateData.code = std.code;
+                    data.updateData.name = std.name;
+                    data.updateData.unit = std.unit;
+                }
+            });
+            this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
+
+            var newRation = null;
+            if (preRation) {
+                var preIndex = br.indexOf(preRation), i;
+                newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
+                newRation.code = std.code;
+                newRation.name = std.name;
+                newRation.unit = std.unit;
+                this.datas.push(newRation);
+                for (i = preIndex + 1; i < br.length; i++) {
+                    br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
+                }
+            } else {
+                newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
+                this.datas.push(newRation);
+            }
+            return newRation;
+        };
+
         return new ration(project);
     }
 };

+ 3 - 1
web/building_saas/main/js/views/std_bills_lib.js

@@ -397,7 +397,9 @@ var LoadStdBills = (function () {
     };
 
     stdBillsSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (sender, args) {
-        ProjectController.addBills(project, billsSpread, stdBillsTree.items[args.row]);
+        if (stdBillsTree.items[args.row].children.length === 0) {
+            ProjectController.addBills(project, controller, stdBillsTree.items[args.row]);
+        }
     });
 
     return LoadData;

+ 15 - 3
web/building_saas/main/js/views/std_ration_lib.js

@@ -7,12 +7,23 @@
 var rationLibObj = {
     rationChapterSpread: null,
     sectionRationsSpread: null,
+    rationChapterTreeController: null,
     checkSpread: function () {
         if (!this.rationChapterSpread) {
             this.rationChapterSpread = SheetDataHelper.createNewSpread($('#stdRationChapter')[0]);
         }
         if (!this.sectionRationsSpread) {
             this.sectionRationsSpread = SheetDataHelper.createNewSpread($('#stdSectionRations')[0]);
+
+
+            this.sectionRationsSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (sender, args) {
+                var select = $('#stdRationLibSelect'), rationCode = args.sheet.getText(args.row, 0);
+                if (rationCode !== '') {
+                    CommonAjax.postRationLib('/rationRepository/api/getRationItem', {user_id: userID, sectionId: rationLibObj.rationChapterTreeController.tree.selected.getID(), code: rationCode}, function (data) {
+                        ProjectController.addRation(project, controller, data);
+                    });
+                }
+            });
         }
     },
     loadStdRationLibs: function () {
@@ -30,6 +41,7 @@ var rationLibObj = {
         });
     },
     loadStdRation: (function () {
+        var that = this;
         var rationChapterTreeSetting = {
             "emptyRows":0,
             "headRows":1,
@@ -57,11 +69,11 @@ var rationLibObj = {
         };
         var showRationChapterTree = function (datas) {
             var rationChapterTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: false});
-            var rationChapterTreeController = TREE_SHEET_CONTROLLER.createNew(rationChapterTree, rationLibObj.rationChapterSpread.getActiveSheet(), rationChapterTreeSetting);
+            rationLibObj.rationChapterTreeController = TREE_SHEET_CONTROLLER.createNew(rationChapterTree, rationLibObj.rationChapterSpread.getActiveSheet(), rationChapterTreeSetting);
             rationChapterTree.loadDatas(datas);
-            rationChapterTreeController.showTreeData();
+            rationLibObj.rationChapterTreeController.showTreeData();
 
-            rationChapterTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
+            rationLibObj.rationChapterTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
                 rationLibObj.loadSectionRations(node.getID());
             });