| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /**
- * 第 2 册(客户)台账的**视图模型** —— 真源=klt-bos 接口,⛔ 内置演示行。
- *
- * 后端边界(页面如实呈现,⛔ 造数填补):
- * * `/customer/get_list@v1` 实测内联 14+ 字段(含 `parent_node_id` 与 `ops_team_names`,2026-11-12 后端扩展)⇒
- * 本页展示列**一次列表口即够**,⛔ 逐行 N+1 补详情(缺口清单 C-7);
- * * 所属区域:列表口返回挂载节点 ID(`parent_node_id`),区域路径由前端用一次 `/region/tree@v1` 的
- * `nodes[].path_name` 字典解析(⛔ 逐行走 `/customer/get@v1` 再拼树);
- * * `/import_batch/get_list@v1` ⛔ 下发文件名与行明细(计数为字符串)⇒ 只展示后端给的列。
- */
- import { useEffect, useMemo, useState } from "react";
- import { ACCT_STATUS, IMPORT_STATUS, SUBJECT_TYPE, VALIDATE_STATUS, label } from "./api/labels";
- import { regionTree } from "./api/bos";
- import type { CustomerListRow, ImportBatchRowT, ImportRowDetail, ImportSubmitResult, RegionTreeResult } from "./api/types";
- import type { StatusTone } from "../domain/statuses";
- import { ApiError } from "./api/http";
- import { useCustomerLedger, type CustomerLedgerQuery } from "./customer-list-store";
- /** 客户台账视图行;`null` =后端未下发该维度,由页面用 `<Tri>` 呈现,⛔ 填「未知」「—」等占位值。 */
- export type DemoCustomer = {
- id: string;
- name: string | null;
- subject: string | null;
- operator: string | null;
- partner: string | null;
- subAccounts: number | null;
- status: string | null;
- activatedAt: string | null;
- /** 后端挂载节点 ID(来自 `customer_region_mount.parent_node_id`),前端用于查区域路径。 */
- parentNodeId: string | null;
- /** 运维团队中文名数组(后端 JOIN ops_team 批量查,2026-11-12 起已内联)。 */
- opsTeamNames: string[];
- /** 在用订阅的套餐名数组(后端批量查 order;空数组=无在用订阅,非「后端未下发」)。 */
- planNames: string[];
- /** 挂载区域完整路径("全网/上海市/市辖区/浦东新区")。后端不下发路径,前端用 region/tree 解析。 */
- regionPath: string | null;
- /** 脱敏识别号(后端 `identity_no_masked`);`null`=该客户未登记识别号。 */
- identityNo: string | null;
- };
- export type ImportRowResult = {
- no: string;
- ok: boolean;
- /** 校验态中文(`VALIDATE_STATUS` 映射结果);后端未回则为 null。 */
- status: string | null;
- /** 后端 `error_list` 原文,⛔ 前端改写。 */
- reason?: string;
- };
- /** 导入批次台账视图行;`file`/`rows` ⛔ 后端下发时为 null,页面标「后端未下发」。 */
- export type DemoImportBatch = {
- id: string;
- batchNo: string | null;
- file: string | null;
- status: string | null;
- total: number | null;
- ok: number | null;
- fail: number | null;
- createdAt: string | null;
- importer: string | null;
- rows: ImportRowResult[] | null;
- };
- /** ⛔ 内置演示客户行——客户台账唯一真源是 `POST /customer/get_list@v1`。 */
- export const initialCustomers: DemoCustomer[] = [];
- /** ⛔ 内置演示批次——台账真源是 `POST /import_batch/get_list@v1`;本导出仅为 `lib/import-batch-store.ts` 的兜底签名保留。 */
- export const initialImportBatches: DemoImportBatch[] = [];
- /** 导入批次状态(中文标签)→ 状态色。 */
- export const importBatchTone: Record<string, StatusTone> = {
- 已完成: "success",
- 已校验: "info",
- 部分失败: "danger",
- 处理中: "info",
- 导入中: "info",
- 校验中: "info",
- };
- /** 状态原文 → 中文标签 → 色调;后端未回状态时落 `neutral`,⛔ 猜一个「处理中」。 */
- export function importBatchToneOf(raw: string | null | undefined): StatusTone {
- if (raw === null || raw === undefined || raw === "") return "neutral";
- return importBatchTone[label(IMPORT_STATUS, raw)] ?? "neutral";
- }
- /** 标签化:后端缺列(null/空串)一律返回 null,⛔ 显示成 0 或「未知」。 */
- function text(value: string | null | undefined): string | null {
- const v = (value ?? "").trim();
- return v ? v : null;
- }
- /** 区域树节点 → `{node_id → path_name}` 字典,用于把客户的 `parent_node_id` 映射成完整区域路径。 */
- function buildRegionPathMap(tree: RegionTreeResult | null | undefined): Record<string, string> {
- const map: Record<string, string> = {};
- if (!tree) return map;
- const nodes = (tree as unknown as { nodes?: Array<{ node_id?: string; path_name?: string; name?: string }> }).nodes ?? [];
- for (const n of nodes) {
- const id = n.node_id;
- const path = n.path_name ?? n.name;
- if (id && path) map[id] = path;
- }
- return map;
- }
- /** `/customer/get_list@v1` 行 → 视图行;`regionPathMap` 只在后端未下发 `region_path` 时兜底。 */
- export function customerListRowToDemo(row: CustomerListRow, regionPathMap?: Record<string, string>): DemoCustomer {
- const pn = text(row.parent_node_id);
- // 优先后端下发的挂载路径(层级真源 `bos_region_meta.path_name`):区域树只下发授权范围内两层,
- // 挂载点常是市/区县级 ⇒ 光按树查必落空(2026-11-15 列表「所属区域」全列「后端未下发」、
- // 导出激活码把全部客户计入「无法判定区域」实证)。`/` 归一为 `" / "`,与 `regionPath` 同一口径。
- const backendPath = text(row.region_path);
- const regionPath = backendPath
- ? backendPath.split("/").map((s) => s.trim()).filter(Boolean).join(" / ")
- : pn ? (regionPathMap?.[pn] ?? null) : null;
- return {
- id: row.customer_id,
- name: text(row.customer_name),
- subject: text(row.subject_type) ? label(SUBJECT_TYPE, row.subject_type) : null,
- operator: text(row.operator_name),
- partner: text(row.partner_name),
- subAccounts: Number.isFinite(row.sub_account_count) ? row.sub_account_count : null,
- status: text(row.acct_status) ? label(ACCT_STATUS, row.acct_status) : null,
- activatedAt: text(row.created_at),
- parentNodeId: pn,
- opsTeamNames: (row.ops_team_names ?? []).map((s) => s.trim()).filter(Boolean),
- planNames: (row.plan_names ?? []).map((s) => s.trim()).filter(Boolean),
- regionPath,
- identityNo: text(row.identity_no_masked),
- };
- }
- /** `/import_batch/get_list@v1` 行 → 视图行;计数是字符串,空串按「后端未下发」处理。 */
- export function importBatchRowToDemo(row: ImportBatchRowT): DemoImportBatch {
- const num = (v: string | null | undefined) => {
- const s = (v ?? "").trim();
- if (!s) return null;
- const n = Number(s);
- return Number.isFinite(n) ? n : null;
- };
- return {
- id: row.batch_id,
- batchNo: text(row.batch_no),
- file: null,
- status: text(row.status) ? label(IMPORT_STATUS, row.status) : null,
- total: num(row.total_rows),
- ok: num(row.success_rows),
- fail: num(row.fail_rows),
- createdAt: text(row.created_at),
- importer: text(row.operator_username),
- rows: null,
- };
- }
- /** `submit` 同步回执的行校验态 → 视图行。 */
- export function submitRowsToDemo(rows: ImportSubmitResult["rows"]): ImportRowResult[] {
- return (rows ?? []).map((row) => ({
- no: String(row.row_no),
- ok: row.validate_status === "valid",
- status: text(row.validate_status) ? label(VALIDATE_STATUS, row.validate_status) : null,
- reason: (row.error_list ?? []).join(";") || undefined,
- }));
- }
- export type CustomerDirectory = {
- rows: DemoCustomer[];
- total: number;
- loading: boolean;
- error: ApiError | null;
- reload: () => void;
- };
- /**
- * 客户台账读源(服务端分页):筛选项与 `page` 下推 `/customer/get_list@v1`。
- * ⛔ 再拼全量进内存。可见范围后端按角色下发。
- *
- * 额外调一次 `/region/tree@v1` 兜底 `node_id → path_name`(列表口已下发 `region_path` 时多数行不依赖)。
- */
- export function useCustomerDirectory(opts: CustomerLedgerQuery = {}): CustomerDirectory {
- const state = useCustomerLedger(opts);
- const [regionMap, setRegionMap] = useState<Record<string, string> | null>(null);
- useEffect(() => {
- let alive = true;
- regionTree()
- .then((tree) => alive && setRegionMap(buildRegionPathMap(tree as RegionTreeResult)))
- .catch(() => alive && setRegionMap({}));
- return () => { alive = false; };
- }, []);
- const rows = useMemo(() => {
- const map = regionMap ?? {};
- return (state.data?.items ?? []).map((row) => customerListRowToDemo(row, map));
- }, [state.data, regionMap]);
- return { rows, total: Number(state.data?.total ?? rows.length), loading: state.loading, error: state.error, reload: state.reload };
- }
- /** `/import_batch/get@v1` 的行明细 → 视图行(⛔ 后端回传原始 14 列,故只回显校验态)。 */
- export function importRowDetailsToDemo(rows: ImportRowDetail[]): ImportRowResult[] {
- return (rows ?? []).map((row) => ({
- no: String(row.row_no),
- ok: row.validate_status === "valid",
- status: text(row.validate_status) ? label(VALIDATE_STATUS, row.validate_status) : null,
- reason: row.errors === null || row.errors === undefined ? undefined : typeof row.errors === "string" ? row.errors : JSON.stringify(row.errors),
- }));
- }
|