/** * 第 4 册(分成与成本规则·成本侧)数据层 —— 真源=klt-bos `/cost_item/*`、`/cost_amount/*`,⛔ 内置演示行。 * * 后端边界(2026-11-10 探针实测 dev 报文 + handler `cost.rs`;2026-11-15 收口概况列): * * `/cost_item/get_list@v1` 一行=一条成本项定义;`columns_pending` 已清空(键保留), * 类别/计算方式/是否摊销/总摊销月份数/计入分成基数/周期均按在册列下发。 * 仍是 `null` 的只可能是 `20261115000005` 补列前建、且名字未命中回填映射的历史行 * ⇒ 那几格才标「后端未下发」,⛔ 折算「否/未分摊/0」。 * * `configured_combo_count`/`configured_total_cents`=后端按 `cost_item LEFT JOIN cost_amount` * 现算的「已配组合数/月合计」(设计册 分成§14.1.1、§14.4 第 1 条)⇒ 概况列⛔ 前端逐行反查。 * * `configured_row_count`=该成本项名下**全部**金额行数:>0 而组合数为 0 ⇒ 存量行缺省份/ * 运营商轴,概况列写「0 个可用组合 · N 条缺轴」,⛔ 写成「尚未配置」(那会掩盖真实金额行)。 * * `referring_plans`=`plan_cost_item.item_name` 名称弱引用反查(订阅§2.1);`[]`=确实没有档位引用。 * * 值域:`category ∈ intl_resource|device_purchase|project_fee`(旧版「线路投入」⛔ 在值域内)、 * `billing_unit ∈ per_customer|per_account|per_copy`,标签统一走 `labels.COST_CATEGORY`/`COST_BILLING_UNIT`; * `cycle` 一期恒 `monthly`(CHECK 钉死),标签走 `labels.COST_CYCLE`。 * * `/cost_amount/get_list@v1` 一次回两份:`items`=透视行(省份×运营商,一览表的行)、 * `rows`/`rows_unattributed`=明细与其中缺省份运营商轴的行。`20261115000006` 已按 * 「成本项 × 金额」把种子金额的两轴回填到 `operator` 档案真有该省该品牌的行上 * ⇒ 透视行有真值;档案里没有该省该品牌运营商的那几条仍留在 `rows_unattributed` * (`available` 随透视行数走;0 行时页面显示后端 `message`,⛔ 把「取不到透视行」写成「空数据」)。 * * `province_name` 取后端 `bos_region_meta.path_name` 叶子(如「上海市」),`null`=该节点未在册; * `operator_name` 取 `operator.name`。 * * 写路径:`/cost_item/save@v1`、`/cost_item/delete@v1`、`/cost_amount/save@v1` 真实落库 * (11102 乐观锁/11103 校验;删除前按 `plan_cost_item.item_name` 反查引用,命中 ⇒ KLT-E002); * `/cost_item/create|update@v1`、`/cost_amount/panel@v1` 经网关一律 `11001 接口未在 OpenAPI 声明` * ⇒ 录入面板⛔ 专用读接口,读侧用 `get_list` 透视结果。 */ import { costAmountGetList, costItemGetList } from "./api/bos.ts"; import { COST_BILLING_UNIT, COST_CATEGORY, COST_CYCLE, PARTNER_STATUS, label } from "./api/labels.ts"; import { WORD_NOT_PROVIDED } from "./api/lockedCopy.ts"; import type { CostAmountRow } from "./api/types.ts"; import { useLoad, type Load } from "./useLoad.ts"; /** 三态文本单元(失败/未下发/真实值三者可区分)。 */ export type FieldView = { text: string; pending: boolean }; const pending = (v: unknown): boolean => v === null || v === undefined || v === ""; function enumView(key: string | null | undefined, dict: Record): FieldView { if (pending(key)) return { text: WORD_NOT_PROVIDED, pending: true }; const text = label(dict, key ?? ""); return { text: text || (key ?? ""), pending: false }; } /** 一条成本项定义(后端 `cost_item` 真值;⛔ 本模块补任何演示定义)。 */ export type CostItemView = { id: string; name: string; category: FieldView; /** 编辑表单回填用的后端原值(`null`=该列未下发,⛔ 猜一个类别)。 */ categoryKey: string | null; /** 周期(设计册 分成§2.1:一期恒「每月」)。 */ cycle: FieldView; cycleKey: string | null; /** 计算方式=计费单位。 */ calc: FieldView; billingUnitKey: string | null; /** `null`=后端未下发该列(⛔ 当成「不分摊」)。 */ amortized: boolean | null; amortizationMonths: number | null; /** `null`=后端未下发该列(⛔ 当成「不计入」)。 */ deduct: boolean | null; remark: string | null; status: FieldView; rowVersion: number | string | null; /** 该成本项名下金额行总数(含缺省份/运营商轴的存量行);`null`=后端未下发该概况。 */ configuredRowCount: number | null; /** 已配金额的「省份×运营商」组合数;`null`=后端未下发该概况。 */ configuredComboCount: number | null; /** 已配金额的月合计(整数分)。 */ configuredTotalCents: number | null; /** 被套餐档引用;`[]`=确实没有引用,`null`=后端未下发该概况。 */ referringPlans: { id: string; name: string }[] | null; }; /** 列表取数:一次取满成本项定义(后端按 `item_name` 排序)。 */ export function useCostItems(): Load { return useLoad(async () => { const res = await costItemGetList({ page: 0, page_size: 200 }); return (res.items ?? []).map((row) => ({ id: row.cost_item_id, name: row.item_name ?? row.cost_item_id, category: enumView(row.category, COST_CATEGORY), categoryKey: pending(row.category) ? null : row.category ?? null, cycle: enumView(row.cycle, COST_CYCLE), cycleKey: pending(row.cycle) ? null : row.cycle ?? null, calc: enumView(row.billing_unit, COST_BILLING_UNIT), billingUnitKey: pending(row.billing_unit) ? null : row.billing_unit ?? null, amortized: typeof row.is_amortized === "boolean" ? row.is_amortized : null, amortizationMonths: typeof row.amortization_months === "number" ? row.amortization_months : null, deduct: typeof row.deduct_from_base === "boolean" ? row.deduct_from_base : null, remark: row.remark ?? null, status: enumView(row.status, PARTNER_STATUS), rowVersion: row.row_version ?? null, configuredRowCount: typeof row.configured_row_count === "number" ? row.configured_row_count : null, configuredComboCount: typeof row.configured_combo_count === "number" ? row.configured_combo_count : null, configuredTotalCents: typeof row.configured_total_cents === "number" ? row.configured_total_cents : null, referringPlans: Array.isArray(row.referring_plans) ? row.referring_plans.map((plan) => ({ id: plan.plan_id, name: plan.plan_name ?? plan.plan_id })) : null, })); }, []); } /** 透视单元键:省份节点 × 运营商 × 成本项(与后端透视行三键一致)。 */ export const costCellKey = (province: string, operator: string, itemId: string): string => `${province}|${operator}|${itemId}`; /** 一个省份×运营商组合(后端透视行)。 */ export type CostComboView = { provinceNodeId: string; operatorId: string; /** 后端 `bos_region_meta.path_name` 的叶子(如「上海市」);`null`=该节点未在册,页面回落区域树名称。 */ provinceName: string | null; operatorName: string | null; /** 该组合下各成本项金额(元);`null`=后端下发了行但金额为空。 */ amounts: Map; }; export type CostAmountView = { combos: CostComboView[]; /** 快取:`province|operator|cost_item` → 金额(元,可为 `null`)。 */ cells: Map; /** `false`=透视行一条都配不出来(后端 `available`),`message` 是代价说明。 */ available: boolean; message: string; /** 明细总行数与其中缺省份运营商轴的行数(后端直出)。 */ rowsTotal: number; unattributedTotal: number; /** 明细里出现过的成本项名(透视表为空时用来兜出「哪些成本项有金额」)。 */ itemNames: Map; total: number; }; /** 金额一览取数:透视行 + 未归属计数(一次调用,⛔ 二次拼表演示金额)。 */ export function useCostAmounts(): Load { return useLoad(async () => { const res = await costAmountGetList({ page: 0, page_size: 200 }); const combos: CostComboView[] = (res.items ?? []).map((row) => { const amounts = new Map(); for (const item of row.items ?? []) { amounts.set(item.cost_item_id, typeof item.amount_cents === "number" ? item.amount_cents / 100 : null); } return { provinceNodeId: row.province_node_id, operatorId: row.operator_id, provinceName: (row.province_name ?? "").trim() || null, operatorName: (row.operator_name ?? "").trim() || null, amounts, }; }); const cells = new Map(); const itemNames = new Map(); const note = (r: CostAmountRow) => { if (r.item_name) itemNames.set(r.cost_item_id, r.item_name); }; for (const combo of combos) { for (const [itemId, cents] of combo.amounts) { cells.set(costCellKey(combo.provinceNodeId, combo.operatorId, itemId), cents); } } for (const row of [...(res.rows ?? []), ...(res.rows_unattributed ?? [])]) note(row); for (const row of res.items ?? []) for (const item of row.items ?? []) note(item); return { combos, cells, available: res.available === false ? false : combos.length > 0, message: res.message ?? "", rowsTotal: res.rows_total ?? (res.rows ?? []).length, unattributedTotal: res.rows_unattributed_total ?? (res.rows_unattributed ?? []).length, itemNames, total: res.total ?? combos.length, }; }, []); } /** 是/否两态与「后端未下发」的三态文本(⛔ 把未下发写成「否」)。 */ export function boolView(value: boolean | null, yes: string, no: string): FieldView { if (value === null) return { text: WORD_NOT_PROVIDED, pending: true }; return { text: value ? yes : no, pending: false }; }