Files
pig-farm-controller-fe/src/components/feed/AIRecipeReviewDialog.vue

189 lines
5.0 KiB
Vue
Raw Normal View History

2025-12-02 16:00:58 +08:00
<template>
<el-dialog
v-model="dialogVisible"
title="AI点评配方"
width="600px"
:before-close="handleClose"
destroy-on-close
>
<el-form label-width="100px">
<el-form-item label="配方名称:">
<span>{{ recipe ? recipe.name : 'N/A' }}</span>
</el-form-item>
<el-form-item label="目标猪类型:">
<el-select
v-model="selectedPigTypeId"
placeholder="请选择目标猪类型"
filterable
:loading="loadingPigTypes"
:disabled="loadingAIReview"
style="width: 100%;"
>
<el-option
v-for="item in pigTypes"
:key="item.id"
:label="item.label"
:value="item.id"
></el-option>
</el-select>
</el-form-item>
<el-form-item label="AI点评结果:" v-if="aiReviewResult || error">
<el-alert
v-if="error"
:title="error"
type="error"
show-icon
:closable="false"
></el-alert>
<el-input
v-else-if="aiReviewResult"
type="textarea"
:rows="8"
:value="`AI模型: ${aiReviewResult.ai_model}\n\n点评内容:\n${aiReviewResult.review_message}`"
readonly
></el-input>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="handleClose" :disabled="loadingAIReview">取消</el-button>
<el-button
type="primary"
@click="handleConfirm"
:loading="loadingAIReview"
:disabled="!selectedPigTypeId || loadingPigTypes"
>
{{ aiReviewResult ? '重新点评' : '开始点评' }}
</el-button>
</span>
</template>
</el-dialog>
</template>
<script>
import { getPigTypes, aiDiagnoseRecipe } from '../../api/feed';
import { ElMessage } from 'element-plus';
export default {
name: 'AIRecipeReviewDialog',
props: {
visible: {
type: Boolean,
default: false,
},
recipe: {
type: Object,
default: null,
},
},
emits: ['update:visible', 'cancel'],
data() {
return {
dialogVisible: this.visible,
pigTypes: [], // 格式化后的猪类型列表
selectedPigTypeId: null,
aiReviewResult: null,
loadingPigTypes: false,
loadingAIReview: false,
error: null,
};
},
watch: {
visible(newVal) {
this.dialogVisible = newVal;
if (newVal) {
this.resetState();
this.loadPigTypes();
}
},
dialogVisible(newVal) {
this.$emit('update:visible', newVal);
},
},
methods: {
/**
* 重置组件内部状态
*/
resetState() {
this.selectedPigTypeId = null;
this.aiReviewResult = null;
this.error = null;
this.pigTypes = [];
},
/**
* 加载猪类型列表
*/
async loadPigTypes() {
this.loadingPigTypes = true;
this.error = null;
try {
// 获取所有猪类型,假设一次性获取足够的数据
const response = await getPigTypes({ page: 1, page_size: 1000 });
if (response.code === 2000 && response.data && response.data.list) {
this.pigTypes = response.data.list.map(type => ({
id: type.id,
label: `${type.breed_name} - ${type.age_stage_name}`,
}));
} else {
this.error = response.msg || '获取猪类型失败';
ElMessage.error(this.error);
}
} catch (err) {
this.error = '加载猪类型失败: ' + (err.message || '未知错误');
ElMessage.error(this.error);
console.error('加载猪类型失败:', err);
} finally {
this.loadingPigTypes = false;
}
},
/**
* 处理确认按钮点击调用AI点评接口
*/
async handleConfirm() {
if (!this.recipe || !this.recipe.id) {
ElMessage.warning('未选择配方');
return;
}
if (!this.selectedPigTypeId) {
ElMessage.warning('请选择目标猪类型');
return;
}
this.loadingAIReview = true;
this.aiReviewResult = null;
this.error = null;
try {
const response = await aiDiagnoseRecipe(this.recipe.id, this.selectedPigTypeId);
if (response.code === 2000 && response.data) {
this.aiReviewResult = response.data;
ElMessage.success('AI点评成功');
} else {
this.error = response.msg || 'AI点评失败';
ElMessage.error(this.error);
}
} catch (err) {
this.error = 'AI点评请求失败: ' + (err.message || '未知错误');
ElMessage.error(this.error);
console.error('AI点评请求失败:', err);
} finally {
this.loadingAIReview = false;
}
},
/**
* 处理对话框关闭
*/
handleClose() {
this.dialogVisible = false;
this.$emit('cancel');
},
},
};
</script>
<style scoped>
/* 可以根据需要添加样式 */
</style>