zhangweicheng 7 years ago
parent
commit
6969cddba3

+ 4 - 0
modules/glj/controllers/glj_controller.js

@@ -619,6 +619,10 @@ class GLJController extends BaseController {
         res.json(result);
     }
 
+    async getProjectGLJsByProjectID(projectId){
+        return await getGLJListByProjectID(projectId)
+    }
+
 }
 
 /**

+ 1 - 1
modules/main/controllers/bills_controller.js

@@ -168,7 +168,7 @@ module.exports = {
     pasteBlock:async function(req,res){
         let result={
             error:0
-        }
+        };
         try {
             let data = req.body.data;
             data = JSON.parse(data);

+ 122 - 8
modules/main/facade/bill_facade.js

@@ -2,9 +2,22 @@
  * Created by zhang on 2018/1/22.
  */
 let mongoose = require('mongoose');
+let projectConst = require('../models/project_consts');
+const rationType = projectConst.rationType;
+const rationKeyArray = projectConst.rationKeyArray;
+const gljKeyArray = projectConst.gljKeyArray;
 let quantity_detail = require("./quantity_detail_facade");
+let quantity_detail_model = mongoose.model('quantity_detail');
+let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
 let Bills_Lib = mongoose.model('std_bills_lib_bills');
+let ration_Model = mongoose.model('ration');
+let ration_glj_Model = mongoose.model('ration_glj');
+let ration_coe_Model = mongoose.model('ration_coe');
+let ration_installation_Model = mongoose.model('ration_installation');
 let bill_Model = require('../models/bills').model;
+import GLJController from "../../glj/controllers/glj_controller";
+
+import GLJListModel from '../../glj/models/glj_list_model';
 let _ = require("lodash");
 module.exports={
     getSectionInfo : async function (data) {
@@ -44,26 +57,117 @@ module.exports={
         return null;
     },
     reorganizeFBFX:async function(data){
-       let result = {};
-       let tasks = generateBillTasks(data);
-       if(data.delete && data.delete.length > 0){
-           let qd_query={projectID: data.projectID, billID: {"$in": data.delete}};
-           await quantity_detail.deleteByQuery(qd_query) ;
+        let result = {};
+        let tasks = generateBillTasks(data);
+        if(data.delete && data.delete.length > 0){
+            let qd_query={projectID: data.projectID, billID: {"$in": data.delete}};
+            await quantity_detail.deleteByQuery(qd_query) ;
         }
         if(tasks.length > 0){
             result =await bill_Model.bulkWrite(tasks);
         }
-        return result;
+       return result;
     },
     pasteBlock : async function(data){
+        let pasteTasks = [
+            pasteRationsAndRationGLJ(data.rations,data.ration_gljs),//这一步会花费比较多时间
+            pasteOtherData(data)
+        ];
+        let [rationsAndRationGLJ,resultMap] = await Promise.all(pasteTasks);
+
+        let gljController = new GLJController();
+        let projectGLJ = await gljController.getProjectGLJsByProjectID(data.projectID);
+        resultMap.rations = rationsAndRationGLJ.rations;
+        resultMap.ration_gljs = rationsAndRationGLJ.new_ration_gljs;
+        resultMap.gljData = projectGLJ.data;
+        return resultMap;
+    }
+};
 
+async function pasteOtherData(data) {
+    let bills = data.bills;
+    let quantity_details = data.quantity_details;
+    let ration_coes = data.ration_coes;
+    let ration_installations = data.ration_installations;
+    let updateData = data.updateData;
+    let uModel = null;
+    let tasks = [];
+
+    //生成更新任务
+    for(let u of updateData){
+        if(u.type == "bills"){
+            uModel = bill_Model;
+        }else {
+            uModel = ration_Model;
+        }
+        let tem = {
+            updateOne:{
+                filter:u.query,
+                update :u.doc
+            }
+        };
+        tasks.push(tem);
+    }
 
+    bills.length > 0 ? await insertMany(bills,bill_Model):'';
+    quantity_details.length > 0 ? await insertMany(quantity_details,quantity_detail_model):'';
+    ration_coes.length > 0 ? await insertMany(ration_coes,ration_coe_Model):'';
+    ration_installations.length > 0 ? await insertMany(ration_installations,ration_installation_Model):'';
+    tasks.length>0?await uModel.bulkWrite(tasks):'';
 
+    return {bills:bills,quantity_details:quantity_details,ration_coes:ration_coes,ration_installations:ration_installations,updateData:updateData}
+}
 
+async function pasteRationsAndRationGLJ (rations,ration_gljs) {
+    let projectGljModel = new GLJListModel();
+    let gljMap = {};
+    let new_ration_gljs=[];
+    //先根据定额类型的工料机,插入到项目工料机中
+    for(let r of rations){
+        if(r.type == rationType.gljRation){
+            await getProjectGLJ(r,rationKeyArray,gljMap);
+        }
+    }
 
-        console.log(data);
+    for(let rg of ration_gljs){
+        await getProjectGLJ(rg,gljKeyArray,gljMap);
+        new_ration_gljs.push(ration_glj_facade.createNewRecord(rg));
     }
-};
+
+    rations.length>0?await insertMany(rations,ration_Model):'';
+    new_ration_gljs.length>0?await insertMany(new_ration_gljs,ration_glj_Model):'';
+
+    return{rations:rations,new_ration_gljs:new_ration_gljs};
+
+    async function getProjectGLJ (glj,keyArray,gljMap) {
+        let keyIndex = projectGljModel.getIndex(glj,keyArray);
+        let pgljResult = null;
+        if(gljMap[keyIndex]){
+            pgljResult = gljMap[keyIndex]
+        }else {
+            let p_glj = ration_glj_facade.getGLJSearchInfo(glj);
+            pgljResult = await projectGljModel.addList(p_glj);//逐条添加到项目工料机
+            gljMap[keyIndex] = pgljResult;
+        }
+        setResult(glj,pgljResult);
+    }
+
+
+    function setResult(glj,result ) {
+        let typeString = result.type+'';
+        glj.marketPrice = result.unit_price.market_price;
+        glj.adjustPrice = result.unit_price.base_price;
+        glj.basePrice = result.unit_price.base_price;
+        glj.isAdd = result.unit_price.is_add;
+        glj.projectGLJID = result.id;
+        if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
+            glj.isEstimate = result.is_evaluate;
+        }
+    }
+
+
+}
+
 
 function generateBillTasks(data) {
     let tasks=[];
@@ -110,4 +214,14 @@ function generateBillTasks(data) {
         }
     }
     return tasks;
+}
+
+
+async function insertMany(datas,model) {
+    while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
+        let newList = datas.splice(0,1000);//一次插入1000条
+        await model.insertMany(newList);
+    }
+    await model.insertMany(datas);
+
 }

+ 2 - 1
modules/main/facade/ration_installation_facade.js

@@ -2,7 +2,8 @@
  * Created by zhang on 2018/2/24.
  */
 let mongoose = require('mongoose');
-let rationInstallationModel = mongoose.model('ration_installation');let consts = require('../models/project_consts');
+let rationInstallationModel = mongoose.model('ration_installation');
+let consts = require('../models/project_consts');
 let projectConsts = consts.projectConst;
 
 module.exports={

+ 9 - 2
modules/main/models/project_consts.js

@@ -45,5 +45,12 @@ let commonConst = {
     UT_DELETE: 'ut_delete'
 };
 
-
-module.exports = {projectConst: projectConst, commonConst: commonConst, projectConstList: projectConstList};
+const gljKeyArray =['code','name','specs','unit','type'];
+const rationKeyArray =['code','name','specs','unit','subType'];
+const rationType = {
+    ration: 1,
+    volumePrice: 2,
+    gljRation: 3,
+    install:4
+};
+module.exports = {projectConst: projectConst, commonConst: commonConst, projectConstList: projectConstList,gljKeyArray:gljKeyArray,rationKeyArray:rationKeyArray,rationType:rationType};

+ 2 - 4
modules/ration_glj/facade/ration_glj_facade.js

@@ -43,7 +43,8 @@ module.exports = {
     insertGLJAsRation: insertGLJAsRation,
     getRationTypeGLJQuantity:getRationTypeGLJQuantity,
     getInfoFromProjectGLJ:getInfoFromProjectGLJ,
-    createNewRecord:createNewRecord
+    createNewRecord:createNewRecord,
+    getGLJSearchInfo:getGLJSearchInfo
 }
 
 let operationMap = {
@@ -757,8 +758,6 @@ async function getGLJClass(info, data) {
 
 async function insertGLJAsRation(data) {
     let gljList = data.gljList;
-    //先更新counter
-    let counter = await projCounter.model.findOneAndUpdate({projectID: data.projectID}, {ration: data.rationCount}, {new: true});
     if (data.hasOwnProperty("selectedSerialNo")) { //如果需要,更新序列号。
         let query = {
             projectID: data.projectID,
@@ -783,7 +782,6 @@ async function insertGLJAsRation(data) {
         }
     }
     await ration.insertMany(gljList);
-    console.log(gljList);
     return gljList;
 }
 

+ 110 - 10
web/building_saas/main/js/controllers/block_controller.js

@@ -189,7 +189,9 @@ let BlockController = {
      * @param position next/pre/sub
      */
     confirmPaste:function (blockData,selected,position) {
-        let Bills = projectObj.project.Bills;
+        let me = this;
+        let project = projectObj.project;
+        let Bills = project.Bills;
         let parent = null,next = null,pre = null;
         let updateData = [],billUpdate = null;
         let billsIDMap = {};//用来做新旧ID映射
@@ -236,15 +238,115 @@ let BlockController = {
             updateData.push(billUpdate);
         }
         dataMap.updateData = updateData;
+        dataMap.projectID = projectObj.project.ID();
         console.log(dataMap);
         $.bootstrapLoading.start();
-        CommonAjax.post('/bills/pasteBlock',dataMap,function (data) {
+        CommonAjax.post('/bills/pasteBlock',dataMap,function (result) {
              $.bootstrapLoading.end();
+             //更新前端缓存
+             me.updateCache(result);
+             //插入树节点
+             let rationNodes = me.addToTree(parentID,nextID,result.bills,result.rations);
+             //主材设备工料机插入主树
+             project.ration_glj.addToMainTree(result.ration_gljs);
+             //更新计算程序模板,并进行重新计算
+             project.calcProgram.calcRationsAndSave(rationNodes);
+        })
 
+    },
 
-        })
+    updateCache:function(result){
+        let project = projectObj.project;
+        let Bills = project.Bills,Ration = project.Ration;
+        let mainTree = project.mainTree;
+        Bills.addDatasToList(result.bills);
+        Ration.addDatasToList(result.rations);
+        project.quantity_detail.addDatasToList(result.quantity_details);
+        project.ration_glj.addDatasToList(result.ration_gljs);
+        project.ration_coe.addDatasToList(result.ration_coes);
+        project.ration_installation.addDatasToList(result.ration_installations)
+        for(let u of  result.updateData){
+            let unode = mainTree.findNode(u.query.ID);
+            if(unode){
+                for(let ukey in u.doc){
+                    unode.data[ukey] = u.doc[ukey];
+                }
+            }
+        }
+        //更新项目工料机模块信息-计算消耗量
+        project.projectGLJ.datas = result.gljData;
+        project.projectGLJ.calcQuantity();
+    },
+
+    addToTree:function (parentID,nextID,bills,rations) {
+        let project = projectObj.project;
+        let Bills = project.Bills,mainTree = project.mainTree;
+        let parentMap_b = {},parentMap_r = {};
+        let newNodes = [],firstNode = null,rationNodes=[];
+
+        createParentMap(parentMap_b,bills,'bills');
+        createParentMap(parentMap_r,rations,'ration');
+        if(parentMap_b[parentID]){
+            firstNode = loadTreeNode(parentID,nextID,parentMap_b[parentID][0],'bills');
+
+        }else if(parentMap_r[parentID]){
+            firstNode = loadTreeNode(parentID,nextID,parentMap_r[parentID][0],'ration');
+        }
+
+        ProjectController.syncDisplayNewNodes(projectObj.mainController, newNodes);
+        let sels = projectObj.mainController.sheet.getSelections();
+        //设置选中并更新下方显示
+        projectObj.mainController.setTreeSelected(firstNode);
+        projectObj.mainController.sheet.setSelection(firstNode.serialNo(), sels[0].col, 1, 1);
+        return rationNodes;
+
+        function loadTreeNode(parentID,nextID,data,type) {
+            let newNode = null;
+            if(type == 'bills'){
+                let newSource = Bills.tree.insertByData(data,parentID,nextID,true);
+                newNode = mainTree.insert(parentID, nextID,data.ID);
+                if (newNode) {
+                    newNode.source = newSource;
+                    newNode.sourceType = Bills.getSourceType();
+                    newNode.data = data;
+                    newNodes.push(newNode);
+                    let subType = 'bills';
+                    let children = parentMap_b[data.ID];
+                    if(!children){
+                        children = parentMap_r[data.ID];
+                        subType = 'ration';
+                    }
+                    if(children){
+                        for(let c of children){
+                            loadTreeNode(data.ID,-1,c,subType);
+                        }
+                    }
+
+                }
+            }else if(type == 'ration'){
+                newNode = project.mainTree.insert(parentID, nextID, data.ID);
+                newNode.source = data;
+                newNode.sourceType = project.Ration.getSourceType();
+                newNode.data = data;
+                newNodes.push(newNode);
+                rationNodes.push(newNode);
+            }
+            return newNode;
+        }
+        
+        
+        function createParentMap(parentMap,list,type) {
+            let parentKey = type=='bills'?'ParentID':'billsItemID';
+            for(let l of list){
+                let parentID = l[parentKey];
+                if(parentMap[parentID]){
+                    parentMap[parentID].push(l);
+                }else {
+                    parentMap[parentID] = [l];
+                }
+            }
+        }
 
-        //  delete fees / feesIndex /__v
     },
 
     preparePasteData : function (data,billsIDMap) {
@@ -257,8 +359,6 @@ let BlockController = {
             billsIDMap[b.ParentID]?b.ParentID = billsIDMap[b.ParentID]:'';
             billsIDMap[b.NextSiblingID]?b.NextSiblingID = billsIDMap[b.NextSiblingID]:'';
         }
-
-
         return {bills:bills,rations:rations,ration_gljs:ration_gljs,ration_coes:ration_coes,quantity_details:quantity_details,ration_installations:ration_installations};
 
         function eachData(data) {
@@ -307,6 +407,7 @@ let BlockController = {
 
         function createQuantityDetails(detailData,pdata,type) {
             let tem_detail = _.cloneDeep(detailData);
+            delete tem_detail._id;
             tem_detail.ID = uuid.v1();
             tem_detail.projectID = projectObj.project.ID();
             if(type == 'bills'){
@@ -321,6 +422,7 @@ let BlockController = {
         function createRationData(rationData) {
             let tem_ration = _.cloneDeep(rationData);
             //删除旧数据
+            delete tem_ration._id;
             delete tem_ration.fees;
             delete tem_ration.feesIndex;
             delete tem_ration.ration_gljs;
@@ -345,6 +447,7 @@ let BlockController = {
         function createBillsData(billsData) { //ID、重新生成code
             let temData = _.cloneDeep(billsData);
             //删除旧数据
+            delete  temData._id;
             delete  temData.fees;
             delete  temData.feesIndex;
             delete  temData.children;
@@ -385,7 +488,6 @@ let BlockController = {
         }
 
     },
-
     calcRationQuantityAndContain : function (billsQuantity,ration) {//计算定额工程量和含量
         let EXPString = ration.quantityEXP+"";
         if(EXPString.indexOf("QDL") != -1){
@@ -411,8 +513,7 @@ let BlockController = {
             ration.contain = tem_contain;
         }
     },
-
-   newFormatCode : function (stdCode, filterCode) {
+    newFormatCode : function (stdCode, filterCode) {
         let matchs = this.sameStdCode(stdCode, filterCode);
         let format = function (Number) {
             let s = Number + '';
@@ -438,7 +539,6 @@ let BlockController = {
         return matchs;
     },
 
-
     removeBlock:function () {
         removeLocalCache('project_block');
     }

+ 8 - 1
web/building_saas/main/js/models/bills.js

@@ -112,7 +112,14 @@ var Bills = {
             // datas load to Tree
             this.tree.loadDatas(this.datas);
         };
-
+        bills.prototype.addDatasToList = function (new_datas) {
+            let me = this;
+            if(me.datas && Array.isArray(me.datas)){
+                for(let d of new_datas){
+                    me.datas.push(d);
+                }
+            }
+        };
         bills.prototype.setMaxID = function (ID) {
             this.tree.maxNodeID(ID);
         };

+ 1 - 1
web/building_saas/main/js/models/calc_program.js

@@ -1603,7 +1603,7 @@ class CalcProgram {
         me.saveNodes(allChangedNodes, callback);
     };
 
-    // 计算多条零散的清单,并计算他们的父清单、引用清单,然后打包存储。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
+    // 计算多条零散的清单,并计算他们的父清单、引用清单,然后打包存储。如:多选删除树结点(如多选清单、定额等,不区分树结点类型)。
     calcBillsAndSave(billNodes,callback){
         let me = this, allChangedNodes = [];
         for (let node of billNodes) {

+ 4 - 4
web/building_saas/main/js/models/project_glj.js

@@ -738,7 +738,7 @@ ProjectGLJ.prototype.getQuantityPerGLJ = function (ration_glj_list,rations,ratio
     let mixRatioMap = this.datas.mixRatioMap;
     let q_decimal = getDecimal("glj.quantity");
     let result={};
-    let quantitySum=0;//工料机汇总消耗量
+    let quantity_sum=0;//工料机汇总消耗量
     let sum = 0;//分部分项总消耗量
     let tech_sum = 0;//技术措施总消耗量
     for(let rg of ration_glj_list){
@@ -749,7 +749,7 @@ ProjectGLJ.prototype.getQuantityPerGLJ = function (ration_glj_list,rations,ratio
                 continue;
             }
             let total = scMathUtil.roundForObj(glj_quantity*r_quantity, q_decimal);
-            quantitySum = scMathUtil.roundForObj(quantitySum+total,q_decimal);
+            quantity_sum = scMathUtil.roundForObj(quantity_sum+total,q_decimal);
             if(_.includes(billIDs,rg.billsItemID)){//计算分部分项
                 sum = scMathUtil.roundForObj(sum+total,q_decimal);
             }
@@ -761,7 +761,7 @@ ProjectGLJ.prototype.getQuantityPerGLJ = function (ration_glj_list,rations,ratio
         if(ra.type == rationType.gljRation&&ra.projectGLJID===pglj.id){
             let r_quantity = scMathUtil.roundForObj(ra.quantity,q_decimal);
             r_quantity = r_quantity?r_quantity:0;
-            quantitySum = scMathUtil.roundForObj(quantitySum+r_quantity,q_decimal);
+            quantity_sum = scMathUtil.roundForObj(quantity_sum+r_quantity,q_decimal);
             if(_.includes(billIDs,ra.billsItemID)){//计算分部分项
                 sum = scMathUtil.roundForObj(sum+r_quantity,q_decimal);
             }
@@ -773,6 +773,6 @@ ProjectGLJ.prototype.getQuantityPerGLJ = function (ration_glj_list,rations,ratio
     }
     result.subdivisionQuantity = sum;
     result.techQuantity = tech_sum;
-    result.quantity = quantitySum;
+    result.quantity = quantity_sum;
     return result;
 } 

+ 10 - 1
web/building_saas/main/js/models/quantity_detail.js

@@ -480,7 +480,16 @@ var quantity_detail = {
         quantity_detail.prototype.getDetailByBillID = function (billID) {
             return _.filter(this.datas,{'billID':billID});
         };
-
+        quantity_detail.prototype.addDatasToList = function (datas) {
+            let  me = projectObj.project.quantity_detail;
+            if(datas&&datas.length>0){
+                if (me.datas && Array.isArray(me.datas)) {
+                    me.datas = me.datas.concat(datas);
+                } else {
+                    me.datas = datas;
+                }
+            }
+        };
         quantity_detail.prototype.deleteByRation = function(ration){
             var detail_list = this.datas;
             _.remove(detail_list,{'rationID':ration.ID});

+ 10 - 1
web/building_saas/main/js/models/ration.js

@@ -68,7 +68,16 @@ var Ration = {
         ration.prototype.setMaxID = function (ID) {
             this.maxRationID(ID);
         }
-
+        ration.prototype.addDatasToList = function (new_datas) {
+            let me = this;
+            if(me.datas && Array.isArray(me.datas)){
+                for(let d of new_datas){
+                    me.datas.push(d);
+                }
+            }else {
+                me.datas = new_datas;
+            }
+        };
         // refresh after update
         ration.prototype.doAfterUpdate = function(err, data){
             if(data.stateRefresh){

+ 0 - 19
web/building_saas/main/js/views/glj_view.js

@@ -780,25 +780,6 @@ var gljOprObj = {
         }
 
     },
-
-    testBatchUpdate:function () {
-        let me=gljOprObj;
-        let temA = [1,2,3];
-        me.b(1,a);
-
-
-        function a(v) {
-            let index = temA.indexOf(v);
-            if(index < temA.length-1){
-               me.b(temA[index+1],a)
-            }
-        }
-
-    },
-    b:function (g,callback) {
-        console.log(g);
-        callback(g);
-    },
     generateHtmlString: function () {
 //        return "<div id='edit'><div>";