mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 02:29:19 +08:00
637 lines
19 KiB
Vue
637 lines
19 KiB
Vue
<script setup lang="ts">
|
||
import type { CompanyVO } from '#/api/organization/company/types';
|
||
import type { ShopVO } from '#/api/ecommerce/shop/types';
|
||
import type { OrderStatVO } from '#/api/system/order-stat';
|
||
|
||
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
|
||
|
||
import { Page } from '@vben/common-ui';
|
||
import { createIconifyIcon } from '@vben/icons';
|
||
|
||
import {
|
||
Button,
|
||
Card,
|
||
DatePicker,
|
||
Descriptions,
|
||
Divider,
|
||
Empty,
|
||
Flex,
|
||
Form,
|
||
Input,
|
||
InputNumber,
|
||
message,
|
||
Pagination,
|
||
Select,
|
||
Space,
|
||
Spin,
|
||
Switch,
|
||
Table,
|
||
Tag,
|
||
} from 'ant-design-vue';
|
||
import dayjs from 'dayjs';
|
||
|
||
import { getCompanyPageApi } from '#/api/organization/company';
|
||
import { getShopPageApi } from '#/api/ecommerce/shop';
|
||
import {
|
||
createOrderStatRebuildApi,
|
||
getOrderStatRebuildStatusApi,
|
||
reconcileOrderStatApi,
|
||
} from '#/api/system/order-stat';
|
||
import {
|
||
SIDEBAR_LIST_SHELL_CLASS,
|
||
sidebarListRowClass,
|
||
} from '#/utils/sidebar-list-styles';
|
||
|
||
defineOptions({ name: 'OrderStatManage' });
|
||
|
||
const BuildingIcon = createIconifyIcon('lucide:building-2');
|
||
const RefreshIcon = createIconifyIcon('lucide:refresh-cw');
|
||
const PlayIcon = createIconifyIcon('lucide:play');
|
||
const SearchIcon = createIconifyIcon('lucide:search-check');
|
||
|
||
const companySearchForm = ref({
|
||
name: '',
|
||
status: undefined as number | undefined,
|
||
});
|
||
|
||
const companyList = ref<CompanyVO.CompanyInfoVO[]>([]);
|
||
const companyLoading = ref(false);
|
||
const companyPagination = ref({ current: 1, pageSize: 20, total: 0 });
|
||
|
||
const currentCompany = ref<CompanyVO.CompanyInfoVO | null>(null);
|
||
const shopId = ref<number | undefined>();
|
||
const shopOptions = ref<{ label: string; value: number }[]>([]);
|
||
const shopLoading = ref(false);
|
||
|
||
const selectedCompanyLabel = computed(() => {
|
||
if (!currentCompany.value) {
|
||
return '请先选择公司';
|
||
}
|
||
return currentCompany.value.name;
|
||
});
|
||
|
||
/** 默认近 30 天 */
|
||
const defaultRange: [dayjs.Dayjs, dayjs.Dayjs] = [
|
||
dayjs().subtract(29, 'day'),
|
||
dayjs(),
|
||
];
|
||
|
||
const rebuildRange = ref<[dayjs.Dayjs, dayjs.Dayjs]>([...defaultRange]);
|
||
const rebuildSubmitting = ref(false);
|
||
const lastTaskId = ref<number | undefined>();
|
||
const rebuildTask = ref<OrderStatVO.RebuildTask | null>(null);
|
||
const pollTaskId = ref<string>('');
|
||
const pollLoading = ref(false);
|
||
const autoPoll = ref(true);
|
||
let pollTimer: ReturnType<typeof setInterval> | null = null;
|
||
|
||
const reconcileRange = ref<[dayjs.Dayjs, dayjs.Dayjs]>([...defaultRange]);
|
||
const sampleSize = ref(100);
|
||
const reconcileLoading = ref(false);
|
||
const reconcileReport = ref<OrderStatVO.ReconcileReport | null>(null);
|
||
const onlyMismatch = ref(false);
|
||
|
||
const statusColorMap: Record<string, string> = {
|
||
pending: 'default',
|
||
running: 'processing',
|
||
done: 'success',
|
||
failed: 'error',
|
||
};
|
||
|
||
const statusLabelMap: Record<string, string> = {
|
||
pending: '待执行',
|
||
running: '执行中',
|
||
done: '已完成',
|
||
failed: '失败',
|
||
};
|
||
|
||
function formatRange(range: [dayjs.Dayjs, dayjs.Dayjs]): [string, string] {
|
||
return [range[0].format('YYYY-MM-DD'), range[1].format('YYYY-MM-DD')];
|
||
}
|
||
|
||
function currentCompanyId(): number | null {
|
||
return currentCompany.value?.id ?? null;
|
||
}
|
||
|
||
async function searchCompanies() {
|
||
companyLoading.value = true;
|
||
try {
|
||
const result = await getCompanyPageApi({
|
||
page: companyPagination.value.current,
|
||
pageSize: companyPagination.value.pageSize,
|
||
filters: {
|
||
name: companySearchForm.value.name || undefined,
|
||
status: companySearchForm.value.status,
|
||
},
|
||
});
|
||
companyList.value = result.records;
|
||
companyPagination.value.total = result.total;
|
||
} catch (e: any) {
|
||
message.error(e?.message || '获取公司列表失败');
|
||
} finally {
|
||
companyLoading.value = false;
|
||
}
|
||
}
|
||
|
||
async function loadShops(cid: number) {
|
||
shopLoading.value = true;
|
||
try {
|
||
const result = await getShopPageApi({
|
||
page: 1,
|
||
pageSize: 500,
|
||
filters: {},
|
||
});
|
||
shopOptions.value = (result.records || [])
|
||
.filter((s: ShopVO.Info) => s.companyId === cid)
|
||
.map((s: ShopVO.Info) => ({
|
||
label: `${s.name}(ID:${s.id})`,
|
||
value: s.id,
|
||
}));
|
||
} catch (e: any) {
|
||
message.error(e?.message || '加载店铺列表失败');
|
||
} finally {
|
||
shopLoading.value = false;
|
||
}
|
||
}
|
||
|
||
function resetRightPanelState() {
|
||
shopId.value = undefined;
|
||
shopOptions.value = [];
|
||
reconcileReport.value = null;
|
||
rebuildTask.value = null;
|
||
lastTaskId.value = undefined;
|
||
pollTaskId.value = '';
|
||
stopPoll();
|
||
}
|
||
|
||
async function selectCompany(company: CompanyVO.CompanyInfoVO) {
|
||
currentCompany.value = company;
|
||
resetRightPanelState();
|
||
await loadShops(company.id);
|
||
}
|
||
|
||
function resetCompanySearch() {
|
||
companySearchForm.value = { name: '', status: undefined };
|
||
companyPagination.value.current = 1;
|
||
searchCompanies();
|
||
}
|
||
|
||
function handleCompanyPageChange(page: number, pageSize: number) {
|
||
companyPagination.value.current = page;
|
||
companyPagination.value.pageSize = pageSize;
|
||
searchCompanies();
|
||
}
|
||
|
||
function companyRowClass(companyId: number) {
|
||
const active = currentCompany.value?.id === companyId;
|
||
return sidebarListRowClass(active, 'w-full cursor-pointer text-left');
|
||
}
|
||
|
||
async function handleCreateRebuild() {
|
||
const cid = currentCompanyId();
|
||
if (!cid) return;
|
||
rebuildSubmitting.value = true;
|
||
try {
|
||
const [startDate, endDate] = formatRange(rebuildRange.value);
|
||
const taskId = await createOrderStatRebuildApi({
|
||
companyId: cid,
|
||
shopId: shopId.value ?? null,
|
||
startDate,
|
||
endDate,
|
||
});
|
||
lastTaskId.value = taskId;
|
||
pollTaskId.value = String(taskId);
|
||
message.success(`rebuild 任务已创建,任务 ID:${taskId}`);
|
||
await fetchRebuildStatus(taskId);
|
||
startPollIfNeeded();
|
||
} catch (e: any) {
|
||
message.error(e?.message || '创建 rebuild 失败');
|
||
} finally {
|
||
rebuildSubmitting.value = false;
|
||
}
|
||
}
|
||
|
||
async function fetchRebuildStatus(id?: number) {
|
||
const tid = id ?? Number(pollTaskId.value);
|
||
if (!tid || Number.isNaN(tid)) {
|
||
message.warning('请输入有效的任务 ID');
|
||
return;
|
||
}
|
||
pollLoading.value = true;
|
||
try {
|
||
rebuildTask.value = await getOrderStatRebuildStatusApi(tid);
|
||
if (rebuildTask.value?.status === 'done') {
|
||
stopPoll();
|
||
message.success('rebuild 已完成');
|
||
} else if (rebuildTask.value?.status === 'failed') {
|
||
stopPoll();
|
||
message.error(rebuildTask.value.errorMessage || 'rebuild 失败');
|
||
}
|
||
} catch (e: any) {
|
||
message.error(e?.message || '查询任务失败');
|
||
} finally {
|
||
pollLoading.value = false;
|
||
}
|
||
}
|
||
|
||
function startPollIfNeeded() {
|
||
stopPoll();
|
||
if (!autoPoll.value) return;
|
||
const status = rebuildTask.value?.status;
|
||
if (status === 'done' || status === 'failed') return;
|
||
pollTimer = setInterval(() => {
|
||
const tid = Number(pollTaskId.value);
|
||
if (tid && !Number.isNaN(tid)) {
|
||
fetchRebuildStatus(tid);
|
||
}
|
||
}, 3000);
|
||
}
|
||
|
||
function stopPoll() {
|
||
if (pollTimer) {
|
||
clearInterval(pollTimer);
|
||
pollTimer = null;
|
||
}
|
||
}
|
||
|
||
watch(autoPoll, (on) => {
|
||
if (on) {
|
||
startPollIfNeeded();
|
||
} else {
|
||
stopPoll();
|
||
}
|
||
});
|
||
|
||
async function handleReconcile() {
|
||
const cid = currentCompanyId();
|
||
if (!cid) return;
|
||
reconcileLoading.value = true;
|
||
reconcileReport.value = null;
|
||
try {
|
||
const [startDate, endDate] = formatRange(reconcileRange.value);
|
||
reconcileReport.value = await reconcileOrderStatApi({
|
||
companyId: cid,
|
||
startDate,
|
||
endDate,
|
||
sampleSize: sampleSize.value || 100,
|
||
});
|
||
if (reconcileReport.value.passed) {
|
||
message.success('对账通过');
|
||
} else {
|
||
message.warning(
|
||
`存在 ${reconcileReport.value.mismatchCount} 条差异(抽样 ${reconcileReport.value.sampleCount})`,
|
||
);
|
||
}
|
||
} catch (e: any) {
|
||
message.error(e?.message || '对账失败');
|
||
} finally {
|
||
reconcileLoading.value = false;
|
||
}
|
||
}
|
||
|
||
const reconcileTableData = () => {
|
||
const items = reconcileReport.value?.items ?? [];
|
||
if (!onlyMismatch.value) return items;
|
||
return items.filter((i) => !i.match);
|
||
};
|
||
|
||
const reconcileColumns = [
|
||
{ title: '店铺 ID', dataIndex: 'shopId', width: 100 },
|
||
{ title: '自然日', dataIndex: 'statDate', width: 120 },
|
||
{
|
||
title: '汇总销售额',
|
||
dataIndex: 'statSales',
|
||
width: 140,
|
||
customRender: ({ text }: { text: number }) => formatMoney(text),
|
||
},
|
||
{
|
||
title: '明细聚合销售额',
|
||
dataIndex: 'aggSales',
|
||
width: 160,
|
||
customRender: ({ text }: { text: number }) => formatMoney(text),
|
||
},
|
||
{
|
||
title: '差额',
|
||
dataIndex: 'diff',
|
||
width: 120,
|
||
customRender: ({ text, record }: { text: number; record: OrderStatVO.ReconcileItem }) =>
|
||
record.match ? '0.00' : formatMoney(text),
|
||
},
|
||
{
|
||
title: '结果',
|
||
dataIndex: 'match',
|
||
width: 90,
|
||
customRender: ({ text }: { text: boolean }) =>
|
||
text ? '一致' : '不一致',
|
||
},
|
||
];
|
||
|
||
function formatMoney(v: number | undefined) {
|
||
if (v == null) return '-';
|
||
return Number(v).toLocaleString('zh-CN', {
|
||
minimumFractionDigits: 2,
|
||
maximumFractionDigits: 2,
|
||
});
|
||
}
|
||
|
||
onBeforeUnmount(() => {
|
||
stopPoll();
|
||
});
|
||
|
||
onMounted(() => {
|
||
searchCompanies();
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<Page title="订单统计汇总管理" :auto-content-height="true">
|
||
<template #description>
|
||
<div class="text-foreground/80 mt-2">
|
||
左侧选择公司,右侧进行历史灌数、任务进度查询与抽样对账(需
|
||
<code class="text-xs">ecom:shop:read</code> 或超管)。
|
||
</div>
|
||
</template>
|
||
|
||
<Flex gap="middle" class="h-full min-h-0">
|
||
<!-- 左:公司列表 -->
|
||
<Card
|
||
class="flex h-full w-[420px] shrink-0 flex-col"
|
||
:body-style="{
|
||
height: '100%',
|
||
display: 'flex',
|
||
flexDirection: 'column',
|
||
gap: '8px',
|
||
minHeight: 0,
|
||
padding: '12px',
|
||
}"
|
||
>
|
||
<template #title>
|
||
<span class="flex items-center gap-2 text-base font-semibold">
|
||
<BuildingIcon class="h-4 w-4" />
|
||
公司
|
||
</span>
|
||
</template>
|
||
|
||
<Flex wrap="wrap" align="center" class="shrink-0 gap-2">
|
||
<Input
|
||
v-model:value="companySearchForm.name"
|
||
placeholder="公司名称"
|
||
allow-clear
|
||
class="min-w-[140px] flex-1"
|
||
@press-enter="searchCompanies"
|
||
/>
|
||
<Select
|
||
v-model:value="companySearchForm.status"
|
||
placeholder="状态"
|
||
allow-clear
|
||
class="w-[88px]"
|
||
:options="[
|
||
{ label: '启用', value: 1 },
|
||
{ label: '禁用', value: 0 },
|
||
]"
|
||
/>
|
||
<Button type="primary" size="small" @click="searchCompanies">
|
||
搜索
|
||
</Button>
|
||
<Button size="small" @click="resetCompanySearch">重置</Button>
|
||
</Flex>
|
||
|
||
<Spin :spinning="companyLoading" class="flex min-h-0 flex-1 flex-col">
|
||
<div :class="SIDEBAR_LIST_SHELL_CLASS">
|
||
<Empty
|
||
v-if="!companyLoading && companyList.length === 0"
|
||
class="py-8"
|
||
description="暂无公司"
|
||
/>
|
||
<div v-else class="space-y-1">
|
||
<button
|
||
v-for="company in companyList"
|
||
:key="company.id"
|
||
type="button"
|
||
:class="companyRowClass(company.id)"
|
||
@click="selectCompany(company)"
|
||
>
|
||
<span class="block truncate">{{ company.name }}</span>
|
||
<span
|
||
class="mt-0.5 block truncate text-xs font-normal text-muted-foreground"
|
||
>
|
||
ID: {{ company.id }} ·
|
||
{{ company.status === 1 ? '启用' : '禁用' }}
|
||
</span>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<Flex justify="center" class="mt-2 shrink-0 pt-1">
|
||
<Pagination
|
||
v-model:current="companyPagination.current"
|
||
v-model:page-size="companyPagination.pageSize"
|
||
:total="companyPagination.total"
|
||
size="small"
|
||
:show-size-changer="true"
|
||
:show-quick-jumper="true"
|
||
@change="handleCompanyPageChange"
|
||
/>
|
||
</Flex>
|
||
</Spin>
|
||
</Card>
|
||
|
||
<!-- 右:汇总运维 -->
|
||
<Card
|
||
class="flex h-full min-h-0 min-w-0 flex-1 flex-col"
|
||
:body-style="{
|
||
height: '100%',
|
||
minHeight: 0,
|
||
padding: 0,
|
||
display: 'flex',
|
||
flexDirection: 'column',
|
||
}"
|
||
>
|
||
<template #title>
|
||
<span class="text-base font-semibold">汇总运维</span>
|
||
</template>
|
||
|
||
<div
|
||
class="shrink-0 border-t border-border px-4 py-2 text-xs text-muted-foreground"
|
||
>
|
||
当前:{{ selectedCompanyLabel }}
|
||
<template v-if="currentCompany">
|
||
<Tag
|
||
class="ml-2"
|
||
:color="currentCompany.status === 1 ? 'green' : 'red'"
|
||
>
|
||
{{ currentCompany.status === 1 ? '启用' : '禁用' }}
|
||
</Tag>
|
||
<span class="ml-3 hidden sm:inline">
|
||
历史灌数按 shop×自然日重算;店铺不选则全店
|
||
</span>
|
||
</template>
|
||
</div>
|
||
|
||
<div
|
||
v-if="!currentCompany"
|
||
class="flex min-h-0 flex-1 items-center justify-center"
|
||
>
|
||
<Empty description="请从左侧选择公司以操作汇总表" />
|
||
</div>
|
||
|
||
<div
|
||
v-else
|
||
class="min-h-0 flex-1 overflow-y-auto px-4 py-3 sm:px-5"
|
||
>
|
||
<Form layout="vertical" class="max-w-xl">
|
||
<Form.Item label="店铺范围(可选)">
|
||
<Select
|
||
v-model:value="shopId"
|
||
allow-clear
|
||
:loading="shopLoading"
|
||
:options="shopOptions"
|
||
placeholder="不选则该公司下全部店铺"
|
||
show-search
|
||
option-filter-prop="label"
|
||
/>
|
||
</Form.Item>
|
||
</Form>
|
||
|
||
<Divider orientation="left" class="!mt-0">
|
||
历史灌数(Rebuild)
|
||
</Divider>
|
||
<Form layout="vertical" class="max-w-xl">
|
||
<Form.Item label="灌数日期区间" required>
|
||
<DatePicker.RangePicker
|
||
v-model:value="rebuildRange"
|
||
class="w-full"
|
||
:allow-clear="false"
|
||
/>
|
||
</Form.Item>
|
||
<Space>
|
||
<Button
|
||
type="primary"
|
||
:loading="rebuildSubmitting"
|
||
@click="handleCreateRebuild"
|
||
>
|
||
<template #icon><PlayIcon /></template>
|
||
创建灌数任务
|
||
</Button>
|
||
</Space>
|
||
<div v-if="lastTaskId" class="mt-2 text-sm text-muted-foreground">
|
||
最近任务 ID:<Tag color="blue">{{ lastTaskId }}</Tag>
|
||
</div>
|
||
</Form>
|
||
|
||
<Divider orientation="left">任务进度</Divider>
|
||
<Space class="mb-3 max-w-xl" wrap>
|
||
<Input
|
||
v-model:value="pollTaskId"
|
||
placeholder="任务 ID"
|
||
style="width: 160px"
|
||
/>
|
||
<Button :loading="pollLoading" @click="fetchRebuildStatus()">
|
||
<template #icon><RefreshIcon /></template>
|
||
查询
|
||
</Button>
|
||
<span class="text-sm text-muted-foreground">
|
||
自动刷新(3s)
|
||
<Switch v-model:checked="autoPoll" class="ml-2" size="small" />
|
||
</span>
|
||
</Space>
|
||
<Spin :spinning="pollLoading && !rebuildTask">
|
||
<Descriptions
|
||
v-if="rebuildTask"
|
||
bordered
|
||
class="max-w-2xl"
|
||
:column="1"
|
||
size="small"
|
||
>
|
||
<Descriptions.Item label="任务 ID">
|
||
{{ rebuildTask.id }}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="状态">
|
||
<Tag :color="statusColorMap[rebuildTask.status] || 'default'">
|
||
{{ statusLabelMap[rebuildTask.status] || rebuildTask.status }}
|
||
</Tag>
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="公司 / 店铺">
|
||
{{ rebuildTask.companyId }} /
|
||
{{ rebuildTask.shopId ?? '全部店铺' }}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="区间">
|
||
{{ rebuildTask.startDate }} ~ {{ rebuildTask.endDate }}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="进度日期">
|
||
{{ rebuildTask.progressDate ?? '-' }}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item
|
||
v-if="rebuildTask.errorMessage"
|
||
label="错误信息"
|
||
>
|
||
<span class="text-red-500">{{ rebuildTask.errorMessage }}</span>
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="更新时间">
|
||
{{ rebuildTask.updateTime ?? '-' }}
|
||
</Descriptions.Item>
|
||
</Descriptions>
|
||
<div
|
||
v-else
|
||
class="max-w-2xl py-4 text-center text-sm text-muted-foreground"
|
||
>
|
||
创建任务后将自动填入 ID,也可手动输入后查询
|
||
</div>
|
||
</Spin>
|
||
|
||
<Divider orientation="left">抽样对账(Reconcile)</Divider>
|
||
<Form layout="vertical" class="max-w-xl">
|
||
<Form.Item label="对账日期区间" required>
|
||
<DatePicker.RangePicker
|
||
v-model:value="reconcileRange"
|
||
class="w-full"
|
||
:allow-clear="false"
|
||
/>
|
||
</Form.Item>
|
||
<Form.Item label="抽样条数">
|
||
<InputNumber
|
||
v-model:value="sampleSize"
|
||
:min="1"
|
||
:max="500"
|
||
class="w-full"
|
||
/>
|
||
</Form.Item>
|
||
<Button
|
||
type="primary"
|
||
:loading="reconcileLoading"
|
||
@click="handleReconcile"
|
||
>
|
||
<template #icon><SearchIcon /></template>
|
||
执行对账
|
||
</Button>
|
||
</Form>
|
||
|
||
<div v-if="reconcileReport" class="mt-4">
|
||
<Space class="mb-3" wrap>
|
||
<Tag :color="reconcileReport.passed ? 'success' : 'error'">
|
||
{{ reconcileReport.passed ? '对账通过' : '存在差异' }}
|
||
</Tag>
|
||
<span class="text-sm">
|
||
抽样 {{ reconcileReport.sampleCount }} 条,差异
|
||
{{ reconcileReport.mismatchCount }} 条
|
||
</span>
|
||
<span class="text-sm text-muted-foreground">
|
||
仅看不一致
|
||
<Switch v-model:checked="onlyMismatch" class="ml-1" size="small" />
|
||
</span>
|
||
</Space>
|
||
<Table
|
||
:columns="reconcileColumns"
|
||
:data-source="reconcileTableData()"
|
||
:pagination="{ pageSize: 10, showSizeChanger: true }"
|
||
:row-key="(r: OrderStatVO.ReconcileItem) => `${r.shopId}-${r.statDate}`"
|
||
size="small"
|
||
:scroll="{ x: 720 }"
|
||
/>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
</Flex>
|
||
</Page>
|
||
</template>
|