Browse Source

工程量明细计算挪到前端

zhangweicheng 7 years ago
parent
commit
7dd804ace5

+ 11 - 5
modules/main/facade/quantity_detail_facade.js

@@ -91,10 +91,14 @@ async function updateRegex(datas) {
     if(datas.doc.regex==null){
         result=0;
         datas.doc.referenceIndexs=[];
-    }else {
-        regex = datas.doc.regex.toUpperCase();
-        let referenceIndexs = datas.doc.referenceIndexs;
-        result = getEvalResult(referenceIndexs,detailList,regex,decimal);
+    }else{
+        if(datas.doc.hasOwnProperty('result')){//如果前端已经计算好了
+            result = datas.doc.result;
+        }else {
+            regex = datas.doc.regex.toUpperCase();
+            let referenceIndexs = datas.doc.referenceIndexs;
+            result = getEvalResult(referenceIndexs,detailList,regex,decimal);
+        }
     }
     detailList[datas.query.index].result =result;
     detailList[datas.query.index].regex=datas.doc.regex;
@@ -248,7 +252,9 @@ async function insertRecodeWithReg (doc) {
         let regex = doc.regex.toUpperCase();
         let referenceIndexs = doc.referenceIndexs;
         let detailList = await getDatailList(doc,returnObjec);
-        doc.result =getEvalResult(referenceIndexs,detailList,regex,decimal);
+        if(!doc.hasOwnProperty('result')){//前端已结计算完了,后端就不用再计算了
+            doc.result =getEvalResult(referenceIndexs,detailList,regex,decimal);
+        }
         let refreshQuantity =false;
         if(doc.refreshQuantity==true){
             refreshQuantity = true;

+ 87 - 3
web/building_saas/main/js/models/quantity_detail.js

@@ -25,6 +25,14 @@ var quantity_detail = {
             this.datas = datas;
         };
 
+        quantity_detail.prototype.getListByID = function (ID,field) {
+             let condition={};
+            condition[field] = ID;
+             let details = _.filter(this.datas, condition);
+            details = _.sortBy(details, 'seq');
+            return details;
+        };
+
         // 提交数据后返回数据处理
         quantity_detail.prototype.doAfterUpdate = function(err, data){
             if(!err){
@@ -130,8 +138,8 @@ var quantity_detail = {
             doc.projectID = selected.data.projectID;
             doc[dataCode]=args.editingText;
             doc.seq=args.row;
-            if(dataCode=='regex'){
-                if(!this.regexChecking(args.editingText)||!this.referenceChecking(args.editingText,args.row,doc)){
+            if(dataCode=='regex'){//  if(this.checkAndCalcResult(args.editingText,args.row,doc) === false){
+                if(this.checkAndCalcResult(args.editingText,args.row,doc) === false){
                     gljOprObj.showQuantityDetailData();
                     return;
                 }
@@ -283,6 +291,82 @@ var quantity_detail = {
             let regExp = new RegExp(FindText, "g");
             return str.replace(regExp, RepText);
         };
+
+        quantity_detail.prototype.calcResult = function (doc) {//return false 代表输入有误
+            let field = doc.billID?"billID":"rationID";// doc.rationID
+            let detailList = this.getListByID(doc[field],field);
+            let result = this.getEvalResult(doc.referenceIndexs,detailList,doc.regex);
+            if(result === null){
+                return false;
+            }else {
+                doc.result = result;
+                return true;
+            }
+        };
+        quantity_detail.prototype.getEvalResult = function(referenceIndexs,detailList,regex) {
+            try {
+                let decimal = getDecimal("quantity_detail");
+                for(let i of referenceIndexs){
+                    regex = this.replaceReference(i,detailList,regex)
+                }
+                console.log('replace all C reference -----'+regex);
+                regex = this.replaceSqr(regex);
+                console.log('replace all sqar reference -----'+regex);
+                return scMathUtil.roundTo(eval(regex), -decimal);
+            }catch (error){
+                alert('输入的表达式有误,请重新输入!');
+                return null;
+            }
+        };
+        quantity_detail.prototype.replaceReference = function(index,detailList,str) {
+            str=str.toUpperCase();
+            let rstr= detailList[index-1].regex==null?'0':'('+detailList[index-1].regex+')';
+            str=this.replaceAll('C'+index,rstr,str);
+            if(detailList[index-1].referenceIndexs.length>0){
+                for (let i of detailList[index-1].referenceIndexs){
+                    str =this.replaceReference(i,detailList,str);
+                }
+            }
+            return str;
+        };
+
+        quantity_detail.prototype.replaceSqr=function(text) {
+            var squarRegex = /\([^\^]+\)\^\d+/g;
+            var sqararr = text.match(squarRegex);
+
+            var squarRegex2 = /C[0-9]+\^\d+|[0-9]+([.]{1}[0-9]+){0,1}\^\d+/g; //匹配没有括号的
+            var sqararr2=text.match(squarRegex2);
+            if(sqararr){
+                text=this.converSqrByArr(sqararr,text);
+            }
+            if(sqararr2){
+                text=this.converSqrByArr(sqararr2,text);
+            }
+            return text;
+        };
+
+        quantity_detail.prototype.converSqrByArr  = function(sqararr,text) {
+            var temp = text;
+            sqararr.forEach(function (item) {
+                var arr = item.split('\^');
+                var y = parseInt(arr[1]);
+                var x_arr = [];
+                for (var i = 0; i < y; i++) {
+                    x_arr.push(arr[0]);
+                }
+                var temStr = x_arr.join('*');
+                temp = temp.replace(item, temStr);
+            });
+            return temp;
+        };
+
+        quantity_detail.prototype.checkAndCalcResult = function (regex,row,doc) {//return false 表示没有通过检查
+            if(!this.regexChecking(regex)||!this.referenceChecking(regex,row,doc)||!this.calcResult(doc)){
+                return false;
+            }
+            return true;
+        };
+
         quantity_detail.prototype.updateQuantityDetail=function (args,dataCode,recode,selected,batchCallback) {
             var doc ={},me = this;
             var query={
@@ -292,7 +376,7 @@ var quantity_detail = {
             var selected = selected?selected:projectObj.project.mainTree.selected;
             doc[dataCode]=args.editingText;
             if (dataCode == 'regex') {
-                if(!this.regexChecking(args.editingText)||!this.referenceChecking(args.editingText,args.row,doc)){
+                if(this.checkAndCalcResult(args.editingText,args.row,doc) === false){
                     gljOprObj.showQuantityDetailData();
                     return;
                 }

+ 2 - 7
web/building_saas/main/js/views/glj_view.js

@@ -705,15 +705,10 @@ var gljOprObj = {
         return temRationGLJs;
     },
     showQuantityDetailData: function (node) {
-        var details = [];
         node = node ? node : projectObj.project.mainTree.selected;
         var quantity_detail = projectObj.project.quantity_detail;
-        if (node.sourceType == ModuleNames.ration) {
-            details = _.filter(quantity_detail.datas, {'rationID': node.data.ID});
-        } else if (node.sourceType == ModuleNames.bills) {
-            details = _.filter(quantity_detail.datas, {'billID': node.data.ID});
-        }
-        details = _.sortBy(details, 'seq');
+        let field = node.sourceType == ModuleNames.ration?'rationID':'billID';
+        let details = quantity_detail.getListByID(node.data.ID,field);
         sheetCommonObj.showData(this.detailSheet, this.detailSetting, details);
         this.detailData = details;
         if (MainTreeCol.readOnly.forQuantifyDetail(node)) {