feat|fix(views): 原料管理面板 | 下拉列表请求逻辑修正

+ 添加原料管理面板以支持手动原料增删改
+ 对`配方管理``饲喂统计`部分有关原料列表获取函数的传参错误做出修正
    使用materialQueryParams.value, 解决引用错误导致后端无法正确收到查询参数
    这将导致分页参数被错误置为默认10,从而使下拉列表无法显示全部原料
This commit is contained in:
2026-01-18 14:50:06 +08:00
parent e43456cb25
commit 2e59c07704
3 changed files with 763 additions and 479 deletions

View File

@@ -201,7 +201,7 @@
import { listFeedStatistic, getFeedStatistic, delFeedStatistic, addFeedStatistic, updateFeedStatistic, initFeedStatistic } from "@/api/feed/FeedStatistic" import { listFeedStatistic, getFeedStatistic, delFeedStatistic, addFeedStatistic, updateFeedStatistic, initFeedStatistic } from "@/api/feed/FeedStatistic"
import { listFormulaManagement } from "@/api/feed/FormulaManagement" import { listFormulaManagement } from "@/api/feed/FormulaManagement"
import { listSheepfold } from "@/api/produce/manage_sheep/trans_group.js"; import { listSheepfold } from "@/api/produce/manage_sheep/trans_group.js";
import { listMaterial } from "@/api/feed/Material" import { listMaterial } from "@/api/feed/material"
import { ref } from "vue"; import { ref } from "vue";
const { proxy } = getCurrentInstance() const { proxy } = getCurrentInstance()
@@ -263,10 +263,12 @@
const materialQueryParams = ref({ const materialQueryParams = ref({
materialId: null, materialId: null,
materialName: null, materialName: null,
isGranular: null isGranular: null,
pageNum: 1,
pageSize: 99999
}) })
function getMaterials() { function getMaterials() {
listMaterial(materialQueryParams).then(response => { listMaterial(materialQueryParams.value).then(response => {
console.log("获取原料列表:", response) console.log("获取原料列表:", response)
materials.value = response.rows.map(item => ({ materials.value = response.rows.map(item => ({
value: item.materialId, value: item.materialId,
@@ -282,7 +284,7 @@
// 从后端获取数据并按isGranular分组 // 从后端获取数据并按isGranular分组
function getGroupMaterials() { function getGroupMaterials() {
listMaterial(materialQueryParams).then(response => { listMaterial(materialQueryParams.value).then(response => {
console.log("获取原料列表:", response) console.log("获取原料列表:", response)
// 根据material_type分组数据 // 根据material_type分组数据

View File

@@ -149,14 +149,14 @@
<el-table-column label="序号" type="index" width="60" align="center" /> <el-table-column label="序号" type="index" width="60" align="center" />
<el-table-column label="原料编号" prop="materialId" align="center" /> <el-table-column label="原料编号" prop="materialId" align="center" />
<el-table-column label="原料" align="center"> <el-table-column label="原料" align="center">
<el-table-column label="名称" prop="materialName" align="center"/> <el-table-column label="名称" prop="materialName" align="center" />
<el-table-column label="类型" align="center" prop="isGranular"> <el-table-column label="类型" align="center" prop="isGranular">
<template #default="scope"> <template #default="scope">
<dict-tag :options="material_type" :value="scope.row.isGranular" /> <dict-tag :options="material_type" :value="scope.row.isGranular" />
</template> </template>
</el-table-column> </el-table-column>
</el-table-column> </el-table-column>
<el-table-column label="比例" align="center" prop="ratio" sortable/> <el-table-column label="比例" align="center" prop="ratio" sortable />
<el-table-column label="类型" prop="isSupplement" align="center"> <el-table-column label="类型" prop="isSupplement" align="center">
<template #default="scope"> <template #default="scope">
<dict-tag :options="materialType" :value="scope.row.isSupplement" /> <dict-tag :options="materialType" :value="scope.row.isSupplement" />
@@ -279,53 +279,53 @@
</template> </template>
<script setup> <script setup>
import { listFormulaManagement, getFormulaManagement, delFormulaManagement, addFormulaManagement, updateFormulaManagement } from "@/api/feed/FormulaManagement" import { listFormulaManagement, getFormulaManagement, delFormulaManagement, addFormulaManagement, updateFormulaManagement } from "@/api/feed/FormulaManagement"
import { ref, reactive, toRefs, getCurrentInstance } from 'vue' import { ref, reactive, toRefs, getCurrentInstance } from 'vue'
import { listFormulaList, getFormulaList, delFormulaList, addFormulaList, updateFormulaList } from "@/api/feed/FormulaList" import { listFormulaList, getFormulaList, delFormulaList, addFormulaList, updateFormulaList } from "@/api/feed/FormulaList"
import { listMaterial } from "@/api/feed/Material" import { listMaterial } from "@/api/feed/material"
import { fa } from "element-plus/es/locales.mjs" import { fa } from "element-plus/es/locales.mjs"
const { proxy } = getCurrentInstance() const { proxy } = getCurrentInstance()
const { pres_status } = proxy.useDict('pres_status') const { pres_status } = proxy.useDict('pres_status')
// Data and refs // Data and refs
const FormulaManagementList = ref([]) const FormulaManagementList = ref([])
const showFormulaManagementList = ref([]) const showFormulaManagementList = ref([])
const FormulaDetailList = ref([]) const FormulaDetailList = ref([])
const open = ref(false) const open = ref(false)
const loading = ref(true) const loading = ref(true)
const showSearch = ref(true) const showSearch = ref(true)
const ids = ref([]) const ids = ref([])
const single = ref(true) const single = ref(true)
const multiple = ref(true) const multiple = ref(true)
const total = ref(0) const total = ref(0)
const title = ref("") const title = ref("")
const expandedRows = ref([]) // Track expanded rows const expandedRows = ref([]) // Track expanded rows
const isView = ref(false) const isView = ref(false)
/** /**
* add : 添加配方 * add : 添加配方
* update : 修改配方 * update : 修改配方
*/ */
const operationType = ref("") // 操作类型add 或 update const operationType = ref("") // 操作类型add 或 update
// 控制批号是否可编辑 // 控制批号是否可编辑
const BatchIdEditable = ref(false) const BatchIdEditable = ref(false)
const FormulaIdEditable = ref(false) const FormulaIdEditable = ref(false)
// 原料下拉列表 // 原料下拉列表
const materials = ref([]) const materials = ref([])
// 原料类型 // 原料类型
const materialType = [ const materialType = [
{ value: '1', label: '补饲原料' }, { value: '1', label: '补饲原料' },
{ value: '0', label: '原料' } { value: '0', label: '原料' }
] ]
// 原料类型字典 // 原料类型字典
const { material_type } = proxy.useDict('material_type') const { material_type } = proxy.useDict('material_type')
console.log("material_type: ", material_type); console.log("material_type: ", material_type);
const useStaus = [ const useStaus = [
{ value: '1', label: '启用' }, { value: '1', label: '启用' },
{ value: '0', label: '停用' } { value: '0', label: '停用' }
] ]
const checkedSwPresDetail = ref([]) // 子表选中的行序号数组 const checkedSwPresDetail = ref([]) // 子表选中的行序号数组
const data = reactive({ const data = reactive({
form: {}, form: {},
queryParams: { queryParams: {
pageNum: 1, pageNum: 1,
@@ -344,20 +344,20 @@ const data = reactive({
{ required: true, message: "比例不能为空", trigger: "blur" }, { required: true, message: "比例不能为空", trigger: "blur" },
] ]
} }
}) })
const { queryParams, form, rules } = toRefs(data) const { queryParams, form, rules } = toRefs(data)
const formulaForm = ref({ const formulaForm = ref({
materialId: '', materialId: '',
materialName: '', materialName: '',
ratio: '', ratio: '',
isGranular: false isGranular: false
}) })
const formulaListOpen = ref(false) const formulaListOpen = ref(false)
/** 查询配方管理列表 */ /** 查询配方管理列表 */
function getList() { function getList() {
loading.value = true loading.value = true
listFormulaManagement(queryParams.value).then(response => { listFormulaManagement(queryParams.value).then(response => {
FormulaManagementList.value = response.rows FormulaManagementList.value = response.rows
@@ -365,10 +365,10 @@ function getList() {
loading.value = false loading.value = false
console.log("配方管理列表数据:", FormulaManagementList.value) console.log("配方管理列表数据:", FormulaManagementList.value)
}) })
} }
// 处理展开行的变化 // 处理展开行的变化
function handleExpandChange(row, expandedRows) { function handleExpandChange(row, expandedRows) {
// 收起其他行 // 收起其他行
expandedRows.forEach(r => { expandedRows.forEach(r => {
if (r !== row) { if (r !== row) {
@@ -382,18 +382,18 @@ function handleExpandChange(row, expandedRows) {
loading.value = true loading.value = true
getList(); getList();
} }
} }
// 取消按钮 // 取消按钮
function cancel() { function cancel() {
open.value = false open.value = false
formulaListOpen.value = false formulaListOpen.value = false
formulaFormReset() formulaFormReset()
reset() reset()
} }
// 表单重置 // 表单重置
function reset() { function reset() {
form.value = { form.value = {
formulaId: null, formulaId: null,
feedStage: null, feedStage: null,
@@ -404,29 +404,29 @@ function reset() {
remark: null remark: null
} }
proxy.resetForm("FormulaManagementRef") proxy.resetForm("FormulaManagementRef")
} }
/** 搜索按钮操作 */ /** 搜索按钮操作 */
function handleQuery() { function handleQuery() {
queryParams.value.pageNum = 1 queryParams.value.pageNum = 1
getList() getList()
} }
/** 重置按钮操作 */ /** 重置按钮操作 */
function resetQuery() { function resetQuery() {
proxy.resetForm("queryRef") proxy.resetForm("queryRef")
handleQuery() handleQuery()
} }
// 多选框选中数据 // 多选框选中数据
function handleSelectionChange(selection) { function handleSelectionChange(selection) {
ids.value = selection.map(item => item.formulaId) ids.value = selection.map(item => item.formulaId)
single.value = selection.length != 1 single.value = selection.length != 1
multiple.value = !selection.length multiple.value = !selection.length
} }
/** 新增按钮操作 */ /** 新增按钮操作 */
function handleAddFormulatemplate() { function handleAddFormulatemplate() {
formulaFormReset() formulaFormReset()
reset() reset()
operationType.value = "add" operationType.value = "add"
@@ -455,10 +455,10 @@ function handleAddFormulatemplate() {
} }
formulaListOpen.value = true formulaListOpen.value = true
title.value = "添加配方管理模板" title.value = "添加配方管理模板"
} }
/** 修改按钮操作 */ /** 修改按钮操作 */
function handleFormulaTemplateUpdate(row) { function handleFormulaTemplateUpdate(row) {
reset() reset()
operationType.value = "update" operationType.value = "update"
const _id = row.formulaId || ids.value const _id = row.formulaId || ids.value
@@ -480,10 +480,10 @@ function handleFormulaTemplateUpdate(row) {
title.value = "修改处方" title.value = "修改处方"
isView.value = false isView.value = false
}) })
} }
/** 提交按钮 */ /** 提交按钮 */
function submitForm() { function submitForm() {
proxy.$refs["FormulaManagementRef"].validate(valid => { proxy.$refs["FormulaManagementRef"].validate(valid => {
if (valid) { if (valid) {
if (form.value.formulaId != null) { if (form.value.formulaId != null) {
@@ -501,10 +501,10 @@ function submitForm() {
} }
} }
}) })
} }
/** 删除按钮操作 */ /** 删除按钮操作 */
function handleDelete(row) { function handleDelete(row) {
const _formulaIds = row.formulaId || ids.value const _formulaIds = row.formulaId || ids.value
proxy.$modal.confirm('是否确认删除配方管理编号为"' + _formulaIds + '"的数据项?').then(function () { proxy.$modal.confirm('是否确认删除配方管理编号为"' + _formulaIds + '"的数据项?').then(function () {
return delFormulaManagement(_formulaIds) return delFormulaManagement(_formulaIds)
@@ -512,10 +512,10 @@ function handleDelete(row) {
getList() getList()
proxy.$modal.msgSuccess("删除成功") proxy.$modal.msgSuccess("删除成功")
}).catch(() => { }) }).catch(() => { })
} }
/** ================= 配方列表操作 ================= */ /** ================= 配方列表操作 ================= */
function handleAddSgFormulaList(row) { function handleAddSgFormulaList(row) {
formulaFormReset() formulaFormReset()
reset() reset()
operationType.value = "add" operationType.value = "add"
@@ -542,10 +542,10 @@ function handleAddSgFormulaList(row) {
} }
formulaListOpen.value = true formulaListOpen.value = true
title.value = "添加配方" title.value = "添加配方"
} }
/** 修改按钮操作 */ /** 修改按钮操作 */
function handleFormulaUpdate(row) { function handleFormulaUpdate(row) {
formulaFormReset() formulaFormReset()
const _code = row.code || ids.value const _code = row.code || ids.value
getFormulaList(_code).then(response => { getFormulaList(_code).then(response => {
@@ -560,10 +560,10 @@ function handleFormulaUpdate(row) {
FormulaIdEditable.value = false FormulaIdEditable.value = false
title.value = "修改配方列表" title.value = "修改配方列表"
}) })
} }
/** 删除按钮操作 */ /** 删除按钮操作 */
function handleFormulaDelete(row) { function handleFormulaDelete(row) {
const _codes = row.formulaId + "/" + row.batchId const _codes = row.formulaId + "/" + row.batchId
proxy.$modal.confirm('是否确认删除配方列表编号为"' + _codes + '"的数据项?').then(function () { proxy.$modal.confirm('是否确认删除配方列表编号为"' + _codes + '"的数据项?').then(function () {
return delFormulaManagement(_codes) return delFormulaManagement(_codes)
@@ -571,10 +571,10 @@ function handleFormulaDelete(row) {
getList() getList()
proxy.$modal.msgSuccess("删除成功") proxy.$modal.msgSuccess("删除成功")
}).catch(() => { }) }).catch(() => { })
} }
/** 提交按钮 */ /** 提交按钮 */
function submitFormulaListForm() { function submitFormulaListForm() {
proxy.$refs["FormulaManagementRef"].validate(valid => { proxy.$refs["FormulaManagementRef"].validate(valid => {
if (valid) { if (valid) {
if (showFormulaManagementList.value.formulaId != null && operationType.value === "update") { if (showFormulaManagementList.value.formulaId != null && operationType.value === "update") {
@@ -592,10 +592,10 @@ function submitFormulaListForm() {
} }
} }
}) })
} }
// 原料下拉选择变化时,更新对应的原料名称 // 原料下拉选择变化时,更新对应的原料名称
function handleMaterialChange(row, selectedValue) { function handleMaterialChange(row, selectedValue) {
const selectedItem = this.materials.find(item => item.value === selectedValue); const selectedItem = this.materials.find(item => item.value === selectedValue);
if (selectedItem) { if (selectedItem) {
// materialName 也在 row 中 // materialName 也在 row 中
@@ -603,13 +603,13 @@ function handleMaterialChange(row, selectedValue) {
row.isGranular = selectedItem.isGranular; // 更新 isGranular row.isGranular = selectedItem.isGranular; // 更新 isGranular
row.isSupplement = selectedItem.isSupplement; // 更新 isSupplement row.isSupplement = selectedItem.isSupplement; // 更新 isSupplement
} }
} }
/** /**
* 查看详情 * 查看详情
* 根据 id 拉取单条记录及子表数据,弹窗只读展示 * 根据 id 拉取单条记录及子表数据,弹窗只读展示
*/ */
function handleView(row) { function handleView(row) {
reset() reset()
const _id = row.formulaId || ids.value const _id = row.formulaId || ids.value
const batchId = row.batchId const batchId = row.batchId
@@ -630,13 +630,13 @@ function handleView(row) {
console.log("handleView showFormulaManagementList: ", showFormulaManagementList.value); console.log("handleView showFormulaManagementList: ", showFormulaManagementList.value);
console.log("handleView response: ", response); console.log("handleView response: ", response);
}) })
} }
/** /**
* 修改按钮 * 修改按钮
* 与查看详情共用接口,但弹窗可编辑 * 与查看详情共用接口,但弹窗可编辑
*/ */
function handleFormulaDetailUpdate(row) { function handleFormulaDetailUpdate(row) {
reset() reset()
operationType.value = "update" operationType.value = "update"
const _id = row.formulaId || ids.value const _id = row.formulaId || ids.value
@@ -657,10 +657,10 @@ function handleFormulaDetailUpdate(row) {
title.value = "修改配方" title.value = "修改配方"
isView.value = false isView.value = false
}) })
} }
// 添加项 // 添加项
function handleAddSgFormulaListDetail() { function handleAddSgFormulaListDetail() {
showFormulaManagementList.value.sgFormulaList.push({ showFormulaManagementList.value.sgFormulaList.push({
materialId: null, materialId: null,
materialName: null, materialName: null,
@@ -668,16 +668,16 @@ function handleAddSgFormulaListDetail() {
isGranular: "0", isGranular: "0",
isSupplement: "0" isSupplement: "0"
}) })
} }
// 删除项 // 删除项
function deleteFormulaListDetail(row) { function deleteFormulaListDetail(row) {
const index = showFormulaManagementList.value.sgFormulaList.findIndex(item => item.index === row.index) const index = showFormulaManagementList.value.sgFormulaList.findIndex(item => item.index === row.index)
if (index !== -1) { if (index !== -1) {
showFormulaManagementList.value.sgFormulaList.splice(index, 1) showFormulaManagementList.value.sgFormulaList.splice(index, 1)
} }
} }
// 表单重置 // 表单重置
function formulaFormReset() { function formulaFormReset() {
form.value = { form.value = {
code: null, code: null,
formulaId: null, formulaId: null,
@@ -688,17 +688,18 @@ function formulaFormReset() {
isSupplement: null isSupplement: null
} }
proxy.resetForm("FormulaListRef") proxy.resetForm("FormulaListRef")
} }
// 原料列表查询参数 // 原料列表查询参数
const materialQueryParams = ref({ const materialQueryParams = ref({
pageNum: 1,
pageSize: 30,
materialId: null, materialId: null,
materialName: null, materialName: null,
isGranular: null isGranular: null
}) })
function getMaterials() { function getMaterials() {
listMaterial(materialQueryParams).then(response => { listMaterial(materialQueryParams.value).then(response => {
console.log("获取原料列表:", response)
materials.value = response.rows.map(item => ({ materials.value = response.rows.map(item => ({
value: item.materialId, value: item.materialId,
label: item.materialName, label: item.materialName,
@@ -707,64 +708,67 @@ function getMaterials() {
})) }))
console.log("原料列表:", materials.value) console.log("原料列表:", materials.value)
}) })
} }
/** ================== 分组下拉列表部分 ======================== */ /** ================== 分组下拉列表部分 ======================== */
const groupedMaterials = ref([]) // 存储分组后的原料数据 const groupedMaterials = ref([]) // 存储分组后的原料数据
// 从后端获取数据并按isGranular分组 // 从后端获取数据并按isGranular分组
function getGroupMaterials() { function getGroupMaterials() {
listMaterial(materialQueryParams).then(response => { listMaterial(materialQueryParams.value).then(response => {
console.log("获取原料列表:", response) console.log("获取原料列表:", response)
// 根据material_type分组数据
const groups = material_type.value.map(type => ({ const groups = material_type.value.map(type => ({
label: type.label, label: type.label,
value: type.value,
options: [] options: []
})) }))
console.log("初始化分组:", groups)
// 遍历原料数据根据isGranular字段分组
response.rows.forEach(item => { response.rows.forEach(item => {
const group = groups.find(group => group.label === material_type.value.find(type => type.value === item.isGranular).label) const group = groups.find(
g => String(g.value) === String(item.isGranular)
)
if (group) {
group.options.push({ group.options.push({
value: item.materialId, value: item.materialId,
label: item.materialName label: item.materialName
}) })
}
}) })
// 将分组后的数据赋值给groupedMaterials // 将分组后的数据赋值给groupedMaterials
groupedMaterials.value = groups groupedMaterials.value = groups
console.log("分组后的原料列表:", groupedMaterials.value) console.log("分组后的原料列表:", groupedMaterials.value)
}) })
} }
/** /**
* 子表多选回调 * 子表多选回调
* 记录被选中的行序号数组 * 记录被选中的行序号数组
*/ */
function handleSgFormulaListDetailSelectionChange(selection) { function handleSgFormulaListDetailSelectionChange(selection) {
checkedSwPresDetail.value = selection.map(item => item.index) checkedSwPresDetail.value = selection.map(item => item.index)
} }
/** /**
* 子表行序号计算 * 子表行序号计算
* 通过 el-table 的 row-class-name 钩子给每一行写入 index从 1 开始) * 通过 el-table 的 row-class-name 钩子给每一行写入 index从 1 开始)
*/ */
function rowSgFormulaListDetailIndex({ row, rowIndex }) { function rowSgFormulaListDetailIndex({ row, rowIndex }) {
row.index = rowIndex + 1 row.index = rowIndex + 1
} }
/** ================================= */ /** ================================= */
/** 导出按钮操作 */ /** 导出按钮操作 */
function handleExport() { function handleExport() {
proxy.download('feed/FormulaManagement/export', { proxy.download('feed/FormulaManagement/export', {
...queryParams.value ...queryParams.value
}, `FormulaManagement_${new Date().getTime()}.xlsx`) }, `FormulaManagement_${new Date().getTime()}.xlsx`)
} }
// 初始化加载数据 // 初始化加载数据
getList() getList()
// 加载原料列表 // 加载原料列表
getMaterials() getMaterials()
// 加载分组原料列表 // 加载分组原料列表
getGroupMaterials() getGroupMaterials()
</script> </script>

View File

@@ -0,0 +1,278 @@
<template>
<div class="app-container">
<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<el-form-item label="原料编码" prop="materialId">
<el-input
v-model="queryParams.materialId"
placeholder="请输入原料编码"
clearable
@keyup.enter="handleQuery"
/>
</el-form-item>
<el-form-item label="原料名称" prop="materialName">
<el-input
v-model="queryParams.materialName"
placeholder="请输入原料名称"
clearable
@keyup.enter="handleQuery"
/>
</el-form-item>
<el-form-item label="颗粒料" prop="isGranular">
<el-select v-model="queryParams.isGranular" placeholder="请选择颗粒料" clearable>
<el-option
v-for="dict in material_type"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
<el-button icon="Refresh" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button
type="primary"
plain
icon="Plus"
@click="handleAdd"
v-hasPermi="['feed:material:add']"
>新增</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="success"
plain
icon="Edit"
:disabled="single"
@click="handleUpdate"
v-hasPermi="['feed:material:edit']"
>修改</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="danger"
plain
icon="Delete"
:disabled="multiple"
@click="handleDelete"
v-hasPermi="['feed:material:remove']"
>删除</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="warning"
plain
icon="Download"
@click="handleExport"
v-hasPermi="['feed:material:export']"
>导出</el-button>
</el-col>
<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
<el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="原料编码" align="center" prop="materialId" />
<el-table-column label="原料名称" align="center" prop="materialName" />
<el-table-column label="颗粒料" align="center" prop="isGranular">
<template #default="scope">
<dict-tag :options="material_type" :value="scope.row.isGranular"/>
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template #default="scope">
<el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['feed:material:edit']">修改</el-button>
<el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['feed:material:remove']">删除</el-button>
</template>
</el-table-column>
</el-table>
<pagination
v-show="total>0"
:total="total"
v-model:page="queryParams.pageNum"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
<!-- 添加或修改原料对话框 -->
<el-dialog :title="title" v-model="open" width="500px" append-to-body>
<el-form ref="materialRef" :model="form" :rules="rules" label-width="80px">
<el-form-item label="原料编码" prop="materialId">
<el-input v-model="form.materialId" placeholder="请输入原料编码" :disabled="option==='edit'"/>
</el-form-item>
<el-form-item label="原料名称" prop="materialName">
<el-input v-model="form.materialName" placeholder="请输入原料名称" />
</el-form-item>
<el-form-item label="颗粒料" prop="isGranular">
<el-select v-model="form.isGranular" placeholder="请选择颗粒料">
<el-option
v-for="dict in material_type"
:key="dict.value"
:label="dict.label"
:value="parseInt(dict.value)"
></el-option>
</el-select>
</el-form-item>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button type="primary" @click="submitForm"> </el-button>
<el-button @click="cancel"> </el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup name="Material">
import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial } from "@/api/feed/material"
const { proxy } = getCurrentInstance()
const { material_type } = proxy.useDict('material_type')
const materialList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const option = ref("")
const data = reactive({
form: {},
queryParams: {
pageNum: 1,
pageSize: 10,
materialId: null,
materialName: null,
isGranular: null
},
rules: {
materialId: [
{ required: true, message: "原料编码不能为空", trigger: "blur" }
],
materialName: [
{ required: true, message: "原料名称不能为空", trigger: "blur" }
],
}
})
const { queryParams, form, rules } = toRefs(data)
/** 查询原料列表 */
function getList() {
loading.value = true
listMaterial(queryParams.value).then(response => {
materialList.value = response.rows
total.value = response.total
loading.value = false
})
}
// 取消按钮
function cancel() {
open.value = false
reset()
}
// 表单重置
function reset() {
form.value = {
materialId: null,
materialName: null,
isGranular: null
}
proxy.resetForm("materialRef")
option.value = ""
}
/** 搜索按钮操作 */
function handleQuery() {
queryParams.value.pageNum = 1
getList()
}
/** 重置按钮操作 */
function resetQuery() {
proxy.resetForm("queryRef")
handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
ids.value = selection.map(item => item.materialId)
single.value = selection.length != 1
multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
reset()
open.value = true
title.value = "添加原料"
option.value = "add"
}
/** 修改按钮操作 */
function handleUpdate(row) {
reset()
const _materialId = row.materialId || ids.value
option.value = "edit"
getMaterial(_materialId).then(response => {
form.value = response.data
open.value = true
title.value = "修改原料"
})
}
/** 提交按钮 */
function submitForm() {
proxy.$refs["materialRef"].validate(valid => {
if (valid) {
if (option.value === "edit") {
updateMaterial(form.value).then(response => {
proxy.$modal.msgSuccess("修改成功")
open.value = false
getList()
})
} else if(option.value === "add") {
addMaterial(form.value).then(response => {
proxy.$modal.msgSuccess("新增成功")
open.value = false
getList()
})
}
}
})
}
/** 删除按钮操作 */
function handleDelete(row) {
const _materialIds = row.materialId || ids.value
proxy.$modal.confirm('是否确认删除原料编号为"' + _materialIds + '"的数据项?').then(function() {
return delMaterial(_materialIds)
}).then(() => {
getList()
proxy.$modal.msgSuccess("删除成功")
}).catch(() => {})
}
/** 导出按钮操作 */
function handleExport() {
proxy.download('feed/material/export', {
...queryParams.value
}, `material_${new Date().getTime()}.xlsx`)
}
getList()
</script>