Browse Source

复制工料机库接口

vian 5 years ago
parent
commit
3a9dbadca0
1 changed files with 45 additions and 0 deletions
  1. 45 0
      modules/std_glj_lib/models/gljModel.js

+ 45 - 0
modules/std_glj_lib/models/gljModel.js

@@ -21,6 +21,51 @@ import async from "async";
 let _ = require("lodash");
 let _ = require("lodash");
 
 
 class GljDao  extends OprDao{
 class GljDao  extends OprDao{
+
+    async copyLib(sourceLibID, targetLibID) {
+        const task = [
+            this.copyClassData(sourceLibID, targetLibID),
+            this.copyGLJData(sourceLibID, targetLibID)
+        ];
+        await Promise.all(task);
+    }
+
+    async copyClassData(sourceLibID, targetLibID) {
+        const sourceClassData = await gljClassModel.find({ repositoryId: sourceLibID }, '-_id').lean();
+        const insertData = sourceClassData.map(item => ({
+            ... item,
+            repositoryId: targetLibID
+        }));
+        if (insertData.length) {
+            await gljClassModel.insertMany(insertData);
+        }
+    }
+
+    async copyGLJData(sourceLibID, targetLibID) {
+        const sourceGLJData = await gljModel.find({ repositoryId: sourceLibID }, '-_id').lean();
+        const IDMapping = {};
+        const countData = await counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, sourceGLJData.length);
+        const countIdx = countData.sequence_value - (sourceGLJData.length - 1);
+        sourceGLJData.forEach((glj, index) => {
+            IDMapping[glj.ID] = countIdx + index;
+        });
+        const insertData = sourceGLJData.map(glj => {
+            const newComponent = (glj.component || []).map(c => ({
+                ID: IDMapping[c.ID],
+                consumeAmt: c.consumeAmt
+            }));
+            return {
+                ...glj,
+                repositoryId: targetLibID,
+                ID: IDMapping[glj.ID],
+                component: newComponent
+            };
+        });
+        if (insertData.length) {
+            await gljModel.insertMany(insertData);
+        }
+    }
+
     async getReference(repositoryId, gljId) {
     async getReference(repositoryId, gljId) {
         const gljLib = await gljMapModel.findOne({ID: repositoryId});
         const gljLib = await gljMapModel.findOne({ID: repositoryId});
         const rationLibIds = gljLib.rationLibs.map(lib => lib.ID);
         const rationLibIds = gljLib.rationLibs.map(lib => lib.ID);