Browse Source

增加汇总指标及汇总指标处理逻辑

TonyKang 6 years ago
parent
commit
13bac7a721

+ 8 - 1
modules/main/models/project_consts.js

@@ -40,6 +40,13 @@ let projectConstList = [
     'calc_program'
 ];
 
+let summaryConstList = [
+    `Construct`,
+    `ConstructDetail`,
+    `Segment`,
+    `SegmentDetail`
+];
+
 let commonConst = {
     UT_UPDATE: 'ut_update',
     UT_CREATE: 'ut_create',
@@ -54,4 +61,4 @@ const rationType = {
     gljRation: 3,
     install:4
 };
-module.exports = {projectConst: projectConst, commonConst: commonConst, projectConstList: projectConstList,gljKeyArray:gljKeyArray,rationKeyArray:rationKeyArray,rationType:rationType};
+module.exports = {projectConst: projectConst, commonConst: commonConst, projectConstList: projectConstList,gljKeyArray:gljKeyArray,rationKeyArray:rationKeyArray,rationType:rationType, summaryConstList: summaryConstList};

+ 60 - 2
modules/reports/controllers/rpt_controller.js

@@ -10,6 +10,7 @@ import JV from "../rpt_component/jpc_value_define";
 let Template = mongoose.model('rpt_templates');
 let rptTplDataFacade = require("../facade/rpt_tpl_data_facade");
 let fsUtil = require("../../../public/fsUtil");
+let pm_facade = require('../../../modules/pm/facade/pm_facade');
 
 import rptTplFacade from "../facade/rpt_template_facade";
 import demoTemplateFacade from "../facade/rpt_tpl_data_demo_facade";
@@ -218,9 +219,65 @@ function getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, custo
         if (rptTpl) {
             let rptDataUtil = new rptDataExtractor();
             rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
-            let filter = rptDataUtil.getDataRequestFilter();
+            let summaryRst = [];
+            let filter = rptDataUtil.getDataRequestFilter(summaryRst);
+            let promiseArr = [null, null];
+            if (summaryRst.length > 0) {
+                if (summaryRst.indexOf(`Construct`) >= 0 || summaryRst.indexOf(`ConstructDetail`) >= 0) {
+                    promiseArr[0] = pm_facade.getSummaryInfoByTender(prj_id, pm_facade.projectType.project);
+                }
+                if (summaryRst.indexOf(`Segment`) >= 0 || summaryRst.indexOf(`SegmentDetail`) >= 0) {
+                    promiseArr[1] = pm_facade.getSummaryInfoByTender(prj_id, pm_facade.projectType.engineering);
+                }
+            }
             rptTplDataFacade.prepareProjectData(user_id, prj_id, filter, function (err, msg, rawDataObj) {
                 if (!err) {
+                    let buildPageData = function() {
+                        let tplData = rptDataUtil.assembleData(rawDataObj);
+                        let printCom = JpcEx.createNew();
+                        if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
+                        //console.log("orientation: " + (orientation === 'null'));
+                        if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
+                        let defProperties = rptUtil.getReportDefaultCache();
+                        if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
+                        let dftOption = option||JV.PAGING_OPTION_NORMAL;
+                        printCom.initialize(rptTpl);
+                        printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
+                        let maxPages = printCom.totalPages;
+                        let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
+                        if (pageRst) {
+                            // fsUtil.writeObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/testBuiltPageResult.jsp");
+                            cb(null, pageRst);
+                        } else {
+                            cb('Have errors while on going...', null);
+                        }
+                    };
+                    //*/
+                    //取汇总数据流程
+                    if (promiseArr[0] !== null) {
+                        promiseArr[0].then(function (rst) {
+                            rawDataObj.Construct = rst.parent;
+                            rawDataObj.ConstructDetail = rst.subList;
+                            if (promiseArr[1] !== null) {
+                                promiseArr[1].then(function (rst) {
+                                    rawDataObj.Segment = rst.parent;
+                                    rawDataObj.SegmentDetail = rst.subList;
+                                    buildPageData(rawDataObj, rptDataUtil, rptTpl);
+                                });
+                            } else {
+                                buildPageData(rawDataObj, rptDataUtil, rptTpl);
+                            }
+                        });
+                    } else  if (promiseArr[1] !== null) {
+                        promiseArr[1].then(function (rst) {
+                            rawDataObj.Segment = rst.parent;
+                            rawDataObj.SegmentDetail = rst.subList;
+                            buildPageData(rawDataObj, rptDataUtil, rptTpl);
+                        });
+                    } else {
+                        buildPageData(rawDataObj, rptDataUtil, rptTpl);
+                    }
+                    /*/
                     let tplData = rptDataUtil.assembleData(rawDataObj);
                     let printCom = JpcEx.createNew();
                     if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
@@ -239,7 +296,7 @@ function getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, custo
                     } else {
                         cb('Have errors while on going...', null);
                     }
-
+                    //*/
                 } else {
                     cb('No report data were found!', null);
                 }
@@ -250,6 +307,7 @@ function getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, custo
     })
 }
 
+
 module.exports = {
     getReportAllPages: function (req, res) {
         let params = JSON.parse(req.body.params),

+ 51 - 15
modules/reports/util/rpt_construct_data_util.js

@@ -14,7 +14,6 @@ let projectConst = consts.projectConst;
 let projectConstList = consts.projectConstList;
 const gljUtil = require('../../../public/gljUtil');
 // const pm_facade = require('../../pm/facade/pm_facade');
-let pm_facade = require('../../pm/facade/pm_facade');
 
 
 const GLJ_TYPE = {
@@ -126,7 +125,7 @@ class Rpt_Data_Extractor {
     };
 
     //-- 根据报表模板映射指标(非离散指标)的定义,罗列出所有需要用到的data对象key,作为数据请求的过滤依据
-    getDataRequestFilter() {
+    getDataRequestFilter(summaryRst) {
         let rst = [];
         let tpl = this.rptTpl;
         let pri_func_chk_filter = function (field) {
@@ -159,6 +158,17 @@ class Rpt_Data_Extractor {
                     }
                 }
             }
+            if (summaryRst instanceof Array) {
+                for (let key of consts.summaryConstList) {
+                    if (summaryRst.indexOf(key) < 0) {
+                        if (field[JV.PROP_FIELD_EXP_MAP]) {
+                            if (field[JV.PROP_FIELD_EXP_MAP].indexOf('.' + key + '.') >= 0) {
+                                summaryRst.push(key);
+                            }
+                        }
+                    }
+                }
+            }
         };
         let pri_setup_filter = function (FIELD_LIST_KEY) {
             if (tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
@@ -214,19 +224,38 @@ class Rpt_Data_Extractor {
         let tpl = this.rptTpl;
         this.COMMON.initialize(tpl, rawDataObj);
         $PROJECT.COMMON = this.COMMON;
-        $PROJECT.MAIN["myOwnRawDataObj"] = rawDataObj.prj._doc;
-        $PROJECT.MAIN.getProperty = ext_mainGetPropety;
-        $PROJECT.MAIN.getFee = ext_mainGetFee;
-        for (let item of rawDataObj.prjData) {
-            setupFunc($PROJECT.DETAIL, item.moduleName, item);
-        }
-        let projectGLJDatas = getModuleDataByKey(rawDataObj.prjData, "projectGLJ");
-        let rationGLJDatas = getModuleDataByKey(rawDataObj.prjData, "ration_glj");
-        let rationDatas = getModuleDataByKey(rawDataObj.prjData, "ration");
-        let billsDatas = getModuleDataByKey(rawDataObj.prjData, "bills");
-        let decimal = rawDataObj.prj.property.decimal.glj.quantity;
-        if (projectGLJDatas && rationGLJDatas && rationDatas && billsDatas) {
-            gljUtil.calcProjectGLJQuantity(projectGLJDatas.data, rationGLJDatas.data, rationDatas.data, billsDatas.data, decimal);
+        if (rawDataObj.hasOwnProperty(`prj`)) {
+            setupMainFunc($PROJECT, `MAIN`, rawDataObj.prj._doc);
+            // $PROJECT.MAIN["myOwnRawDataObj"] = rawDataObj.prj._doc;
+            // $PROJECT.MAIN.getProperty = ext_mainGetPropety;
+            // $PROJECT.MAIN.getFee = ext_mainGetFee;
+            for (let item of rawDataObj.prjData) {
+                setupFunc($PROJECT.DETAIL, item.moduleName, item);
+            }
+            let projectGLJDatas = getModuleDataByKey(rawDataObj.prjData, "projectGLJ");
+            let rationGLJDatas = getModuleDataByKey(rawDataObj.prjData, "ration_glj");
+            let rationDatas = getModuleDataByKey(rawDataObj.prjData, "ration");
+            let billsDatas = getModuleDataByKey(rawDataObj.prjData, "bills");
+            let decimal = rawDataObj.prj.property.decimal.glj.quantity;
+            if (projectGLJDatas && rationGLJDatas && rationDatas && billsDatas) {
+                gljUtil.calcProjectGLJQuantity(projectGLJDatas.data, rationGLJDatas.data, rationDatas.data, billsDatas.data, decimal);
+            }
+        }
+        //还有汇总的...
+        if (rawDataObj.hasOwnProperty(`Construct`) || rawDataObj.hasOwnProperty(`ConstructDetail`) || rawDataObj.hasOwnProperty(`Segment`) || rawDataObj.hasOwnProperty(`SegmentDetail`)) {
+            $PROJECT.SUMMARY = {};
+            if (rawDataObj.Construct) {
+                setupMainFunc($PROJECT.SUMMARY, `Construct`, rawDataObj.Construct);
+            }
+            if (rawDataObj.ConstructDetail) {
+                setupFunc($PROJECT.SUMMARY, `ConstructDetail`, {"data": rawDataObj.ConstructDetail});
+            }
+            if (rawDataObj.Segment) {
+                setupMainFunc($PROJECT.SUMMARY, `Segment`, rawDataObj.Construct);
+            }
+            if (rawDataObj.SegmentDetail) {
+                setupFunc($PROJECT.SUMMARY, `SegmentDetail`, {"data": rawDataObj.SegmentDetail});
+            }
         }
 
         if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
@@ -961,6 +990,13 @@ function sortData(sourceData, sortCfg, prjData) {
     return rst;
 }
 
+function setupMainFunc(obj, prop, ownRawObj) {
+    obj[prop] = {};
+    obj[prop]["myOwnRawDataObj"] = ownRawObj;
+    obj[prop].getProperty = ext_mainGetPropety;
+    obj[prop].getFee = ext_mainGetFee;
+}
+
 function setupFunc(obj, prop, ownRawObj) {
     obj[prop] = {};
     obj[prop]["myOwnRawDataObj"] = ownRawObj;

+ 148 - 0
test/unit/reports/test_tpl_05.js

@@ -0,0 +1,148 @@
+/**
+ * Created by Tony on 2018/8/15.
+ */
+
+let test = require('tape');
+import JpcEx from "../../../modules/reports/rpt_component/jpc_ex";
+import JV from "../../../modules/reports/rpt_component/jpc_value_define";
+let config = require("../../../config/config.js");
+config.setupDb(process.env.NODE_ENV);
+let mongoose = require("mongoose");
+let fileUtils = require("../../../modules/common/fileUtils");
+let path = require('path');
+let dbm = require("../../../config/db/db_manager");
+let rpt_cfg = require('./rpt_cfg');
+let consts = require('../../../modules/main/models/project_consts');
+dbm.connect(process.env.NODE_ENV);
+
+//统一引用models
+fileUtils.getGlobbedFiles('../../../modules/all_models/*.js').forEach(function(modelPath) {
+    require(path.resolve(modelPath));
+});
+
+let cfgCacheUtil = require("../../../config/cacheCfg");
+cfgCacheUtil.setupDftCache();
+
+let fsUtil = require("../../../public/fsUtil");
+
+let demoPrjId = - 1;
+let demoRptId = 384, pagesize = "A4";
+
+// demoPrjId = 720; //QA: DW3
+demoPrjId = 3417; //QA:
+//*/
+let userId_Leng = "59cdf14a0034a1000ba52b97"; //小冷User Id 换成_id了
+let userId_Dft = userId_Leng;
+/*/
+ let userId_Dft = "595328da1934dc327cad08eb";
+ //*/
+
+let rptTplFacade = require("../../../modules/reports/facade/rpt_template_facade");
+let rptTplDataFacade = require("../../../modules/reports/facade/rpt_tpl_data_facade");
+let pm_facade = require('../../../modules/pm/facade/pm_facade');
+
+import rptDataExtractor from "../../../modules/reports/util/rpt_construct_data_util";
+
+let fs = require('fs');
+//设置Date Format函数
+fs.readFile(__dirname.slice(0, __dirname.length - 18) + '/public/web/date_util.js', 'utf8', 'r', function (err, data) {
+    eval(data);
+});
+
+//*
+test('测试 - 模板05汇总表: ', function (t) {
+    rptTplFacade.getRptTemplate(demoRptId).then(function(rptTpl) {
+        let rptDataUtil = new rptDataExtractor();
+        rptDataUtil.initialize(rptTpl._doc);
+        let summaryRst = [];
+        let filter = rptDataUtil.getDataRequestFilter(summaryRst);
+        console.log(filter);
+        console.log(summaryRst);
+        let promiseArr = [null, null];
+        if (summaryRst.length > 0) {
+            if (summaryRst.indexOf(`Construct`) >= 0 || summaryRst.indexOf(`ConstructDetail`) >= 0) {
+                promiseArr[0] = pm_facade.getSummaryInfoByTender(demoPrjId, pm_facade.projectType.project);
+            }
+            if (summaryRst.indexOf(`Segment`) >= 0 || summaryRst.indexOf(`SegmentDetail`) >= 0) {
+                promiseArr[1] = pm_facade.getSummaryInfoByTender(demoPrjId, pm_facade.projectType.engineering);
+            }
+        }
+        //*/
+        rptTplDataFacade.prepareProjectData(userId_Dft, demoPrjId, filter, function (err, msg, rawDataObj) {
+            if (!err) {
+                //取汇总数据流程
+                if (promiseArr[0] !== null) {
+                    promiseArr[0].then(function (rst) {
+                        rawDataObj.Construct = rst.parent;
+                        rawDataObj.ConstructDetail = rst.subList;
+                        if (promiseArr[1] !== null) {
+                            promiseArr[1].then(function (rst) {
+                                rawDataObj.Segment = rst.parent;
+                                rawDataObj.SegmentDetail = rst.subList;
+                                buildPageData(t, rawDataObj, rptDataUtil, rptTpl);
+                            });
+                        } else {
+                            buildPageData(t, rawDataObj, rptDataUtil, rptTpl);
+                        }
+                    });
+                } else  if (promiseArr[1] !== null) {
+                    promiseArr[1].then(function (rst) {
+                        rawDataObj.Segment = rst.parent;
+                        rawDataObj.SegmentDetail = rst.subList;
+                        buildPageData(t, rawDataObj, rptDataUtil, rptTpl);
+                    });
+                } else {
+                    buildPageData(t, rawDataObj, rptDataUtil, rptTpl);
+                }
+            } else {
+                console.log(msg);
+                t.pass('pass with error!');
+                t.end();
+            }
+        });
+        //*/
+        //正常应该根据报表模板定义的数据类型来请求数据
+    });
+});
+//*/
+
+function buildPageData(t, rawDataObj, rptDataUtil, rptTpl) {
+    try {
+        // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptTplRawDataObject.jsp");
+        let tplData = rptDataUtil.assembleData(rawDataObj);
+        //it's time to build the report!!!
+        let printCom = JpcEx.createNew();
+        rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pagesize;
+        let defProperties = rpt_cfg;
+        let dftOption = JV.PAGING_OPTION_NORMAL;
+        printCom.initialize(rptTpl);
+        // fsUtil.writeObjToFile(tplData, "D:/GitHome/ConstructionCost/tmp/rptAssembledData.jsp");
+        // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptRawDataAfterAssembled.jsp");
+        printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
+        let maxPages = printCom.totalPages;
+        let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
+        if (pageRst) {
+            fsUtil.writeObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/rptPageResult_05.jsp");
+        } else {
+            console.log("oh! no pages were created!");
+        }
+    } catch (ex) {
+        console.log(ex);
+        t.pass('pass with exception!');
+        t.end();
+    }
+
+    t.pass('pass succeeded!');
+    t.end();
+}
+
+test('close the connection', function (t) {
+    setTimeout(function () {
+        mongoose.disconnect();
+        t.pass('closing db connection');
+        t.end();
+    }, 3000);
+    // mongoose.disconnect();
+    // t.pass('closing db connection');
+    // t.end();
+});