猪群管理让用户选择猪栏

This commit is contained in:
2025-10-23 12:14:39 +08:00
parent 98cd24ee29
commit 70e3a4f2b0
3 changed files with 98 additions and 6 deletions

View File

@@ -191,3 +191,21 @@ export const recordDeathInBatch = (id, deathData) => {
export const recordCullInBatch = (id, cullData) => {
return http.post(`/api/v1/pig-batches/record-cull/${id}`, cullData);
};
// --- 新增的猪栏和猪舍API ---
/**
* 获取所有猪栏的列表
* @returns {Promise<*>}
*/
export const getAllPens = () => {
return http.get('/api/v1/pens');
};
/**
* 获取所有猪舍的列表
* @returns {Promise<*>}
*/
export const getAllPigHouses = () => {
return http.get('/api/v1/pig-houses');
};

View File

@@ -8,7 +8,7 @@
<span>初始数量: {{ batch.initial_count }}</span>
</div>
<div class="batch-actions">
<el-button size="small" type="primary" @click.stop="emitAddPen(batch)">增加猪栏</el-button>
<el-button size="small" type="primary" @click.stop="showAddPenDialog(batch)">增加猪栏</el-button>
<el-button size="small" @click.stop="emitEditBatch(batch)">编辑</el-button>
<el-button size="small" type="danger" @click.stop="emitDeleteBatch(batch)">删除</el-button>
</div>
@@ -28,11 +28,30 @@
</div>
</div>
</div>
<!-- 增加猪栏对话框 -->
<el-dialog title="选择猪栏" v-model="addPenDialogVisible" width="30%">
<el-select v-model="selectedPenId" placeholder="请选择猪栏" style="width: 100%;">
<el-option
v-for="pen in availablePens"
:key="pen.id"
:label="pen.label"
:value="pen.id">
</el-option>
</el-select>
<template #footer>
<span class="dialog-footer">
<el-button @click="addPenDialogVisible = false"> </el-button>
<el-button type="primary" @click="assignPen"> </el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script>
import PigPenInfoCard from './PigPenInfoCard.vue';
import { getAllPens, getAllPigHouses } from '../api/pigBatch'; // Import new API functions
export default {
name: 'PigBatchList',
@@ -45,15 +64,59 @@ export default {
required: true
}
},
emits: ['edit-batch', 'delete-batch', 'add-pen', 'edit-pen', 'delete-pen'],
emits: ['edit-batch', 'delete-batch', 'add-pen', 'edit-pen', 'delete-pen', 'assign-pen-to-batch'],
data() {
return {
addPenDialogVisible: false,
availablePens: [],
selectedPenId: null,
currentBatch: null // To store the batch for which we are adding a pen
};
},
methods: {
toggleExpand(batch) {
batch.isExpanded = !batch.isExpanded;
},
// 猪群操作
emitAddPen(batch) {
this.$emit('add-pen', batch);
async fetchAvailablePens() {
try {
const [pensResponse, housesResponse] = await Promise.all([
getAllPens(),
getAllPigHouses()
]);
const pens = pensResponse.data;
const houses = housesResponse.data;
// Create a map for quick lookup of house names by ID
const houseMap = new Map(houses.map(house => [house.id, house.name]));
this.availablePens = pens.map(pen => ({
id: pen.id,
label: `${pen.pen_number}(${houseMap.get(pen.house_id) || '未知猪舍'})` // Changed format here
}));
} catch (error) {
console.error("Error fetching pens or houses:", error);
this.$message.error("获取猪栏或猪舍信息失败");
}
},
showAddPenDialog(batch) {
this.currentBatch = batch;
this.selectedPenId = null; // Reset selection
this.fetchAvailablePens();
this.addPenDialogVisible = true;
},
assignPen() {
if (this.selectedPenId && this.currentBatch) {
this.$emit('assign-pen-to-batch', {
batchId: this.currentBatch.id,
penId: this.selectedPenId
});
this.addPenDialogVisible = false;
} else {
this.$message.warning("请选择一个猪栏");
}
},
// 猪群操作
emitEditBatch(batch) {
this.$emit('edit-batch', batch);
},

View File

@@ -41,6 +41,7 @@
@add-pen="handleAddPen"
@edit-pen="handleEditPen"
@delete-pen="handleDeletePen"
@assign-pen-to-batch="handleAssignPenToBatch"
/>
<el-empty v-else description="暂无数据" />
</div>
@@ -68,7 +69,7 @@
</template>
<script>
import { getPigBatches, deletePigBatch } from '@/api/pigBatch.js';
import { getPigBatches, deletePigBatch, assignPensToBatch } from '@/api/pigBatch.js'; // Import assignPensToBatch
import { getPens, deletePen } from '@/api/pen.js';
import { getPigHouses } from '@/api/pigHouse.js';
import PigBatchList from '@/components/PigBatchList.vue';
@@ -253,6 +254,16 @@ export default {
this.$message.success('猪栏添加成功');
}
}
},
async handleAssignPenToBatch({ batchId, penId }) {
try {
await assignPensToBatch(batchId, { pen_ids: [penId] });
this.$message.success('猪栏分配成功');
await this.loadData(); // Refresh data to show assigned pen
} catch (error) {
this.$message.error('分配猪栏失败: ' + (error.response?.data?.message || error.message || '未知错误'));
console.error('Failed to assign pen to batch:', error);
}
}
}
}