Procházet zdrojové kódy

导入广联达清单

zhongzewei před 7 roky
rodič
revize
f2944575cf

+ 61 - 19
modules/main/controllers/bills_controller.js

@@ -23,6 +23,8 @@ const billType ={
     BILL:4,//清单
     BX:5//补项
 };
+//上传的09表、广联达表
+const uploadType = {lj: 'lj', gld: 'gld'};
 // 上传控件
 const multiparty = require("multiparty");
 const fs = require("fs");
@@ -209,7 +211,6 @@ module.exports = {
         const uploadOption = {
             uploadDir: './public'
         };
-        const uploadType = {lj: 'lj', gld: 'gld'};
         const form = new multiparty.Form(uploadOption);
         let uploadFullName;
         form.parse(req, async function(err, fields, files) {
@@ -244,8 +245,14 @@ module.exports = {
                 //获取表的列设置确定导入的格式是否合法(09、广联达)
                //console.log(sheet[0].data);
                 let colMapping = getColMapping(sheet[0].data);
+                console.log(fileType);
                 console.log(`colMapping`);
                 console.log(colMapping);
+                console.log(`sheet[0].data`);
+                console.log(sheet[0].data);
+                if(!isValidSheet(colMapping, fileType)){
+                    throw `excel数据格式错误`;
+                }
 
                 //导入的数据是否含有固定行(分部分项、施工技术措施项目、施工组织措施项目,通过文件名判断)、确定导入位置
                 let flag = getImportFlag(file.originalFilename);
@@ -305,8 +312,6 @@ module.exports = {
                     stdCharacters = await stdBillCharacterModel.find({billsLibId: billsLibId, deleted: false});
                 }
                 //将excel数据转换成清单树结构数据
-                console.log(`sheet[0].data`);
-                console.log(sheet[0].data);
                 let insertDatas = parseToBillData(getValidImportData(colMapping, sheet[0].data, fixedBill), colMapping, fixedBill, projectID, {stdBills: stdBills, stdJobs: stdJobs, stdCharacters: stdCharacters});
                 console.log(`insertDatas`);
                 console.log(insertDatas);
@@ -341,30 +346,58 @@ module.exports = {
 
 };
 
-//是否是有效的表头列格式
-function isValidSheet(sheetData, fileType){
-
+//是否是有效的表头列格式,只要含有各表需要的列就行,不严格控制多少列
+function isValidSheet(colMapping, fileType){
+    //09表:序号、项目编码、项目名称、项目特征、计量单位、工程量、金额
+    let isValid = true;
+    function hasField(field, all){
+        for(let i of all){
+            if(field === i){
+                return true;
+            }
+        }
+        return false;
+    }
+    let needFields;
+    if(fileType === uploadType.lj){
+        needFields = ['serialNo', 'code', 'name', 'money'];
+    }
+    else {
+        needFields = ['serialNo', 'code', 'name', 'itemCharacterText', 'unit', 'quantity', 'quantityDetail', 'feeDetail'];
+    }
+    let hasFieldCount = 0;
+    for(let attr in colMapping){
+        if(hasField(attr, needFields)){
+            hasFieldCount++;
+        }
+    }
+    return hasFieldCount === needFields.length;
+    //广联达表:序号、项目编码、项目名称、项目特征、计量单位、工程量、工程量明细、费用明细
 }
 
 //提取excel表头列对应数据
 function getColMapping(sheetData){
     //获取表头
-    let headRow = [];
-    for(let rData of sheetData) {
-        //寻找含有序号的行,认作表头行
-        for(let cData of rData){
-            if (cData && cData.toString().replace(/\s/g, '') === '序号') {
-                headRow = rData;
-                break;
+    function getHeadRow(sheetData){
+        for(let rData of sheetData) {
+            //寻找含有序号的行,认作表头行
+            for(let cData of rData){
+                if (cData && cData.toString().replace(/\s/g, '') === '序号') {
+                    headRow = rData;
+                    return rData;
+                }
             }
         }
+        return [];
     }
+    let headRow = getHeadRow(sheetData);
     //获取需要的表头列与列号对应关系
     let colMapping = {};
     for(let c = 0; c < headRow.length; c++){
         if(headRow[c]){
             headRow[c] = headRow[c].toString().replace(/\s/g, '');
             //重复的,只取第一个
+            console.log(headRow[c]);
             if(headRow[c] === '序号' && colMapping.serialNo === undefined){
                 colMapping.serialNo = c;
             }
@@ -380,9 +413,18 @@ function getColMapping(sheetData){
             else if((headRow[c] === '单位' || headRow[c] === '计量单位') && colMapping.unit === undefined){
                 colMapping.unit = c;
             }
-            else if((headRow[c] === '工程量' || headRow[c] === '项目工程量') && colMapping.code === undefined){
+            else if((headRow[c] === '工程量' || headRow[c] === '项目工程量') && colMapping.quantity === undefined){
                 colMapping.quantity = c;
             }
+            else if(headRow[c].includes('金额') && colMapping.money === undefined){
+                colMapping.money = c;
+            }
+            else if(headRow[c] === '工程量明细' && colMapping.quantityDetail === undefined){
+                colMapping.quantityDetail = c;
+            }
+            else if(headRow[c] === '费用明细' && colMapping.feeDetail === undefined){
+                colMapping.feeDetail = c;
+            }
         }
     }
     return colMapping;
@@ -460,6 +502,9 @@ function getImportFlag(sheetName){
     }
     return null;
 }
+function isDef(data){
+    return typeof data !== 'undefined' && data !== null && data !== '';
+}
 //excel数据转换成清单数据
 function parseToBillData(validData, colMapping, fixedBill, projectID, stdData){
     let rst = [];
@@ -467,9 +512,6 @@ function parseToBillData(validData, colMapping, fixedBill, projectID, stdData){
     let preRootID = -1,
         preLeafID = -1,
         preID = -1;
-    function isDef(data){
-        return typeof data !== 'undefined' && data !== null && data !== '';
-    }
     //去除转义字符
     function removeESC(data){
         return isDef(data) ? data.toString().replace(/[\r,\n,\s,\t]/g, '') : data;
@@ -589,8 +631,8 @@ function parseToBillData(validData, colMapping, fixedBill, projectID, stdData){
             //set bill data
             billIdx[newID] = {
                 ID: newID, ParentID: pID, NextSiblingID: -1,
-                code: rData[colMapping.code] ? rData[colMapping.code] : '',
-                name: rData[colMapping.name] ? rData[colMapping.name] : '',
+                code: rData[colMapping.code] ? removeESC(rData[colMapping.code]) : '',
+                name: rData[colMapping.name] ? removeESC(rData[colMapping.name]) : '',
                 itemCharacterText: rData[colMapping.itemCharacterText] ? rData[colMapping.itemCharacterText] : '',
                 itemCharacter: [],
                 jobContentText: '',

+ 1 - 1
web/common/html/header.html

@@ -15,7 +15,7 @@
             <a class="nav-link dropdown-toggle" href="javascript:void(0);" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa  fa-cloud-upload"></i> 导入</a>
             <div class="dropdown-menu">
                 <a id="uploadLj" class="dropdown-item" href="#import" data-toggle="modal" data-target="#import">导入09表Excel清单</a>
-              <!--  <a id="uploadGld" class="dropdown-item" href="#import" data-toggle="modal" data-target="#import">导入广联达算量Excel清单</a>-->
+                <a id="uploadGld" class="dropdown-item" href="#import" data-toggle="modal" data-target="#import">导入广联达算量Excel清单</a>
             </div>
         </li>
         <li class="nav-item dropdown">