Files
34047007@qq.com b95053c52c init: 初始化 dpb 桃育种系统代码库
前后端 + 后端 FastAPI 全量源码、部署脚本与文档。
2026-08-06 00:17:49 +08:00

564 lines
15 KiB
Plaintext

<!-- 试验地管理 CRUD:基于 site 模板改写,含所属基地(site)下拉;site_name 由后端联表填充 -->
<template>
<div class="fa-full-height">
<FaSearchBar
v-show="showSearchBar"
ref="searchBarRef"
v-model="searchForm"
:items="plotSearchItems"
:rules="searchBarRules"
:is-expand="false"
:show-expand="true"
:show-reset="true"
:show-search="true"
:disabled-search="false"
:default-expanded="false"
include-audit
@search="handleSearch"
@reset="onResetSearch"
/>
<ElCard class="fa-table-card" :style="{ 'margin-top': showSearchBar ? '12px' : '0' }">
<FaTableHeader
v-model:columns="columnChecks"
v-model:showSearchBar="showSearchBar"
:loading="loading"
@refresh="refreshData"
>
<template #left>
<FaTableHeaderLeft
:remove-ids="selectedIds"
:perm-create="['module_bre:plot:create']"
:perm-import="['module_bre:plot:import']"
:perm-export="['module_bre:plot:export']"
:perm-delete="['module_bre:plot:delete']"
:delete-loading="batchDeleting"
:create-loading="createLoading"
@add="handleAdd"
@import="openImport"
@export="openExport"
@delete="handleBatchDelete"
/>
</template>
</FaTableHeader>
<FaTable
ref="faTableRef"
:loading="loading"
:data="data"
:columns="columns"
:pagination="pagination"
@selection-change="onTableSelectionChange"
@pagination:size-change="handleSizeChange"
@pagination:current-change="handleCurrentChange"
/>
</ElCard>
<FaDialog
v-model="dialogVisible.visible"
:title="dialogVisible.title"
width="920px"
dialog-class="crud-embed-dialog"
modal-class="crud-embed-dialog"
:form-mode="dialogVisible.type"
:confirm-loading="submitLoading"
:close-on-click-modal="false"
@cancel="crud.handleCloseDialog"
@close="crud.handleCloseDialog"
@confirm="crud.handleSubmit"
>
<template v-if="dialogVisible.type === 'detail'">
<FaDescriptions
:column="4"
:data="detailFormData"
:items="plotDetailItems"
max-height="70vh"
/>
</template>
<template v-else>
<FaForm
:key="plotFormRenderKey"
scrollbar
max-height="70vh"
ref="dataFormRef"
v-model="formData"
:items="plotDialogFormItems"
:rules="rules"
label-suffix=":"
:label-width="120"
label-position="right"
:span="12"
:gutter="16"
:show-reset="false"
:show-submit="false"
class="crud-dialog-art-form"
/>
</template>
</FaDialog>
<FaImportDialog
v-model="importVisible"
:content-config="plotImportContentConfig"
default-template-file-name="plot_import_template.xlsx"
@upload="handleCrudImportUpload"
/>
<FaExportDialog
v-model="exportVisible"
:content-config="plotExportContentConfig"
:query-params="exportQueryParams"
:page-data="data"
:selection-data="selectedRows"
/>
</div>
</template>
<script setup lang="ts">
import type { TableOperationAction } from "@/utils/table";
import { renderTableOperationCell, stripPaginationParams, toCrudCols } from "@utils";
import { useCrudForm } from "@/hooks/core/useCrudForm";
import { ResultEnum } from "@/enums/api/result.enum";
import type { IContentConfig, IObject } from "@/components/modal/types";
import type { AuditSearchFormParams } from "@/components/forms/fa-search-bar/auditSearchFormItems";
import type { FormItem } from "@/components/forms/fa-form/index.vue";
import SiteAPI, { type SiteOption } from "@/api/module_bre/site";
import PlotAPI, { type PlotForm, type PlotPageQuery, type PlotTable } from "@/api/module_bre/plot";
import type { ColumnOption } from "@/types/component";
import FaDescriptions from "@/components/others/fa-descriptions/index.vue";
import FaForm from "@/components/forms/fa-form/index.vue";
import FaTableHeader from "@/components/tables/fa-table-header/index.vue";
defineOptions({
name: "Plot",
inheritAttrs: false,
});
// 行向固定选项
const ROW_ORIENTATION_OPTIONS = [
{ label: "南北行", value: "南北行" },
{ label: "东西行", value: "东西行" },
] as const;
// 所属基地下拉
const siteOptions = ref<SiteOption[]>([]);
const siteOptionsMap = computed(() => {
const m: Record<number, string> = {};
for (const o of siteOptions.value) m[o.value] = o.label;
return m;
});
async function loadSiteOptions() {
try {
const res = await SiteAPI.getSiteOptions();
if (res.data.code === ResultEnum.SUCCESS) {
siteOptions.value = res.data.data ?? [];
}
} catch (error: unknown) {
if (import.meta.env.DEV) console.error("[Plot] loadSiteOptions", error);
}
}
const createInitialFormData = (): PlotForm => ({
id: undefined,
site_id: undefined,
plot_code: "",
row_orientation: undefined,
row_count: undefined,
col_count: undefined,
grid_note: undefined,
area: undefined,
});
type PlotSearchFormParams = {
site_id?: number;
plot_code?: string;
row_orientation?: string;
} & AuditSearchFormParams;
const searchForm = ref<PlotSearchFormParams>({
site_id: undefined,
plot_code: undefined,
row_orientation: undefined,
created_id: undefined,
updated_id: undefined,
created_time: [],
updated_time: [],
});
const showSearchBar = ref(true);
const searchBarRef = ref<{ validate: () => Promise<boolean> } | null>(null);
const searchBarRules: Record<string, unknown> = {};
const plotSearchItems = computed(() => [
{
label: "所属基地",
key: "site_id",
type: "select",
props: {
placeholder: "请选择基地",
options: siteOptions.value,
clearable: true,
filterable: true,
},
span: 8,
},
{
label: "地块编号",
key: "plot_code",
type: "input",
placeholder: "请输入地块编号",
clearable: true,
span: 8,
},
{
label: "行向",
key: "row_orientation",
type: "select",
props: {
placeholder: "请选择行向",
options: ROW_ORIENTATION_OPTIONS,
clearable: true,
},
span: 8,
},
]);
const faTableRef = ref<{ elTableRef?: { clearSelection: () => void } } | null>(null);
const { selectedRows, selectedIds, batchDeleting, onTableSelectionChange } = useTableSelection<PlotTable>();
const createLoading = ref(false);
const {
columns,
columnChecks,
data,
loading,
pagination,
searchParams,
getData,
replaceSearchParams,
resetSearchParams,
handleSizeChange,
handleCurrentChange,
refreshData,
refreshCreate,
refreshUpdate,
refreshRemove,
} = useTable({
core: {
apiFn: PlotAPI.getPlotList,
apiParams: {
page_no: 1,
page_size: 10,
},
columnsFactory: (): ColumnOption<PlotTable>[] => [
{ type: "globalIndex", width: 56, label: "序号" },
{ type: "selection", width: 48, fixed: "left" },
{ prop: "site_name", label: "所属基地", minWidth: 140, showOverflowTooltip: true },
{ prop: "plot_code", label: "地块编号/名称", minWidth: 140, showOverflowTooltip: true },
{ prop: "row_orientation", label: "行向", minWidth: 100 },
{ prop: "row_count", label: "行数", minWidth: 80 },
{ prop: "col_count", label: "每行株数", minWidth: 100 },
{ prop: "grid_note", label: "株行距/网格说明", minWidth: 160, showOverflowTooltip: true },
{ prop: "area", label: "面积(亩)", minWidth: 100 },
{
prop: "created_time",
label: "创建时间",
width: 168,
sortable: true,
showOverflowTooltip: true,
},
{
prop: "updated_time",
label: "更新时间",
width: 168,
sortable: true,
showOverflowTooltip: true,
},
{
prop: "created_by",
label: "创建人",
minWidth: 100,
formatter: (row: PlotTable) => row.created_by?.name ?? "—",
},
{
prop: "updated_by",
label: "更新人",
minWidth: 100,
formatter: (row: PlotTable) => row.updated_by?.name ?? "—",
},
{
prop: "operation",
label: "操作",
width: 180,
fixed: "right",
align: "center",
formatter: (row: PlotTable) => formatPlotOperationCell(row),
},
],
},
});
const plotCrudCols = toCrudCols(columns);
const exportQueryParams = computed(() => {
return stripPaginationParams(searchParams as Record<string, unknown>);
});
const plotImportContentConfig = computed<IContentConfig>(() => ({
permPrefix: "module_bre:plot",
cols: plotCrudCols.value,
indexAction: async () => ({}),
importTemplate: () => PlotAPI.downloadTemplatePlot(),
}));
const plotExportContentConfig = computed(() => ({
permPrefix: "module_bre:plot",
cols: plotCrudCols.value,
exportsBlobAction: async (params: IObject) => {
const merged = {
...(exportQueryParams.value as unknown as Record<string, unknown>),
...params,
} as unknown as PlotPageQuery;
const res = await PlotAPI.exportPlot(merged);
return res.data as Blob;
},
}));
const { dialogVisible } = useCrudDialog();
const detailFormData = ref<PlotTable>({});
const plotDetailItems: import("@/components/others/fa-descriptions/index.vue").DescriptionsItem[] = [
{ label: "所属基地", prop: "site_name" },
{ label: "UUID", prop: "uuid" },
{ label: "地块编号/名称", prop: "plot_code" },
{ label: "行向", prop: "row_orientation" },
{ label: "行数", prop: "row_count" },
{ label: "每行株数", prop: "col_count" },
{ label: "株行距/网格说明", prop: "grid_note" },
{ label: "面积(亩)", prop: "area" },
{ label: "创建人", prop: "created_by.name" },
{ label: "更新人", prop: "updated_by.name" },
{ label: "创建时间", prop: "created_time" },
{ label: "更新时间", prop: "updated_time" },
];
const plotDialogFormItems: FormItem[] = [
{
key: "site_id",
label: "所属基地",
type: "select",
props: {
placeholder: "请选择基地",
options: siteOptions.value,
clearable: true,
filterable: true,
},
span: 12,
},
{
key: "plot_code",
label: "地块编号/名称",
type: "input",
props: { placeholder: "请输入地块编号", maxlength: 50 },
span: 12,
},
{
key: "row_orientation",
label: "行向",
type: "select",
props: {
placeholder: "请选择行向",
options: ROW_ORIENTATION_OPTIONS,
clearable: true,
},
span: 12,
},
{
key: "row_count",
label: "行数",
type: "number",
props: { placeholder: "整数" },
span: 12,
},
{
key: "col_count",
label: "每行株数",
type: "number",
props: { placeholder: "整数" },
span: 12,
},
{
key: "area",
label: "面积(亩)",
type: "number",
props: { placeholder: "如 1.2" },
span: 12,
},
{
key: "grid_note",
label: "株行距/网格说明",
type: "input",
props: { type: "textarea", rows: 3, placeholder: "如 株距2m×行距4m" },
span: 24,
},
];
const formData = ref<PlotForm>(createInitialFormData());
const rules = reactive({
site_id: [{ required: true, message: "请选择所属基地", trigger: "change" }],
plot_code: [{ required: true, message: "请输入地块编号/名称", trigger: "blur" }],
});
const dataFormRef = ref<InstanceType<typeof FaForm> | null>(null);
const plotFormRenderKey = ref(0);
const crud = useCrudForm<PlotForm>({
formData,
initialFormData: createInitialFormData(),
dialogVisible,
dataFormRef,
formRenderKey: plotFormRenderKey,
detailApi: PlotAPI.getPlotDetail,
createApi: PlotAPI.createPlot,
updateApi: PlotAPI.updatePlot,
titles: { create: "新增试验地", update: "修改试验地", detail: "试验地详情" },
detailFormData,
onCreateSuccess: async () => {
await refreshCreate();
},
onUpdateSuccess: async () => {
await refreshUpdate();
},
});
const { submitLoading } = crud;
const { importVisible, exportVisible, openImport, openExport } = useImportExport();
const handleSearch = async (params: PlotSearchFormParams) => {
await searchBarRef.value?.validate();
replaceSearchParams({
site_id: params.site_id,
plot_code: params.plot_code,
row_orientation: params.row_orientation,
created_id: params.created_id ?? undefined,
updated_id: params.updated_id ?? undefined,
created_time:
Array.isArray(params.created_time) && params.created_time.length === 2
? params.created_time
: undefined,
updated_time:
Array.isArray(params.updated_time) && params.updated_time.length === 2
? params.updated_time
: undefined,
} as Record<string, unknown>);
await getData();
};
const onResetSearch = async () => {
searchForm.value = {
site_id: undefined,
plot_code: undefined,
row_orientation: undefined,
created_id: undefined,
updated_id: undefined,
created_time: [],
updated_time: [],
};
await resetSearchParams();
};
function buildPlotRowActions(row: PlotTable): TableOperationAction[] {
const all: TableOperationAction[] = [
{
key: "detail",
label: "详情",
artType: "view",
perm: "module_bre:plot:detail",
run: () => void crud.handleOpenDialog("detail", row.id),
},
{
key: "edit",
label: "编辑",
artType: "edit",
icon: "ri:edit-2-line",
perm: "module_bre:plot:update",
run: () => void crud.handleOpenDialog("update", row.id),
},
{
key: "delete",
label: "删除",
artType: "delete",
icon: "ri:delete-bin-4-line",
perm: "module_bre:plot:delete",
run: () => deletePlotRow(row),
},
];
return all;
}
function formatPlotOperationCell(row: PlotTable) {
return renderTableOperationCell(buildPlotRowActions(row), {
wrapperClass: "inline-flex flex-wrap items-center justify-end gap-1",
});
}
async function handleAdd() {
createLoading.value = true;
try {
await crud.handleOpenDialog("create");
} finally {
createLoading.value = false;
}
}
const deletePlotRow = async (row: PlotTable) => {
if (!row.id) return;
try {
await confirmDelete(`确定删除「${row.plot_code ?? row.id}」吗?此操作不可恢复!`);
await PlotAPI.deletePlot([row.id!]);
faTableRef.value?.elTableRef?.clearSelection();
await refreshRemove();
} catch {
// 用户取消
}
};
async function handleBatchDelete() {
const ids = selectedIds.value;
if (ids.length === 0) return;
try {
await confirmBatchDelete(ids.length);
batchDeleting.value = true;
await PlotAPI.deletePlot(ids);
faTableRef.value?.elTableRef?.clearSelection();
await refreshRemove();
} catch {
// 用户取消
} finally {
batchDeleting.value = false;
}
}
async function handleCrudImportUpload(formData: FormData) {
try {
const res = await PlotAPI.importPlot(formData);
if (res.data.code === ResultEnum.SUCCESS) {
ElMessage.success(res.data.msg || "导入成功");
importVisible.value = false;
await refreshData();
}
} catch (error: unknown) {
if (import.meta.env.DEV) console.error("[Import]", error);
}
}
onMounted(() => {
loadSiteOptions();
});
</script>