| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- /**
- * 工单详情右栏「客户与订阅」参考卡的弹窗(右侧抽屉)。
- *
- * 参考卡上只放摘要;客户名称、关联订阅等改为链接,点开本件按主键现取完整档案:
- * · 客户 → `POST /customer/get@v1`(+ `POST /subscription/get_list@v1` 列名下订阅);
- * · 订阅 → `POST /subscription/get@v1`(合同编号跳合同详情、客户主体可反查客户档案)。
- *
- * 取数口径遵守总则「查不到 ≠ 查不了」:请求失败走 `FetchError` 并保留重试,
- * 业务空值只写「未登记」,⛔ 用 0/「正常」冒充。
- *
- * 抽屉内容可以逐层深入(客户 → 名下订阅 → 客户),所以页面持有的是一个**栈**:
- * 本件始终渲染栈顶,栈深 > 1 时给出「返回上一级」,关闭则整栈清空。
- */
- import { useState, type ReactNode } from "react";
- import { Link, useLocation } from "react-router";
- import { ArrowLeft, ArrowRight, Building2, Package, Tag } from "lucide-react";
- import { FetchError } from "./fetchError";
- import { ListPager } from "./ListPager";
- import { OpsDrawer, OpsStatus } from "./OperationsKit";
- import { catalogGet, customerGet, orderGet, orderGetList } from "../lib/api/bos";
- import type { CatalogPlanRow, OrderGetResult, OrderListResult } from "../lib/api/types";
- import { ACCT_STATUS, PLAN_FAMILY, PLAN_KIND, ORDER_MODE, ORDER_STATUS, SUBJECT_TYPE, TERM, label } from "../lib/api/labels";
- import { bandwidthTextOfRow, moneyCell } from "../lib/package-catalog";
- import { detailLinkState } from "../lib/detail-navigation";
- import { LIST_PAGE_SIZE, clampPage } from "../lib/list-paging";
- import { fmtDate } from "../lib/ticket";
- import { useLoad } from "../lib/useLoad";
- import "../styles/work-order-record.css";
- /** 抽屉栈里的一层:客户档案或订阅详情,`name` 只作加载期的标题兜底。 */
- export type RefTarget =
- | { kind: "customer"; id: string; name?: string | null }
- | { kind: "order"; id: string; name?: string | null }
- | { kind: "plan"; id: string; name?: string | null };
- const missing = (value: string | null | undefined) => (value && value.trim() ? value : "未登记");
- /** 日期列(`created_at` 等)按全站口径显示到日;缺失回「未登记」,⛔ 原样吐 ISO 串。 */
- const dateText = (value: string | null | undefined) => (value && value.trim() ? fmtDate(value) : "未登记");
- const statusTone = (status: string | null | undefined) =>
- status === "active" ? "success" : status === "pending" ? "warning" : "neutral";
- function Row({ label: name, children }: { label: string; children: ReactNode }) {
- return <div><dt>{name}</dt><dd>{children}</dd></div>;
- }
- function period(start: string | null | undefined, end: string | null | undefined) {
- if (!start && !end) return "未登记";
- return `${start || "未登记"} 至 ${end || "未登记"}`;
- }
- export function ReferenceDrawer({ stack, onPush, onBack, onClose }: {
- stack: RefTarget[];
- onPush: (target: RefTarget) => void;
- onBack: () => void;
- onClose: () => void;
- }) {
- const top = stack[stack.length - 1];
- if (!top) return null;
- const title = top.kind === "customer" ? "客户档案" : top.kind === "plan" ? "套餐档位详情" : "订阅详情";
- const subtitle = top.name
- ? `${top.name} · ${top.id}`
- : top.kind === "customer" ? "客户主体参考信息" : top.kind === "plan" ? "套餐目录参考信息" : "订阅参考信息";
- return <OpsDrawer title={title} subtitle={subtitle} onClose={onClose}>
- {stack.length > 1 && <button type="button" className="record-ref-back" onClick={onBack}>
- <ArrowLeft size={15} />返回上一级
- </button>}
- {top.kind === "customer"
- ? <CustomerPanel customerId={top.id} onOpenOrder={onPush} />
- : top.kind === "plan"
- ? <PlanPanel planId={top.id} onOpenCustomer={onPush} />
- : <OrderPanel orderId={top.id} onOpenCustomer={onPush} />}
- </OpsDrawer>;
- }
- function CustomerPanel({ customerId, onOpenOrder }: {
- customerId: string;
- onOpenOrder: (target: RefTarget) => void;
- }) {
- const location = useLocation();
- const linkState = detailLinkState(location.pathname, location.state);
- const [orderPage, setOrderPage] = useState(0);
- const [orderPageSize, setOrderPageSize] = useState(LIST_PAGE_SIZE);
- const detail = useLoad(() => customerGet({ customer_id: customerId }), [customerId]);
- const orders = useLoad<OrderListResult>(
- () => orderGetList({ customer_id: customerId, page: orderPage, page_size: orderPageSize }),
- [customerId, orderPage, orderPageSize],
- );
- if (detail.loading) return <p className="record-ref-status">正在读取客户档案…</p>;
- if (detail.error) return <FetchError className="record-ref-status" message={`客户档案取数失败:${detail.error.message}`} onRetry={detail.reload} />;
- const c = detail.data?.customer;
- if (!c) return <p className="record-ref-status">未取到该客户档案(可能不在当前负责范围)。</p>;
- const mount = detail.data?.region_mounts?.[0]?.path_name ?? null;
- const teams = detail.data?.team_ids ?? [];
- const rows = orders.data?.items ?? [];
- const orderTotal = Number(orders.data?.total ?? 0);
- const safeOrderPage = clampPage(orderPage, orderTotal, orderPageSize);
- return <div className="ops-detail">
- <div className="ops-detail-hero">
- <div>
- <OpsStatus tone={c.acct_status === "active" || c.acct_status === "normal" ? "success" : "neutral"}>
- {label(ACCT_STATUS, c.acct_status) || missing(c.acct_status)}
- </OpsStatus>
- {c.subject_type && <span className="ops-tag tone-brand">{label(SUBJECT_TYPE, c.subject_type) || c.subject_type}</span>}
- </div>
- <h3>{missing(c.customer_name)}</h3>
- <p>{c.customer_id} · {missing(mount)}</p>
- </div>
- <section>
- <h4>01 · 主体与归属</h4>
- <dl className="ops-detail-grid">
- <Row label="客户主体号">{c.customer_id}</Row>
- <Row label="客户名称">{missing(c.customer_name)}</Row>
- <Row label="主体类型">{label(SUBJECT_TYPE, c.subject_type) || missing(c.subject_type)}</Row>
- <Row label="账户状态">{label(ACCT_STATUS, c.acct_status) || missing(c.acct_status)}</Row>
- <Row label="脱敏识别号">{missing(c.identity_no_masked)}</Row>
- <Row label="所属区域">{missing(mount)}</Row>
- <Row label="运营商 / 合伙人">{missing(c.operator_name)} / {missing(c.partner_name)}</Row>
- <Row label="承接团队">{teams.length ? `${teams.length} 支` : "未登记"}</Row>
- <Row label="分账号数">{detail.data?.sub_account_count ?? "未登记"}</Row>
- <Row label="建档时间">{dateText(c.created_at)}</Row>
- <Row label="最近更新">{dateText(c.updated_at)}</Row>
- </dl>
- </section>
- <section>
- <h4>02 · 联系方式</h4>
- <dl className="ops-detail-grid">
- <Row label="联系人">{missing(c.contact_name)}</Row>
- <Row label="联系电话">{missing(c.contact_mobile)}</Row>
- <Row label="通信地址">{missing(c.address)}</Row>
- </dl>
- </section>
- <section>
- <h4>03 · 名下订阅<span>{orders.loading ? "读取中…" : `共 ${orderTotal} 条`}</span></h4>
- {orders.error && <FetchError className="record-ref-status" message={`订阅列表取数失败:${orders.error.message}`} onRetry={orders.reload} />}
- {!orders.loading && !orders.error && !rows.length && <p className="record-ref-status">该客户暂无可读订阅。</p>}
- {rows.length > 0 && <ul className="record-ref-links">{rows.map((sub) => <li key={sub.subscription_id}>
- <button type="button" onClick={() => onOpenOrder({ kind: "order", id: sub.subscription_id, name: sub.plan_name })}>
- <Package size={15} />
- <span>
- <strong>{missing(sub.plan_name ?? sub.subscription_id)}</strong>
- <small>{[
- label(ORDER_STATUS, sub.status) || sub.status,
- label(TERM, sub.term),
- sub.actual_opened_at ? `实际开通 ${dateText(sub.actual_opened_at)}` : null,
- ].filter(Boolean).join(" · ") || "订阅字段未下发"}</small>
- </span>
- <ArrowRight size={15} />
- </button>
- </li>)}</ul>}
- <ListPager
- total={orderTotal}
- page={safeOrderPage}
- pageSize={orderPageSize}
- onPageChange={(page) => setOrderPage(page)}
- onPageSizeChange={(size) => { setOrderPageSize(size); setOrderPage(0); }}
- unit="条"
- />
- <p className="record-ref-note">订阅行来自 <code>/subscription/get_list@v1</code>(默认每页 {LIST_PAGE_SIZE},由本抽屉请求决定),点开按订单号取详情。</p>
- </section>
- <footer className="record-ref-footer">
- <Link className="ops-card-link" to={`/customers/${encodeURIComponent(c.customer_id)}`} state={linkState}>
- <Building2 size={15} />打开完整客户档案<ArrowRight size={15} />
- </Link>
- </footer>
- </div>;
- }
- /** 套餐档位详情:合同条目的「套餐 / 配置」是外部引用,点开按 `plan_id` 现取目录档。 */
- function PlanPanel({ planId, onOpenCustomer }: {
- planId: string;
- onOpenCustomer: (target: RefTarget) => void;
- }) {
- const location = useLocation();
- const linkState = detailLinkState(location.pathname, location.state);
- const detail = useLoad(() => catalogGet({ plan_id: planId }), [planId]);
- if (detail.loading) return <p className="record-ref-status">正在读取套餐档位…</p>;
- if (detail.error) return <FetchError className="record-ref-status" message={`套餐档位取数失败:${detail.error.message}`} onRetry={detail.reload} />;
- const p: CatalogPlanRow | null = detail.data;
- if (!p) return <p className="record-ref-status">未取到该套餐档位(可能已删除或不在当前负责范围)。</p>;
- const bandwidth = bandwidthTextOfRow(p);
- const annual = moneyCell(p.annual_price_cents);
- const monthly = moneyCell(p.monthly_settle_cents);
- const cost = moneyCell(p.plan_cost_cents);
- const costs = detail.data?.cost_items ?? [];
- return <div className="ops-detail">
- <div className="ops-detail-hero">
- <div>
- <span className="ops-tag tone-brand">{label(PLAN_KIND, p.plan_kind) || missing(p.plan_kind)}</span>
- {p.plan_family && <span className="ops-tag tone-info">{label(PLAN_FAMILY, p.plan_family) || p.plan_family}</span>}
- </div>
- <h3>{missing(p.plan_name)}</h3>
- <p>{p.plan_id} · {p.customer_name ?? "平台公共档"}</p>
- </div>
- <section>
- <h4>01 · 档位事实</h4>
- <dl className="ops-detail-grid">
- <Row label="套餐编号">{p.plan_id}</Row>
- <Row label="套餐名称">{missing(p.plan_name)}</Row>
- <Row label="套餐种类">{label(PLAN_KIND, p.plan_kind) || missing(p.plan_kind)}</Row>
- <Row label="交付形态">{label(PLAN_FAMILY, p.plan_family) || missing(p.plan_family)}</Row>
- <Row label="年订阅价">{annual.text}</Row>
- <Row label="月结算标准">{monthly.text}</Row>
- <Row label="月成本">{cost.text}</Row>
- <Row label="分账号额度">{p.sub_account_quota ?? "未配置"}</Row>
- <Row label="带宽口径">{bandwidth.text}</Row>
- <Row label="被引用份数">{p.referred_count ?? "未下发"}</Row>
- <Row label="档位状态">{missing(p.status)}</Row>
- <Row label="套餐说明">{missing(p.plan_desc)}</Row>
- <Row label="建档时间">{dateText(p.created_at)}</Row>
- </dl>
- </section>
- <section>
- <h4>02 · 随附成本项<span>{costs.length ? `${costs.length} 项` : "暂无"}</span></h4>
- {costs.length ? <ul className="record-ref-links">{costs.map((item, index) => <li key={`${item.item_id ?? item.item_name}-${index}`}>
- <div className="record-ref-static"><Tag size={15} /><span><strong>{missing(item.item_name)}</strong><small>{[item.category, item.billing_unit, item.remark].filter(Boolean).join(" · ") || "明细未配置"}</small></span><em>{item.amount_cents === null ? "未配置" : moneyCell(item.amount_cents).text}</em></div>
- </li>)}</ul> : <p className="record-ref-status">暂无随附成本项。</p>}
- </section>
- {p.customer_id && <section>
- <h4>03 · 归属客户</h4>
- <ul className="record-ref-links">
- <li>
- <button type="button" onClick={() => onOpenCustomer({ kind: "customer", id: p.customer_id!, name: p.customer_name })}>
- <Building2 size={15} />
- <span><strong>{missing(p.customer_name ?? p.customer_id)}</strong><small>{p.customer_id}</small></span>
- <ArrowRight size={15} />
- </button>
- </li>
- </ul>
- </section>}
- <footer className="record-ref-footer">
- <Link className="ops-card-link" to="/packages" state={linkState}>
- <Package size={15} />打开套餐配置<ArrowRight size={15} />
- </Link>
- </footer>
- </div>;
- }
- function OrderPanel({ orderId, onOpenCustomer }: {
- orderId: string;
- onOpenCustomer: (target: RefTarget) => void;
- }) {
- const location = useLocation();
- const linkState = detailLinkState(location.pathname, location.state);
- const detail = useLoad(() => orderGet({ subscription_id: orderId }), [orderId]);
- if (detail.loading) return <p className="record-ref-status">正在读取订阅详情…</p>;
- if (detail.error) return <FetchError className="record-ref-status" message={`订阅详情取数失败:${detail.error.message}`} onRetry={detail.reload} />;
- const s: OrderGetResult | null = detail.data;
- if (!s) return <p className="record-ref-status">未取到该订阅详情(可能不在当前负责范围)。</p>;
- const contractKey = s.contract_id ?? s.contract_no;
- const contractHref = contractKey ? `/contracts/${encodeURIComponent(contractKey)}` : null;
- const exits = Array.isArray(s.line_exits) ? s.line_exits.filter(Boolean) : [];
- return <div className="ops-detail">
- <div className="ops-detail-hero">
- <div>
- <OpsStatus tone={statusTone(s.status)}>{label(ORDER_STATUS, s.status) || missing(s.status)}</OpsStatus>
- {s.plan_kind && <span className="ops-tag tone-brand">{label(PLAN_KIND, s.plan_kind) || s.plan_kind}</span>}
- {s.plan_family && <span className="ops-tag tone-info">{label(PLAN_FAMILY, s.plan_family) || s.plan_family}</span>}
- </div>
- <h3>{missing(s.plan_name ?? s.subscription_id)}</h3>
- <p>{s.subscription_id} · {period(s.start_date, s.end_date)}</p>
- </div>
- <section>
- <h4>01 · 订阅事实</h4>
- <dl className="ops-detail-grid">
- <Row label="订单号">{s.subscription_id}</Row>
- <Row label="套餐档">{missing(s.plan_name)}</Row>
- <Row label="套餐种类 / 形态">{label(PLAN_KIND, s.plan_kind) || missing(s.plan_kind)} / {label(PLAN_FAMILY, s.plan_family) || missing(s.plan_family)}</Row>
- <Row label="订阅方式">{s.sub_mode_label || label(ORDER_MODE, s.sub_mode) || missing(s.sub_mode)}</Row>
- <Row label="订阅时长">{label(TERM, s.term) || missing(s.term)}</Row>
- <Row label="订阅状态">{label(ORDER_STATUS, s.status) || missing(s.status)}</Row>
- <Row label="服务期">{period(s.start_date, s.end_date)}</Row>
- <Row label="生效日 / 到期日">{dateText(s.effective_date)} / {dateText(s.expire_date)}</Row>
- <Row label="实际开通日">{dateText(s.actual_opened_at)}</Row>
- <Row label="提前停机日">{dateText(s.early_stopped_at)}</Row>
- <Row label="配额槽位">{s.quota_slots ?? "未登记"}</Row>
- </dl>
- </section>
- <section>
- <h4>02 · 商务与关联</h4>
- <dl className="ops-detail-grid">
- <Row label="年订阅价格">{moneyCell(s.annual_price_cents).text}</Row>
- <Row label="月结算标准">{moneyCell(s.monthly_settle_cents).text}</Row>
- {s.trial_months ? <Row label="试用优惠">
- {s.trial_months} 个月{s.discount_monthly_cents ? ` · ${moneyCell(s.discount_monthly_cents).text} / 月` : ""}
- </Row> : null}
- <Row label="合同编号">
- {contractHref
- ? <Link className="record-ref-inline-link" to={contractHref} state={linkState}>{(s.contract_no || s.contract_id) as string}<ArrowRight size={13} /></Link>
- : "未关联合同"}
- </Row>
- <Row label="运营商订单号">{missing(s.order_no)}</Row>
- <Row label="线路出口">{exits.length ? exits.join("、") : "未登记"}</Row>
- <Row label="建档时间">{dateText(s.created_at)}</Row>
- </dl>
- </section>
- <section>
- <h4>03 · 客户主体</h4>
- <ul className="record-ref-links">
- <li>
- <button type="button" onClick={() => onOpenCustomer({ kind: "customer", id: s.customer_id, name: s.customer_name })}>
- <Building2 size={15} />
- <span>
- <strong>{missing(s.customer_name ?? s.customer_id)}</strong>
- <small>{s.customer_id}</small>
- </span>
- <ArrowRight size={15} />
- </button>
- </li>
- </ul>
- </section>
- <footer className="record-ref-footer">
- <Link className="ops-card-link" to={`/orders/${encodeURIComponent(s.subscription_id)}`} state={linkState}>
- <Package size={15} />打开完整订阅详情<ArrowRight size={15} />
- </Link>
- </footer>
- </div>;
- }
|