浏览代码

人材机组成物导入excel表格功能

zhongzewei 5 年之前
父节点
当前提交
0618b34930

+ 54 - 1
modules/std_glj_lib/controllers/gljController.js

@@ -226,7 +226,60 @@ class GljController extends BaseController{
                 }
                 //更新人材机价格
                 await gljDao.batchUpdateGljPrice(gljLibId, sheet[0].data);
-                console.log('endeeee');
+                // 删除文件
+                if(uploadFullName && fs.existsSync(uploadFullName)){
+                    fs.unlink(uploadFullName);
+                }
+                response.json(responseData);
+            }
+            catch (error){
+                console.log(error);
+                if(uploadFullName && fs.existsSync(uploadFullName)){
+                    fs.unlink(uploadFullName);
+                }
+                responseData.err = 1;
+                responseData.msg = error;
+                response.json(responseData);
+            }
+        });
+    }
+
+    async importComponents(request, response) {
+        let responseData = {
+            err: 0,
+            msg: ''
+        };
+        const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
+        const uploadOption = {
+            uploadDir: './public'
+        };
+        const form = new multiparty.Form(uploadOption);
+        let uploadFullName;
+        form.parse(request, async function(err, fields, files) {
+            try{
+                const gljLibId = fields.gljLibId !== undefined && fields.gljLibId.length > 0 ?
+                    fields.gljLibId[0] : 0;
+                if (gljLibId <= 0) {
+                    throw '参数错误';
+                }
+                const file = files.file !== undefined ? files.file[0] : null;
+                if (err || file === null) {
+                    throw '上传失败';
+                }
+                // 判断类型
+                if (file.headers['content-type'] === undefined || allowHeader.indexOf(file.headers['content-type']) < 0) {
+                    throw '不支持该类型';
+                }
+                // 重命名文件名
+                uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
+                fs.renameSync(file.path, uploadFullName);
+
+                const sheet = excel.parse(uploadFullName);
+                if (sheet[0] === undefined || sheet[0].data === undefined || sheet[0].data.length <= 0) {
+                    throw 'excel没有对应数据';
+                }
+                // 更新组成物
+                await gljDao.importComponents(gljLibId, sheet[0].data);
                 // 删除文件
                 if(uploadFullName && fs.existsSync(uploadFullName)){
                     fs.unlink(uploadFullName);

+ 58 - 0
modules/std_glj_lib/models/gljModel.js

@@ -637,6 +637,64 @@ class GljDao  extends OprDao{
         }
 
     }
+
+    async importComponents(gljLibId, sheetData) {
+        const gljLib = await gljMapModel.findOne({ID: gljLibId});
+        if (!gljLib) {
+            throw '不存在此人材机库';
+        }
+        const compilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(gljLib.compilationId)});
+        if (!compilation) {
+            throw '不存在此费用定额';
+        }
+        // 将所有人材机进行编码映射
+        const allGLJs = await gljModel.find({repositoryId: gljLibId}, {ID: true, code: true}).lean();
+        const codeMapping = {};
+        allGLJs.forEach(glj => codeMapping[glj.code] = glj);
+        // excel表格列号与字段的映射
+        const colMapping = {
+            // 材料编码
+            code: 0,
+            // 组成物编码
+            componentCode: 1,
+            // 组成物消耗量
+            consumeAmt: 2
+        };
+        // 跳过列头
+        for (let row = 1; row < sheetData.length; row++) {
+            const rowData = sheetData[row];
+            const code = rowData[colMapping.code];
+            const componentCode = rowData[colMapping.componentCode];
+            const consumeAmt = +rowData[colMapping.consumeAmt];
+            const glj = codeMapping[code];
+            const component = codeMapping[componentCode];
+            if (!glj || !component) {
+                continue;
+            }
+            if (!glj.component) {
+                glj.component = [];
+            }
+            glj.component.push({
+                ID: component.ID,
+                consumeAmt: consumeAmt
+            });
+        }
+        // 更新数据
+        const tasks = [];
+        allGLJs.filter(glj => glj.component && glj.component.length).forEach(glj => {
+            tasks.push({
+                updateOne: {
+                    filter: {
+                        ID: glj.ID
+                    },
+                    update: {$set: {component: glj.component}}
+                }
+            });
+        });
+        if (tasks.length) {
+            await gljModel.bulkWrite(tasks);
+        }
+    }
 }
 
 export default GljDao;

+ 1 - 0
modules/std_glj_lib/routes/routes.js

@@ -43,6 +43,7 @@ module.exports = function (app) {
     router.post("/getGljItemsOccupied",gljController.auth, gljController.init, gljController.getGljItemsOccupied);
     router.post("/isUsed",gljController.auth, gljController.init, gljController.isUsed);//工料机是否被引用
     router.post('/importPrice', gljController.auth, gljController.init, gljController.importPrice);
+    router.post('/importComponents', gljController.auth, gljController.init, gljController.importComponents);
 
 
     app.use("/stdGljRepository/api", router);

+ 1 - 4
web/maintain/std_glj_lib/html/main.html

@@ -33,11 +33,8 @@
                 <div class="col-md-8">
                     <div class="warp-p2 mt-3">
                         <table class="table table-hover table-bordered">
-                            <thead><tr><th>人材机库名称</th><th>费用定额</th><th>定额库</th><th width="160">添加时间</th><th width="90">操作</th><th width="90">价格数据</th><th width="90">补充模板</th></tr></thead>
+                            <thead><tr><th>人材机库名称</th><th>费用定额</th><th>定额库</th><th width="160">添加时间</th><th width="90">操作</th><th width="90">价格数据</th><th width="100">组成物数据</th><th width="90">补充模板</th></tr></thead>
                             <tbody id="showArea">
-                            <!--    <tr><td><a href="gongliao.html">XX工料机库</a></td><td>重庆2018</td><td>XXX定额库(重庆2018)</td><td>2017-01-01 </td><td><a href="javacript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a> <a href="javacript:void(0);" data-toggle="modal" data-target="#del" class="text-danger" title="删除"><i class="fa fa-remove"></i></a></td></tr>
-                                <tr><td><a href="gongliao.html">XX工料机库</a></td><td>重庆2018</td><td></td><td>2017-01-01 </td><td><a href="javacript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a> <a href="javacript:void(0);" data-toggle="modal" data-target="#del" class="text-danger" title="删除"><i class="fa fa-remove"></i></a></td></tr>
-                                <tr><td><a href="gongliao.html">XX工料机库</a></td><td>重庆2018</td><td></td><td>2017-01-01 </td><td><a href="javacript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a> <a href="javacript:void(0);" data-toggle="modal" data-target="#del" class="text-danger" title="删除"><i class="fa fa-remove"></i></a></td></tr>-->
                             </tbody>
                         </table>
                     </div>

+ 1 - 1
web/maintain/std_glj_lib/js/glj.js

@@ -38,7 +38,7 @@ $(document).ready(function () {
     SlideResize.horizontalSlide(rightElesObj, {min: 200, max: `$('#dataRow').width() - $('#leftContent').width() - 200`}, function () {
         let resizeRate = SlideResize.resizeWidth * 100 / $('#midContent').width(),
             sheetRate = 100 - resizeRate;
-        $('#leftResize').css('width', `${resizeRate}%`);
+        $('#slideResizeLeft').css('width', `${resizeRate}%`);
         $('#GLJListSheet').css('width', `${sheetRate}%`);
         refreshALlWorkBook();
     });

+ 31 - 4
web/maintain/std_glj_lib/js/main.js

@@ -122,7 +122,13 @@ $(function () {
         });
     });
     let selLibId = -1;
+    const importType = {
+        price: 1,
+        component: 2
+    };
+    let importAction;
     $("#showArea").on("click", ".import-data", function () {
+        importAction = importType.price;
         let id = $(this).data("id");
         id = parseInt(id);
         if (isNaN(id) || id <= 0) {
@@ -131,10 +137,20 @@ $(function () {
         selLibId = id;
         $("#import").modal("show");
     });
-    //导入单价数据
-    $("#data-import").click(function() {
+    $("#showArea").on("click", ".import-components", function () {
+        importAction = importType.component;
+        let id = $(this).data("id");
+        id = parseInt(id);
+        if (isNaN(id) || id <= 0) {
+            return false;
+        }
+        selLibId = id;
+        $("#import").modal("show");
+    });
+    function importExcel(url) {
         $.bootstrapLoading.start();
         const self = $(this);
+        console.log(self);
         try {
             let formData = new FormData();
             let file = $("input[name='import_data']")[0];
@@ -148,7 +164,7 @@ $(function () {
             }
             formData.append('gljLibId', selLibId);
             $.ajax({
-                url: 'api/importPrice',
+                url: url,
                 type: 'POST',
                 data: formData,
                 cache: false,
@@ -186,6 +202,16 @@ $(function () {
             alert(error);
             $.bootstrapLoading.end();
         }
+    }
+    //导入数据
+    $("#data-import").click(function() {
+        // 导入单价
+        if (importAction === importType.price) {
+            importExcel.call(this, '/stdGljRepository/api/importPrice');
+        } else {
+            // 导入组成物
+            importExcel.call(this, '/stdGljRepository/api/importComponents');
+        }
     });
     //设置补充人材机库分类树模板
     $("#showArea").on("click", ".set-comple", function () {
@@ -260,6 +286,7 @@ function getAllGljLib(callback){
                         "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
                         "<i class='fa fa-remove'></i></a></td>" +
                         "<td><a class='btn btn-secondary btn-sm import-data' href='javacript:void(0);' data-id='"+ id +"' title='导入数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
+                        "<td><a class='btn btn-secondary btn-sm import-components' href='javacript:void(0);' data-id='"+ id +"' title='导入组成物'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
                         "<td><a class='btn btn-secondary btn-sm set-comple' href='javacript:void(0);' data-id='"+ id +"' title='将分类树设为补充模板数据'><i class='fa fa-sign-in fa-rotate-90'></i>设置</a></td>" +
                         "</tr>");
                 }
@@ -274,7 +301,6 @@ function getCompilationList(callback){
         url: 'api/getCompilationList',
         dataType: 'json',
         success: function (result) {
-            //addoptions
             for(let i = 0; i < result.data.length; i++){
                 let $option =  $("<option >"+ result.data[i].name +"</option>");
                 $option.val( result.data[i]._id);
@@ -310,6 +336,7 @@ function createGljLib(gljLibObj, dispNamesArr, usedCom){
                     "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
                     "<i class='fa fa-remove'></i></a></td>" +
                     "<td><a class='btn btn-secondary btn-sm import-data' href='javacript:void(0);' data-id='"+ id +"' title='导入数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
+                    "<td><a class='btn btn-secondary btn-sm import-components' href='javacript:void(0);' data-id='"+ id +"' title='导入组成物'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
                     "<td><a class='btn btn-secondary btn-sm set-comple' href='javacript:void(0);' data-id='"+ id +"' title='将分类树设为补充模板数据'><i class='fa fa-sign-in fa-rotate-90'></i>设置</a></td>" +
                     "</tr>");
             }