| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 | 
							- /**
 
-  * Created by zhang on 2018/1/22.
 
-  */
 
- let mongoose = require('mongoose');
 
- let projectConst = require('../models/project_consts');
 
- const rationType = projectConst.rationType;
 
- const rationKeyArray = projectConst.rationKeyArray;
 
- const gljKeyArray = projectConst.gljKeyArray;
 
- let quantity_detail = require("./quantity_detail_facade");
 
- let quantity_detail_model = mongoose.model('quantity_detail');
 
- let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
 
- let Bills_Lib = mongoose.model('std_bills_lib_bills');
 
- let ration_Model = mongoose.model('ration');
 
- let ration_glj_Model = mongoose.model('ration_glj');
 
- let ration_coe_Model = mongoose.model('ration_coe');
 
- let ration_installation_Model = mongoose.model('ration_installation');
 
- let bill_Model = require('../models/bills').model;
 
- import GLJController from "../../glj/controllers/glj_controller";
 
- import GLJListModel from '../../glj/models/glj_list_model';
 
- let _ = require("lodash");
 
- module.exports={
 
-     getSectionInfo : async function (data) {
 
-         let conditions=[];
 
-         let fxList=[];
 
-         let sectionInfo ={};
 
-         for(let libID in data){
 
-             let codes=[];
 
-             let temp={};
 
-             codes= _.keys(data[libID]);
 
-             temp['billsLibId']=libID;
 
-             temp['code'] = {"$in": codes};
 
-             conditions.push(temp);
 
-         }
 
-         if(conditions.length>0){
 
-             fxList = await Bills_Lib.find({"$or":conditions});
 
-         }
 
-         if(fxList.length>0){
 
-             let sectionIDs = {};
 
-             for(let f of fxList){
 
-                 if(f._doc.sectionInfo){
 
-                     f._doc.sectionInfo.first?sectionIDs[f._doc.sectionInfo.first]=true:"";
 
-                     f._doc.sectionInfo.second?sectionIDs[f._doc.sectionInfo.second]=true:"";
 
-                     f._doc.sectionInfo.third?sectionIDs[f._doc.sectionInfo.third]=true:"";
 
-                 }
 
-             }
 
-             let IDList = _.keys(sectionIDs);
 
-             let sectionList = await Bills_Lib.find({'ID':{'$in':IDList}});
 
-             let sectionMap = _.mapKeys(sectionList,'ID');
 
-             let fxMap =  _.mapKeys(fxList,'code');
 
-             sectionInfo={
 
-                 fxMap:fxMap,
 
-                 sectionMap :sectionMap
 
-             }
 
-             return sectionInfo;
 
-         }
 
-         return null;
 
-     },
 
-     reorganizeFBFX:async function(data){
 
-         let result = {};
 
-         let tasks = generateBillTasks(data);
 
-         if(data.delete && data.delete.length > 0){
 
-             let qd_query={projectID: data.projectID, billID: {"$in": data.delete}};
 
-             await quantity_detail.deleteByQuery(qd_query) ;
 
-         }
 
-         if(tasks.length > 0){
 
-             result =await bill_Model.bulkWrite(tasks);
 
-         }
 
-        return result;
 
-     },
 
-     pasteBlock : async function(data){
 
-         let pasteTasks = [
 
-             pasteRationsAndRationGLJ(data.rations,data.ration_gljs),//这一步会花费比较多时间
 
-             pasteOtherData(data)
 
-         ];
 
-         let [rationsAndRationGLJ,resultMap] = await Promise.all(pasteTasks);
 
-         let gljController = new GLJController();
 
-         let projectGLJ = await gljController.getProjectGLJsByProjectID(data.projectID);
 
-         resultMap.rations = rationsAndRationGLJ.rations;
 
-         resultMap.ration_gljs = rationsAndRationGLJ.new_ration_gljs;
 
-         resultMap.gljData = projectGLJ.data;
 
-         return resultMap;
 
-     }
 
- };
 
- async function pasteOtherData(data) {
 
-     let bills = data.bills;
 
-     let quantity_details = data.quantity_details;
 
-     let ration_coes = data.ration_coes;
 
-     let ration_installations = data.ration_installations;
 
-     let updateData = data.updateData;
 
-     let uModel = null;
 
-     let tasks = [];
 
-     //生成更新任务
 
-     for(let u of updateData){
 
-         if(u.type == "bills"){
 
-             uModel = bill_Model;
 
-         }else {
 
-             uModel = ration_Model;
 
-         }
 
-         let tem = {
 
-             updateOne:{
 
-                 filter:u.query,
 
-                 update :u.doc
 
-             }
 
-         };
 
-         tasks.push(tem);
 
-     }
 
-     bills.length > 0 ? await insertMany(bills,bill_Model):'';
 
-     quantity_details.length > 0 ? await insertMany(quantity_details,quantity_detail_model):'';
 
-     ration_coes.length > 0 ? await insertMany(ration_coes,ration_coe_Model):'';
 
-     ration_installations.length > 0 ? await insertMany(ration_installations,ration_installation_Model):'';
 
-     tasks.length>0?await uModel.bulkWrite(tasks):'';
 
-     return {bills:bills,quantity_details:quantity_details,ration_coes:ration_coes,ration_installations:ration_installations,updateData:updateData}
 
- }
 
- async function pasteRationsAndRationGLJ (rations,ration_gljs) {
 
-     let projectGljModel = new GLJListModel();
 
-     let gljMap = {};
 
-     let new_ration_gljs=[];
 
-     //先根据定额类型的工料机,插入到项目工料机中
 
-     for(let r of rations){
 
-         if(r.type == rationType.gljRation){
 
-             await getProjectGLJ(r,rationKeyArray,gljMap);
 
-         }
 
-     }
 
-     for(let rg of ration_gljs){
 
-         await getProjectGLJ(rg,gljKeyArray,gljMap);
 
-         let temRecord = ration_glj_facade.createNewRecord(rg);
 
-         rg.rcode?temRecord.rcode = rg.rcode:'';
 
-         rg.hasOwnProperty("customQuantity")?temRecord.customQuantity = rg.customQuantity:'';
 
-         new_ration_gljs.push(temRecord);
 
-     }
 
-     rations.length>0?await insertMany(rations,ration_Model):'';
 
-     new_ration_gljs.length>0?await insertMany(new_ration_gljs,ration_glj_Model):'';
 
-     return{rations:rations,new_ration_gljs:new_ration_gljs};
 
-     async function getProjectGLJ (glj,keyArray,gljMap) {
 
-         let keyIndex = projectGljModel.getIndex(glj,keyArray);
 
-         let pgljResult = null;
 
-         if(gljMap[keyIndex]){
 
-             pgljResult = gljMap[keyIndex]
 
-         }else {
 
-             let p_glj = ration_glj_facade.getGLJSearchInfo(glj);
 
-             pgljResult = await projectGljModel.addList(p_glj);//逐条添加到项目工料机
 
-             gljMap[keyIndex] = pgljResult;
 
-         }
 
-         setResult(glj,pgljResult);
 
-     }
 
-     function setResult(glj,result ) {
 
-         let typeString = result.type+'';
 
-         glj.marketPrice = result.unit_price.market_price;
 
-         glj.adjustPrice = result.unit_price.base_price;
 
-         glj.basePrice = result.unit_price.base_price;
 
-         glj.isAdd = result.unit_price.is_add;
 
-         glj.projectGLJID = result.id;
 
-         if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
 
-             glj.isEstimate = result.is_evaluate;
 
-         }
 
-     }
 
- }
 
- function generateBillTasks(data) {
 
-     let tasks=[];
 
-     let user_id = data.user_id,projectID = data.projectID;
 
-     let deleteInfo={deleted: true, deleteDateTime: new Date(), deleteBy: user_id};
 
-     if(data.delete && data.delete.length > 0){
 
-         for(let d_ID of data.delete){
 
-             let task={
 
-                 updateOne:{
 
-                     filter:{
 
-                         ID:d_ID,
 
-                         projectID:projectID
 
-                     },
 
-                     update :{
 
-                         deleteInfo:deleteInfo
 
-                     }
 
-                 }
 
-             };
 
-             tasks.push(task);
 
-         }
 
-     }
 
-     if(data.update && data.update.length > 0){
 
-         for(let u_data of data.update){
 
-             let task ={
 
-                 updateOne:{
 
-                     filter : {
 
-                         ID:u_data.ID,
 
-                         projectID:projectID
 
-                     },
 
-                     update : u_data.data
 
-                 }
 
-             };
 
-             tasks.push(task);
 
-         }
 
-     }
 
-     if(data.create && data.create.length > 0){
 
-         for(let n_data of data.create){
 
-             let task = {
 
-                 insertOne :{
 
-                     document:n_data
 
-                 }
 
-             };
 
-             tasks.push(task);
 
-         }
 
-     }
 
-     return tasks;
 
- }
 
- async function insertMany(datas,model) {
 
-     while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
 
-         let newList = datas.splice(0,1000);//一次插入1000条
 
-         await model.insertMany(newList);
 
-     }
 
-     await model.insertMany(datas);
 
- }
 
 
  |