Bläddra i källkod

导入Excel调整

MaiXinRong 5 år sedan
förälder
incheckning
9cec6e8a40
3 ändrade filer med 112 tillägg och 43 borttagningar
  1. 32 32
      app/lib/analysis_excel.js
  2. 21 2
      app/service/deal_bills.js
  3. 59 9
      test/app/lib/rpt_data_analysis.test.js

+ 32 - 32
app/lib/analysis_excel.js

@@ -533,41 +533,41 @@ class AnalysisGclExcelTree {
             this.cacheTree = new ImportGclBaseTree(this.ctx, parent, maxId, defaultData);
             this.errorData = [];
             this.loadEnd = false;
-
-            // for (const iRow in sheet.rows) {
-            //     const row = sheet.rows[iRow];
-            //     if (this.colsDef && !this.loadEnd) {
-            //         const result = this.loadRowData(row);
-            //         // 读取失败则写入错误数据 todo 返回前端告知用户?
-            //         if (!result) {
-            //             this.errorData.push({
-            //                 serialNo: iRow,
-            //                 data: row,
-            //             });
-            //         }
-            //     } else {
-            //         this.checkColHeader(row);
-            //     }
-            // }
-            // 固定列导入
-            this.colsDef = {
-                b_code: 0,
-                name: 1,
-                unit: 2,
-                quantity: 3,
-                unit_price: 4
-            };
-            for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
+            // 识别表头导入
+            for (const iRow in sheet.rows) {
                 const row = sheet.rows[iRow];
-                const result = this.loadRowData(row);
-                // 读取失败则写入错误数据 todo 返回前端告知用户?
-                if (!result) {
-                    this.errorData.push({
-                        serialNo: iRow,
-                        data: row,
-                    });
+                if (this.colsDef && !this.loadEnd) {
+                    const result = this.loadRowData(row);
+                    // 读取失败则写入错误数据 todo 返回前端告知用户?
+                    if (!result) {
+                        this.errorData.push({
+                            serialNo: iRow,
+                            data: row,
+                        });
+                    }
+                } else {
+                    this.checkColHeader(row);
                 }
             }
+            // 固定列导入
+            // this.colsDef = {
+            //     b_code: 0,
+            //     name: 1,
+            //     unit: 2,
+            //     quantity: 3,
+            //     unit_price: 4
+            // };
+            // for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
+            //     const row = sheet.rows[iRow];
+            //     const result = this.loadRowData(row);
+            //     // 读取失败则写入错误数据 todo 返回前端告知用户?
+            //     if (!result) {
+            //         this.errorData.push({
+            //             serialNo: iRow,
+            //             data: row,
+            //         });
+            //     }
+            // }
             return this.cacheTree;
         } catch(err) {
             this.ctx.helper.log(err);

+ 21 - 2
app/service/deal_bills.js

@@ -32,6 +32,7 @@ module.exports = app => {
             const transaction = await this.db.beginTransaction();
             try {
                 const bills = [];
+                // 识别表头导入
                 let iCode = -1, iName = -1, iUnit = -1, iUp = -1, iQty = -1, iTp = -1, bCheckCol = false;
                 for (let iRow = 0; iRow < sheet.data.length; iRow++) {
                     const row = sheet.data[iRow];
@@ -39,8 +40,8 @@ module.exports = app => {
                         for (let iCol = 0; iCol < row.length; iCol++) {
                             const value = row[iCol];
                             if (typeof value !== "string") { continue }
-                            if (value === '子目号' || value === '清单编号') iCode = iCol;
-                            if (value.indexOf('名称') >= 0) iName = iCol;
+                            if (['清单编号', '子目号', '子目编号', '编号', '清单编号'].indexOf(value) >= 0) iCode = iCol;
+                            if (['清单名称', '名称', '子目名称'].indexOf(value) >= 0) iName = iCol;
                             if (value.indexOf('单位') >= 0) iUnit = iCol;
                             if (value.indexOf('单价') >= 0) iUp = iCol;
                             if (value.indexOf('数量') >= 0) iQty = iCol;
@@ -73,6 +74,24 @@ module.exports = app => {
                 if (!bCheckCol) {
                     throw '导入的Excel表头定义有误,请下载示例检查';
                 }
+                // 固定列,从第一行开始导入
+                // let iCode = 0, iName = 1, iUnit = 2, iUp = 4, iQty = 3;
+                // for (let iRow = 1; iRow < sheet.data.length; iRow++) {
+                //     const data = {
+                //         deal_id: bills.length + 1,
+                //         tender_id: tenderId,
+                //         code: row[iCode],
+                //         name: row[iName],
+                //         unit: row[iUnit],
+                //         unit_price: row[iUp],
+                //         quantity: row[iQty],
+                //     };
+                //     if (!data.code || data.code === '' || !data.name || data.name === '') continue;
+                //     if ((data.unit_price && !this._.isNumber(data.unit_price)) || (data.quantity && !this._.isNumber(data.quantity))) {
+                //         throw '导入的Excel的数据类型有误,请检查第' + (iRow + 1) + '行';
+                //     }
+                //     bills.push(data);
+                // }
                 if (bills.length > 0) {
                     await transaction.delete(this.tableName, {tender_id: tenderId});
                     const billsResult = await transaction.insert(this.tableName, bills);

+ 59 - 9
test/app/lib/rpt_data_analysis.test.js

@@ -19,16 +19,16 @@ describe('test/app/service/report_memory.test.js', () => {
     before(function* () {
         const ctx = app.mockContext();
         // 模拟登录session
-        // const postData = {
-        //     account: 'fuqingqing',
-        //     project: 'P0505',
-        //     project_password: '123456',
-        // };
         const postData = {
-            account: '734406061@qq.com',
-            project: 'T201711273363',
-            project_password: 'mai654321',
+            account: 'fuqingqing',
+            project: 'P0505',
+            project_password: '123456',
         };
+        // const postData = {
+        //     account: '734406061@qq.com',
+        //     project: 'T201711273363',
+        //     project_password: 'mai654321',
+        // };
         ctx.session = {};
         const loginResult = yield ctx.service.projectAccount.accountLogin(postData, 2);
         assert(loginResult);
@@ -119,6 +119,7 @@ describe('test/app/service/report_memory.test.js', () => {
             tender_id: stage.tid,
             stage_id: stage.id,
         };
+        const params = {tender_id: 2072};
         const filters = ['mem_stage_bills', 'tender_info'];
         const data = yield ctx.service.report.getReportData(params, filters, {
             mem_stage_bills: [
@@ -136,11 +137,11 @@ describe('test/app/service/report_memory.test.js', () => {
                 'chapter',
             ]
         });
-        const bills = data.mem_stage_bills.find(function (x) {return x.b_code === '103-3-a'});
         reportDataAnalysis.analysisObj.gatherChapter.fun(ctx, data, [
             {field: 'b_code', table: 'mem_stage_bills'},
             {field: 'is_leaf', table: 'mem_stage_bills'},
         ]);
+        console.log(data.mem_stage_bills);
         const chapter100 = ctx.helper._.find(data.mem_stage_bills, {code: '100'});
         assert(chapter100.total_price === 1045756);
         const chapter200 = ctx.helper._.find(data.mem_stage_bills, {code: '200'});
@@ -149,6 +150,55 @@ describe('test/app/service/report_memory.test.js', () => {
         const chapter400 = ctx.helper._.find(data.mem_stage_bills, {code: '400'});
         assert(chapter400.total_price.toFixed(0) == 1231018);
     });
+    // it('test gatherChapter custom', function* () {
+    //     const ctx = app.mockContext(mockData);
+    //
+    //     const params = {tender_id: 2072};
+    //     const filters = ['mem_stage_bills', 'tender_info'];
+    //     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',
+    //             'qc_bgl_code',
+    //             'chapter',
+    //         ]
+    //     });
+    //     reportDataAnalysis.analysisObj.gatherChapter.fun(ctx, data, [
+    //         {field: 'b_code', table: 'mem_stage_bills'},
+    //         {field: 'is_leaf', table: 'mem_stage_bills'},
+    //     ], {
+    //         count: 7,
+    //         gclSum: {
+    //             name: '第100章至700章清单合计',
+    //             order: 1,
+    //         },
+    //         custom: [
+    //             {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2},
+    //             {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3},
+    //             {name: '计日工合计', node_type: '计日工', order: 4},
+    //             {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', node_type: '暂列金额', order: 5},
+    //             {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},
+    //         ],
+    //     });
+    //     const chapter100 = ctx.helper._.find(data.mem_stage_bills, {code: '100'});
+    //     assert(ctx.helper.checkZero(chapter100.total_price));
+    //     const chapter200 = ctx.helper._.find(data.mem_stage_bills, {code: '200'});
+    //     assert(chapter200.total_price === 33211);
+    //     const chapterGclSum = ctx.helper._.find(data.mem_stage_bills, {cType: 11});
+    //     assert(chapterGclSum.total_price == 33211);
+    //     const chapterJRG = ctx.helper._.find(data.mem_stage_bills, {name: '计日工合计'});
+    //     assert(chapterJRG.total_price === 1076);
+    //     const chapterSumWithoutZGJ = ctx.helper._.find(data.mem_stage_bills, {serialNo: 11});
+    //     assert(chapterSumWithoutZGJ.total_price === 33211);
+    //     const chapterZL = ctx.helper._.find(data.mem_stage_bills, {serialNo: 13});
+    //     assert(chapterZL.total_price === 500000);
+    //     const chapterSum = ctx.helper._.find(data.mem_stage_bills, {serialNo: 14});
+    //     assert(chapterSum.total_price === 534287);
+    // });
     it('test analysisDefine', function() {
         const define = reportDataAnalysis.analysisDefine;
         assert(define.length === 4);