Parcourir la source

修改定额库名称后,引用处应自动更新

vian il y a 5 ans
Parent
commit
b3c967d40b

+ 57 - 0
modules/ration_repository/models/ration_item.js

@@ -18,9 +18,66 @@ const installationDao = new InstallationDao();
 import GljDao from "../../std_glj_lib/models/gljModel";
 const stdGljDao = new GljDao();
 import stdgljutil  from "../../../public/cache/std_glj_type_util";
+//add
+const stdGLJItemModel = mongoose.model('std_glj_lib_gljList');
 
 var rationItemDAO = function(){};
 
+// 处理部颁数据
+rationItemDAO.prototype.handleBBData = async function (rationLibID, gljLibID) {
+    const rations = await rationItemModel.find({ rationRepId: rationLibID }, '-_id ID rationGljList').lean();
+    const gljs = await stdGLJItemModel.find({ repositoryId: gljLibID, 'component.0': {$exists: true} }, '-_id ID component').lean();
+    const gljIDMap = {};
+    gljs.forEach(glj => gljIDMap[glj.ID] = gljs);
+    const updateData = [];
+    const errorRange = 0.004;
+    for (const ration of rations) {
+        if (!ration.rationGljList) {
+            continue;
+        }
+        const componentAmtMap = {};
+        for (const rGLJ of ration.rationGljList) {
+            const stdGLJ = gljIDMap[rGLJ.gljId];
+            if (!stdGLJ) {
+                continue;
+            }
+            for (const c of stdGLJ.component) {
+                const amt = c.consumeAmt * rGLJ.consumeAmt;
+                if (componentAmtMap[c.ID]) {
+                    componentAmtMap[c.ID] += amt;
+                } else {
+                    componentAmtMap[c.ID] = amt;
+                }
+            }
+        }
+        const newRationGljList = [];
+        let isChanged = false;
+        for (const rGLJ of ration.rationGljList) {
+            if (componentAmtMap[rGLJ.gljId]) {
+                const diff = Math.abs(componentAmtMap[rGLJ.gljId] - rGLJ.consumeAmt);
+                if (diff <= errorRange) {
+                    isChanged = true;
+                } else {
+                    newRationGljList.push(rGLJ);    
+                }
+            } else {
+                newRationGljList.push(rGLJ);
+            }
+        }
+        if (isChanged) {
+            updateData.push({
+                updateOne: {
+                    filter: { ID: ration.ID },
+                    update: { rationGljList: newRationGljList }
+                }
+            });
+        }
+    }
+    if (updateData.length) {
+        await rationItemModel.bulkWrite(updateData);
+    }
+};
+
 // 由于导入excel时,excel数据存在负的工程量,所以导入后一些定额人材机的消耗量可能为负,需要处理
 rationItemDAO.prototype.handleMinusQuantity = async function() {
     const updateTask = [];

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

@@ -192,7 +192,7 @@ rationRepositoryDao.prototype.updateName = function(oprtor, renameObj, callback)
                     callback(err, '更新最近操作者失败!');
                 }
                 else{
-                    engLibModel.update({'ration_lib.id': renameObj.ID}, {$set: {'ration_lib.$.name': renameObj.newName}}, {multi: true}, function (err) {
+                    engLibModel.update({'ration_lib.id': {$in: [String(renameObj.ID), +renameObj.ID]}}, {$set: {'ration_lib.$.name': renameObj.newName}}, {multi: true}, function (err) {
                         if(err){
                             callback(err, '更新工程专业引用失败!');
                         }