Browse Source

1. 报表预处理,计量单元排序
2. 报表预处理,拼接计量单元

MaiXinRong 5 years ago
parent
commit
1763d6831c
2 changed files with 156 additions and 0 deletions
  1. 118 0
      app/lib/rpt_data_analysis.js
  2. 38 0
      test/app/lib/rpt_data_analysis.test.js

+ 118 - 0
app/lib/rpt_data_analysis.js

@@ -13,6 +13,59 @@ const standard = require('../const/standard');
 const moment = require('moment');
 moment.locale('zh-cn');
 
+class PosIndex {
+    /**
+     * 构造函数
+     * @param {id|String, masterId|String, order|String} setting
+     */
+    constructor (setting) {
+        // 无索引
+        this.datas = [];
+        // 以key为索引
+        this.items = {};
+        // 以分类id为索引的有序
+        this.ledgerPos = {};
+        // pos设置
+        this.setting = setting;
+    }
+
+    /**
+     * 加载部位明细数据
+     * @param datas
+     */
+    loadDatas(datas) {
+        this.datas = datas;
+        this.items = {};
+        this.ledgerPos = {};
+        for (const data of this.datas) {
+            const key = data[this.setting.id];
+            this.items[key] = data;
+
+            const masterKey =  data[this.setting.masterId];
+            if (!this.ledgerPos[masterKey]) {
+                this.ledgerPos[masterKey] = [];
+            }
+            this.ledgerPos[masterKey].push(data);
+        }
+        for (const prop in this.ledgerPos) {
+            this.resortLedgerPos(this.ledgerPos[prop]);
+        }
+    }
+
+    getLedgerPos(mid) {
+        return this.ledgerPos[mid];
+    }
+
+    resortLedgerPos(ledgerPos) {
+        const self = this;
+        if (ledgerPos instanceof Array) {
+            ledgerPos.sort(function (a, b) {
+                return a[self.setting.order] - b[self.setting.order];
+            })
+        }
+    }
+}
+
 // 通用方法
 const rdaUtils = {
     orderCalc: function (ctx, data, fields) {
@@ -1069,6 +1122,69 @@ const gatherSelectConverse = {
         }
     }
 };
+const sortPos = {
+    name: '计量单元排序',
+    hint: '',
+    defaultSetting: {
+        bills: 'mem_stage_bills',
+        pos: 'mem_stage_pos',
+    },
+    fun: function (ctx, data, fieldsKey, options, csRela) {
+        if (!options || !options.bills || !options.pos) return;
+        const bills = data[options.bills];
+        const pos = data[options.pos];
+        if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
+        const billsIndex = {};
+        const findBillsIndex = function (billsId) {
+            if (!billsIndex[billsId]) {
+                billsIndex[billsId] = ctx.helper._.findIndex(bills, function (x) {
+                    return x.id === billsId;
+                });
+            }
+            return billsIndex[billsId];
+        };
+        pos.sort(function (x, y) {
+            const xIndex = findBillsIndex(x) * 1000 + x.porder;
+            const yIndex = findBillsIndex(y) * 1000 + y.porder;
+            return xIndex - yIndex;
+        });
+    }
+};
+const unionPos = {
+    name: '拼接计量单元',
+    hint: '',
+    defaultSetting: {
+        bills: 'mem_stage_bills',
+        pos: 'mem_stage_pos',
+    },
+    fun: function (ctx, data, fieldsKey, options, csRela) {
+        if (!options || !options.bills || !options.pos) return;
+        const bills = data[options.bills];
+        const pos = data[options.pos];
+        console.log(bills.length);
+        console.log(pos.length);
+        if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
+        const posIndex = new PosIndex({
+            id: 'id',
+            masterId: 'lid',
+            order: 'porder',
+        });
+        posIndex.loadDatas(pos);
+        const unionData = [];
+        for (const b of bills) {
+            unionData.push(b);
+            const posRange = posIndex.getLedgerPos(b.id);
+            b.posCount = posRange ? posRange.length : 0;
+            console.log(b.id + '-posCount: ' + b.posCount);
+            if (!posRange || posRange.length === 0) continue;
+
+            for (const p of posRange) {
+                unionData.push(p);
+            }
+        }
+        data[options.bills] = unionData;
+    }
+}
 
 const analysisObj = {
     changeSort,
@@ -1084,6 +1200,8 @@ const analysisObj = {
     auditSelect,
     datetimeFormat,
     gatherSelectConverse,
+    sortPos,
+    unionPos,
 };
 const analysisDefine = (function (obj) {
     const result = [];

+ 38 - 0
test/app/lib/rpt_data_analysis.test.js

@@ -11,6 +11,7 @@
 const { app, assert } = require('egg-mock/bootstrap');
 const mockData = {};
 const path = require('path');
+let savePath;
 
 const reportDataAnalysis = require('../../../app/lib/rpt_data_analysis');
 
@@ -18,6 +19,7 @@ describe('test/app/service/report_memory.test.js', () => {
     // 准备测试数据
     before(function* () {
         const ctx = app.mockContext();
+        savePath = path.join(ctx.app.baseDir,'report_temp');
         // 模拟登录session
         // const postData = {
         //     account: 'fuqingqing',
@@ -491,4 +493,40 @@ describe('test/app/service/report_memory.test.js', () => {
         // };
         // ctx.helper.saveBufferFile(JSON.stringify(x, "", "\t"), ctx.app.baseDir + '/mem_stage_pos.json');
     });
+    it('test sortPos/unionPos', function* () {
+        const ctx = app.mockContext(mockData);
+
+        // test12 - 第6期
+        const stage = yield ctx.service.stage.getDataByCondition({tid: 12, order: 6});
+        const params = {
+            tender_id: stage.tid,
+            stage_id: stage.id,
+        };
+        const filters = ['mem_stage_bills', 'tender_info', 'mem_stage_pos'];
+        const data = yield ctx.service.report.getReportData(params, filters, {
+            mem_stage_bills: [
+                'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
+                'code', 'b_code', 'name', 'unit', 'unit_price',
+                'deal_qty', 'deal_tp',
+                'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
+                'dgn_qty1', 'dgn_qty2',
+                'drawing_code', 'memo', 'node_type', 'is_tp',
+                'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil',
+                'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
+                'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp',
+                'final_tp', 'final_ratio',
+                'qc_bgl_code',
+                'chapter',
+            ],
+            mem_stage_pos: [],
+        });
+        // sortPos
+        // reportDataAnalysis.analysisObj.sortPos.fun(ctx, data, [], {bills: 'mem_stage_bills', pos: 'mem_stage_pos'});
+        // yield ctx.helper.saveBufferFile(JSON.stringify(data.mem_stage_bills, "", "\t"), path.join(savePath, 'mem_stage_bills.json'));
+        // yield ctx.helper.saveBufferFile(JSON.stringify(data.mem_stage_pos, "", "\t"), path.join(savePath, 'mem_stage_pos.json'));
+
+        // unionPos
+        reportDataAnalysis.analysisObj.unionPos.fun(ctx, data, [], {bills: 'mem_stage_bills', pos: 'mem_stage_pos'});
+        yield ctx.helper.saveBufferFile(JSON.stringify(data.mem_stage_bills, "", "\t"), path.join(savePath, 'mem_stage_bills.json'));
+    });
 });