| 
					
				 | 
			
			
				@@ -11,6 +11,7 @@ const scMathUtil = require('../../../public/scMathUtil').getUtil(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const rationItemModel = mongoose.model('std_ration_lib_ration_items'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const stdRationLibModel = mongoose.model('std_ration_lib_map'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const stdRationSectionModel = mongoose.model('std_ration_lib_ration_chapter_trees'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const stdCoeModel = mongoose.model('std_ration_lib_coe_list'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const compleRationModel = mongoose.model('complementary_ration_items'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import STDGLJListModel from '../../std_glj_lib/models/gljModel'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import InstallationDao from '../models/installation'; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -20,9 +21,119 @@ const stdGljDao = new GljDao(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import stdgljutil  from "../../../public/cache/std_glj_type_util"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 //add 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const stdGLJItemModel = mongoose.model('std_glj_lib_gljList'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// const _rationItemModelBackup = mongoose.model('std_ration_lib_ration_items_backup'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 var rationItemDAO = function(){}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+async function getIDMapping(counterName, data) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const counterInfo = await counter.counterDAO.getIDAfterCount(counterName, data.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const maxID = +counterInfo.sequence_value; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const IDMapping = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    data.forEach((item, index) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        IDMapping[item.ID] = maxID - (data.length - 1) + index; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return IDMapping; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 拷贝分类树 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+async function copyClassData(sourceLibID, targetLibID) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const sourceClassData = await stdRationSectionModel.find({ rationRepId: sourceLibID }, '-_id').lean(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const IDMapping = await getIDMapping(counter.moduleName.rationTree, sourceClassData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const insertData = sourceClassData.map(item => ({ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ...item, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        rationRepId: targetLibID, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ID: IDMapping[item.ID], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ParentID: IDMapping[item.ParentID] || -1, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        NextSiblingID: IDMapping[item.NextSiblingID] || -1, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    })); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (insertData.length) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        await stdRationSectionModel.insertMany(insertData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return IDMapping; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 拷贝子目换算 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+async function copyCoeData(sourceLibID, targetLibID) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const sourceCoeData = await stdCoeModel.find({ libID: sourceLibID }, '-_id').lean(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const IDMapping = await getIDMapping(counter.moduleName.coeList, sourceCoeData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const insertData = sourceCoeData.map(item => ({ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ...item, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        libID: targetLibID, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ID: IDMapping[item.ID], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    })); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (insertData.length) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        await stdCoeModel.insertMany(insertData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return IDMapping; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 拷贝定额库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+rationItemDAO.prototype.copyLib = async function(sourceLibID, targetLibID, sourceGLJLibID, targetGLJLibID) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const sourceRationData = await rationItemModel.find({ rationRepId: sourceLibID }, '-_id').lean(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const rationIDMapping = await getIDMapping(counter.moduleName.rations, sourceRationData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const classIDMapping = await copyClassData(sourceLibID, targetLibID); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const coeIDMapping = await copyCoeData(sourceLibID, targetLibID); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const sourceGLJData = await stdGLJItemModel.find({ repositoryId: sourceGLJLibID }, '-_id code ID').lean(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const sourceGLJCodeMapping = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    sourceGLJData.forEach(glj => sourceGLJCodeMapping[glj.code] = glj.ID); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const targetGLJData = await stdGLJItemModel.find({ repositoryId: targetGLJLibID }, '-_id code ID').lean(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // 旧ID-新ID映射 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const gljIDMapping = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    targetGLJData.forEach(glj => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        const orgID = sourceGLJCodeMapping[glj.code]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (orgID) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            gljIDMapping[orgID] = glj.ID; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    sourceRationData.forEach(ration => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ration.rationRepId = targetLibID; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ration.ID = rationIDMapping[ration.ID]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ration.sectionId = classIDMapping[ration.sectionId]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ration.rationCoeList.forEach(coe => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            coe.ID = coeIDMapping[coe.ID]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        const rationGLJList = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ration.rationGljList.forEach(rGLJ => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const newGLJID = gljIDMapping[rGLJ.gljId]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (newGLJID) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                rGLJ.gljId = newGLJID; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                rationGLJList.push(rGLJ); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ration.rationGljList = rationGLJList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (sourceRationData.length) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        await rationItemModel.insertMany(sourceRationData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+rationItemDAO.prototype.handleGLJCode = async function (rationLibID, gljLibID) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const rations = await _rationItemModelBackup.find({ rationRepId: rationLibID }, 'ID rationGljList').lean(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const gljs = await stdGLJItemModel.find({ repositoryId: gljLibID }, 'ID code').lean(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const gljMap = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    gljs.forEach(glj => gljMap[glj.ID] = glj.code); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const bulks = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    rations.forEach(ration => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (ration.rationGljList && ration.rationGljList.length) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ration.rationGljList.forEach(rGLJ => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                rGLJ.gljCode = gljMap[rGLJ.gljId]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            bulks.push({ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                updateOne: { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    filter: { ID: ration.ID }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    update: { $set: { rationGljList: ration.rationGljList } } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (bulks.length) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        await _rationItemModelBackup.bulkWrite(bulks); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 rationItemDAO.prototype.updateRationGLJByOrgID = async function (rationLibID, gljLibID) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     const gljList = await stdGLJItemModel.find({ repositoryId: gljLibID }, 'ID orgID').lean(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     const IDMap = {}; 
			 |