Explorar el Código

历史数据导入并发问题修改

Zhangbw hace 1 mes
padre
commit
3f1be876d8
Se han modificado 2 ficheros con 83 adiciones y 4 borrados
  1. 11 0
      src/api/stock/history/index.ts
  2. 72 4
      src/views/stock/history/index.vue

+ 11 - 0
src/api/stock/history/index.ts

@@ -146,3 +146,14 @@ export function downloadTemplate() {
     responseType: 'blob'
   });
 }
+
+/**
+ * 检查异步任务处理状态
+ * @returns true: 正在处理, false: 空闲
+ */
+export function checkProcessing(): AxiosPromise<boolean> {
+  return request({
+    url: '/stock/history/checkProcessing',
+    method: 'get'
+  });
+}

+ 72 - 4
src/views/stock/history/index.vue

@@ -39,7 +39,9 @@
             <el-button v-has-permi="['stock:history:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
           </el-col>
           <el-col :span="1.5">
-            <el-button v-has-permi="['stock:history:import']" type="warning" plain icon="Upload" @click="handleImport">导入数据</el-button>
+            <el-button v-has-permi="['stock:history:import']" type="warning" plain icon="Upload" @click="handleImport" :disabled="isProcessing">
+              {{ isProcessing ? '处理中...' : '导入数据' }}
+            </el-button>
           </el-col>
           <el-col :span="1.5">
             <el-button v-has-permi="['stock:history:import']" type="info" plain icon="Download" @click="handleDownloadTemplate">下载模版</el-button>
@@ -255,8 +257,8 @@
 </template>
 
 <script setup name="StockHistory" lang="ts">
-import { ref, reactive, getCurrentInstance } from 'vue';
-import { listStockHistory, getStockHistory, addStockHistory, updateStockHistory, delStockHistory, importStockHistory, downloadTemplate } from '@/api/stock/history';
+import { ref, reactive, getCurrentInstance, onUnmounted } from 'vue';
+import { listStockHistory, getStockHistory, addStockHistory, updateStockHistory, delStockHistory, importStockHistory, downloadTemplate, checkProcessing } from '@/api/stock/history';
 import type { UploadFile, UploadFiles, UploadInstance } from 'element-plus';
 
 const { proxy } = getCurrentInstance() as ComponentInternalInstance;
@@ -270,6 +272,8 @@ const multiple = ref(true);
 const total = ref(0);
 const dateRange = ref<[string, string]>();
 const importing = ref(false);
+const isProcessing = ref(false); // 异步任务处理状态
+let pollingTimer: number | null = null; // 轮询定时器
 
 const queryFormRef = ref<ElFormInstance>();
 const historyFormRef = ref<ElFormInstance>();
@@ -474,9 +478,12 @@ const submitImport = () => {
           importForm.value.recordDate,
           importForm.value.updateSupport
         );
-        proxy?.$modal.msgSuccess(res.msg || '导入成功');
+        proxy?.$modal.msgSuccess(res.msg || '导入成功,正在后台处理数据...');
         importDialog.visible = false;
         await getList();
+
+        // 开始轮询检查异步任务状态
+        startPolling();
       } catch (error: any) {
         proxy?.$modal.msgError(error.msg || '导入失败');
       } finally {
@@ -486,6 +493,45 @@ const submitImport = () => {
   });
 };
 
+/** 开始轮询检查异步任务状态 */
+const startPolling = () => {
+  // 清除之前的定时器
+  stopPolling();
+
+  // 立即检查一次
+  checkAsyncStatus();
+
+  // 每2秒检查一次
+  pollingTimer = window.setInterval(() => {
+    checkAsyncStatus();
+  }, 2000);
+};
+
+/** 停止轮询 */
+const stopPolling = () => {
+  if (pollingTimer) {
+    clearInterval(pollingTimer);
+    pollingTimer = null;
+  }
+};
+
+/** 检查异步任务状态 */
+const checkAsyncStatus = async () => {
+  try {
+    const res = await checkProcessing();
+    isProcessing.value = res.data;
+
+    // 如果任务完成,停止轮询并刷新列表
+    if (!res.data) {
+      stopPolling();
+      proxy?.$modal.msgSuccess('数据处理完成!');
+      await getList();
+    }
+  } catch (error) {
+    console.error('检查处理状态失败:', error);
+  }
+};
+
 /** 取消导入 */
 const cancelImport = () => {
   importDialog.visible = false;
@@ -513,6 +559,28 @@ const handleDelete = async (row?: any) => {
   await getList();
 };
 
+// 页面加载时检查是否有异步任务正在执行
+const initCheckProcessing = async () => {
+  try {
+    const res = await checkProcessing();
+    if (res.data) {
+      // 如果有任务正在执行,启动轮询
+      isProcessing.value = true;
+      startPolling();
+      proxy?.$modal.msgWarning('检测到有数据正在后台处理中,请等待完成...');
+    }
+  } catch (error) {
+    console.error('初始化检查处理状态失败:', error);
+  }
+};
+
+// 组件卸载时清理定时器
+onUnmounted(() => {
+  stopPolling();
+});
+
+// 初始化:先检查处理状态,再加载列表
+initCheckProcessing();
 getList();
 </script>