add: recycle product

This commit is contained in:
Timi
2026-11-08 20:06:15 +08:00
parent 585d8956bc
commit ad1787a035
22 changed files with 4284 additions and 8 deletions
+437
View File
@@ -0,0 +1,437 @@
<template>
<div class="tui-page-layout tui-navbar recycle-stat">
<t-navbar
fixed
:z-index="20"
title="回收兑换统计"
left-arrow
@left-click="$router.back()"
>
<template #right>
<t-button
:loading="loading"
theme="primary"
variant="text"
size="small"
content="刷新"
@click="load"
/>
</template>
</t-navbar>
<main class="tui-page-content scroll column">
<div v-can="permissionCodes.recycleExchange.query" class="stat-body">
<t-cell-group class="range-card" theme="card" v-entrance-transition>
<t-cell title="统计区间" :note="rangeLabel" arrow @click="rangePickerVisible = true" />
<template v-if="isCustom">
<t-cell title="开始日期" :note="customRange[0] || '-'" arrow @click="openStartPicker" />
<t-cell title="结束日期" :note="customRange[1] || '-'" arrow @click="openEndPicker" />
</template>
<t-cell title="统计范围" :note="rangeText" />
</t-cell-group>
<section class="panel" v-entrance-transition>
<div class="panel-head">
<div class="title">回收统计</div>
<div class="range" v-text="rangeText" />
</div>
<div class="rows">
<div v-for="row in recycleStatList" :key="`${row.recycleItemId}-${row.recycleName}-${row.recycleUnit}`" class="row">
<div class="name">
<span class="clip-text" v-text="row.recycleName || '未命名回收物'" />
<span v-if="row.recycleUnit" class="unit" v-text="`(${row.recycleUnit})`" />
</div>
<div class="values">
<span class="value" v-text="`收到 ${quantityText(row.receivedQuantity)}`" />
<span class="value" v-text="`退回 ${quantityText(row.returnedQuantity)}`" />
<span class="value net" v-text="`净 ${quantityText(row.netQuantity)}`" />
</div>
</div>
<center-tips v-if="!recycleStatList.length" :content="loading ? '正在统计' : '所选区间没有回收记录'" />
</div>
<div class="panel-total">
<span v-text="`合计收到 ${quantityText(recycleTotals.received)}`" />
<span v-text="`合计退回 ${quantityText(recycleTotals.returned)}`" />
<span class="net" v-text="`净回收 ${quantityText(recycleTotals.net)}`" />
</div>
<div class="panel-tip" v-text="'收到量按兑换单业务时间归集,退回量按撤销或冲正业务时间归集,跨期退回可能让净值为负。'" />
</section>
<section class="panel" v-entrance-transition>
<div class="panel-head">
<div class="title">兑换统计</div>
<div class="range" v-text="rangeText" />
</div>
<div class="rows">
<div v-for="row in exchangeStatList" :key="`${row.skuId}-${row.skuUnitId}-${row.productName}`" class="row">
<div class="name">
<span class="clip-text" v-text="row.productName || '未命名产品'" />
<span v-if="row.unit" class="unit" v-text="`(${row.unit})`" />
</div>
<div class="values">
<span class="value" v-text="`创建 ${quantityText(row.createdQuantity)}`" />
<span class="value" v-text="`核销 ${quantityText(row.verifiedQuantity)}`" />
<span class="value net" v-text="`冲正 ${quantityText(row.revokedQuantity)}`" />
</div>
</div>
<center-tips v-if="!exchangeStatList.length" :content="loading ? '正在统计' : '所选区间没有兑换记录'" />
</div>
<div class="panel-total">
<span v-text="`合计创建 ${quantityText(exchangeTotals.created)}`" />
<span v-text="`合计核销 ${quantityText(exchangeTotals.verified)}`" />
<span class="net" v-text="`合计冲正 ${quantityText(exchangeTotals.revoked)}`" />
</div>
<div class="panel-tip" v-text="'数量按业务单位统计,兑换不计入销售金额、收款或销售订单数量。'" />
</section>
</div>
</main>
<t-popup v-model="rangePickerVisible" placement="bottom">
<t-picker
v-model="rangeDraft"
:columns="rangeColumns"
title="统计区间"
@cancel="rangePickerVisible = false"
@confirm="confirmRange"
/>
</t-popup>
<t-popup v-model="startPickerVisible" placement="bottom">
<t-date-time-picker
v-if="startPickerVisible"
v-model="startDraft"
format="YYYY-MM-DD"
:mode="['date']"
title="开始日期"
@cancel="startPickerVisible = false"
@confirm="confirmStart"
/>
</t-popup>
<t-popup v-model="endPickerVisible" placement="bottom">
<t-date-time-picker
v-if="endPickerVisible"
v-model="endDraft"
format="YYYY-MM-DD"
:mode="['date']"
title="结束日期"
@cancel="endPickerVisible = false"
@confirm="confirmEnd"
/>
</t-popup>
</div>
</template>
<script setup lang="ts">
import { Time } from "timi-web";
import RecycleAPI from "@/api/RecycleAPI";
import type { RecycleExchangeStatRow, RecycleStatRow } from "@/types/Recycle";
import { getErrorMessage } from "@/utils/error";
import { Message } from "@/utils/message";
import { addPackageQuantity, getPackageQuantityText } from "@/utils/package";
import { permissionCodes } from "@/utils/permission";
defineOptions({ name: "RecycleStat" });
type RangeType = "today" | "thisWeek" | "thisMonth" | "lastMonth" | "custom";
const loading = ref(false);
const rangeType = ref<RangeType>("thisMonth");
const customRange = ref<[string, string]>(["", ""]);
const rangePickerVisible = ref(false);
const rangeDraft = ref<Array<string | number>>(["thisMonth"]);
const startDraft = ref("");
const endDraft = ref("");
const startPickerVisible = ref(false);
const endPickerVisible = ref(false);
const recycleStatList = ref<RecycleStatRow[]>([]);
const exchangeStatList = ref<RecycleExchangeStatRow[]>([]);
const rangeOptions: Array<{ label: string; value: RangeType }> = [
{ label: "今天", value: "today" },
{ label: "本周", value: "thisWeek" },
{ label: "本月", value: "thisMonth" },
{ label: "上个月", value: "lastMonth" },
{ label: "自定义", value: "custom" }
];
const rangeColumns = rangeOptions.map(item => ({ label: item.label, value: item.value }));
const rangeLabel = computed(() => rangeOptions.find(item => item.value === rangeType.value)?.label || "时间范围");
const isCustom = computed(() => rangeType.value === "custom");
const activeRange = computed(() => (isCustom.value ? customRange.value : buildPresetRange(rangeType.value)));
const rangeText = computed(() => {
const [begin, end] = activeRange.value;
if (!begin || !end) {
return "未选择区间";
}
// 接口区间右开,展示时把结束日回退一天,避免显示成次日
const endDate = new Date(`${end} 00:00:00`);
endDate.setDate(endDate.getDate() - 1);
return `${begin} 至 ${Time.toDate(endDate)}`;
});
const recycleTotals = computed(() => sumRecycle(recycleStatList.value));
const exchangeTotals = computed(() => sumExchange(exchangeStatList.value));
// ---------- 区间 ----------
function toDateText(date: Date): string {
return Time.toDate(date);
}
function startOfDay(date: Date): Date {
const value = new Date(date);
value.setHours(0, 0, 0, 0);
return value;
}
/** 统计接口的结束时间是开区间,所有预设都换算成「次日零点」。 */
function buildPresetRange(type: RangeType): [string, string] {
const now = new Date();
const today = startOfDay(now);
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
if (type === "today") {
return [toDateText(today), toDateText(tomorrow)];
}
if (type === "thisWeek") {
const day = today.getDay() || 7;
const monday = new Date(today);
monday.setDate(monday.getDate() - day + 1);
return [toDateText(monday), toDateText(tomorrow)];
}
if (type === "lastMonth") {
const begin = new Date(now.getFullYear(), now.getMonth() - 1, 1);
const end = new Date(now.getFullYear(), now.getMonth(), 1);
return [toDateText(begin), toDateText(end)];
}
const begin = new Date(now.getFullYear(), now.getMonth(), 1);
return [toDateText(begin), toDateText(tomorrow)];
}
function buildTimestamps(): [number, number] | undefined {
const [begin, end] = activeRange.value;
if (!begin || !end) {
return undefined;
}
const startAt = Time.toTimestamp(new Date(`${begin} 00:00:00`));
const endAt = Time.toTimestamp(new Date(`${end} 00:00:00`));
if (!(startAt < endAt)) {
return undefined;
}
return [startAt, endAt];
}
// ---------- 统计 ----------
function quantityText(value?: number | string | null): string {
return getPackageQuantityText(value);
}
function sumRecycle(rows: RecycleStatRow[]): { received: string; returned: string; net: string } {
let received = "0";
let returned = "0";
let net = "0";
for (const row of rows) {
received = addPackageQuantity(received, row.receivedQuantity ?? "0") || received;
returned = addPackageQuantity(returned, row.returnedQuantity ?? "0") || returned;
net = addPackageQuantity(net, row.netQuantity ?? "0") || net;
}
return { received, returned, net };
}
function sumExchange(rows: RecycleExchangeStatRow[]): { created: string; verified: string; revoked: string } {
let created = "0";
let verified = "0";
let revoked = "0";
for (const row of rows) {
created = addPackageQuantity(created, row.createdQuantity ?? "0") || created;
verified = addPackageQuantity(verified, row.verifiedQuantity ?? "0") || verified;
revoked = addPackageQuantity(revoked, row.revokedQuantity ?? "0") || revoked;
}
return { created, verified, revoked };
}
async function load(): Promise<void> {
const timestamps = buildTimestamps();
if (!timestamps) {
recycleStatList.value = [];
exchangeStatList.value = [];
if (isCustom.value) {
Message.warning("自定义区间需要开始日期早于结束日期");
}
return;
}
loading.value = true;
try {
const [startAt, endAt] = timestamps;
const [recycleResult, exchangeResult] = await Promise.all([
RecycleAPI.statRecycle(startAt, endAt),
RecycleAPI.statExchange(startAt, endAt)
]);
recycleStatList.value = recycleResult || [];
exchangeStatList.value = exchangeResult || [];
} catch (error) {
Message.error(getErrorMessage(error, "回收兑换统计加载失败"));
} finally {
loading.value = false;
}
}
// ---------- 区间交互 ----------
async function confirmRange(value: Array<string | number>): Promise<void> {
rangeType.value = String(value[0] || rangeDraft.value[0] || "thisMonth") as RangeType;
rangePickerVisible.value = false;
if (isCustom.value && (!customRange.value[0] || !customRange.value[1])) {
const [begin, end] = buildPresetRange("thisMonth");
// 自定义初始值取本月区间,结束日要回退成可选的日期
customRange.value = [begin, toDateText(new Date(`${end} 00:00:00`))];
}
await load();
}
function openStartPicker(): void {
startDraft.value = customRange.value[0] || Time.toDate(new Date());
startPickerVisible.value = true;
}
function openEndPicker(): void {
endDraft.value = customRange.value[1] || Time.toDate(new Date());
endPickerVisible.value = true;
}
async function confirmStart(value?: string | number): Promise<void> {
startPickerVisible.value = false;
customRange.value = [String(value || startDraft.value), customRange.value[1]];
await load();
}
async function confirmEnd(value?: string | number): Promise<void> {
endPickerVisible.value = false;
customRange.value = [customRange.value[0], String(value || endDraft.value)];
await load();
}
onMounted(load);
</script>
<style scoped lang="less">
.recycle-stat {
.stat-body {
gap: 1rem;
display: flex;
padding: 0 1rem 2rem;
flex-direction: column;
}
.range-card {
margin: 0;
:deep(.t-cell__title) {
flex: 0 0 5rem;
}
:deep(.t-cell__note) {
flex: 1 1 0;
min-width: 0;
overflow: hidden;
text-align: right;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.panel {
padding: 1rem 1rem .75rem;
background: var(--td-bg-color-container);
border-radius: 1rem;
.panel-head {
gap: .5rem;
display: flex;
padding-bottom: .75rem;
align-items: baseline;
justify-content: space-between;
.title {
font-size: 1rem;
font-weight: 600;
}
.range {
color: var(--td-text-color-secondary);
font-size: .75rem;
}
}
.rows {
gap: .5rem;
display: flex;
flex-direction: column;
.row {
gap: .5rem;
display: flex;
padding: .5rem 0;
flex-direction: column;
border-bottom: 1px solid var(--td-component-border);
&:last-child {
border-bottom: none;
}
.name {
display: flex;
min-width: 0;
font-size: .875rem;
align-items: baseline;
// clip-text 默认 flex:1 会把单位推到最右,这里让它跟随名称
.clip-text {
flex: 0 1 auto;
font-weight: 600;
}
.unit {
color: var(--td-text-color-secondary);
flex: 0 0 auto;
font-size: .75rem;
}
}
.values {
gap: .75rem;
display: flex;
flex-wrap: wrap;
font-size: .75rem;
.value {
color: var(--td-text-color-secondary);
}
.net {
color: var(--td-text-color-primary);
font-weight: 600;
}
}
}
}
.panel-total {
gap: .75rem;
display: flex;
margin-top: .5rem;
font-size: .75rem;
flex-wrap: wrap;
padding-top: .75rem;
color: var(--td-text-color-secondary);
border-top: 1px solid var(--td-component-border);
.net {
color: var(--td-brand-color);
font-weight: 600;
}
}
.panel-tip {
color: var(--td-text-color-placeholder);
font-size: .75rem;
line-height: 1.5;
padding-top: .5rem;
}
}
}
</style>