zhangweicheng 5 years ago
parent
commit
99697c2901

+ 21 - 1
modules/common/base/base_model.js

@@ -6,6 +6,8 @@
  * @version
  */
 import MongooseHelper from "../helper/mongoose_helper";
+let mongoose = require('mongoose');
+let counterModel =  mongoose.model('counter');
 
 class BaseModel {
 
@@ -113,7 +115,25 @@ class BaseModel {
 
         return result;
     }
-
+    
+    async setIDfromCounter(name,list,map,keyfield){
+      let update = {$inc: {sequence_value: list.length}};
+      let condition = {_id: name};
+      let options = {new: true};
+    
+      // 先查找更新
+      let counter = await counterModel.findOneAndUpdate(condition, update, options);
+      let firstID = counter.sequence_value - (list.length - 1);
+      for(let a of list){
+          console.log(firstID)
+          a.id = firstID;
+          firstID+=1
+          if(map && keyfield){
+            let key = a[keyfield];
+            map[key]?map[key].push(a):map[key]=[a]
+          }
+      }
+    }
     /**
      * 更新数据
      *

+ 4 - 0
modules/glj/facade/glj_facade.js

@@ -10,6 +10,7 @@ const mongoose = require('mongoose');
 const ProjectModel = require('../../pm/models/project_model').project;
 import UnitPriceFileModel from "../models/unit_price_file_model";
 import UnitPriceModel from "../models/unit_price_model";
+import MixRatioModel from "../models/mix_ratio_model";
 let evaluateListModel = mongoose.model("evaluate_list");
 let bidEvaluationMode = mongoose.model("bid_evaluation_list");
 let contractorListModel = mongoose.model("contractor_list");
@@ -65,6 +66,9 @@ async function changeUnitFile(projectData,unitFile,type,userID) {
                     copyList.push(n);
                 }
                 copyList.length>0 ? await unitPriceModel.add(copyList):'';
+                  //也要复制一份组成物信息和材料计算信息
+                  let mixRatioModel = new MixRatioModel();
+                  await mixRatioModel.copyNotExist(changeUnitPriceId, targetUnitPriceFile.id,{},true);//复制组成物
             }
         }
 

+ 9 - 11
modules/glj/models/mix_ratio_model.js

@@ -50,14 +50,15 @@ class MixRatioModel extends BaseModel {
     async add(data) {
         let counterModel = new CounterModel();
         if (data instanceof Array) {
-            for(let tmp in data) {
+          await this.setIDfromCounter(collectionName,data);
+            /* for(let tmp in data) {
                 data[tmp].id = await counterModel.getId(collectionName);
-            }
+            } */
         } else {
             data.id = await counterModel.getId(collectionName);
         }
 
-        return this.db.model.create(data);
+        return await this.db.model.create(data);
     }
 
     /**
@@ -79,23 +80,20 @@ class MixRatioModel extends BaseModel {
     }
 
     //复制组成物到切换后的组成物映射表
-    async copyNotExist(currentUnitPriceId, changeUnitPriceId,gljMap){
+    async copyNotExist(currentUnitPriceId, changeUnitPriceId,gljMap,newFile = false){
         let currentMap = {},targetMap = {}, insertData = [];
         //取原单价文件所有的的组成物
-        let currentList = await  this.db.find({'unit_price_file_id':currentUnitPriceId});
-        // 过滤mongoose格式
-        currentList = JSON.stringify(currentList);
-        currentList = JSON.parse(currentList);
+        let currentList = await  this.model.find({'unit_price_file_id':currentUnitPriceId}).lean();;
         this.getConnectionMap(currentMap,currentList);
 
         //切换后的单价文件所有的的组成物
-        let targetList = await this.db.find({'unit_price_file_id':changeUnitPriceId});
+        let targetList = await this.model.find({'unit_price_file_id':changeUnitPriceId}).lean();
         this.getConnectionMap(targetMap,targetList);
         for(let ckey in currentMap){
             if(targetMap[ckey]){//如果切换后的单价文件已经存在,则不用复
                 continue;
             }
-            if(gljMap[ckey]){//在本项目中有用到
+            if(gljMap[ckey] || newFile == true){//在本项目中有用到
                 for(let ratio of  currentMap[ckey]){
                     delete ratio._id;  // 删除原有id信息
                     delete ratio.id;
@@ -104,7 +102,7 @@ class MixRatioModel extends BaseModel {
                 }
             }
         }
-        return insertData.length > 0 ? this.add(insertData) : true;
+        return insertData.length > 0 ? await this.add(insertData) : true;
     }
     getConnectionMap(map,list){
         for(let l of list){

+ 5 - 6
modules/glj/models/unit_price_model.js

@@ -158,9 +158,10 @@ class UnitPriceModel extends BaseModel {
         let counterModel = new CounterModel();
         if (data instanceof Array) {
             // 如果是批量新增
-            for(let tmp in data) {
+            await this.setIDfromCounter(collectionName,data);
+            /* for(let tmp in data) {
                 data[tmp].id = await counterModel.getId(collectionName);
-            }
+            } */
         } else {
             data.id = await counterModel.getId(collectionName);
         }
@@ -392,13 +393,11 @@ class UnitPriceModel extends BaseModel {
     async copyNotExist(currentUnitPriceId, changeUnitPriceId,projectId) {
         let result = false;
         // 首先查找原单价文件id下的数据
-        let currentUnitList = await this.findDataByCondition({unit_price_file_id: currentUnitPriceId}, null, false);
+        let currentUnitList = await this.model.find({unit_price_file_id: currentUnitPriceId}).lean();
         if (currentUnitList === null) {
             return result;
         }
-        // 过滤mongoose格式
-        currentUnitList = JSON.stringify(currentUnitList);
-        currentUnitList = JSON.parse(currentUnitList);
+      
 
         let gljList = await gljListModel.find({'project_id':projectId});
         let gljMap = {};//用来记录glj的映射表,本项目有使用的工料机才需要copy过去