feat: 采集任务可按即席查询分类选表,并按相对时间导出 CSV
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { NButton, NInput, NInputNumber, NModal, NSelect, NSwitch, useDialog } from "naive-ui";
|
||||
import Shell from "../../shell/Shell.vue";
|
||||
import { api } from "../../shared/http";
|
||||
@@ -12,7 +12,13 @@ interface Source {
|
||||
periodDays: number;
|
||||
enabled: boolean;
|
||||
lastError: string;
|
||||
config: Record<string, string | number | boolean>;
|
||||
config: Record<string, unknown>;
|
||||
}
|
||||
interface QueryTable {
|
||||
code: number;
|
||||
category: string;
|
||||
tablename: string;
|
||||
name: string;
|
||||
}
|
||||
const sources = ref<Source[]>([]);
|
||||
const show = ref(false);
|
||||
@@ -27,9 +33,30 @@ const protocol = ref("sftp");
|
||||
const username = ref("");
|
||||
const password = ref("");
|
||||
const path = ref("");
|
||||
const url = ref("");
|
||||
const categories = ref<{ code: number; name: string }[]>([]);
|
||||
const categoryCode = ref<number | null>(null);
|
||||
const categoryTables = ref<{ tablename: string; name: string }[]>([]);
|
||||
const chosen = ref<QueryTable[]>([]);
|
||||
const timeMode = ref("prev_month");
|
||||
const timeCount = ref(1);
|
||||
const city = ref("");
|
||||
const catalogLoading = ref(false);
|
||||
const tablesLoading = ref(false);
|
||||
const dialog = useDialog();
|
||||
const toast = useToast();
|
||||
const timeOptions = [
|
||||
{ label: "上一个月", value: "prev_month" },
|
||||
{ label: "上一周", value: "prev_week" },
|
||||
{ label: "前一天", value: "prev_day" },
|
||||
{ label: "前几周", value: "prev_weeks" },
|
||||
{ label: "前几月", value: "prev_months" },
|
||||
{ label: "前多少天", value: "prev_days" },
|
||||
];
|
||||
const needsCount = computed(() => ["prev_weeks", "prev_months", "prev_days"].includes(timeMode.value));
|
||||
const countLabel = computed(() => (timeMode.value === "prev_weeks" ? "周数" : timeMode.value === "prev_months" ? "月数" : "天数"));
|
||||
const categoryOptions = computed(() => categories.value.map((item) => ({ label: item.name, value: item.code })));
|
||||
const tableOptions = computed(() => categoryTables.value.map((item) => ({ label: item.name, value: item.tablename })));
|
||||
const pickedNames = computed(() => chosen.value.filter((item) => item.code === categoryCode.value).map((item) => item.tablename));
|
||||
|
||||
function reset(source?: Source) {
|
||||
editing.value = source || null;
|
||||
@@ -43,8 +70,72 @@ function reset(source?: Source) {
|
||||
username.value = String(source?.config.username || "");
|
||||
password.value = "";
|
||||
path.value = String(source?.config.path || "");
|
||||
url.value = String(source?.config.url || "");
|
||||
const stored = Array.isArray(source?.config.tables) ? source.config.tables as QueryTable[] : [];
|
||||
chosen.value = stored.filter((item) => item && item.tablename).map((item) => ({
|
||||
code: Number(item.code) || 0,
|
||||
category: String(item.category || ""),
|
||||
tablename: String(item.tablename),
|
||||
name: String(item.name || item.tablename),
|
||||
}));
|
||||
timeMode.value = String(source?.config.timeMode || "prev_month");
|
||||
timeCount.value = Number(source?.config.timeCount || 1);
|
||||
city.value = String(source?.config.city || "");
|
||||
categoryCode.value = chosen.value[0]?.code || categories.value[0]?.code || null;
|
||||
categoryTables.value = [];
|
||||
show.value = true;
|
||||
if (kind.value === "nqi") {
|
||||
void loadCategories();
|
||||
}
|
||||
}
|
||||
|
||||
async function changeKind(value: string) {
|
||||
kind.value = value;
|
||||
if (value === "nqi") await loadCategories();
|
||||
}
|
||||
|
||||
async function loadCategories() {
|
||||
catalogLoading.value = true;
|
||||
try {
|
||||
const result = await api<{ categories: { code: number; name: string }[] }>("/api/admin/nqi/catalog");
|
||||
categories.value = result.categories;
|
||||
if (categoryCode.value == null && categories.value.length) categoryCode.value = categories.value[0].code;
|
||||
if (categoryCode.value != null) await loadTables(categoryCode.value);
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "查询分类没有获取到");
|
||||
} finally {
|
||||
catalogLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadTables(code: number) {
|
||||
categoryCode.value = code;
|
||||
tablesLoading.value = true;
|
||||
try {
|
||||
const result = await api<{ tables: { tablename: string; name: string }[] }>(`/api/admin/nqi/catalog?code=${code}`);
|
||||
categoryTables.value = result.tables;
|
||||
} catch (error) {
|
||||
categoryTables.value = [];
|
||||
toast.error(error instanceof Error ? error.message : "数据表没有获取到");
|
||||
} finally {
|
||||
tablesLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function setPicked(names: string[]) {
|
||||
const code = categoryCode.value;
|
||||
const category = categories.value.find((item) => item.code === code)?.name || "";
|
||||
const current = categoryTables.value
|
||||
.filter((item) => names.includes(item.tablename))
|
||||
.map((item) => ({ code: code || 0, category, tablename: item.tablename, name: item.name }));
|
||||
chosen.value = [...chosen.value.filter((item) => item.code !== code), ...current];
|
||||
}
|
||||
|
||||
function selectCategory() {
|
||||
setPicked(categoryTables.value.map((item) => item.tablename));
|
||||
}
|
||||
|
||||
function removeTable(tablename: string) {
|
||||
chosen.value = chosen.value.filter((item) => item.tablename !== tablename);
|
||||
}
|
||||
|
||||
async function load() {
|
||||
@@ -53,8 +144,12 @@ async function load() {
|
||||
}
|
||||
|
||||
async function save() {
|
||||
if (kind.value === "nqi" && !chosen.value.length) {
|
||||
toast.error("请选择要采集的表");
|
||||
return false;
|
||||
}
|
||||
const config = kind.value === "nqi"
|
||||
? { url: url.value }
|
||||
? { tables: chosen.value, timeMode: timeMode.value, timeCount: timeCount.value, city: city.value }
|
||||
: { protocol: protocol.value, host: host.value, port: port.value, username: username.value, password: password.value, path: path.value };
|
||||
const body = { name: name.value, kind: kind.value, periodDays: periodDays.value, enabled: enabled.value, config };
|
||||
try {
|
||||
@@ -140,7 +235,7 @@ onMounted(load);
|
||||
</div>
|
||||
</section>
|
||||
<n-modal v-model:show="show" class="app-dialog app-dialog-wide" preset="dialog" :show-icon="false" title="采集" positive-text="保存" negative-text="取消" @positive-click="save">
|
||||
<div class="form-row"><label>来源</label><n-select v-model:value="kind" :options="[{ label: 'FTP/SFTP', value: 'ftp' }, { label: 'NQI', value: 'nqi' }]" /></div>
|
||||
<div class="form-row"><label>来源</label><n-select :value="kind" :options="[{ label: 'FTP/SFTP', value: 'ftp' }, { label: 'NQI', value: 'nqi' }]" @update:value="changeKind" /></div>
|
||||
<div class="form-row"><label>名称</label><n-input v-model:value="name" /></div>
|
||||
<p class="form-section">连接</p>
|
||||
<template v-if="kind === 'ftp'">
|
||||
@@ -152,7 +247,23 @@ onMounted(load);
|
||||
<div class="form-row"><label>路径</label><n-input v-model:value="path" /></div>
|
||||
</template>
|
||||
<template v-else>
|
||||
<div class="form-row"><label>地址</label><n-input v-model:value="url" /></div>
|
||||
<div class="form-row"><label>查询分类</label><n-select :value="categoryCode" :options="categoryOptions" :loading="catalogLoading" @update:value="loadTables" /></div>
|
||||
<div class="form-row">
|
||||
<label>数据表</label>
|
||||
<div>
|
||||
<n-select multiple filterable :value="pickedNames" :options="tableOptions" :loading="tablesLoading" @update:value="setPicked" />
|
||||
<n-button size="small" quaternary :disabled="!categoryTables.length" @click="selectCategory">分类全选</n-button>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="chosen.length" class="form-row">
|
||||
<label>已选</label>
|
||||
<div class="picked-tables">
|
||||
<button v-for="item in chosen" :key="item.tablename" type="button" @click="removeTable(item.tablename)">{{ item.name }}</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-row"><label>数据时间</label><n-select v-model:value="timeMode" :options="timeOptions" /></div>
|
||||
<div v-if="needsCount" class="form-row"><label>{{ countLabel }}</label><n-input-number v-model:value="timeCount" :min="1" :max="366" :show-button="false" /></div>
|
||||
<div class="form-row"><label>城市</label><n-input v-model:value="city" placeholder="不填则不按城市过滤" /></div>
|
||||
</template>
|
||||
<p class="form-section">周期</p>
|
||||
<div class="form-row"><label>周期(天)</label><n-input-number v-model:value="periodDays" :min="1" :max="31" :show-button="false" /></div>
|
||||
@@ -160,3 +271,19 @@ onMounted(load);
|
||||
</n-modal>
|
||||
</Shell>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.picked-tables {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
}
|
||||
.picked-tables button {
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 999px;
|
||||
background: transparent;
|
||||
color: var(--ink);
|
||||
padding: 2px 8px;
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user