ReferenceDrawer.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * 工单详情右栏「客户与订阅」参考卡的弹窗(右侧抽屉)。
  3. *
  4. * 参考卡上只放摘要;客户名称、关联订阅等改为链接,点开本件按主键现取完整档案:
  5. * · 客户 → `POST /customer/get@v1`(+ `POST /subscription/get_list@v1` 列名下订阅);
  6. * · 订阅 → `POST /subscription/get@v1`(合同编号跳合同详情、客户主体可反查客户档案)。
  7. *
  8. * 取数口径遵守总则「查不到 ≠ 查不了」:请求失败走 `FetchError` 并保留重试,
  9. * 业务空值只写「未登记」,⛔ 用 0/「正常」冒充。
  10. *
  11. * 抽屉内容可以逐层深入(客户 → 名下订阅 → 客户),所以页面持有的是一个**栈**:
  12. * 本件始终渲染栈顶,栈深 > 1 时给出「返回上一级」,关闭则整栈清空。
  13. */
  14. import { useState, type ReactNode } from "react";
  15. import { Link, useLocation } from "react-router";
  16. import { ArrowLeft, ArrowRight, Building2, Package, Tag } from "lucide-react";
  17. import { FetchError } from "./fetchError";
  18. import { ListPager } from "./ListPager";
  19. import { OpsDrawer, OpsStatus } from "./OperationsKit";
  20. import { catalogGet, customerGet, orderGet, orderGetList } from "../lib/api/bos";
  21. import type { CatalogPlanRow, OrderGetResult, OrderListResult } from "../lib/api/types";
  22. import { ACCT_STATUS, PLAN_FAMILY, PLAN_KIND, ORDER_MODE, ORDER_STATUS, SUBJECT_TYPE, TERM, label } from "../lib/api/labels";
  23. import { bandwidthTextOfRow, moneyCell } from "../lib/package-catalog";
  24. import { detailLinkState } from "../lib/detail-navigation";
  25. import { LIST_PAGE_SIZE, clampPage } from "../lib/list-paging";
  26. import { fmtDate } from "../lib/ticket";
  27. import { useLoad } from "../lib/useLoad";
  28. import "../styles/work-order-record.css";
  29. /** 抽屉栈里的一层:客户档案或订阅详情,`name` 只作加载期的标题兜底。 */
  30. export type RefTarget =
  31. | { kind: "customer"; id: string; name?: string | null }
  32. | { kind: "order"; id: string; name?: string | null }
  33. | { kind: "plan"; id: string; name?: string | null };
  34. const missing = (value: string | null | undefined) => (value && value.trim() ? value : "未登记");
  35. /** 日期列(`created_at` 等)按全站口径显示到日;缺失回「未登记」,⛔ 原样吐 ISO 串。 */
  36. const dateText = (value: string | null | undefined) => (value && value.trim() ? fmtDate(value) : "未登记");
  37. const statusTone = (status: string | null | undefined) =>
  38. status === "active" ? "success" : status === "pending" ? "warning" : "neutral";
  39. function Row({ label: name, children }: { label: string; children: ReactNode }) {
  40. return <div><dt>{name}</dt><dd>{children}</dd></div>;
  41. }
  42. function period(start: string | null | undefined, end: string | null | undefined) {
  43. if (!start && !end) return "未登记";
  44. return `${start || "未登记"} 至 ${end || "未登记"}`;
  45. }
  46. export function ReferenceDrawer({ stack, onPush, onBack, onClose }: {
  47. stack: RefTarget[];
  48. onPush: (target: RefTarget) => void;
  49. onBack: () => void;
  50. onClose: () => void;
  51. }) {
  52. const top = stack[stack.length - 1];
  53. if (!top) return null;
  54. const title = top.kind === "customer" ? "客户档案" : top.kind === "plan" ? "套餐档位详情" : "订阅详情";
  55. const subtitle = top.name
  56. ? `${top.name} · ${top.id}`
  57. : top.kind === "customer" ? "客户主体参考信息" : top.kind === "plan" ? "套餐目录参考信息" : "订阅参考信息";
  58. return <OpsDrawer title={title} subtitle={subtitle} onClose={onClose}>
  59. {stack.length > 1 && <button type="button" className="record-ref-back" onClick={onBack}>
  60. <ArrowLeft size={15} />返回上一级
  61. </button>}
  62. {top.kind === "customer"
  63. ? <CustomerPanel customerId={top.id} onOpenOrder={onPush} />
  64. : top.kind === "plan"
  65. ? <PlanPanel planId={top.id} onOpenCustomer={onPush} />
  66. : <OrderPanel orderId={top.id} onOpenCustomer={onPush} />}
  67. </OpsDrawer>;
  68. }
  69. function CustomerPanel({ customerId, onOpenOrder }: {
  70. customerId: string;
  71. onOpenOrder: (target: RefTarget) => void;
  72. }) {
  73. const location = useLocation();
  74. const linkState = detailLinkState(location.pathname, location.state);
  75. const [orderPage, setOrderPage] = useState(0);
  76. const [orderPageSize, setOrderPageSize] = useState(LIST_PAGE_SIZE);
  77. const detail = useLoad(() => customerGet({ customer_id: customerId }), [customerId]);
  78. const orders = useLoad<OrderListResult>(
  79. () => orderGetList({ customer_id: customerId, page: orderPage, page_size: orderPageSize }),
  80. [customerId, orderPage, orderPageSize],
  81. );
  82. if (detail.loading) return <p className="record-ref-status">正在读取客户档案…</p>;
  83. if (detail.error) return <FetchError className="record-ref-status" message={`客户档案取数失败:${detail.error.message}`} onRetry={detail.reload} />;
  84. const c = detail.data?.customer;
  85. if (!c) return <p className="record-ref-status">未取到该客户档案(可能不在当前负责范围)。</p>;
  86. const mount = detail.data?.region_mounts?.[0]?.path_name ?? null;
  87. const teams = detail.data?.team_ids ?? [];
  88. const rows = orders.data?.items ?? [];
  89. const orderTotal = Number(orders.data?.total ?? 0);
  90. const safeOrderPage = clampPage(orderPage, orderTotal, orderPageSize);
  91. return <div className="ops-detail">
  92. <div className="ops-detail-hero">
  93. <div>
  94. <OpsStatus tone={c.acct_status === "active" || c.acct_status === "normal" ? "success" : "neutral"}>
  95. {label(ACCT_STATUS, c.acct_status) || missing(c.acct_status)}
  96. </OpsStatus>
  97. {c.subject_type && <span className="ops-tag tone-brand">{label(SUBJECT_TYPE, c.subject_type) || c.subject_type}</span>}
  98. </div>
  99. <h3>{missing(c.customer_name)}</h3>
  100. <p>{c.customer_id} · {missing(mount)}</p>
  101. </div>
  102. <section>
  103. <h4>01 · 主体与归属</h4>
  104. <dl className="ops-detail-grid">
  105. <Row label="客户主体号">{c.customer_id}</Row>
  106. <Row label="客户名称">{missing(c.customer_name)}</Row>
  107. <Row label="主体类型">{label(SUBJECT_TYPE, c.subject_type) || missing(c.subject_type)}</Row>
  108. <Row label="账户状态">{label(ACCT_STATUS, c.acct_status) || missing(c.acct_status)}</Row>
  109. <Row label="脱敏识别号">{missing(c.identity_no_masked)}</Row>
  110. <Row label="所属区域">{missing(mount)}</Row>
  111. <Row label="运营商 / 合伙人">{missing(c.operator_name)} / {missing(c.partner_name)}</Row>
  112. <Row label="承接团队">{teams.length ? `${teams.length} 支` : "未登记"}</Row>
  113. <Row label="分账号数">{detail.data?.sub_account_count ?? "未登记"}</Row>
  114. <Row label="建档时间">{dateText(c.created_at)}</Row>
  115. <Row label="最近更新">{dateText(c.updated_at)}</Row>
  116. </dl>
  117. </section>
  118. <section>
  119. <h4>02 · 联系方式</h4>
  120. <dl className="ops-detail-grid">
  121. <Row label="联系人">{missing(c.contact_name)}</Row>
  122. <Row label="联系电话">{missing(c.contact_mobile)}</Row>
  123. <Row label="通信地址">{missing(c.address)}</Row>
  124. </dl>
  125. </section>
  126. <section>
  127. <h4>03 · 名下订阅<span>{orders.loading ? "读取中…" : `共 ${orderTotal} 条`}</span></h4>
  128. {orders.error && <FetchError className="record-ref-status" message={`订阅列表取数失败:${orders.error.message}`} onRetry={orders.reload} />}
  129. {!orders.loading && !orders.error && !rows.length && <p className="record-ref-status">该客户暂无可读订阅。</p>}
  130. {rows.length > 0 && <ul className="record-ref-links">{rows.map((sub) => <li key={sub.subscription_id}>
  131. <button type="button" onClick={() => onOpenOrder({ kind: "order", id: sub.subscription_id, name: sub.plan_name })}>
  132. <Package size={15} />
  133. <span>
  134. <strong>{missing(sub.plan_name ?? sub.subscription_id)}</strong>
  135. <small>{[
  136. label(ORDER_STATUS, sub.status) || sub.status,
  137. label(TERM, sub.term),
  138. sub.actual_opened_at ? `实际开通 ${dateText(sub.actual_opened_at)}` : null,
  139. ].filter(Boolean).join(" · ") || "订阅字段未下发"}</small>
  140. </span>
  141. <ArrowRight size={15} />
  142. </button>
  143. </li>)}</ul>}
  144. <ListPager
  145. total={orderTotal}
  146. page={safeOrderPage}
  147. pageSize={orderPageSize}
  148. onPageChange={(page) => setOrderPage(page)}
  149. onPageSizeChange={(size) => { setOrderPageSize(size); setOrderPage(0); }}
  150. unit="条"
  151. />
  152. <p className="record-ref-note">订阅行来自 <code>/subscription/get_list@v1</code>(默认每页 {LIST_PAGE_SIZE},由本抽屉请求决定),点开按订单号取详情。</p>
  153. </section>
  154. <footer className="record-ref-footer">
  155. <Link className="ops-card-link" to={`/customers/${encodeURIComponent(c.customer_id)}`} state={linkState}>
  156. <Building2 size={15} />打开完整客户档案<ArrowRight size={15} />
  157. </Link>
  158. </footer>
  159. </div>;
  160. }
  161. /** 套餐档位详情:合同条目的「套餐 / 配置」是外部引用,点开按 `plan_id` 现取目录档。 */
  162. function PlanPanel({ planId, onOpenCustomer }: {
  163. planId: string;
  164. onOpenCustomer: (target: RefTarget) => void;
  165. }) {
  166. const location = useLocation();
  167. const linkState = detailLinkState(location.pathname, location.state);
  168. const detail = useLoad(() => catalogGet({ plan_id: planId }), [planId]);
  169. if (detail.loading) return <p className="record-ref-status">正在读取套餐档位…</p>;
  170. if (detail.error) return <FetchError className="record-ref-status" message={`套餐档位取数失败:${detail.error.message}`} onRetry={detail.reload} />;
  171. const p: CatalogPlanRow | null = detail.data;
  172. if (!p) return <p className="record-ref-status">未取到该套餐档位(可能已删除或不在当前负责范围)。</p>;
  173. const bandwidth = bandwidthTextOfRow(p);
  174. const annual = moneyCell(p.annual_price_cents);
  175. const monthly = moneyCell(p.monthly_settle_cents);
  176. const cost = moneyCell(p.plan_cost_cents);
  177. const costs = detail.data?.cost_items ?? [];
  178. return <div className="ops-detail">
  179. <div className="ops-detail-hero">
  180. <div>
  181. <span className="ops-tag tone-brand">{label(PLAN_KIND, p.plan_kind) || missing(p.plan_kind)}</span>
  182. {p.plan_family && <span className="ops-tag tone-info">{label(PLAN_FAMILY, p.plan_family) || p.plan_family}</span>}
  183. </div>
  184. <h3>{missing(p.plan_name)}</h3>
  185. <p>{p.plan_id} · {p.customer_name ?? "平台公共档"}</p>
  186. </div>
  187. <section>
  188. <h4>01 · 档位事实</h4>
  189. <dl className="ops-detail-grid">
  190. <Row label="套餐编号">{p.plan_id}</Row>
  191. <Row label="套餐名称">{missing(p.plan_name)}</Row>
  192. <Row label="套餐种类">{label(PLAN_KIND, p.plan_kind) || missing(p.plan_kind)}</Row>
  193. <Row label="交付形态">{label(PLAN_FAMILY, p.plan_family) || missing(p.plan_family)}</Row>
  194. <Row label="年订阅价">{annual.text}</Row>
  195. <Row label="月结算标准">{monthly.text}</Row>
  196. <Row label="月成本">{cost.text}</Row>
  197. <Row label="分账号额度">{p.sub_account_quota ?? "未配置"}</Row>
  198. <Row label="带宽口径">{bandwidth.text}</Row>
  199. <Row label="被引用份数">{p.referred_count ?? "未下发"}</Row>
  200. <Row label="档位状态">{missing(p.status)}</Row>
  201. <Row label="套餐说明">{missing(p.plan_desc)}</Row>
  202. <Row label="建档时间">{dateText(p.created_at)}</Row>
  203. </dl>
  204. </section>
  205. <section>
  206. <h4>02 · 随附成本项<span>{costs.length ? `${costs.length} 项` : "暂无"}</span></h4>
  207. {costs.length ? <ul className="record-ref-links">{costs.map((item, index) => <li key={`${item.item_id ?? item.item_name}-${index}`}>
  208. <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>
  209. </li>)}</ul> : <p className="record-ref-status">暂无随附成本项。</p>}
  210. </section>
  211. {p.customer_id && <section>
  212. <h4>03 · 归属客户</h4>
  213. <ul className="record-ref-links">
  214. <li>
  215. <button type="button" onClick={() => onOpenCustomer({ kind: "customer", id: p.customer_id!, name: p.customer_name })}>
  216. <Building2 size={15} />
  217. <span><strong>{missing(p.customer_name ?? p.customer_id)}</strong><small>{p.customer_id}</small></span>
  218. <ArrowRight size={15} />
  219. </button>
  220. </li>
  221. </ul>
  222. </section>}
  223. <footer className="record-ref-footer">
  224. <Link className="ops-card-link" to="/packages" state={linkState}>
  225. <Package size={15} />打开套餐配置<ArrowRight size={15} />
  226. </Link>
  227. </footer>
  228. </div>;
  229. }
  230. function OrderPanel({ orderId, onOpenCustomer }: {
  231. orderId: string;
  232. onOpenCustomer: (target: RefTarget) => void;
  233. }) {
  234. const location = useLocation();
  235. const linkState = detailLinkState(location.pathname, location.state);
  236. const detail = useLoad(() => orderGet({ subscription_id: orderId }), [orderId]);
  237. if (detail.loading) return <p className="record-ref-status">正在读取订阅详情…</p>;
  238. if (detail.error) return <FetchError className="record-ref-status" message={`订阅详情取数失败:${detail.error.message}`} onRetry={detail.reload} />;
  239. const s: OrderGetResult | null = detail.data;
  240. if (!s) return <p className="record-ref-status">未取到该订阅详情(可能不在当前负责范围)。</p>;
  241. const contractKey = s.contract_id ?? s.contract_no;
  242. const contractHref = contractKey ? `/contracts/${encodeURIComponent(contractKey)}` : null;
  243. const exits = Array.isArray(s.line_exits) ? s.line_exits.filter(Boolean) : [];
  244. return <div className="ops-detail">
  245. <div className="ops-detail-hero">
  246. <div>
  247. <OpsStatus tone={statusTone(s.status)}>{label(ORDER_STATUS, s.status) || missing(s.status)}</OpsStatus>
  248. {s.plan_kind && <span className="ops-tag tone-brand">{label(PLAN_KIND, s.plan_kind) || s.plan_kind}</span>}
  249. {s.plan_family && <span className="ops-tag tone-info">{label(PLAN_FAMILY, s.plan_family) || s.plan_family}</span>}
  250. </div>
  251. <h3>{missing(s.plan_name ?? s.subscription_id)}</h3>
  252. <p>{s.subscription_id} · {period(s.start_date, s.end_date)}</p>
  253. </div>
  254. <section>
  255. <h4>01 · 订阅事实</h4>
  256. <dl className="ops-detail-grid">
  257. <Row label="订单号">{s.subscription_id}</Row>
  258. <Row label="套餐档">{missing(s.plan_name)}</Row>
  259. <Row label="套餐种类 / 形态">{label(PLAN_KIND, s.plan_kind) || missing(s.plan_kind)} / {label(PLAN_FAMILY, s.plan_family) || missing(s.plan_family)}</Row>
  260. <Row label="订阅方式">{s.sub_mode_label || label(ORDER_MODE, s.sub_mode) || missing(s.sub_mode)}</Row>
  261. <Row label="订阅时长">{label(TERM, s.term) || missing(s.term)}</Row>
  262. <Row label="订阅状态">{label(ORDER_STATUS, s.status) || missing(s.status)}</Row>
  263. <Row label="服务期">{period(s.start_date, s.end_date)}</Row>
  264. <Row label="生效日 / 到期日">{dateText(s.effective_date)} / {dateText(s.expire_date)}</Row>
  265. <Row label="实际开通日">{dateText(s.actual_opened_at)}</Row>
  266. <Row label="提前停机日">{dateText(s.early_stopped_at)}</Row>
  267. <Row label="配额槽位">{s.quota_slots ?? "未登记"}</Row>
  268. </dl>
  269. </section>
  270. <section>
  271. <h4>02 · 商务与关联</h4>
  272. <dl className="ops-detail-grid">
  273. <Row label="年订阅价格">{moneyCell(s.annual_price_cents).text}</Row>
  274. <Row label="月结算标准">{moneyCell(s.monthly_settle_cents).text}</Row>
  275. {s.trial_months ? <Row label="试用优惠">
  276. {s.trial_months} 个月{s.discount_monthly_cents ? ` · ${moneyCell(s.discount_monthly_cents).text} / 月` : ""}
  277. </Row> : null}
  278. <Row label="合同编号">
  279. {contractHref
  280. ? <Link className="record-ref-inline-link" to={contractHref} state={linkState}>{(s.contract_no || s.contract_id) as string}<ArrowRight size={13} /></Link>
  281. : "未关联合同"}
  282. </Row>
  283. <Row label="运营商订单号">{missing(s.order_no)}</Row>
  284. <Row label="线路出口">{exits.length ? exits.join("、") : "未登记"}</Row>
  285. <Row label="建档时间">{dateText(s.created_at)}</Row>
  286. </dl>
  287. </section>
  288. <section>
  289. <h4>03 · 客户主体</h4>
  290. <ul className="record-ref-links">
  291. <li>
  292. <button type="button" onClick={() => onOpenCustomer({ kind: "customer", id: s.customer_id, name: s.customer_name })}>
  293. <Building2 size={15} />
  294. <span>
  295. <strong>{missing(s.customer_name ?? s.customer_id)}</strong>
  296. <small>{s.customer_id}</small>
  297. </span>
  298. <ArrowRight size={15} />
  299. </button>
  300. </li>
  301. </ul>
  302. </section>
  303. <footer className="record-ref-footer">
  304. <Link className="ops-card-link" to={`/orders/${encodeURIComponent(s.subscription_id)}`} state={linkState}>
  305. <Package size={15} />打开完整订阅详情<ArrowRight size={15} />
  306. </Link>
  307. </footer>
  308. </div>;
  309. }