فهرست منبع

人材机库导入单价支持导入税率

vian 5 سال پیش
والد
کامیت
da11adfa33
1فایلهای تغییر یافته به همراه124 افزوده شده و 12 حذف شده
  1. 124 12
      modules/std_glj_lib/models/gljModel.js

+ 124 - 12
modules/std_glj_lib/models/gljModel.js

@@ -10,7 +10,7 @@ const scMathUtil = require('../../../public/scMathUtil').getUtil();
 const rationMapModel = mongoose.model('std_ration_lib_map');
 const rationModel = mongoose.model('std_ration_lib_ration_items');
 const complementaryRationModel = mongoose.model('complementary_ration_items');
-const { isDef } = require('../../../public/common_util');
+const { isDef, isEmptyVal } = require('../../../public/common_util');
 import {OprDao} from  "./gljMapModel";
 import moment from "moment";
 import counter from "../../../public/counter/counter";
@@ -603,7 +603,7 @@ class GljDao  extends OprDao{
          return gljModel.find({"repositoryId": repositoryId}, returnFields);
     }
 
-    async batchUpdateGljPrice(gljLibId, sheetData){
+    /* async batchUpdateGljPrice(gljLibId, sheetData){
         let gljLib = await gljMapModel.findOne({ID: gljLibId});
         if(!gljLib){
             throw '不存在此人材机库';
@@ -617,17 +617,17 @@ class GljDao  extends OprDao{
         let colMapping = {};
         for(let col = 0; col < sheetData[0].length; col++){
             let cData = sheetData[0][col];
-            if(cData === '编码'){
-                colMapping['code'] = col;
-            }
-            else {
+            if (cData === '编码'){
+                colMapping.code = col;
+            } else if (cData === '税率') {
+                colMapping.taxRate = col;
+            } else {
                 if(priceProperties.length === 0){
                     if(cData === '定额价'){
-                        colMapping['basePrice'] = col;
+                        colMapping.basePrice = col;
                         break;
                     }
-                }
-                else {
+                } else {
                     for(let priceProp of priceProperties){
                         if(priceProp.price.dataName === cData){
                             colMapping[priceProp.price.dataCode] = col;
@@ -659,7 +659,14 @@ class GljDao  extends OprDao{
                 existGlj = existMapping[gljCode];
             //更新多单价、不覆盖priceProperty字段,覆盖priceProperty下的子字段'priceProperty.x'
             if(gljCode && gljCode !== '' && !updateCodes.includes(gljCode) && existGlj){
-                if(priceProperties.length > 0){
+                const updateSet = {};
+                // 税率
+                const taxRateCol = colMapping.taxRate;
+                if (isDef(taxRateCol) && !isEmptyVal(sheetData[row][taxRateCol])) {
+                    updateSet.taxRate = sheetData[row][taxRateCol];
+                }
+                // 价格
+                if(priceProperties.length > 0){ // 多单价
                     for(let priceProp of priceProperties){
                         let dataCode = priceProp.price.dataCode;
                         let priceCellData = sheetData[row][colMapping[dataCode]];
@@ -675,8 +682,7 @@ class GljDao  extends OprDao{
                         });
                     }
                     updateCodes.push(gljCode);
-                }
-                else {
+                } else { // 单单价
                     if(colMapping.basePrice){
                         let priceCellData = sheetData[row][colMapping.basePrice];
                         let basePrice = priceCellData  && !isNaN(priceCellData) ?
@@ -696,6 +702,112 @@ class GljDao  extends OprDao{
             }
         }
 
+    } */
+
+    async batchUpdateGljPrice(gljLibId, sheetData){
+        let gljLib = await gljMapModel.findOne({ID: gljLibId});
+        if(!gljLib){
+            throw '不存在此人材机库';
+        }
+        let compilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(gljLib.compilationId)});
+        if(!compilation){
+            throw '不存在此费用定额';
+        }
+        let priceProperties = compilation.priceProperties ? compilation.priceProperties : [];
+        //根据第一行数据,获取列下标与字段名映射
+        let colMapping = {};
+        for(let col = 0; col < sheetData[0].length; col++){
+            let cData = sheetData[0][col];
+            if (cData === '编码'){
+                colMapping.code = col;
+            } else if (cData === '税率') {
+                colMapping.taxRate = col;
+            } else {
+                if(priceProperties.length === 0){
+                    if(cData === '定额价'){
+                        colMapping.basePrice = col;
+                        break;
+                    }
+                } else {
+                    for(let priceProp of priceProperties){
+                        if(priceProp.price.dataName === cData){
+                            colMapping[priceProp.price.dataCode] = col;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+        let colMappingKeys = Object.keys(colMapping);
+        if(colMappingKeys.length < 2){
+            throw 'excel数据不正确'
+        }
+        let updateBulk = [];
+        //避免重复
+        let updateCodes = [];
+        //库中存在的人材机
+        let dateA = Date.now();
+        let existGljs = await gljModel.find({repositoryId: gljLibId}, '-_id code ID');
+        let existMapping = {};
+        for (let glj of existGljs) {
+            existMapping[glj.code] = glj;
+        }
+        for(let row = 0; row < sheetData.length; row++){
+            if(row === 0){
+                continue;
+            }
+            let gljCode = sheetData[row][colMapping.code],
+                existGlj = existMapping[gljCode];
+            //更新多单价、不覆盖priceProperty字段,覆盖priceProperty下的子字段'priceProperty.x'
+            if(gljCode && gljCode !== '' && !updateCodes.includes(gljCode) && existGlj){
+                const updateSet = {};
+                // 税率
+                const taxRateCol = colMapping.taxRate;
+                if (isDef(taxRateCol) && !isEmptyVal(sheetData[row][taxRateCol])) {
+                    updateSet.taxRate = sheetData[row][taxRateCol];
+                }
+                // 价格
+                if(priceProperties.length > 0){ // 多单价
+                    for(let priceProp of priceProperties){
+                        let dataCode = priceProp.price.dataCode;
+                        let priceCellData = sheetData[row][colMapping[dataCode]];
+                        //Excel中没有这个单价则跳过
+                        if (!colMapping[dataCode] || isEmptyVal(priceCellData)) {
+                            continue;
+                        }
+                        updateSet['priceProperty.' + dataCode] = priceCellData && !isNaN(priceCellData) ?
+                            scMathUtil.roundTo(parseFloat(priceCellData), -2) : 0;
+                        /* updateBulk.push({
+                            updateOne: {filter: {ID: existGlj.ID}, update: {$set: updateSet}}
+                        }); */
+                    }
+                    //updateCodes.push(gljCode);
+                } else { // 单单价
+                    if(colMapping.basePrice && !isEmptyVal(sheetData[row][colMapping.basePrice])){
+                        let priceCellData = sheetData[row][colMapping.basePrice];
+                        let basePrice = priceCellData  && !isNaN(priceCellData) ?
+                            scMathUtil.roundTo(priceCellData, -2) : 0;
+                        updateSet.basePrice = basePrice;
+                        /* updateCodes.push(gljCode);
+                        updateBulk.push({
+                            updateOne: {filter: {ID: existGlj.ID}, update: {$set: {basePrice: basePrice}}}
+                        }); */
+                    }
+                }
+                if (Object.keys(updateSet).length) {
+                    updateCodes.push(gljCode);
+                    updateBulk.push({      
+                        updateOne: {filter: {ID: existGlj.ID}, update: {$set: updateSet}}
+                    });
+                }
+            }
+        }
+        if(updateBulk.length > 0){
+            while (updateBulk.length > 0) {
+                let sliceBulk = updateBulk.splice(0, 1000);
+                await gljModel.bulkWrite(sliceBulk);
+            }
+        }
     }
 
     // 导入组成物(替换原本的数据)