| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import { useHorizontalOverflow } from "../lib/use-horizontal-overflow";
- import { OpsTableColumns } from "../components/OperationsKit";
- import { FilterSelect } from "../components/ui/filter-select";
- import { useMemo, useState } from "react";
- import {
- CalendarClock, CalendarDays, ChevronRight, Clock3, Info,
- Search, Signal, X,
- } from "lucide-react";
- import { Button } from "../components/ui/button";
- import { useNavigate, useSearchParams } from "react-router";
- import { Input } from "../components/ui/input";
- import { orderStatuses, statusPresentation, type OrderStatus } from "../domain/statuses";
- import { orderView, useAllOrders, type OrderView } from "../lib/order-demo";
- import { Tri } from "../components/triState";
- import { rowsOf } from "../lib/api/bos";
- import { SUBS_READONLY_ENTRY, WORD_NOT_PROVIDED, WORD_FETCH_FAILED, WORD_NOT_OPENED, WORD_UNCONFIGURED } from "../lib/api/lockedCopy";
- import { ListPager } from "../components/ListPager";
- import { slicePage, useLocalPage } from "../lib/list-paging";
- import { EXPIRING_SOON_DAYS, expiringLabel, isExpiringSoon } from "../lib/expiring-soon";
- import "../styles/operations.css";
- import "../styles/orders.css";
- type StatusFilter = OrderStatus | "全部状态";
- type PlanFilter = "全部类型" | "固定套餐" | "自定义套餐";
- /** 三态文本格:键缺失=取数失败,null=未配置,⛔ 用 0/「—」冒充有值。 */
- function cell(value: string | null | undefined, missing: string = WORD_UNCONFIGURED) {
- if (value === undefined) return <Tri tone="fail" text={WORD_FETCH_FAILED} />;
- if (value === null || value === "") return <Tri tone="missing" text={missing} />;
- return value;
- }
- function dateCell(value: string | null | undefined, missing: string = WORD_UNCONFIGURED) {
- if (value === undefined) return <Tri tone="fail" text={WORD_FETCH_FAILED} />;
- if (value === null || value === "") return <Tri tone="missing" text={missing} />;
- return value.replaceAll("-", ".");
- }
- const moneyCell = (c: { tone: "val" | "missing" | "fail"; text: string }) =>
- (c.tone === "val" ? c.text : <Tri tone={c.tone} text={c.text} title="金额取数失败=接口未下发该键;未配置=后端明确回 null" />);
- const exitsOf = (record: OrderView) => (record.exits ?? []);
- function StatusPill({ record }: { record: OrderView }) {
- if (!record.statusLabel) return <Tri tone="fail" text={WORD_FETCH_FAILED} title="订阅状态编码未下发" />;
- const tone = statusPresentation.order[record.statusLabel as OrderStatus]?.tone ?? "neutral";
- return <span className={`order-status order-status-${tone}`} title={record.statusCode}><i />{record.statusLabel}</span>;
- }
- /**
- * 全部订阅(原「全部订单」):**只读查询**。
- *
- * 需求 5.2:「全部订阅页只提供查询……页面不提供新增、编辑和删除入口,并说明办理入口在客户管理。」
- * 因此本页 ⛔ 渲染新增订阅/升降配/叠加的办理入口——三者的唯一入口是
- * 「客户管理 → 套餐订阅管理」(`CustomerOrdersPage`)与订阅详情页头;
- * 本页保留的只有筛选、检索与「详情」。
- */
- export function OrderManagementPage() {
- const tableOverflow = useHorizontalOverflow();
- const contentWidth = Math.max(1460, tableOverflow.width) - 204;
- /** 操作列只余「详情」一颗按钮(写入口已按需求 5.2 移出本页)⇒ 末两列 = 状态 104 + 操作 96。 */
- const columnWidths = [.21, .23, .14, .09, .12, .10, .11].map((weight) => contentWidth * weight).concat([104, 96]);
- const navigate = useNavigate();
- const [params] = useSearchParams();
- const orders = useAllOrders();
- const records = useMemo(() => rowsOf(orders.data?.items).map(orderView), [orders.data]);
- const [status, setStatus] = useState<StatusFilter>("全部状态");
- const [planKind, setPlanKind] = useState<PlanFilter>("全部类型");
- const [search, setSearch] = useState(params.get("order") ?? params.get("customer") ?? "");
- const [exitFilter, setExitFilter] = useState("");
- /** 快到期快捷筛选(2026-11-17 四轮):只看订阅止期在 30 天内的行;判据与合同管理同源。 */
- const [expiringOnly, setExpiringOnly] = useState(false);
- const counts = useMemo(() => Object.fromEntries(orderStatuses.map((item) => [item, records.filter((record) => record.statusLabel === item).length])) as Record<OrderStatus, number>, [records]);
- const expiringCount = useMemo(() => records.filter((record) => isExpiringSoon(record.end)).length, [records]);
- const filtered = useMemo(() => records.filter((record) =>
- (status === "全部状态" || record.statusLabel === status) &&
- (!expiringOnly || isExpiringSoon(record.end)) &&
- (planKind === "全部类型" || record.planKindLabel === planKind) &&
- (!exitFilter || exitsOf(record).includes(exitFilter)) &&
- `${record.customer} ${record.no} ${record.parentSubscriptionId ?? ""} ${record.planName ?? ""} ${record.contractNo ?? ""} ${record.orderNo ?? ""} ${exitsOf(record).join(" ")}`.toLowerCase().includes(search.trim().toLowerCase()),
- ), [records, status, expiringOnly, planKind, search, exitFilter]);
- const paging = useLocalPage(filtered.length);
- const pageRows = slicePage(filtered, paging.safePage);
- const exits = [...new Set(records.flatMap((record) => exitsOf(record)))].sort();
- function resetFilters() { setStatus("全部状态"); setPlanKind("全部类型"); setSearch(""); setExitFilter(""); setExpiringOnly(false); paging.resetPage(); }
- const statuses: StatusFilter[] = ["全部状态", ...orderStatuses];
- return <section className="order-page page-fill page-fill-window">
- <header className="order-heading">
- <div><p className="order-eyebrow"><span /> CONTRACT OPERATIONS <b>/</b> 产品与订阅</p><h1>全部订阅</h1><p className="order-intro order-ledger-readonly"><Info size={15} />本页只做查询;新增订阅/升降配/叠加的{SUBS_READONLY_ENTRY}。</p></div>
- </header>
- <div className="order-list-shell list-shell-fill">
- <div className="order-tabs" role="group" aria-label="按订阅状态筛选">
- {statuses.map((item) => <button key={item} type="button" aria-pressed={status === item} className={status === item ? "active" : ""} onClick={() => { setStatus(item); paging.resetPage(); }}>{item}<span>{item === "全部状态" ? records.length : counts[item]}</span></button>)}
- </div>
- <div className="ops-toolbar order-filters">
- <label className="ops-search order-search"><Search size={17} /><Input aria-label="搜索订阅" placeholder="搜索客户、套餐、合同、订单号或出口" value={search} onChange={(e) => { setSearch(e.target.value); paging.resetPage(); }} />{search && <button type="button" aria-label="清空搜索" onClick={() => { setSearch(""); paging.resetPage(); }}><X size={15} /></button>}</label>
- <FilterSelect icon="type" aria-label="套餐类型筛选" value={planKind} onChange={(e) => { setPlanKind(e.target.value as PlanFilter); paging.resetPage(); }}><option>全部类型</option><option>固定套餐</option><option>自定义套餐</option></FilterSelect>
- <FilterSelect icon="exit" aria-label="线路出口筛选" value={exitFilter} onChange={(e) => { setExitFilter(e.target.value); paging.resetPage(); }}><option value="">全部线路出口</option>{exits.map((exit) => <option key={exit}>{exit}</option>)}</FilterSelect>
- {/* 快到期快捷筛选(2026-11-17 四轮,与合同管理同源判据):只看订阅止期在 30 天内的行。 */}
- <Button variant={expiringOnly ? "default" : "outline"} size="sm" aria-pressed={expiringOnly} title={`只看订阅止期在 ${EXPIRING_SOON_DAYS} 天内的订阅(已到期的不算);续签入口在合同管理与客户档案`} onClick={() => { setExpiringOnly(!expiringOnly); paging.resetPage(); }}><CalendarClock size={15} />快到期 {EXPIRING_SOON_DAYS} 天内{expiringCount ? `(${expiringCount})` : ""}</Button>
- {(status !== "全部状态" || planKind !== "全部类型" || exitFilter || expiringOnly || search) && <Button variant="ghost" size="sm" onClick={resetFilters}>重置筛选</Button>}
- </div>
- <div ref={tableOverflow.ref} tabIndex={0} role="region" aria-label="全部订阅,可横向滚动查看字段" className={`measured-table-scroll order-table-wrap order-ledger-scroll${tableOverflow.overflow ? " has-horizontal-overflow" : ""}${tableOverflow.canScrollLeft ? " can-scroll-left" : ""}${tableOverflow.canScrollRight ? " can-scroll-right" : ""}`}><table className="order-table order-ledger"><OpsTableColumns widths={columnWidths} /><thead><tr><th>企业 / 订阅编号</th><th>套餐与合同</th><th>运营商订单号</th><th>线路出口</th><th>年订阅价格</th><th>合同周期</th><th>实际开通</th><th>状态</th><th>操作</th></tr></thead><tbody>
- {orders.loading && <tr><td colSpan={9}><div className="order-empty">正在从订阅接口读取台账…</div></td></tr>}
- {orders.error && <tr><td colSpan={9}><div className="order-empty" role="alert"><h3>全部订阅取数失败</h3><p>{orders.error.message}</p><Button variant="ghost" size="sm" onClick={() => orders.reload()}>重试</Button></div></td></tr>}
- {!orders.loading && !orders.error && pageRows.map((item) => {
- return <tr key={item.no}>
- <td><strong title={item.customer}>{cell(item.customer)}</strong><small title={item.no}>{cell(item.no)}</small>{item.parentSubscriptionId && <small className="order-parent-mark">附加于 {item.parentSubscriptionId}</small>}</td>
- <td><strong title={item.planName ?? ""}>{cell(item.planName)}</strong><small title={`${item.contractNo ?? ""} · ${item.modeLabel}`}>{item.contractNo === undefined ? <Tri tone="missing" text={WORD_NOT_PROVIDED} title="全部订阅接口不下发 contract_no 口径" /> : cell(item.contractNo)}{item.modeLabel ? ` · ${item.modeLabel}` : ""}</small></td>
- <td title={item.orderNo ?? "订单号由第4册工单回填"}>{cell(item.orderNo, WORD_NOT_OPENED)}</td>
- <td title={exitsOf(item).join("、")}>{item.exits === undefined ? <Tri tone="missing" text={WORD_NOT_PROVIDED} title="全部订阅接口不下发 line_exits 口径" /> : item.exits === null ? <Tri tone="missing" text={WORD_UNCONFIGURED} /> : exitsOf(item).length ? exitsOf(item).join("、") : <Tri tone="missing" text={WORD_UNCONFIGURED} />}</td>
- <td><strong className="order-row-price">{moneyCell(item.annual)}</strong><small title="由套餐配置价带出">{moneyCell(item.settle)} / 月结算</small></td>
- <td><strong className="order-date">{dateCell(item.start)}</strong><small>至 {dateCell(item.end)}{isExpiringSoon(item.end) ? `(快到期 · ${expiringLabel(item.end)})` : ""}</small></td>
- <td><strong className="order-date">{dateCell(item.openedOn, WORD_NOT_OPENED)}</strong><small>{item.openedOn ? "计费取较晚日期" : "暂无计费起点"}</small></td>
- <td><StatusPill record={item} /></td>
- {/* 操作列只有「详情」:本页只读,办理入口在客户管理 → 套餐订阅管理(需求 5.2)。 */}
- <td><div className="ops-inline-actions">
- <button type="button" className="ops-table-action" onClick={() => navigate(`/orders/${encodeURIComponent(item.no)}`)} aria-label={`查看${item.customer}订阅详情`}>详情</button>
- </div></td>
- </tr>;
- })}
- </tbody></table></div>
- <ListPager total={filtered.length} page={paging.safePage} pageSize={paging.pageSize} onPageChange={paging.setPage} unit="条" className="list-pager" />
- <div className="order-mobile-list">{pageRows.map((item) => <article key={item.no} className="order-mobile-card"><div><small>{item.no}</small><StatusPill record={item} /></div><h3>{item.customer}</h3><p>{item.planName ?? WORD_UNCONFIGURED} <span>·</span> {item.termLabel || WORD_UNCONFIGURED}</p><dl><div><dt>年订阅价格</dt><dd>{moneyCell(item.annual)}</dd></div><div><dt>合同周期</dt><dd>{dateCell(item.start)} — {dateCell(item.end)}</dd></div></dl><button type="button" onClick={() => navigate(`/orders/${encodeURIComponent(item.no)}`)}>查看订阅详情</button></article>)}</div>
- {!filtered.length && !orders.loading && !orders.error && <div className="order-empty"><Search size={25} /><h3>{expiringOnly ? `没有 ${EXPIRING_SOON_DAYS} 天内到期的订阅` : "没有匹配的订阅"}</h3><p>{expiringOnly ? "已到期的不计入快到期;换一个状态或点「重置筛选」查看全部。" : "换一个企业名称或状态试试。"}</p><Button variant="ghost" size="sm" onClick={resetFilters}>重置筛选</Button></div>}
- <details className="order-rule-strip is-footer"><summary><CalendarDays size={16} />订阅计费规则<span>合同生效 → 实际开通 → 自然月核算</span><ChevronRight size={16} /></summary><div className="order-rule-content">
- <div><CalendarDays size={18} /><span><strong>合同生效</strong><small>确定年度订阅周期</small></span></div><ChevronRight size={15} />
- <div><Signal size={18} /><span><strong>实际开通</strong><small>未开通不计应收</small></span></div><ChevronRight size={15} />
- <div><Clock3 size={18} /><span><strong>自然月核算</strong><small>按有效计费天数结算</small></span></div>
- </div></details>
- </div>
- </section>;
- }
|