新增羊只,转场,转群,改耳号,改品种,改备注,体尺测量,体况评分,乳况评分,修蹄,去势初版
This commit is contained in:
@@ -6,11 +6,12 @@
|
||||
v-hasPermi="['produce:add_sheep:import']">导入</el-button>
|
||||
|
||||
<!-- 新增/编辑表单 -->
|
||||
<el-form :model="form" ref="formRef" label-position="left" label-width="100px" style="margin-top:15px">
|
||||
<el-form :model="form" ref="formRef" label-position="left" label-width="120px" style="margin-top:15px"
|
||||
:rules="rules">
|
||||
<el-row :gutter="20">
|
||||
<el-col :span="10">
|
||||
<el-form-item label="耳号" prop="earNumber">
|
||||
<el-input v-model="form.earNumber" placeholder="请输入耳号" />
|
||||
<el-input v-model="form.earNumber" placeholder="请输入耳号" @blur="handleEarNumberBlur" />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="10">
|
||||
@@ -55,7 +56,6 @@
|
||||
<el-option label="公" :value="2" />
|
||||
<el-option label="母" :value="1" />
|
||||
<el-option label="阉羊" :value="3" />
|
||||
<!-- <el-option label="兼性" :value="3" /> -->
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
@@ -127,7 +127,6 @@ import { getToken } from '@/utils/auth'
|
||||
|
||||
|
||||
const { proxy } = getCurrentInstance()
|
||||
/* -------------------- 表单 -------------------- */
|
||||
const form = ref({
|
||||
earNumber: '',
|
||||
sheepfold: '',
|
||||
@@ -145,24 +144,63 @@ const form = ref({
|
||||
const formRef = ref(null)
|
||||
|
||||
const rules = {
|
||||
earNumber: [{ required: true, message: '请输入耳号', trigger: 'blur' }],
|
||||
earNumber: [
|
||||
{ required: true, message: '请输入耳号', trigger: 'blur' },
|
||||
{ validator: checkEarNumberExists, trigger: 'blur' }
|
||||
],
|
||||
sheepfold: [{ required: true, message: '请选择羊舍', trigger: 'change' }],
|
||||
bornWeight: [{ required: true, message: '请输入出生体重', trigger: 'blur' }],
|
||||
birthday: [{ required: true, message: '请选择出生日期', trigger: 'change' }],
|
||||
gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
|
||||
varietyId: [{ required: true, message: '请选择品种', trigger: 'change' }]
|
||||
varietyId: [{ required: true, message: '请选择品种', trigger: 'change' }],
|
||||
joinDate:[{ required: true, message: '请选择入群日期', trigger: 'change' }],
|
||||
technician:[{ required: true, message: '请输入技术员', trigger: 'change' }],
|
||||
}
|
||||
|
||||
/* -------------------- 下拉数据 -------------------- */
|
||||
// 校验耳号是否存在
|
||||
function checkEarNumberExists(rule, value, callback) {
|
||||
if (!value.trim()) {
|
||||
return callback(new Error('请输入耳号'))
|
||||
}
|
||||
|
||||
request({
|
||||
url: `/sheep/sheep/existsByManageTags/${value.trim()}`,
|
||||
method: 'get'
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data === true) {
|
||||
callback(new Error('耳号已存在,无法新增'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
const status = error.response?.status;
|
||||
const errorMsg = error.response?.data?.msg || '';
|
||||
if (status === 404) {
|
||||
callback();
|
||||
} else {
|
||||
callback(new Error('请求异常,请稍后重试'));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handleEarNumberBlur() {
|
||||
formRef.value.validateField('earNumber')
|
||||
}
|
||||
|
||||
|
||||
const sheepfoldOptions = ref([])
|
||||
const varietyOptions = ref([])
|
||||
|
||||
//羊舍列表
|
||||
function getSheepfoldOptions() {
|
||||
listSheepfold({ pageNum: 1, pageSize: 9999 }).then(res => {
|
||||
sheepfoldOptions.value = res.rows || []
|
||||
})
|
||||
}
|
||||
|
||||
//品种列表
|
||||
function getVarietyOptions() {
|
||||
request({
|
||||
url: '/base/variety/list',
|
||||
@@ -173,21 +211,38 @@ function getVarietyOptions() {
|
||||
})
|
||||
}
|
||||
|
||||
/* -------------------- 功能方法 -------------------- */
|
||||
//确保日期格式一致
|
||||
function formatDate(date) {
|
||||
if (!date) return null;
|
||||
const d = new Date(date);
|
||||
const yyyy = d.getFullYear();
|
||||
const mm = String(d.getMonth() + 1).padStart(2, '0');
|
||||
const dd = String(d.getDate()).padStart(2, '0');
|
||||
return `${yyyy}-${mm}-${dd}`;
|
||||
}
|
||||
//提交表单
|
||||
function submitForm() {
|
||||
formRef.value.validate(valid => {
|
||||
if (!valid) return
|
||||
addSheep(form.value).then(res => {
|
||||
if (!valid) return;
|
||||
|
||||
const formData = JSON.parse(JSON.stringify(form.value));
|
||||
|
||||
formData.birthday = formatDate(formData.birthday);
|
||||
formData.joinDate = formatDate(formData.joinDate);
|
||||
|
||||
if (formData.bornWeight) {
|
||||
formData.bornWeight = Number(formData.bornWeight);
|
||||
}
|
||||
|
||||
addSheep(formData).then(res => {
|
||||
if (res.code === 200) {
|
||||
proxy.$modal.msgSuccess('新增成功')
|
||||
resetForm()
|
||||
proxy.$modal.msgSuccess('新增成功');
|
||||
resetForm();
|
||||
} else {
|
||||
proxy.$modal.msgError(res.msg || '新增失败') // 这里显示“耳号已存在”等后端报错
|
||||
proxy.$modal.msgError(res.msg || '新增失败');
|
||||
}
|
||||
}).catch(() => {
|
||||
proxy.$modal.msgError('请求异常,请稍后重试')
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function resetForm() {
|
||||
@@ -206,7 +261,7 @@ function resetForm() {
|
||||
technician: ''
|
||||
}
|
||||
}
|
||||
|
||||
//导出
|
||||
function handleExportForm() {
|
||||
exportSheepForm(form.value).then(res => {
|
||||
const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
|
||||
@@ -239,29 +294,27 @@ function submitUpload() {
|
||||
uploadRef.value?.submit()
|
||||
}
|
||||
|
||||
// 上传成功回调
|
||||
// 导入成功
|
||||
function handleImportSuccess(res) {
|
||||
if (res.code === 200) {
|
||||
proxy.$modal.msgSuccess(res.msg || '导入成功')
|
||||
importOpen.value = false
|
||||
uploadRef.value?.clearFiles()
|
||||
} else {
|
||||
// 业务失败
|
||||
// 失败
|
||||
proxy.$modal.msgError('导入失败:' + (res.msg || '未知原因'))
|
||||
}
|
||||
}
|
||||
|
||||
// 上传失败(网络/服务器异常)
|
||||
// 导入失败(网络/服务器异常)
|
||||
function handleImportError(err) {
|
||||
const msg = JSON.parse(err.message || '{}')?.msg || '网络异常'
|
||||
proxy.$modal.msgError('导入失败:' + msg)
|
||||
}
|
||||
|
||||
|
||||
/* -------------------- 初始化 -------------------- */
|
||||
|
||||
getSheepfoldOptions()
|
||||
getVarietyOptions()
|
||||
getSheepfoldOptions()
|
||||
getVarietyOptions()
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user