zhangweicheng 5 år sedan
förälder
incheckning
7f7f80d9dd

+ 1 - 0
modules/all_models/mix_ratio.js

@@ -46,6 +46,7 @@ let modelSchema = {
     code: String,
     // 工料机类型
     type: Number,
+    from:String,
     model: Number// 机型
 };
 mongoose.model(collectionName, new Schema(modelSchema, {versionKey: false, collection: collectionName}));

+ 10 - 4
modules/glj/models/glj_list_model.js

@@ -662,6 +662,7 @@ class GLJListModel extends BaseModel {
                 code: tmp.code,
                 specs:tmp.specs?tmp.specs:"",
                 name:tmp.name,
+                from:tmp.from,
                 unit:tmp.unit?tmp.unit:''
             };
             mixRatioInsertData.push(mixRatioData);
@@ -821,7 +822,7 @@ class GLJListModel extends BaseModel {
         let gljInsertData = [];
         for(let mk in ratioMap){//找出缺少的工料机
             if(!matchGljs[mk]){
-                lessMix.push(ratioMap[mk]);
+                lessMix.push(ratioMap[mk]);     
                 lessIDList.push(ratioMap[mk].glj_id);
             }
         }
@@ -836,9 +837,14 @@ class GLJListModel extends BaseModel {
                 }
             }else {
                 for(let m of lessMix){
-                    m.from = 'cpt';
-                    let newProjctData =  this.getProjectGLJNewData(m,projectId);
-                    gljInsertData.push(newProjctData);
+                  let mt = m;
+                  if(m.from == "std"){
+                    mt = await gljListModel.model.findOne({ID:m.glj_id}).lean();
+                  }else{
+                    mt.from = 'cpt';
+                  }
+                  let newProjctData =  this.getProjectGLJNewData(mt,projectId);
+                  gljInsertData.push(newProjctData);
                 }
             }
 

+ 297 - 7
modules/main/facade/ration_facade.js

@@ -3,6 +3,8 @@
  */
 let mongoose = require('mongoose');
 import SearchDao from '../../complementary_ration_lib/models/searchModel';
+import GLJListModel from "../../glj/models/glj_list_model";
+import e from 'express';
 const scMathUtil = require('../../../public/scMathUtil').getUtil();
 let gljUtil = require('../../../public/gljUtil');
 let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
@@ -26,11 +28,15 @@ let originaltCalcModel = mongoose.model('original_calc');
 
 let coeMolde = mongoose.model('std_ration_lib_coe_list');
 let compleCoeModel = mongoose.model('complementary_ration_coe_list');
-
+let projectGLJModel = mongoose.model("glj_list");
+let mixRatioModel = mongoose.model("mix_ratio");
+let complementaryGljLibModel = mongoose.model('complementary_glj_lib');
+let counterModel =  mongoose.model('counter');
 
 let _= require('lodash');
 const projectDao = require('../../pm/models/project_model').project;
 let projectModel = mongoose.model('projects');
+let unitPriceModel = mongoose.model('unit_price');
 const fs = require('fs');
 
 module.exports = {
@@ -482,7 +488,8 @@ async function addRationGLJ(std,newRation,compilation,isMaterial,connect_key) {
     let rationGLJShowList = [];
     let projectGLJList = [];
     let gljKeyMap = {};
-    let maxRatioMap={};
+    let mixRatioMap={};
+    let gljCodes=[];
     let unitPriceFileId = 0;
     let property = await projectDao.getProjectProperty(newRation.projectID);
     if(property){
@@ -530,9 +537,10 @@ async function addRationGLJ(std,newRation,compilation,isMaterial,connect_key) {
             if(std_glj){
                 let tindex = getIndex(newGLJ);
                 ration_glj_facade.setPropertyFromStd(newGLJ,std_glj);
-                if(std_glj.component && std_glj.component.length > 0) maxRatioMap[tindex] = std_glj.component
-                gljKeyMap[tindex] = newGLJ;
-
+                if(std_glj.component && std_glj.component.length > 0) mixRatioMap[tindex] = std_glj.component
+                let tdata = ration_glj_facade.getGLJSearchInfo(newGLJ);
+                gljKeyMap[tindex] = tdata;
+                gljCodes.push(tdata.code);
                /*  let [info,projectGLJ] =  await ration_glj_facade.getInfoFromProjectGLJ(newGLJ,unitPriceFileId,ext);
                 newGLJ = ration_glj_facade.createNewRecord(info);
                 newRationGLJList.push(newGLJ);
@@ -543,7 +551,7 @@ async function addRationGLJ(std,newRation,compilation,isMaterial,connect_key) {
             //console.log("找到项目工料机时间-------------------------------"+(InfoFromProjectGLJ - std_gljTime));
         }
 
-        
+
 
     }
     let before = +new Date();
@@ -559,6 +567,288 @@ async function addRationGLJ(std,newRation,compilation,isMaterial,connect_key) {
     return [rationGLJShowList,projectGLJList];
 }
 
+async function getProjectGLJinfo(projectID,gljKeyMap,gljCodes,mixRatioMap,unitPriceFileId,ext){//批量插入或查找项目工料机信息
+  //先根据工料机编号在项目工料机中查找工料机是否存在
+  let projectGLJMap={};
+  let unitPriceMap = {};
+  let newProjectGLJList=[];//工料机ID要重新去取
+  let connectKeyList = [];
+  let existMixRatioMap={};
+
+  let gljListModel = new GLJListModel();
+  let projectGLJList = await projectGLJModel.find({'project_id':projectID,'code':{'$in':gljCodes}}).lean();
+  for(let pg of projectGLJList){
+     let pindex = getIndex(pg);  
+     projectGLJMap[pindex] = pg;
+  }
+  for(let key in gljKeyMap){
+    if(!projectGLJMap[key]){//如果项目工料机不存在,则添加
+      newProjectGLJList.push(gljKeyMap[key]);
+      projectGLJMap[key] = gljKeyMap[key];
+    }
+    //查看组成物
+    if(gljListModel.ownCompositionTypes.indexOf(data.type)!=-1){//有组成物的类型
+      connectKeyList.push(key);
+    }
+  }
+
+  
+//处理车般税相关 --- todo
+
+  
+
+
+
+
+  let unitPriceList = await unitPriceModel.find({unit_price_file_id: unitPriceFileId,'code':{'$in':gljCodes}}).lean();
+
+
+
+
+  gljListModel.ownCompositionTypes
+
+
+
+}
+
+
+async function getMixRatioInfo(projectID,projectGLJMap,mixRatioMap,connectKeyList,unitPriceFileId,ext){//取组成物信息,得到缺少的组成物情况
+  let missCodeList = [];
+  let existMixRatioMap ={};
+  let codeMap={};//用来去重
+  let newProjectGLJList=[];
+
+  if(connectKeyList.length > 0){//有组成物的话从数据库中取出组成物信息
+    let mixRatioList = await mixRatioModel.find({'unit_price_file_id': unitPriceFileId,'connect_key': {'$in':connectKeyList}}).lean();
+    for(let m of mixRatioList){
+        //组成物信息分组,查看哪些是已经存在的
+       existMixRatioMap[m.connect_key]?existMixRatioMap[m.connect_key].push(m):existMixRatioMap[m.connect_key]=[m];
+       //查看组成物对应的项目工料机是否存在,如果不存在,要插入项目工料机
+       let mkey = getIndex(m);
+       if(!projectGLJMap[mkey] && !codeMap[m.code]){//如果之前查出来的项目工料机中不包含组成物的信息,要加到missCode里面再查找一次项目工料机看是否存在
+          missCodeList.push(m.code);
+          codeMap[m.code] = true;
+       } 
+    }
+    let stdIDs = []; 
+    let comIDs = [];
+    let missMixRatioGroup = [];
+    for(let ck of connectKeyList){//查看项目中组成物信息是否已经存在,如果不存在,则用插定额时获取的组成物信息从数据库中获取
+       if(!existMixRatioMap[ck] && mixRatioMap[ck] && mixRatioMap[ck].length > 0){//组成物信息不存在
+         let pglj = projectGLJMap[ck];//取出父数据 
+         let from =  pglj.from === undefined|| pglj.from ===null || pglj.from === ""?'std' : pglj.from;
+         for(let c of mixRatioMap[ck]){
+            if(from == "std"){//标准的工料机只来自标准的
+                stdIDs.push(c.ID); 
+            }else{
+              c.isStd?stdIDs.push(c.ID):comIDs.push(c.ID);  
+            } 
+         } 
+         missMixRatioGroup.push({'connect_key':ck,'list':mixRatioMap[ck],'from':from});
+       }
+    }
+
+
+    let stdMixMap = {};
+    //整理需插入的组成物列表的数据
+    //来自标准工料机
+    if(stdIDs.length > 0){
+      stdIDs = _.uniq(stdIDs);//去重
+      let stdMixList =  await std_glj_lib_gljList_model.find({'ID':{'$in':stdIDs}}).lean();
+      for(let sm of stdMixList){
+        stdMixMap[sm.ID] = sm;
+        let skey = getIndex(sm,['code','name','specs','unit','gljType']);
+        if(!projectGLJMap[skey] && !codeMap[sm.code]){
+          missCodeList.push(sm.code);
+          codeMap[sm.code] = true;
+        }
+      }
+    }
+    //来自组成物工料机
+    let comMixMap = {};
+    if(comIDs.length > 0){
+      comIDs = _.uniq(comIDs);//去重
+      let comMixList = await complementaryGljLibModel.find({'ID':{'$in':comIDs}}).lean();
+      for(let cm of comMixList){
+        comMixMap[cm.ID] = cm;
+        let ckey = getIndex(cm,['code','name','specs','unit','gljType']);
+        if(!projectGLJMap[ckey] && codeMap[cm.code]){
+          missCodeList.push(cm.code);
+          codeMap[cm.code] = true;
+        } 
+      }
+    }
+  
+
+    let mixRatioInsertData = [];
+
+    for(let mg of missMixRatioGroup){//整理需要插入组成物列表的数据
+        for(let tc of mg.list){
+          let consumpiton = tc.consumeAmt;
+          //只有标准的工料机的组成物才会有多单价、多组成物消耗量的情况
+          if(mg.from == 'std' && ext && ext.quantityField &&( tc.consumeAmtProperty[ext.quantityField]!= undefined && tc.consumeAmtProperty[ext.quantityField]!=null)){
+              consumpiton = tc.consumeAmtProperty[ext.quantityField];
+          }
+          let mfrom = mg.from == 'std' || tc.isStd?'std':'cpt'; 
+          let tmp = mfrom == 'std'?stdMixMap[tc.ID]:comMixMap[tc.ID];//取出之前库中查到的工料机
+          let mixRatioData = {
+              consumption: consumpiton,
+              glj_id: tmp.ID,
+              unit_price_file_id: unitPriceFileId,
+              connect_key: mg.connect_key,
+              type: tmp.gljType,
+              code: tmp.code,
+              specs:tmp.specs?tmp.specs:"",
+              name:tmp.name,
+              unit:tmp.unit?tmp.unit:'',
+              from:mfrom
+            };
+          mixRatioInsertData.push(mixRatioData);
+        }
+    }
+
+    if(mixRatioInsertData.length > 0) await setIDfromCounter("mix_ratio",mixRatioInsertData,existMixRatioMap,'connect_key');
+    //await mixRatioModel.insertMany(mixRatioInsertData);  因为没有事务添加组成物数据要放在添加单价文件数据之后
+     
+
+    //查询组成物对应的项目工料机是否存在,如果不存在,则重新插入
+    let projectGLJList = await projectGLJModel.find({'project_id':projectID,'code':{'$in':missCodeList}}).lean();
+    for(let pg of projectGLJList){
+      let pindex = getIndex(pg);  
+      projectGLJMap[pindex] = pg;
+    }
+
+    let lessMix = [];//组成物表存在,项目工料机不存在的数据
+    for(let connect_key in existMixRatioMap){
+      let mixRatios = existMixRatioMap[connect_key]; 
+      for(let m of mixRatios){
+        let mk = getIndex(m);
+        if(!projectGLJMap[mk]){//如果组成物对应的项目工料机不存在
+          let nglj = null;
+          if(m.from == 'std'){//这里有值,说明是刚添加到组成物文件中的数据   
+            nglj = stdMixMap[m.glj_id];
+          }else if(m.from == 'cpt'){//这里有值,说明是刚添加到组成物文件中的数据   
+            nglj = comMixMap[m.glj_id];
+          }    
+          if(nglj){
+            nglj.from = m.from;
+            let np = getProjectGLJNewData(nglj,projectID,ext);
+            newProjectGLJList.push(np);
+            projectGLJMap[mk] = np;
+          }else{//这里没找到,说明是组成物文件里有,但是项目工料机没有的数据
+            lessMix.push(m);
+          }
+        }
+      }
+    }
+
+    let lessIDList=[];
+    let uniqMap ={};//去重
+    let lessStdMix = [];//防止组成物中改了名称等,但是通过glj_id取出来的是还没改前的原始数据
+    if(lessMix.length > 0){
+      for(let lm of lessMix){
+        let parentglj = projectGLJMap[lm.connect_key];
+        if(!parentglj) throw `含有组成物工料机${lm.connect_key},没有找到,添加定额失败`;
+        if(parentglj.from == "std" || lm.from == "std"){
+          if(!uniqMap[lm.glj_id]){
+            lessIDList.push(lm.glj_id);
+            uniqMap[lm.glj_id] = lm;
+          }
+          lessStdMix.push(lm);
+        }else {//来自组成物的直接设置    
+          lm.from = 'cpt';
+          lm.gljType = lm.type;
+          let t_mg = getProjectGLJNewData(lm,projectID);
+          newProjectGLJLis.push(t_mg);
+          projectGLJMap[getIndex(lm)] = t_mg;
+        }
+      }
+    }
+    if(lessIDList.length > 0){
+      let less_stds =  await std_glj_lib_gljList_model.find({'ID':{'$in':lessIDList}}).lean();
+      let less_stds_map = {};
+      for(let les of  less_stds){
+        less_stds_map[les.ID] = les;
+      }
+      for(let t_l_m of lessStdMix){
+        let t_nglj = less_stds_map[t_l_m.glj_id];
+        t_nglj.from = 'std';
+        //防止组成物中改了名称等,但是通过glj_id取出来的是还没改前的原始数据
+        t_nglj.name = t_l_m.name;
+        t_nglj.code = t_l_m.code;
+        t_nglj.gljType = t_l_m.type;
+        t_nglj.specs = t_l_m.specs;
+        t_nglj.unit = t_l_m.unit;
+        let t_np = getProjectGLJNewData(t_nglj,projectID,ext);
+        newProjectGLJList.push(t_np);
+        projectGLJMap[t_l_m.connect_key] = t_np;
+      }
+    }
+
+
+
+
+
+  }
+  return [existMixRatioMap,mixRatioInsertData,newProjectGLJList,missCodeList]
+
+}
+
+
+function getProjectGLJNewData(tmp,projectId,ext){
+  let gljData = {
+      glj_id: tmp.ID,
+      repositoryId:tmp.repositoryId,
+      project_id: projectId,
+      code: tmp.code,
+      name: tmp.name,
+      specs: tmp.specs?tmp.specs:'',
+      unit: tmp.unit === undefined ? '' : tmp.unit,
+      type: tmp.gljType,
+      adjCoe:tmp.adjCoe,
+      original_code:tmp.code,
+      materialType: tmp.materialType,   //三材类别
+      materialCoe: tmp.materialCoe,
+      base_price: tmp.basePrice,
+      market_price: tmp.marketPrice,
+      from:tmp.from?tmp.from:"std"
+  };
+  if(gljData.from == 'std' && ext && ext.priceField &&( tmp.priceProperty[ext.priceField]!= undefined && tmp.priceProperty[ext.priceField]!=null)){
+    basePrice = scMathUtil.roundTo(tmp.priceProperty[ext.priceField],-6);
+    gljData.base_price = basePrice;
+    gljData.market_price = basePrice;
+  }
+  return gljData;
+}
+
+async function setIDfromCounter(name,list,map,keyfield){//map,keyfield
+
+  let update = {$inc: {sequence_value: list.length}};
+  let condition = {_id: name};
+  let options = {new: true};
+
+  // 先查找更新
+  let maxID = await counterModel.findAndModify(condition, update, options);
+  let firstID = maxID - list.length - 1;
+  for(a of list){
+      a.id = firstID;
+      firstID+=1
+      if(map && keyfield){
+        let key = a[keyfield];
+        map[key]?map[key].push(a):map[key]=[a]
+      }
+  }
+
+
+
+}
+
+
+async function getUnitPriceData(unitPriceMap){
+
+}
+
+
 function getStdGlj(sub,stdGLJMap,cptGLJMap,newGLJ,ext) {
     let std_glj = null;
     if(sub.type == 'complementary'){//有可能来自标准工料机库或补充工料机库
@@ -778,7 +1068,7 @@ function FilterNumberFromUnit (unit) {
     }
 };
 
-getIndex(obj,tpops){
+function getIndex(obj,tpops){
   let pops = tpops?tpops:['code','name','specs','unit','type'];
   let t_index = '';
   let k_arr=[];

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 1 - 1
public/web/gljUtil.js