Compare commits

..

2 Commits

Author SHA1 Message Date
d5f91b9b12 跨群调栏 2025-10-23 18:41:08 +08:00
a8d9b033f3 优化展示 2025-10-23 18:27:11 +08:00
3 changed files with 272 additions and 16 deletions

View File

@@ -29,20 +29,35 @@
</div>
</div>
<div class="batch-actions">
<el-button size="small" type="primary" @click.stop="showAddPenDialog(batch)" :disabled="!batch.is_active">
增加猪栏
</el-button>
<el-button
size="small"
type="success"
@click.stop="emitTransferPigs(batch)"
:disabled="!batch.is_active || !batch.pens || batch.pens.length < 2"
title="将猪从一个猪栏转移到同一批次的另一个猪栏"
>
群内调栏
</el-button>
<el-button size="small" @click.stop="emitEditBatch(batch)">编辑</el-button>
<el-button size="small" type="danger" @click.stop="emitDeleteBatch(batch)">删除</el-button>
<el-dropdown trigger="click" class="batch-dropdown">
<el-button type="primary" size="small">
管理猪群<el-icon class="el-icon--right"><arrow-down /></el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item @click="showAddPenDialog(batch)" :disabled="!batch.is_active">增加猪栏</el-dropdown-item>
<el-dropdown-item @click="emitEditBatch(batch)">编辑</el-dropdown-item>
<el-dropdown-item @click="emitDeleteBatch(batch)" divided>删除</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
<el-dropdown trigger="click" class="batch-dropdown">
<el-button type="success" size="small">
调栏<el-icon class="el-icon--right"><arrow-down /></el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item
@click="emitTransferPigs(batch)"
:disabled="!batch.is_active || !batch.pens || batch.pens.length < 2"
>群内调栏</el-dropdown-item>
<el-dropdown-item
@click="emitTransferPigsAcrossBatches(batch)"
:disabled="!batch.is_active || !batch.pens || batch.pens.length === 0"
>跨群调栏</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</div>
<div v-if="batch.isExpanded" class="batch-content">
@@ -96,12 +111,14 @@ import PigBatchPenCard from './PigBatchPenCard.vue';
import AllocatePigsDialog from './AllocatePigsDialog.vue';
import {getAllPens, getAllPigHouses, movePigsIntoPen} from '../api/pigBatch';
import {formatRFC3339} from '../utils/format'; // 导入格式化函数
import { ArrowDown } from '@element-plus/icons-vue'; // 导入 ArrowDown 图标
export default {
name: 'PigBatchList',
components: {
PigBatchPenCard,
AllocatePigsDialog
AllocatePigsDialog,
ArrowDown // 注册 ArrowDown 图标
},
props: {
pigBatches: {
@@ -109,7 +126,7 @@ export default {
required: true
}
},
emits: ['edit-batch', 'delete-batch', 'add-pen', 'remove-pen', 'assign-pen-to-batch', 'reload-data', 'transfer-pigs'],
emits: ['edit-batch', 'delete-batch', 'add-pen', 'remove-pen', 'assign-pen-to-batch', 'reload-data', 'transfer-pigs', 'transfer-pigs-across-batches'],
data() {
return {
addPenDialogVisible: false,
@@ -193,6 +210,9 @@ export default {
emitTransferPigs(batch) {
this.$emit('transfer-pigs', batch);
},
emitTransferPigsAcrossBatches(batch) {
this.$emit('transfer-pigs-across-batches', batch);
},
// 猪栏操作
emitRemovePen(pen) {
this.$emit('remove-pen', pen);
@@ -253,6 +273,10 @@ export default {
gap: 10px;
}
.batch-dropdown {
margin-left: 10px; /* 为下拉菜单添加左边距 */
}
.batch-content {
padding: 16px;
border-top: 1px solid #eee;

View File

@@ -0,0 +1,204 @@
<template>
<el-dialog
title="跨群调栏"
:model-value="visible"
@update:model-value="$emit('update:visible', $event)"
width="40%"
@close="resetForm"
>
<el-form ref="form" :model="form" :rules="rules" label-width="120px">
<el-form-item label="源猪群批次">
<span>{{ batch.batch_number }}</span>
</el-form-item>
<el-form-item label="源猪栏" prop="fromPenID">
<el-select v-model="form.fromPenID" placeholder="请选择源猪栏" style="width: 100%;">
<el-option
v-for="pen in sourcePens"
:key="pen.id"
:label="`${pen.pen_number} (存栏: ${pen.current_pig_count})`"
:value="pen.id"
></el-option>
</el-select>
</el-form-item>
<el-form-item label="目标猪群批次" prop="destBatchID">
<el-select v-model="form.destBatchID" placeholder="请选择目标猪群" style="width: 100%;" @change="onDestBatchChange">
<el-option
v-for="b in availableBatches"
:key="b.id"
:label="b.batch_number"
:value="b.id"
></el-option>
</el-select>
</el-form-item>
<el-form-item label="目标猪栏" prop="toPenID">
<el-select v-model="form.toPenID" placeholder="请选择目标猪栏" style="width: 100%;">
<el-option
v-for="pen in destinationPens"
:key="pen.id"
:label="`${pen.pen_number} (容量: ${pen.capacity})`"
:value="pen.id"
></el-option>
</el-select>
</el-form-item>
<el-form-item label="调栏数量" prop="quantity">
<el-input-number v-model="form.quantity" :min="1" :max="maxTransferQuantity" />
</el-form-item>
<el-form-item label="备注" prop="remarks">
<el-input v-model="form.remarks" type="textarea" :rows="2" placeholder="请输入备注" />
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="$emit('update:visible', false)"> </el-button>
<el-button type="primary" @click="handleConfirm"> </el-button>
</span>
</template>
</el-dialog>
</template>
<script>
import { getPigBatches, transferPigsAcrossBatches } from '@/api/pigBatch';
import { getPens } from '@/api/pen';
export default {
name: 'TransferPigsAcrossBatchesModal',
props: {
visible: {
type: Boolean,
required: true
},
batch: {
type: Object,
required: true // 源猪群批次信息
}
},
emits: ['update:visible', 'success'],
data() {
return {
form: {
fromPenID: null,
destBatchID: null,
toPenID: null,
quantity: 1,
remarks: ''
},
rules: {
fromPenID: [{ required: true, message: '请选择源猪栏', trigger: 'change' }],
destBatchID: [{ required: true, message: '请选择目标猪群批次', trigger: 'change' }],
toPenID: [{ required: true, message: '请选择目标猪栏', trigger: 'change' }],
quantity: [
{ required: true, message: '请输入调栏数量', trigger: 'blur' },
{ type: 'integer', message: '请输入整数', trigger: 'blur' },
{ validator: this.validateQuantity, trigger: 'blur' }
]
},
sourcePens: [], // 源猪群的猪栏列表
availableBatches: [], // 可用的目标猪群列表
destinationPens: [], // 目标猪群的猪栏列表
maxTransferQuantity: 1 // 最大可调栏数量
};
},
watch: {
visible(newVal) {
if (newVal) {
this.initData();
}
},
'form.fromPenID': function(newVal) {
if (newVal) {
const selectedPen = this.sourcePens.find(pen => pen.id === newVal);
this.maxTransferQuantity = selectedPen ? selectedPen.current_pig_count : 1;
// 如果当前数量大于最大值,重置数量
if (this.form.quantity > this.maxTransferQuantity) {
this.form.quantity = this.maxTransferQuantity;
}
}
}
},
methods: {
async initData() {
this.resetForm();
this.sourcePens = this.batch.pens.filter(pen => pen.current_pig_count > 0); // 过滤掉没有猪的猪栏
await this.fetchAvailableBatchesAndPens();
},
async fetchAvailableBatchesAndPens() {
try {
const [batchesResponse, pensResponse] = await Promise.all([
getPigBatches({ is_active: true }), // 获取所有活跃猪群
getPens() // 获取所有猪栏
]);
const allBatches = batchesResponse.data || [];
const allPens = pensResponse.data || [];
// 过滤掉源猪群自身,以及非活跃的猪群
this.availableBatches = allBatches.filter(b => b.id !== this.batch.id && b.is_active);
// 将所有猪栏按批次ID分组方便后续查找
const pensByBatch = allPens.reduce((acc, pen) => {
if (pen.pig_batch_id) {
if (!acc[pen.pig_batch_id]) {
acc[pen.pig_batch_id] = [];
}
acc[pen.pig_batch_id].push(pen);
}
return acc;
}, {});
this.pensByBatch = pensByBatch; // 存储起来,以便 onDestBatchChange 使用
} catch (error) {
console.error("Error fetching batches or pens:", error);
this.$message.error("获取猪群或猪栏信息失败");
}
},
onDestBatchChange(batchId) {
this.form.toPenID = null; // 重置目标猪栏选择
// 过滤出目标批次下,且未满的猪栏
this.destinationPens = (this.pensByBatch[batchId] || []).filter(pen => pen.current_pig_count < pen.capacity);
},
validateQuantity(rule, value, callback) {
if (value > this.maxTransferQuantity) {
callback(new Error(`调栏数量不能超过源猪栏存栏数量 (${this.maxTransferQuantity})`));
} else {
callback();
}
},
handleConfirm() {
this.$refs.form.validate(async valid => {
if (valid) {
try {
await transferPigsAcrossBatches(this.batch.id, {
fromPenID: this.form.fromPenID,
destBatchID: this.form.destBatchID,
toPenID: this.form.toPenID,
quantity: this.form.quantity,
remarks: this.form.remarks
});
this.$message.success('跨群调栏成功');
this.$emit('success');
this.$emit('update:visible', false);
} catch (error) {
console.error('Error transferring pigs across batches:', error);
this.$message.error('跨群调栏失败: ' + (error.response?.data?.message || error.message || '未知错误'));
}
}
});
},
resetForm() {
this.$refs.form?.resetFields();
this.form.fromPenID = null;
this.form.destBatchID = null;
this.form.toPenID = null;
this.form.quantity = 1;
this.form.remarks = '';
this.sourcePens = [];
this.availableBatches = [];
this.destinationPens = [];
this.maxTransferQuantity = 1;
}
}
};
</script>
<style scoped>
</style>

View File

@@ -43,6 +43,7 @@
@remove-pen="handleRemovePen"
@assign-pen-to-batch="handleAssignPenToBatch"
@transfer-pigs="handleTransferPigs"
@transfer-pigs-across-batches="handleTransferPigsAcrossBatches"
@reload-data="loadData"
/>
<el-empty v-else description="暂无数据" />
@@ -75,6 +76,13 @@
:batch="currentBatchForTransfer"
@success="handleTransferSuccess"
/>
<!-- 跨群调栏对话框 -->
<TransferPigsAcrossBatchesModal
v-model:visible="transferAcrossBatchesDialogVisible"
:batch="currentBatchForTransferAcrossBatches"
@success="handleTransferAcrossBatchesSuccess"
/>
</div>
</template>
@@ -86,6 +94,7 @@ import PigBatchList from '@/components/PigBatchList.vue';
import PigBatchForm from '@/components/PigBatchForm.vue';
import PenForm from '@/components/PenForm.vue';
import TransferPigsModal from '@/components/TransferPigsModal.vue';
import TransferPigsAcrossBatchesModal from '@/components/TransferPigsAcrossBatchesModal.vue';
import { Refresh } from '@element-plus/icons-vue';
export default {
@@ -95,6 +104,7 @@ export default {
PigBatchForm,
PenForm,
TransferPigsModal,
TransferPigsAcrossBatchesModal,
Refresh
},
data() {
@@ -113,6 +123,9 @@ export default {
// 调栏模态框状态
transferDialogVisible: false,
currentBatchForTransfer: {},
// 跨群调栏模态框状态
transferAcrossBatchesDialogVisible: false,
currentBatchForTransferAcrossBatches: {},
// 辅助映射
houseMap: new Map(), // 用于猪栏显示猪舍名称
};
@@ -222,6 +235,11 @@ export default {
}
},
// --- 猪栏操作 (在猪群管理中) ---
handleAddPen(house) {
this.currentPen = { ...house }; // 修正这里应该是传入house对象而不是pen对象
this.isEditPen = false;
this.penDialogVisible = true;
},
handleModifyPigCountPen(pen) {
this.currentPen = { ...pen };
this.isEditPen = true;
@@ -287,6 +305,16 @@ export default {
handleTransferSuccess() {
this.transferDialogVisible = false;
this.loadData(); // 重新加载数据以反映变化
},
handleTransferPigsAcrossBatches(batch) {
console.log('handleTransferPigsAcrossBatches called with batch:', batch);
this.currentBatchForTransferAcrossBatches = batch;
this.transferAcrossBatchesDialogVisible = true;
console.log('transferAcrossBatchesDialogVisible set to:', this.transferAcrossBatchesDialogVisible);
},
handleTransferAcrossBatchesSuccess() {
this.transferAcrossBatchesDialogVisible = false;
this.loadData(); // 重新加载数据以反映变化
}
}
}