|  | @@ -9,6 +9,7 @@ let gljDao = require('./glj_repository');
 | 
	
		
			
				|  |  |  let rationRepositoryDao = require('./repository_map');
 | 
	
		
			
				|  |  |  const scMathUtil = require('../../../public/scMathUtil').getUtil();
 | 
	
		
			
				|  |  |  const rationItemModel = mongoose.model('std_ration_lib_ration_items');
 | 
	
		
			
				|  |  | +const _rationItemModelBackup = mongoose.model('std_ration_lib_ration_items_backup');
 | 
	
		
			
				|  |  |  const stdRationLibModel = mongoose.model('std_ration_lib_map');
 | 
	
		
			
				|  |  |  const stdRationSectionModel = mongoose.model('std_ration_lib_ration_chapter_trees');
 | 
	
		
			
				|  |  |  const compleRationModel = mongoose.model('complementary_ration_items');
 | 
	
	
		
			
				|  | @@ -21,14 +22,85 @@ import stdgljutil  from "../../../public/cache/std_glj_type_util";
 | 
	
		
			
				|  |  |  const stdGLJItemModel = mongoose.model('std_glj_lib_gljList');
 | 
	
		
			
				|  |  |  var rationItemDAO = function(){};
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +rationItemDAO.prototype.overwriteRationGLJList = async function (sourceLibID, targetLibID) {
 | 
	
		
			
				|  |  | +    const rations = await _rationItemModelBackup.find({ rationRepId: sourceLibID }, 'ID code rationGljList').lean();
 | 
	
		
			
				|  |  | +    const bulks = [];
 | 
	
		
			
				|  |  | +    rations.forEach(ration => {
 | 
	
		
			
				|  |  | +        if (ration.rationGljList && ration.rationGljList.length) {
 | 
	
		
			
				|  |  | +            ration.rationGljList.forEach(rGLJ => {
 | 
	
		
			
				|  |  | +                delete rGLJ.gljCode;
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            bulks.push({
 | 
	
		
			
				|  |  | +                updateOne: {
 | 
	
		
			
				|  |  | +                    filter: { rationRepId: targetLibID, code: ration.code },
 | 
	
		
			
				|  |  | +                    update: { $set: { rationGljList: ration.rationGljList } }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            })
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    if (bulks) {
 | 
	
		
			
				|  |  | +        await rationItemModel.bulkWrite(bulks);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +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.handleGLJCodeID = 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.code] = glj.ID);
 | 
	
		
			
				|  |  | +    const bulks = [];
 | 
	
		
			
				|  |  | +    rations.forEach(ration => {
 | 
	
		
			
				|  |  | +        if (ration.rationGljList && ration.rationGljList.length) {
 | 
	
		
			
				|  |  | +            ration.rationGljList.forEach(rGLJ => {
 | 
	
		
			
				|  |  | +                rGLJ.gljId = gljMap[rGLJ.gljCode];
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            bulks.push({
 | 
	
		
			
				|  |  | +                updateOne: {
 | 
	
		
			
				|  |  | +                    filter: { ID: ration.ID },
 | 
	
		
			
				|  |  | +                    update: { $set: { rationGljList: ration.rationGljList } }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            })
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    if (bulks.length) {
 | 
	
		
			
				|  |  | +        await _rationItemModelBackup.bulkWrite(bulks);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  // 处理部颁数据
 | 
	
		
			
				|  |  |  rationItemDAO.prototype.handleBBData = async function (rationLibID, gljLibID) {
 | 
	
		
			
				|  |  |      const rations = await rationItemModel.find({ rationRepId: rationLibID }, '-_id code ID rationGljList').lean();
 | 
	
		
			
				|  |  | -    const gljs = await stdGLJItemModel.find({ repositoryId: gljLibID, 'component.0': {$exists: true} }, '-_id ID component').lean();
 | 
	
		
			
				|  |  | +    const types = [202, 203]
 | 
	
		
			
				|  |  | +    const gljs = await stdGLJItemModel.find({ repositoryId: gljLibID, gljType: {$in: types}, 'component.0': {$exists: true} }, '-_id ID component').lean();
 | 
	
		
			
				|  |  |      const gljIDMap = {};
 | 
	
		
			
				|  |  |      gljs.forEach(glj => gljIDMap[glj.ID] = glj);
 | 
	
		
			
				|  |  |      const updateData = [];
 | 
	
		
			
				|  |  | -    const errorRange = 0.004;
 | 
	
		
			
				|  |  | +    const errorRange = 0.04;
 | 
	
		
			
				|  |  |      for (const ration of rations) {
 | 
	
		
			
				|  |  |          if (!ration.rationGljList) {
 | 
	
		
			
				|  |  |              continue;
 | 
	
	
		
			
				|  | @@ -52,16 +124,19 @@ rationItemDAO.prototype.handleBBData = async function (rationLibID, gljLibID) {
 | 
	
		
			
				|  |  |          let isChanged = false;
 | 
	
		
			
				|  |  |          for (let i = 0; i < ration.rationGljList.length; i++) {
 | 
	
		
			
				|  |  |              const rGLJ = ration.rationGljList[i];
 | 
	
		
			
				|  |  | +            if (rGLJ) {
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |              if (componentAmtMap[rGLJ.gljId]) {
 | 
	
		
			
				|  |  |                  isChanged = true;
 | 
	
		
			
				|  |  |                  const diff = scMathUtil.roundTo(rGLJ.consumeAmt - componentAmtMap[rGLJ.gljId], -3);
 | 
	
		
			
				|  |  | -                if (diff > errorRange || diff < -errorRange) {
 | 
	
		
			
				|  |  | +                if (diff < 0) {
 | 
	
		
			
				|  |  | +                    console.log(`ration.code`);
 | 
	
		
			
				|  |  | +                    console.log(ration.code);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                if (diff > errorRange) {
 | 
	
		
			
				|  |  |                      // 扣减
 | 
	
		
			
				|  |  |                      rGLJ.consumeAmt = diff;
 | 
	
		
			
				|  |  | -                    if (diff < 0) {
 | 
	
		
			
				|  |  | -                        console.log(`ration.code`);
 | 
	
		
			
				|  |  | -                        console.log(ration.code);
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  |                      newRationGljList.push(rGLJ);    
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              } else {
 | 
	
	
		
			
				|  | @@ -82,7 +157,7 @@ rationItemDAO.prototype.handleBBData = async function (rationLibID, gljLibID) {
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  };
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -/* rationItemDAO.prototype.copyLib = async function (sourceLibID, targetLibID) {
 | 
	
		
			
				|  |  | +rationItemDAO.prototype.copyLib = async function (sourceLibID, targetLibID) {
 | 
	
		
			
				|  |  |      // coe-list
 | 
	
		
			
				|  |  |      const coeIDMap = {};
 | 
	
		
			
				|  |  |      const newCoeData = [];
 | 
	
	
		
			
				|  | @@ -157,7 +232,7 @@ rationItemDAO.prototype.handleBBData = async function (rationLibID, gljLibID) {
 | 
	
		
			
				|  |  |      await rationItemModel.insertMany(newRationData);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  };
 | 
	
		
			
				|  |  | - */
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  // 由于导入excel时,excel数据存在负的工程量,所以导入后一些定额人材机的消耗量可能为负,需要处理
 | 
	
		
			
				|  |  |  rationItemDAO.prototype.handleMinusQuantity = async function() {
 | 
	
		
			
				|  |  |      const updateTask = [];
 |