import { useBusinessView } from "../lib/use-business-view"; import { useState } from "react"; import { FetchError } from "../components/fetchError"; import { ArrowLeft, ArrowRight, CalendarPlus, Layers3, RefreshCw } from "lucide-react"; import { Link, useLocation, useParams } from "react-router"; import { OpsMore, OpsNotice, OpsStatus } from "../components/OperationsKit"; import { OrderEditor, StackEditor } from "../components/OrderChangeDialogs"; import { Tri } from "../components/triState"; import { getBillableStartDate } from "../domain/order-dates"; import { contractRawByParam, contractStatusOf } from "../lib/contract-demo"; import { orderView, useAllOrders } from "../lib/order-demo"; import { bandwidthTextOfRow, moneyCell, useCatalogPlans } from "../lib/package-catalog"; import { rowsOf, customerGet, subAccountGetList } from "../lib/api/bos"; import { ticketList, statusLabel, typeLabel } from "../lib/api/ticket"; import { fmtDate } from "../lib/ticket"; import { ACCT_STATUS, SUBJECT_TYPE, label, PLAN_FAMILY } from "../lib/api/labels"; import { regionMountPath } from "../lib/organization-demo"; import { WORD_FETCH_FAILED, WORD_UNCONFIGURED } from "../lib/api/lockedCopy"; import { useLoad } from "../lib/useLoad"; import { detailBack, detailLinkState } from "../lib/detail-navigation"; import "../styles/order-record.css"; const statusTone = (status: string) => status === "生效中" || status === "在用" ? "success" : status === "停用" ? "danger" : status === "待生效" || status === "未开通" ? "warning" : "neutral"; /** 三态文本格:键缺失=取数失败,null=未配置,⛔ 用 0/「—」冒充有值。 */ function cell(value: string | null | undefined, missing: string = WORD_UNCONFIGURED) { if (value === undefined) return ; if (value === null || value === "") return ; return value; } const moneyCellView = (c: { tone: "val" | "missing" | "fail"; text: string }) => (c.tone === "val" ? {c.text} : ); export function OrderRecordPage() { // 本页不直接读组织集合:工单口径的 `scopedTeamTickets` 所需团队名单由 `useBusinessView` 按身份补齐。 const view = useBusinessView([]); const { id } = useParams(); const param = decodeURIComponent(id ?? ""); const location = useLocation(); const back = detailBack(location.state, view.customerView ? "/workspace/plan" : view.readOnly ? "/customers" : "/orders", view.customerView ? "返回我的套餐" : view.readOnly ? `返回${view.customerListTitle}` : "返回全部订阅"); const outgoingState = detailLinkState(location.pathname, location.state); /** 台账与详情同源取数:数据范围由服务端裁剪,⛔ 在此再按身份/客户过滤。 */ const orders = useAllOrders(); const all = orders.data === undefined ? [] : rowsOf(orders.data).map(orderView); const item = all.find((record) => record.no === param); const catalog = useCatalogPlans(); const plan = item?.planId ? rowsOf(catalog.data?.items).find((record) => record.plan_id === item.planId) : undefined; const contractId = item?.contractId ?? ""; const contract = useLoad(async () => (contractId ? await contractRawByParam(contractId) : null), [contractId]); /** * ④-A(2026-11-14):客户档案真源=`POST /customer/get@v1`,分账号计数=`/sub_account/get_list@v1`。 * * 此前本节从 `view.customers`(浏览器演示存储 `oss-customer-records-v1`)反查客户,而真实登录态下 * 那份存储是空的 ⇒ `customer` 恒 undefined ⇒ 页头「发起服务停用」对任何运营角色都不渲染 * (P-50 FAIL:接口层早已打通,界面层从未可达),主体类型/识别号/账户状态也一律显示「未登记」。 * 档案取数失败⛔ 熄灭写入口(判据只看订阅行自带的 `customer_id`),只在展示格标取数失败。 */ const customerId = item?.customerId ?? ""; const cust = useLoad(() => (customerId ? customerGet({ customer_id: customerId }) : Promise.resolve(null)), [customerId]); const accounts = useLoad(() => (customerId ? subAccountGetList({ customer_id: customerId, page: 0, page_size: 200 }) : Promise.resolve(null)), [customerId]); /** * 关联工单(P3-D6):改读 `/ticket/list@v1` 的 `subscription_id` 过滤键。 * * 旧口径从浏览器演示存储 `oss-work-order-records-v2` 里按 `orderId` 过滤——真实登录态里 * 那份存储是空的 ⇒ 「关联工单(0 张)」常驻,即便开户工单已经落在 `ops_work_order`。 * 现在按本条订单号现查服务端台账(可见性由服务端裁剪,⛔ 前端再按身份过滤)。 */ const relatedTickets = useLoad( () => (item?.no ? ticketList({ subscription_id: item.no, page: 0, page_size: 20 }) : Promise.resolve(null)), [item?.no], ); /** 升降配/叠加:本条订阅就地办理(判据同全部订阅页=生效中且非只读身份),⛔ 再跳一次列表页。 */ const [changeOpen, setChangeOpen] = useState(false); const [stackOpen, setStackOpen] = useState(false); const [notice, setNotice] = useState(""); if (orders.loading) return
{back.label}

正在从订阅接口读取订阅记录…

; if (orders.error) return
{back.label}

订阅记录取数失败

; if (!item) return
{back.label}

订阅不存在或不在当前负责范围

该记录可能已被删除,或订阅编号已变更。

; const contractRow = contract.data; const contractState = contractRow ? contractStatusOf(contractRow.start_date ?? "", contractRow.end_date ?? "") : undefined; const siblings = all.filter((record) => record.contractId && record.contractId === item.contractId && record.no !== item.no); const billableStart = item.start ? getBillableStartDate(item.start, item.openedOn ?? null) : null; const allCustomerSubs = all.filter((record) => record.customerId === item.customerId); /** 客户档案行(`/customer/get@v1.customer`):取数中/取数失败时由 `custCell` 承接三态。 */ const custRow = cust.data?.customer; const accountRows = accounts.data?.items ?? []; const custCell = (what: string) => cust.loading ? : cust.error ? : ; /** 档案已取到则显示真值(空值按 `empty` 说明),未取到一律走三态,⛔ 冒充「未登记」。 */ const custText = (value: string | null | undefined, empty: string, what: string) => (custRow ? value || empty : custCell(what)); /** * 契约里 `CustomerDetail.region_mounts` 是必有键,`/customer/get@v1` 实测确实下发 * (2026-11-14 复核更正:此前"15 个键里没有它"是取错了接口所致)⇒ 键缺失才按「接口未回」显示, * ⛔ 说成「未挂载区域」。区域名走 [`regionMountPath`](../lib/organization-demo):优先后端下发的 * `path_name`,区域树只下发省级两层、挂载点常在市/区县层,光靠树查会显示「—」。 */ const regionMounts = cust.data?.region_mounts; const regionText = regionMounts === undefined ? custCell("所属区域") : regionMounts.length === 0 ? "未挂载区域" : `${regionMountPath(view.organization, regionMounts[0]) || custCell("所属区域")}${regionMounts.length > 1 ? ` 等 ${regionMounts.length} 个挂载点` : ""}`; /** 客户视图的工单可见性由服务端裁剪;本页只按订单号现查(见上方 `relatedTickets`)。 */ const ticketRows = relatedTickets.data?.rows ?? []; /** 可否编辑取后端下发的 `editable`,⛔ 前端按状态/角色自行推导。 */ const editable = item.editable === true; const editReason = item.editable === undefined ? "写口可见性未随记录下发,按只读呈现。" : item.editable ? "服务端判定可写;已开通计费的订阅请新增替换订阅,不改写本条。" : "服务端判定本条只读。"; const bandwidth = plan ? bandwidthTextOfRow(plan) : null; const catalogMatches = plan !== undefined && plan.annual_price_cents === item.row.annual_price_cents && plan.monthly_settle_cents === item.row.monthly_settle_cents; const orderKind = item.row.plan_family === "addon" ? "叠加" : "正式"; const catalogMode = item.row.plan_family === "addon" ? "可叠加" : "可升降配"; /** * 出账四卡(第 6 册):取订阅行随附的 `billing` 聚合,⛔ 前端按价格×期数推算。 * 键未下发 ⇒ 取数失败;`no_receivable` ⇒ 四值 `null` → 未配置(⛔ 0 元/「未出账」)。 */ const billingCell = (key: "receivable" | "billed" | "unbilled" | "cost", what: string) => { const c = item.billing?.[key]; if (!c) return ; return c.tone === "val" ? {c.text} : ; }; const billingHint = item.billing ? item.billing.periods > 0 ? `${item.billing.periods} 个应收期次 · 第 6 册口径` : "尚无应收期次" : "出账聚合未随订阅行下发"; return
{back.label}
SUBSCRIPTION / {item.no || WORD_FETCH_FAILED}

{view.customerView ? "套餐详情" : "订阅详情"}

{cell(item.planName)}{cell(item.customer)}合同 {cell(item.contractNo)}订阅 {cell(item.orderNo, "无运营商订单号")}{item.parentSubscriptionId && 附加于 {item.parentSubscriptionId}}第 {all.indexOf(item) + 1} 号订阅

{orderKind}订阅{item.planKindLabel === undefined || item.planKindLabel === null ? (item.planKindLabel === undefined ? WORD_FETCH_FAILED : WORD_UNCONFIGURED) : item.planKindLabel}{item.statusLabel ? {item.statusLabel} : }
{/* ⑦(2026-11-16):升降配/叠加收进页头「更多」——单笔替换的办理点在详情页, 「全部订阅」(`/orders`)已按需求 5.2 收敛为只读查询、⛔ 再挂写入口。 只对「生效中」且非只读身份挂出;原「套餐订阅管理」跳转一并下线(变更不必离页)。 */} {!view.readOnly && !view.customerView && item.statusLabel === "生效中" &&
, title: "新建替换订阅,原订阅保留为历史记录", onSelect: () => setChangeOpen(true) }, { label: "叠加", icon: , title: "只改本条订阅的服务结束日期", onSelect: () => setStackOpen(true) }, ]} />
} {/* ④-A:入口判据=订阅行自带的 `customer_id`(服务端真源)+ 运营侧角色; 「仅在用客户/有无生效中订阅/有无在途停机单」交服务端裁决,⛔ 在此读演示存储预判。 */} {!view.readOnly && item.customerId && }
年订阅价格{item.annual.tone === "val" ? item.annual.text : }本条订阅的冻结价格
月结算标准{item.settle.tone === "val" ? item.settle.text : }首末月按在网天数折算
{!view.customerView && <>
应收合计{billingCell("receivable", "应收合计")}{billingHint}
已出账金额{billingCell("billed", "已出账金额")}已执行批次明细合计
未出账金额{billingCell("unbilled", "未出账金额")}应收合计 − 已出账(不足按 0)
{!view.readOnly &&
累计成本{billingCell("cost", "累计成本")}应收期次成本扣减合计
} } {view.customerView && <>
分账号额度{item.quotaSlots === undefined ? : item.quotaSlots === null ? : item.quotaSlots}本条订阅约定额度
总带宽{bandwidth ? (bandwidth.tone === "val" ? bandwidth.text : ) : }取目录档位规格
}

本条订阅

{item.trialMonths ? : null}
套餐档{cell(item.planName)}{plan ? · {label(PLAN_FAMILY, plan.plan_family) || "形态未填"} · 目录年价 {moneyCell(plan.annual_price_cents).text} / 月标准 {moneyCell(plan.monthly_settle_cents).text} · 额度 {plan.sub_account_quota ?? WORD_UNCONFIGURED} : · {catalog.error ? `目录档位取数失败:${catalog.error.message}` : "目录档位未取到,本条按冻结价继续计费"}}
冻结价格年订阅价格 {moneyCellView(item.annual)} · 月结算标准 {moneyCellView(item.settle)}({plan ? catalogMatches ? "与当前目录价一致" : "与当前目录价不同,以本条冻结值为准" : "目录档位未取到,以本条冻结值为准"})
订阅方式{item.modeLabel || WORD_UNCONFIGURED} · 交付形态({item.familyLabel || WORD_UNCONFIGURED}){catalogMode}{item.original && <> · 替换 {item.original.subscription_id}}{item.replacedBy && <> · 已被 {item.replacedBy.subscription_id} 替换}
订阅状态{item.statusLabel ? {item.statusLabel} : }{item.reason && · 办理说明:{item.reason}}
起止时间{cell(item.start)} 至 {cell(item.end)} · 应收从实际开通日与合同生效日的较晚者起算{billableStart ? `(${billableStart})` : "(尚未开通)"}
试用优惠{item.trialMonths} 个月 · {item.trialDiscount === undefined ? : item.trialDiscount === null ? "优惠金额未填写" : `${moneyCell(item.trialDiscount).text} / 月`}
线路出口{item.exits === undefined ? : item.exits === null ? : item.exits.length ? item.exits.join("、") : } · 随合同服务构成条目带出,保存时冻结
合同编号{cell(item.contractNo)}{contract.error ? (合同记录取数失败:{contract.error.message}) : contract.loading ? (正在读取合同…) : contractRow ? <> · {contractRow.start_date ?? WORD_UNCONFIGURED} 至 {contractRow.end_date ?? WORD_UNCONFIGURED} · 合同状态 {contractState ?? WORD_UNCONFIGURED} · 合同总额 {moneyCell(contractRow.amount_cents).text} 查看合同 : (合同记录不存在或未下发 contract_id)}
订单号{cell(item.orderNo, "无运营商订单号")} · 保存时从合同外部单号冻结;订阅写入方在第4册{!view.readOnly && item.orderNo && <> · 按该单查询}
实际开通日{item.openedOn === undefined ? : item.openedOn ? item.openedOn : } · 由运维回填,本册只读
可否编辑{item.editable === undefined ? "未知" : editable ? "可编辑" : "只读"} · {editReason}{editable && <> · 前往办理 }

订阅是计费与开通的最小单位。价格与线路出口在保存时冻结;续订、升降配与叠加均新增记录,原订阅保留。本条订阅的升降配/叠加在页头「更多」办理,新增订阅在「客户管理 → 套餐订阅」办理。

{siblings.length > 0 &&
{siblings.map((sibling) => )}
序号套餐 / 用途方式订阅状态期间订阅价格月标准实际开通日
{all.indexOf(sibling) + 1}{cell(sibling.planName)}{sibling.row.plan_family === "addon" ? "叠加" : "正式"}{sibling.statusLabel ? {sibling.statusLabel} : }{cell(sibling.start)} 至 {cell(sibling.end)}{sibling.annual.tone === "val" ? sibling.annual.text : }{sibling.settle.tone === "val" ? sibling.settle.text : }{sibling.openedOn === undefined ? : sibling.openedOn || }

这些订阅与本条引用同一份合同{item.orderNo ? `(订单号 ${item.orderNo})` : ""}。合同中每条服务构成条目分别形成订阅,各自冻结价格、计费与开通;点击序号查看详情。

} {!view.customerView &&

计费与出账

{item.billing ? <>
应收合计已出账金额未出账金额累计成本应收期次
{billingCell("receivable", "应收合计")} {billingCell("billed", "已出账金额")} {billingCell("unbilled", "未出账金额")} {billingCell("cost", "累计成本")} {item.billing.periods > 0 ? item.billing.periods : }

取数口径(第 6 册):应收合计/累计成本=`billing_receivable` 按订阅求和的应收与成本扣减;已出账=`bill_detail` 中已执行批次、未作废明细的分成基数合计;未出账=应收合计 − 已出账(不足按 0)。逐期清单与对账批次状态请到对账页核对。

: item.billing === null ?

{WORD_UNCONFIGURED}——本条订阅尚无应收期次,四卡与期次数不显示 0 元冒充结论。

:

出账聚合取数失败,不以 0 元或 0 期兜底。

}
} {!view.identity || !["partner", "operator"].includes(view.identity.role) &&

关联工单({relatedTickets.loading ? "…" : ticketRows.length} 张)

{relatedTickets.loading ?

正在按订单号读取工单台账…

: relatedTickets.error ?

工单台账取数失败,不得把「查不了」渲染成「0 张」。

: ticketRows.length ?
{ticketRows.map((ticket) => )}
我方工单号类型状态创建日完成日
{ticket.ticket_no ?? ticket.ticket_id}{typeLabel(ticket.ticket_type)}{statusLabel(ticket.status, ticket.ticket_type)}{fmtDate(ticket.created_at)}{fmtDate(ticket.completed_at)}
:

本条订阅暂无关联工单(按订单号现查 /ticket/list@v1,服务端可见范围内为空)。

}

订阅保存后,未开户走开户工单,已在用走服务开通工单;运维回填的实际开通日写回本条订阅。点击工单号查看详情。

}

合同文件(随本条订阅留档)

{WORD_UNCONFIGURED}——订阅记录未下发文件字段,合同扫描件只随合同记录留档{contractRow ? <>,可在上方「查看合同」中查看 : "。"}

合同文件挂在订阅上,仅保存文档名与存储键,不解析正文;后端未提供上传与私有桶下载接口。

{!view.customerView &&

相关客户主体

客户档案

下面是本条订阅所属客户的主体信息;归属变更、主账号与全部分账号请到客户档案查看。

客户主体{item.customerId || WORD_UNCONFIGURED} · {cell(custRow?.customer_name ?? item.customer)}({custRow ? label(SUBJECT_TYPE, custRow.subject_type) : custCell("主体类型")} · 识别号 {custText(custRow?.identity_no_masked, "未登记", "识别号")})
账户状态{custRow ? {label(ACCT_STATUS, custRow.acct_status)} : custCell("账户状态")} · 所属区域 {regionText} · 运营商 {custText(custRow?.operator_name, "未关联", "运营商")} / 合伙人 {custText(custRow?.partner_name, "无", "合伙人")}
经办联系人{custText(custRow?.contact_name, "未填", "经办联系人")} / {custText(custRow?.contact_mobile, "未填", "经办联系电话")} · 地址 {custText(custRow?.address, "未填", "装机地址")}
分账号{accounts.error ? : accounts.loading ? : <>共 {accounts.data?.total ?? 0} 个 / 额度 {custText(custRow?.quota_cents, WORD_UNCONFIGURED, "分账号额度")} · 在用 {accountRows.filter((row) => row.status === "in_use").length} · 本条订阅计入额度 {item.quotaSlots === undefined ? WORD_FETCH_FAILED : item.quotaSlots === null ? WORD_UNCONFIGURED : item.quotaSlots}(未到期订阅按档位额度累加)}
该客户订阅共 {allCustomerSubs.length} 份,其中生效中 {allCustomerSubs.filter((record) => record.statusLabel === "生效中").length} 份
} {notice && setNotice("")} />} {changeOpen && setChangeOpen(false)} onSaved={(message) => { setChangeOpen(false); setNotice(message); orders.reload(); }} />} {stackOpen && setStackOpen(false)} onSaved={(message) => { setStackOpen(false); setNotice(message); orders.reload(); }} />}
; } import { CustomerServiceStopAction } from "../components/CustomerServiceStopAction";