فهرست منبع

插入优化,跨项目使用单价文件bug

zhangweicheng 5 سال پیش
والد
کامیت
c0a5f5975c

+ 1 - 1
modules/all_models/mix_ratio.js

@@ -11,7 +11,7 @@ let Schema = mongoose.Schema;
 let collectionName = 'mix_ratio';
 let modelSchema = {
     // 自增id
-    id: Number,
+    id: {type:Number,unique: true},
     // 消耗量(有别于总消耗,此字段记录配合比的消耗量)
     consumption: {
         type: String,

+ 21 - 0
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 {
 
@@ -114,6 +116,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]
+          }
+      }
+    }
+
     /**
      * 更新数据
      *

+ 5 - 7
modules/glj/models/mix_ratio_model.js

@@ -50,9 +50,10 @@ 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);
         }
@@ -82,14 +83,11 @@ class MixRatioModel extends BaseModel {
     async copyNotExist(currentUnitPriceId, changeUnitPriceId,gljMap){
         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]){//如果切换后的单价文件已经存在,则不用复

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

@@ -165,9 +165,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);
         }
@@ -432,7 +433,7 @@ class UnitPriceModel extends BaseModel {
             return result;
         }
 
-        let gljList = await gljListModel.find({'project_id':projectId});
+        let gljList = await gljListModel.find({'project_id':projectId}).lean();
         let gljMap = {};//用来记录glj的映射表,本项目有使用的工料机才需要copy过去
         for(let g of gljList){
             let g_index = this.getIndex(g,['code','name','specs','unit','type']);
@@ -460,6 +461,7 @@ class UnitPriceModel extends BaseModel {
         let insertData = [];
         for (let tmp of currentUnitList) {
             let t_index = this.getIndex(tmp,['code','name','specs','unit','type']);
+            if(gljMap[t_index])  gljMap[t_index].copy = true;//复制标记,用于组成物,材料计算等的复制
             if (targetUnitList !== null && targetUnitList[t_index] !== undefined) {
                 continue;
             }
@@ -468,7 +470,6 @@ class UnitPriceModel extends BaseModel {
                 delete tmp.id;
                 tmp.unit_price_file_id = changeUnitPriceId;
                 insertData.push(tmp);
-                gljMap[t_index].copy = true;//复制标记,用于组成物,材料计算等的复制
             }
         }
         let uResult = insertData.length > 0 ? this.add(insertData) : true;