customer-demo.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * 第 2 册(客户)台账的**视图模型** —— 真源=klt-bos 接口,⛔ 内置演示行。
  3. *
  4. * 后端边界(页面如实呈现,⛔ 造数填补):
  5. * * `/customer/get_list@v1` 实测内联 14+ 字段(含 `parent_node_id` 与 `ops_team_names`,2026-11-12 后端扩展)⇒
  6. * 本页展示列**一次列表口即够**,⛔ 逐行 N+1 补详情(缺口清单 C-7);
  7. * * 所属区域:列表口返回挂载节点 ID(`parent_node_id`),区域路径由前端用一次 `/region/tree@v1` 的
  8. * `nodes[].path_name` 字典解析(⛔ 逐行走 `/customer/get@v1` 再拼树);
  9. * * `/import_batch/get_list@v1` ⛔ 下发文件名与行明细(计数为字符串)⇒ 只展示后端给的列。
  10. */
  11. import { useEffect, useMemo, useState } from "react";
  12. import { ACCT_STATUS, IMPORT_STATUS, SUBJECT_TYPE, VALIDATE_STATUS, label } from "./api/labels";
  13. import { regionTree } from "./api/bos";
  14. import type { CustomerListRow, ImportBatchRowT, ImportRowDetail, ImportSubmitResult, RegionTreeResult } from "./api/types";
  15. import type { StatusTone } from "../domain/statuses";
  16. import { ApiError } from "./api/http";
  17. import { useCustomerLedger, type CustomerLedgerQuery } from "./customer-list-store";
  18. /** 客户台账视图行;`null` =后端未下发该维度,由页面用 `<Tri>` 呈现,⛔ 填「未知」「—」等占位值。 */
  19. export type DemoCustomer = {
  20. id: string;
  21. name: string | null;
  22. subject: string | null;
  23. operator: string | null;
  24. partner: string | null;
  25. subAccounts: number | null;
  26. status: string | null;
  27. activatedAt: string | null;
  28. /** 后端挂载节点 ID(来自 `customer_region_mount.parent_node_id`),前端用于查区域路径。 */
  29. parentNodeId: string | null;
  30. /** 运维团队中文名数组(后端 JOIN ops_team 批量查,2026-11-12 起已内联)。 */
  31. opsTeamNames: string[];
  32. /** 在用订阅的套餐名数组(后端批量查 order;空数组=无在用订阅,非「后端未下发」)。 */
  33. planNames: string[];
  34. /** 挂载区域完整路径("全网/上海市/市辖区/浦东新区")。后端不下发路径,前端用 region/tree 解析。 */
  35. regionPath: string | null;
  36. /** 脱敏识别号(后端 `identity_no_masked`);`null`=该客户未登记识别号。 */
  37. identityNo: string | null;
  38. };
  39. export type ImportRowResult = {
  40. no: string;
  41. ok: boolean;
  42. /** 校验态中文(`VALIDATE_STATUS` 映射结果);后端未回则为 null。 */
  43. status: string | null;
  44. /** 后端 `error_list` 原文,⛔ 前端改写。 */
  45. reason?: string;
  46. };
  47. /** 导入批次台账视图行;`file`/`rows` ⛔ 后端下发时为 null,页面标「后端未下发」。 */
  48. export type DemoImportBatch = {
  49. id: string;
  50. batchNo: string | null;
  51. file: string | null;
  52. status: string | null;
  53. total: number | null;
  54. ok: number | null;
  55. fail: number | null;
  56. createdAt: string | null;
  57. importer: string | null;
  58. rows: ImportRowResult[] | null;
  59. };
  60. /** ⛔ 内置演示客户行——客户台账唯一真源是 `POST /customer/get_list@v1`。 */
  61. export const initialCustomers: DemoCustomer[] = [];
  62. /** ⛔ 内置演示批次——台账真源是 `POST /import_batch/get_list@v1`;本导出仅为 `lib/import-batch-store.ts` 的兜底签名保留。 */
  63. export const initialImportBatches: DemoImportBatch[] = [];
  64. /** 导入批次状态(中文标签)→ 状态色。 */
  65. export const importBatchTone: Record<string, StatusTone> = {
  66. 已完成: "success",
  67. 已校验: "info",
  68. 部分失败: "danger",
  69. 处理中: "info",
  70. 导入中: "info",
  71. 校验中: "info",
  72. };
  73. /** 状态原文 → 中文标签 → 色调;后端未回状态时落 `neutral`,⛔ 猜一个「处理中」。 */
  74. export function importBatchToneOf(raw: string | null | undefined): StatusTone {
  75. if (raw === null || raw === undefined || raw === "") return "neutral";
  76. return importBatchTone[label(IMPORT_STATUS, raw)] ?? "neutral";
  77. }
  78. /** 标签化:后端缺列(null/空串)一律返回 null,⛔ 显示成 0 或「未知」。 */
  79. function text(value: string | null | undefined): string | null {
  80. const v = (value ?? "").trim();
  81. return v ? v : null;
  82. }
  83. /** 区域树节点 → `{node_id → path_name}` 字典,用于把客户的 `parent_node_id` 映射成完整区域路径。 */
  84. function buildRegionPathMap(tree: RegionTreeResult | null | undefined): Record<string, string> {
  85. const map: Record<string, string> = {};
  86. if (!tree) return map;
  87. const nodes = (tree as unknown as { nodes?: Array<{ node_id?: string; path_name?: string; name?: string }> }).nodes ?? [];
  88. for (const n of nodes) {
  89. const id = n.node_id;
  90. const path = n.path_name ?? n.name;
  91. if (id && path) map[id] = path;
  92. }
  93. return map;
  94. }
  95. /** `/customer/get_list@v1` 行 → 视图行;`regionPathMap` 只在后端未下发 `region_path` 时兜底。 */
  96. export function customerListRowToDemo(row: CustomerListRow, regionPathMap?: Record<string, string>): DemoCustomer {
  97. const pn = text(row.parent_node_id);
  98. // 优先后端下发的挂载路径(层级真源 `bos_region_meta.path_name`):区域树只下发授权范围内两层,
  99. // 挂载点常是市/区县级 ⇒ 光按树查必落空(2026-11-15 列表「所属区域」全列「后端未下发」、
  100. // 导出激活码把全部客户计入「无法判定区域」实证)。`/` 归一为 `" / "`,与 `regionPath` 同一口径。
  101. const backendPath = text(row.region_path);
  102. const regionPath = backendPath
  103. ? backendPath.split("/").map((s) => s.trim()).filter(Boolean).join(" / ")
  104. : pn ? (regionPathMap?.[pn] ?? null) : null;
  105. return {
  106. id: row.customer_id,
  107. name: text(row.customer_name),
  108. subject: text(row.subject_type) ? label(SUBJECT_TYPE, row.subject_type) : null,
  109. operator: text(row.operator_name),
  110. partner: text(row.partner_name),
  111. subAccounts: Number.isFinite(row.sub_account_count) ? row.sub_account_count : null,
  112. status: text(row.acct_status) ? label(ACCT_STATUS, row.acct_status) : null,
  113. activatedAt: text(row.created_at),
  114. parentNodeId: pn,
  115. opsTeamNames: (row.ops_team_names ?? []).map((s) => s.trim()).filter(Boolean),
  116. planNames: (row.plan_names ?? []).map((s) => s.trim()).filter(Boolean),
  117. regionPath,
  118. identityNo: text(row.identity_no_masked),
  119. };
  120. }
  121. /** `/import_batch/get_list@v1` 行 → 视图行;计数是字符串,空串按「后端未下发」处理。 */
  122. export function importBatchRowToDemo(row: ImportBatchRowT): DemoImportBatch {
  123. const num = (v: string | null | undefined) => {
  124. const s = (v ?? "").trim();
  125. if (!s) return null;
  126. const n = Number(s);
  127. return Number.isFinite(n) ? n : null;
  128. };
  129. return {
  130. id: row.batch_id,
  131. batchNo: text(row.batch_no),
  132. file: null,
  133. status: text(row.status) ? label(IMPORT_STATUS, row.status) : null,
  134. total: num(row.total_rows),
  135. ok: num(row.success_rows),
  136. fail: num(row.fail_rows),
  137. createdAt: text(row.created_at),
  138. importer: text(row.operator_username),
  139. rows: null,
  140. };
  141. }
  142. /** `submit` 同步回执的行校验态 → 视图行。 */
  143. export function submitRowsToDemo(rows: ImportSubmitResult["rows"]): ImportRowResult[] {
  144. return (rows ?? []).map((row) => ({
  145. no: String(row.row_no),
  146. ok: row.validate_status === "valid",
  147. status: text(row.validate_status) ? label(VALIDATE_STATUS, row.validate_status) : null,
  148. reason: (row.error_list ?? []).join(";") || undefined,
  149. }));
  150. }
  151. export type CustomerDirectory = {
  152. rows: DemoCustomer[];
  153. total: number;
  154. loading: boolean;
  155. error: ApiError | null;
  156. reload: () => void;
  157. };
  158. /**
  159. * 客户台账读源(服务端分页):筛选项与 `page` 下推 `/customer/get_list@v1`。
  160. * ⛔ 再拼全量进内存。可见范围后端按角色下发。
  161. *
  162. * 额外调一次 `/region/tree@v1` 兜底 `node_id → path_name`(列表口已下发 `region_path` 时多数行不依赖)。
  163. */
  164. export function useCustomerDirectory(opts: CustomerLedgerQuery = {}): CustomerDirectory {
  165. const state = useCustomerLedger(opts);
  166. const [regionMap, setRegionMap] = useState<Record<string, string> | null>(null);
  167. useEffect(() => {
  168. let alive = true;
  169. regionTree()
  170. .then((tree) => alive && setRegionMap(buildRegionPathMap(tree as RegionTreeResult)))
  171. .catch(() => alive && setRegionMap({}));
  172. return () => { alive = false; };
  173. }, []);
  174. const rows = useMemo(() => {
  175. const map = regionMap ?? {};
  176. return (state.data?.items ?? []).map((row) => customerListRowToDemo(row, map));
  177. }, [state.data, regionMap]);
  178. return { rows, total: Number(state.data?.total ?? rows.length), loading: state.loading, error: state.error, reload: state.reload };
  179. }
  180. /** `/import_batch/get@v1` 的行明细 → 视图行(⛔ 后端回传原始 14 列,故只回显校验态)。 */
  181. export function importRowDetailsToDemo(rows: ImportRowDetail[]): ImportRowResult[] {
  182. return (rows ?? []).map((row) => ({
  183. no: String(row.row_no),
  184. ok: row.validate_status === "valid",
  185. status: text(row.validate_status) ? label(VALIDATE_STATUS, row.validate_status) : null,
  186. reason: row.errors === null || row.errors === undefined ? undefined : typeof row.errors === "string" ? row.errors : JSON.stringify(row.errors),
  187. }));
  188. }