Pārlūkot izejas kodu

优化插入sql及清单重复判断提醒

laiguoran 2 gadi atpakaļ
vecāks
revīzija
7ccf4c98ea

+ 9 - 0
app/base/base_service.js

@@ -229,5 +229,14 @@ class BaseService extends Service {
     round(value, decimal) {
         return this.ctx.helper.round(value, this._.isNumber(decimal) ? decimal : 8);
     }
+
+    // 针对大批量数据插入,优化插入datas,按50条为分隔
+    async insertBigDatas(transaction, insertDatas) {
+        const batchSize = 50;
+        for (let i = 0; i < insertDatas.length; i += batchSize) {
+            const batchDatas = insertDatas.splice(i, i + batchSize);
+            await transaction.insert(this.tableName, batchDatas);
+        }
+    }
 }
 module.exports = BaseService;

+ 22 - 0
app/public/js/change_information_set.js

@@ -1618,9 +1618,25 @@ function checkChangeFrom() {
     if(!checkAuditorFrom ()) {
         returnFlag = true;
     }
+    // 判断非空白变更清单是否有重复值(gcl_id及mx_id),如果存在完全相同则需要提示并删除,上报失败,这功能主要是因为清单数据过多服务器处理不过来偶然出现
+    const doubleList = findDuplicates(_.filter(changeList, item => item.lid != 0));
+    if (doubleList.length > 0) {
+        for (const double of doubleList) {
+            const msgIndex = [];
+            for (const one of double) {
+                const index = _.findIndex(changeList, { id: one.id });
+                if (index !== -1) msgIndex.push(index+1);
+            }
+            if (msgIndex.length > 0) {
+                toastr.error('第 ' + msgIndex.splice(',') + ' 行变更清单 ' + double[0].code + ' 重复,请只保留一条数据');
+            }
+        }
+        returnFlag = true;
+    }
     if (returnFlag) {
         return false;
     }
+    return false;
 }
 // 检查上报情况
 function checkAuditorFrom () {
@@ -1634,6 +1650,12 @@ function checkAuditorFrom () {
     }
     return true;
 }
+// 找出数组中相同的清单
+function findDuplicates(arr, keys = ['gcl_id', 'mx_id']) {
+    const groupedData = _.groupBy(arr, item => JSON.stringify(_.pick(item, keys)));
+    const duplicates = _.filter(groupedData, group => group.length > 1);
+    return _.values(duplicates);
+}
 
 function tableDataRemake(changeListData) {
     $('#table-list-select tr').removeClass('table-warning');

+ 5 - 1
app/service/change_audit_list.js

@@ -313,7 +313,7 @@ module.exports = app => {
                     order = order ? order + 1 : null;
                     insertDatas.push(data);
                 }
-                if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
+                if (insertDatas.length > 0) await this.insertBigDatas(transaction, insertDatas);
                 await this.calcCamountSum(transaction);
                 if (!order_by) {
                     // 更新stage_change和stage_change_final的cbid
@@ -343,6 +343,10 @@ module.exports = app => {
                         }
                     }
                 }
+                const flag = true;
+                if (flag) {
+                    throw 'fail';
+                }
                 await transaction.commit();
                 return true;
             } catch (err) {