支持分配猪只
This commit is contained in:
80
src/components/AllocatePigsDialog.vue
Normal file
80
src/components/AllocatePigsDialog.vue
Normal file
@@ -0,0 +1,80 @@
|
||||
<template>
|
||||
<el-dialog
|
||||
title="分配猪只"
|
||||
:model-value="visible"
|
||||
@update:model-value="$emit('update:visible', $event)"
|
||||
width="30%"
|
||||
@close="resetForm"
|
||||
>
|
||||
<el-form ref="form" :model="form" :rules="rules" label-width="100px">
|
||||
<el-form-item label="未分配数量">
|
||||
<span>{{ unassignedPigCount }}</span>
|
||||
</el-form-item>
|
||||
<el-form-item label="分配数量" prop="quantity">
|
||||
<el-input-number v-model="form.quantity" :min="1" :max="unassignedPigCount" />
|
||||
</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>
|
||||
export default {
|
||||
name: 'AllocatePigsDialog',
|
||||
props: {
|
||||
visible: {
|
||||
type: Boolean,
|
||||
required: true
|
||||
},
|
||||
unassignedPigCount: {
|
||||
type: Number,
|
||||
required: true
|
||||
},
|
||||
penId: {
|
||||
type: Number,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
emits: ['update:visible', 'confirm'],
|
||||
data() {
|
||||
return {
|
||||
form: {
|
||||
quantity: 1
|
||||
},
|
||||
rules: {
|
||||
quantity: [
|
||||
{ required: true, message: '请输入分配数量', trigger: 'blur' },
|
||||
{ type: 'integer', message: '请输入整数', trigger: 'blur' },
|
||||
{ validator: this.validateQuantity, trigger: 'blur' }
|
||||
]
|
||||
}
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
validateQuantity(rule, value, callback) {
|
||||
if (value > this.unassignedPigCount) {
|
||||
callback(new Error('分配数量不能超过未分配数量'));
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
},
|
||||
handleConfirm() {
|
||||
this.$refs.form.validate(valid => {
|
||||
if (valid) {
|
||||
this.$emit('confirm', { penId: this.penId, quantity: this.form.quantity });
|
||||
this.$emit('update:visible', false);
|
||||
}
|
||||
});
|
||||
},
|
||||
resetForm() {
|
||||
this.$refs.form.resetFields();
|
||||
this.form.quantity = 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
@@ -39,7 +39,7 @@
|
||||
:key="pen.id"
|
||||
:pen="pen"
|
||||
:isBatchActive="batch.is_active"
|
||||
@modify-pig-count="emitModifyPigCountPen"
|
||||
@allocate-pigs="showAllocatePigsDialog($event, batch)"
|
||||
@remove="emitRemovePen"
|
||||
/>
|
||||
</div>
|
||||
@@ -66,18 +66,29 @@
|
||||
</span>
|
||||
</template>
|
||||
</el-dialog>
|
||||
|
||||
<!-- 分配猪只对话框 -->
|
||||
<AllocatePigsDialog
|
||||
v-if="allocatePigsDialogVisible"
|
||||
:visible.sync="allocatePigsDialogVisible"
|
||||
:unassigned-pig-count="currentBatch ? currentBatch.unassigned_pig_count : 0"
|
||||
:pen-id="selectedPenForAllocation ? selectedPenForAllocation.id : 0"
|
||||
@confirm="handleAllocatePigs"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import PigBatchPenCard from './PigBatchPenCard.vue';
|
||||
import { getAllPens, getAllPigHouses } from '../api/pigBatch';
|
||||
import AllocatePigsDialog from './AllocatePigsDialog.vue';
|
||||
import { getAllPens, getAllPigHouses, movePigsIntoPen } from '../api/pigBatch';
|
||||
import { formatRFC3339 } from '../utils/format'; // 导入格式化函数
|
||||
|
||||
export default {
|
||||
name: 'PigBatchList',
|
||||
components: {
|
||||
PigBatchPenCard
|
||||
PigBatchPenCard,
|
||||
AllocatePigsDialog
|
||||
},
|
||||
props: {
|
||||
pigBatches: {
|
||||
@@ -85,13 +96,15 @@ export default {
|
||||
required: true
|
||||
}
|
||||
},
|
||||
emits: ['edit-batch', 'delete-batch', 'add-pen', 'modify-pig-count-pen', 'remove-pen', 'assign-pen-to-batch'],
|
||||
emits: ['edit-batch', 'delete-batch', 'add-pen', 'remove-pen', 'assign-pen-to-batch', 'reload-data'],
|
||||
data() {
|
||||
return {
|
||||
addPenDialogVisible: false,
|
||||
availablePens: [],
|
||||
selectedPenId: null,
|
||||
currentBatch: null // To store the batch for which we are adding a pen
|
||||
currentBatch: null, // To store the batch for which we are adding a pen
|
||||
allocatePigsDialogVisible: false,
|
||||
selectedPenForAllocation: null
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
@@ -138,6 +151,22 @@ export default {
|
||||
this.$message.warning("请选择一个猪栏");
|
||||
}
|
||||
},
|
||||
showAllocatePigsDialog(pen, batch) {
|
||||
this.currentBatch = batch;
|
||||
this.selectedPenForAllocation = pen;
|
||||
this.allocatePigsDialogVisible = true;
|
||||
},
|
||||
async handleAllocatePigs({ penId, quantity }) {
|
||||
try {
|
||||
await movePigsIntoPen(this.currentBatch.id, { toPenID: penId, quantity });
|
||||
this.$message.success('猪只分配成功');
|
||||
this.allocatePigsDialogVisible = false;
|
||||
this.$emit('reload-data'); // 通知父组件重新加载数据
|
||||
} catch (error) {
|
||||
console.error('Error allocating pigs:', error);
|
||||
this.$message.error('分配猪只失败');
|
||||
}
|
||||
},
|
||||
// 猪群操作
|
||||
emitEditBatch(batch) {
|
||||
this.$emit('edit-batch', batch);
|
||||
@@ -146,9 +175,6 @@ export default {
|
||||
this.$emit('delete-batch', batch);
|
||||
},
|
||||
// 猪栏操作
|
||||
emitModifyPigCountPen(pen) {
|
||||
this.$emit('modify-pig-count-pen', pen);
|
||||
},
|
||||
emitRemovePen(pen) {
|
||||
this.$emit('remove-pen', pen);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<div class="info-item border-left">存栏: {{ pen.current_pig_count || 0 }}</div>
|
||||
</div>
|
||||
<div class="actions-section">
|
||||
<el-button size="small" @click="emitModifyPigCount" :disabled="!isBatchActive">修改猪只数量</el-button>
|
||||
<el-button size="small" @click="emitAllocatePigs" :disabled="!isBatchActive">分配猪只</el-button>
|
||||
<el-button size="small" type="danger" @click="emitRemove" :disabled="!isBatchActive">移除</el-button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -31,7 +31,7 @@ export default {
|
||||
default: true // 默认活跃,以防万一没有传递
|
||||
}
|
||||
},
|
||||
emits: ['remove', 'modify-pig-count'],
|
||||
emits: ['remove', 'allocate-pigs'],
|
||||
setup(props, { emit }) {
|
||||
const statusType = computed(() => {
|
||||
switch (props.pen.status) {
|
||||
@@ -48,8 +48,8 @@ export default {
|
||||
}
|
||||
});
|
||||
|
||||
const emitModifyPigCount = () => {
|
||||
emit('modify-pig-count', props.pen);
|
||||
const emitAllocatePigs = () => {
|
||||
emit('allocate-pigs', props.pen);
|
||||
};
|
||||
|
||||
const emitRemove = () => {
|
||||
@@ -58,7 +58,7 @@ export default {
|
||||
|
||||
return {
|
||||
statusType,
|
||||
emitModifyPigCount,
|
||||
emitAllocatePigs,
|
||||
emitRemove
|
||||
};
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
@modify-pig-count-pen="handleModifyPigCountPen"
|
||||
@remove-pen="handleRemovePen"
|
||||
@assign-pen-to-batch="handleAssignPenToBatch"
|
||||
@reload-data="loadData"
|
||||
/>
|
||||
<el-empty v-else description="暂无数据" />
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user