Browse Source

Merge branch 'master' of http://smartcost.f3322.net:3000/SmartCost/ConstructionOperation

TonyKang 7 years ago
parent
commit
6d25d317c8

+ 7 - 4
modules/ration_repository/controllers/ration_repository_controller.js

@@ -156,7 +156,9 @@ class RationRepositoryController extends baseController {
             form.parse(request, async function(err, fields, files) {
                 const rationRepId = fields.rationRepId !== undefined && fields.rationRepId.length > 0 ?
                     fields.rationRepId[0] : 0;
-                if (rationRepId <= 0) {
+                const type = fields.type !== undefined && fields.type.length > 0 ?
+                    fields.type[0] : '';
+                if (rationRepId <= 0 || type === '') {
                     throw '参数错误';
                 }
                 const file = files.file !== undefined ? files.file[0] : null;
@@ -175,11 +177,12 @@ class RationRepositoryController extends baseController {
                 if (sheet[0] === undefined || sheet[0].data === undefined) {
                     throw 'excel没有对应数据';
                 }
-
-                const result = await rationItem.batchAddFromExcel(rationRepId, sheet[0].data);
+                const result = type === 'source_file' ?
+                    await rationItem.batchAddFromExcel(rationRepId, sheet[0].data) :
+                    await rationItem.batchUpdateSectionIdFromExcel(sheet[0].data);
                 // 删除文件
                 if (result) {
-                    fs.unlink(uploadFullName, null);
+                    fs.unlink(uploadFullName);
                 }
                 response.json(responseData);
             });

+ 50 - 5
modules/ration_repository/models/ration_item.js

@@ -9,6 +9,7 @@ let gljDao = require('./glj_repository');
 let rationRepositoryDao = require('./repository_map');
 const scMathUtil = require('../../../public/scMathUtil').getUtil();
 import {rationItemModel} from './schemas';
+import STDGLJListModel from '../../std_glj_lib/models/gljModel';
 
 var rationItemDAO = function(){};
 
@@ -494,6 +495,19 @@ rationItemDAO.prototype.batchAddFromExcel = async function(rationRepId, data) {
     if (data.length <= 0) {
         return false;
     }
+    // 获取定额库相关数据
+    const rationRepository = await rationRepositoryDao.getRepositoryById(rationRepId);
+    if (rationRepository.length !== 1 || rationRepository[0].gljLib === undefined) {
+        return false;
+    }
+    // 获取标准工料机库数据
+    const stdBillsLibListsModel = new STDGLJListModel();
+    const stdGLJData = await stdBillsLibListsModel.getGljItemsByRep(rationRepository[0].gljLib);
+    // 整理标准工料机库数据
+    let stdGLJList = {};
+    for (const tmp of stdGLJData) {
+        stdGLJList[tmp.code.toString()] = tmp.ID;
+    }
     let lastData = {};
     const rationData = [];
     // 编码列表,用于查找库中是否有对应数据
@@ -505,8 +519,12 @@ rationItemDAO.prototype.batchAddFromExcel = async function(rationRepId, data) {
         }
         // 如果第一个字段为null则是工料机数据,放入上一个数据的工料机字段
         if (tmp[0] === undefined && Object.keys(lastData).length > 0) {
+            // 如果不存在对应的工料机库数据则跳过
+            if (stdGLJList[tmp[1]] === undefined) {
+                continue;
+            }
             const tmpRationGlj = {
-                gljId: 1,
+                gljId: stdGLJList[tmp[1]],
                 consumeAmt: tmp[4],
                 proportion: 0,
             };
@@ -528,6 +546,7 @@ rationItemDAO.prototype.batchAddFromExcel = async function(rationRepId, data) {
             unit: tmp[3],
             caption: tmp[2],
             rationRepId: rationRepId,
+            sectionId: 0,
             rationGljList: []
         };
         // 防止重复加入
@@ -556,7 +575,6 @@ rationItemDAO.prototype.batchAddFromExcel = async function(rationRepId, data) {
     if (insertData.length <= 0) {
         return true;
     }
-
     // 组织id
     const counterInfo = await counter.counterDAO.getIDAfterCount(counter.moduleName.rations, insertData.length);
     let maxId = counterInfo.value.sequence_value;
@@ -583,18 +601,45 @@ rationItemDAO.prototype.exportExcelData = async function(rationRepId) {
         rationRepId: rationRepId
     };
     // @todo 限制导出的数量以防内存溢出
-    const rationDataList = await this.getRationItemByCondition(condition, ['name', 'code']);
+    const rationDataList = await this.getRationItemByCondition(condition, ['name', 'code', 'ID']);
     // 整理数据
     let rationData = [];
     for (const tmp of rationDataList) {
-        const ration = [null, tmp.code, tmp.name];
+        const ration = [null, tmp.ID, tmp.code, tmp.name];
         rationData.push(ration);
     }
-    const excelData = [['树ID', '定额名', '定额编码']];
+    const excelData = [['树ID', '定额ID', '定额编码', '定额名']];
     excelData.push.apply(excelData, rationData);
 
     return excelData;
 };
 
+/**
+ * 批量更改章节id
+ *
+ * @param {Object} data
+ * @return {bool}
+ */
+rationItemDAO.prototype.batchUpdateSectionIdFromExcel = async function(data) {
+    if (data.length <= 0) {
+        return false;
+    }
+    // 批量执行update
+    const bulk = rationItemModel.collection.initializeOrderedBulkOp();
+    for (const tmp of data) {
+        let rationId = parseInt(tmp[1]);
+        rationId = isNaN(rationId) || rationId <= 0 ? 0 : rationId;
+        let sectionId = parseInt(tmp[0]);
+        sectionId = isNaN(sectionId) || sectionId <= 0 ? 0 : sectionId;
+        if (sectionId <= 0 || rationId <= 0) {
+            continue;
+        }
+        bulk.find({"ID": rationId}).update({$set: { sectionId: sectionId }});
+    }
+
+    const result = await bulk.execute();
+    return result.isOk();
+};
+
 module.exports = new rationItemDAO();
 

+ 1 - 1
modules/ration_repository/models/repository_map.js

@@ -108,7 +108,7 @@ rationRepositoryDao.prototype.getLibIDByName = function(dispName, callback){
     });
 };
 
-rationRepositoryDao.prototype.getRepositoryById = function(repId,callback){
+rationRepositoryDao.prototype.getRepositoryById = function(repId,callback = null){
     if (callback) {
         rationRepository.find({"ID": repId}, function(err,data){
             if (err) {

+ 12 - 8
modules/std_glj_lib/models/gljModel.js

@@ -67,15 +67,19 @@ class GljDao  extends OprDao{
         }
     }
 
-    getGljItemsByRep(repositoryId,callback){
+    getGljItemsByRep(repositoryId,callback = null){
         let me = this;
-        gljModel.find({"repositoryId": repositoryId},function(err,data){
-            if(err) callback(true, "")
-            else {
-                me.sortToNumber(data);
-                callback(false,data);
-            }
-        })
+        if (callback === null) {
+            return gljModel.find({"repositoryId": repositoryId});
+        } else {
+            gljModel.find({"repositoryId": repositoryId},function(err,data){
+                if(err) callback(true, "")
+                else {
+                    me.sortToNumber(data);
+                    callback(false,data);
+                }
+            })
+        }
     }
 
     getGljItemByType (repositoryId, type, callback){

+ 17 - 4
web/maintain/ration_repository/js/main.js

@@ -88,13 +88,25 @@ $(function () {
         rationRepId = id;
         $("#import").modal("show");
     });
+    // 导入内部数据
+    $("#showArea").on("click", ".import-data", function () {
+        let id = $(this).data("id");
+        id = parseInt(id);
+        if (isNaN(id) || id <= 0) {
+            return false;
+        }
+        rationRepId = id;
+        $("#import2").modal("show");
+    });
 
     // 导入原始数据确认
-    $("#source-import").click(function() {
+    $("#source-import,#data-import").click(function() {
         const self = $(this);
+        const type = self.is("#source-import") ? 'source_file' : 'import_data';
+        const dialog = type === 'source_file' ? $("#import") : $("#import2");
         try {
             let formData = new FormData();
-            let file = $("input[name='source_file']")[0];
+            let file = $("input[name='"+ type +"']")[0];
             if (file.files.length <= 0) {
                 throw '请选择文件!';
             }
@@ -104,6 +116,7 @@ $(function () {
                 return false;
             }
             formData.append('rationRepId', rationRepId);
+            formData.append('type', type);
             $.ajax({
                 url: '/rationRepository/api/upload',
                 type: 'POST',
@@ -120,7 +133,7 @@ $(function () {
                     self.text('确定导入');
                     if (response.err === 0) {
                         // 成功则关闭窗体
-                        $("#import").modal("hide");
+                        dialog.modal("hide");
                     } else {
                         const message = response.msg !== undefined ? response.msg : '上传失败!';
                         alert(message);
@@ -176,7 +189,7 @@ function getAllRationLib(callback){
                         "<i class='fa fa-remove'></i></a></td>" +
                         "<td><a class='btn btn-secondary btn-sm import-source' 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-success btn-sm export' href='javacript:void(0);' data-toggle='modal' data-id='"+ id +"' data-target='#emport' title='导出内部数据'><i class='fa fa-sign-out fa-rotate-270'></i>导出</a> " +
-                        "<a class='btn btn-secondary btn-sm' href='javacript:void(0);' data-toggle='modal' data-target='#import2' title='导入内部数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></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>" +
                         "</tr>");
                     var newHref = "/rationRepository/ration?repository="+id;
                     $("#tempId td:first a").attr("href", newHref);

+ 2 - 4
web/maintain/ration_repository/main.html

@@ -177,7 +177,6 @@
                     </div>
                     <div class="modal-footer">
                         <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
-                        <input type="hidden" name="rationRepId" value="0"/>
                         <button type="button" class="btn btn-primary" id="source-import">确定导入</button>
                     </div>
                 </div>
@@ -202,14 +201,13 @@
                     <form>
                         <div class="form-group">
                             <label>请选择Excel格式文件</label>
-                            <input class="form-control-file" type="file">
+                            <input class="form-control-file" type="file" name="import_data"/>
                         </div>
                     </form>
                 </div>
                 <div class="modal-footer">
                     <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
-                    <input type="hidden" name="rationRepId" value="0"/>
-                    <a href="" class="btn btn-primary">确定导入</a>
+                    <button type="button" class="btn btn-primary" id="data-import">确定导入</button>
                 </div>
             </div>
         </div>