5 天以前 91c82965b2d987452ca276e3a03b48c8dcda2ae9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<script lang="ts" setup>
import type { MesProAiApi } from '#/api/mes/pro/ai';
 
import { onUnmounted, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { IconifyIcon } from '@vben/icons';
 
import { Alert, Button, Progress, Tag } from 'ant-design-vue';
 
import { predictRisk } from '#/api/mes/pro/ai';
 
defineOptions({ name: 'MesProAiPredictRisk' });
 
const props = defineProps<{ workOrderId?: number; mpsId?: number }>();
 
const loading = ref(false);
const progress = ref(0);
const result = ref<MesProAiApi.PredictRiskRespVO>();
const errorMsg = ref('');
 
let progressTimer: ReturnType<typeof setInterval> | null = null;
 
const riskLevelColor: Record<number, string> = { 1: 'green', 2: 'orange', 3: 'red' };
const riskLevelText: Record<number, string> = { 1: '低风险', 2: '中风险', 3: '高风险' };
const severityColor: Record<string, string> = { '高': 'red', '中': 'orange', '低': 'green' };
 
function startProgress() {
  progress.value = 0;
  progressTimer = setInterval(() => {
    if (progress.value < 30) {
      progress.value += 3;
    } else if (progress.value < 70) {
      progress.value += 2;
    } else if (progress.value < 88) {
      progress.value += 0.5;
    }
  }, 100);
}
 
function stopProgress() {
  if (progressTimer) {
    clearInterval(progressTimer);
    progressTimer = null;
  }
}
 
function finishProgress() {
  stopProgress();
  progress.value = 100;
}
 
onUnmounted(() => stopProgress());
 
async function handlePredict() {
  loading.value = true;
  result.value = undefined;
  errorMsg.value = '';
  modalApi.open();
  startProgress();
  try {
    result.value = await predictRisk(props.workOrderId, props.mpsId);
    finishProgress();
  } catch {
    errorMsg.value = 'AI 分析暂时不可用,请稍后重试';
    stopProgress();
  } finally {
    loading.value = false;
  }
}
 
const [ResultModal, modalApi] = useVbenModal({
  footer: false,
  onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      stopProgress();
    }
  },
});
</script>
 
<template>
  <Button :loading="loading" @click="handlePredict">
    <template #icon><IconifyIcon icon="ant-design:warning-outlined" /></template>
    AI 风险预测
  </Button>
 
  <ResultModal title="AI 生产风险预测" class="w-1/2">
    <!-- 加载中 -->
    <div v-if="loading" class="flex flex-col items-center py-10">
      <div class="relative mb-6">
        <div class="absolute inset-0 animate-ping rounded-full bg-orange-400 opacity-20" style="width: 80px; height: 80px" />
        <div class="relative flex h-20 w-20 items-center justify-center rounded-full bg-orange-50">
          <IconifyIcon icon="ant-design:warning-outlined" class="text-3xl text-orange-500" />
        </div>
      </div>
      <div class="mb-3 text-base font-medium text-gray-700">
        AI 正在评估生产风险...
      </div>
      <div class="w-2/3">
        <Progress
          :percent="Math.round(progress)"
          :stroke-color="{ '0%': '#fa8c16', '100%': '#ffd591' }"
          :show-info="true"
          status="active"
        />
      </div>
      <div class="mt-2 text-sm text-gray-400">正在综合评估物料、设备、质量等多维度风险</div>
    </div>
 
    <!-- 错误 -->
    <div v-else-if="errorMsg" class="flex flex-col items-center py-10">
      <IconifyIcon icon="ant-design:close-circle-filled" class="mb-4 text-5xl text-red-400" />
      <span class="text-base text-gray-500">{{ errorMsg }}</span>
    </div>
 
    <!-- 结果 -->
    <template v-else-if="result">
      <div class="mb-4 flex items-center gap-2">
        <span class="text-base font-medium">整体风险等级:</span>
        <Tag :color="riskLevelColor[result.overallRiskLevel]">
          {{ riskLevelText[result.overallRiskLevel] || '-' }}
        </Tag>
      </div>
 
      <div class="mb-4 rounded bg-gray-50 p-3 text-sm leading-relaxed">
        {{ result.reasoning }}
      </div>
 
      <div v-if="result.keyPoints?.length" class="mb-4">
        <div class="mb-2 text-base font-medium">关键关注点:</div>
        <Alert
          v-for="(point, index) in result.keyPoints"
          :key="index"
          type="warning"
          show-icon
          class="mb-2"
          :message="point"
        />
      </div>
 
      <div v-if="result.risks?.length">
        <div class="mb-2 text-base font-medium">风险明细:</div>
        <div
          v-for="(risk, index) in result.risks"
          :key="index"
          class="mb-2 rounded border border-gray-200 p-3"
        >
          <div class="mb-1 flex items-center gap-2">
            <Tag>{{ risk.category }}</Tag>
            <Tag :color="severityColor[risk.severity] || 'default'">
              严重程度:{{ risk.severity }}
            </Tag>
          </div>
          <div class="text-sm text-gray-700">{{ risk.description }}</div>
          <div class="mt-1 text-sm text-blue-600">{{ risk.suggestion }}</div>
        </div>
      </div>
    </template>
  </ResultModal>
</template>