cost-demo.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * 第 4 册(分成与成本规则·成本侧)数据层 —— 真源=klt-bos `/cost_item/*`、`/cost_amount/*`,⛔ 内置演示行。
  3. *
  4. * 后端边界(2026-11-10 探针实测 dev 报文 + handler `cost.rs`;2026-11-15 收口概况列):
  5. * * `/cost_item/get_list@v1` 一行=一条成本项定义;`columns_pending` 已清空(键保留),
  6. * 类别/计算方式/是否摊销/总摊销月份数/计入分成基数/周期均按在册列下发。
  7. * 仍是 `null` 的只可能是 `20261115000005` 补列前建、且名字未命中回填映射的历史行
  8. * ⇒ 那几格才标「后端未下发」,⛔ 折算「否/未分摊/0」。
  9. * * `configured_combo_count`/`configured_total_cents`=后端按 `cost_item LEFT JOIN cost_amount`
  10. * 现算的「已配组合数/月合计」(设计册 分成§14.1.1、§14.4 第 1 条)⇒ 概况列⛔ 前端逐行反查。
  11. * * `configured_row_count`=该成本项名下**全部**金额行数:>0 而组合数为 0 ⇒ 存量行缺省份/
  12. * 运营商轴,概况列写「0 个可用组合 · N 条缺轴」,⛔ 写成「尚未配置」(那会掩盖真实金额行)。
  13. * * `referring_plans`=`plan_cost_item.item_name` 名称弱引用反查(订阅§2.1);`[]`=确实没有档位引用。
  14. * * 值域:`category ∈ intl_resource|device_purchase|project_fee`(旧版「线路投入」⛔ 在值域内)、
  15. * `billing_unit ∈ per_customer|per_account|per_copy`,标签统一走 `labels.COST_CATEGORY`/`COST_BILLING_UNIT`;
  16. * `cycle` 一期恒 `monthly`(CHECK 钉死),标签走 `labels.COST_CYCLE`。
  17. * * `/cost_amount/get_list@v1` 一次回两份:`items`=透视行(省份×运营商,一览表的行)、
  18. * `rows`/`rows_unattributed`=明细与其中缺省份运营商轴的行。`20261115000006` 已按
  19. * 「成本项 × 金额」把种子金额的两轴回填到 `operator` 档案真有该省该品牌的行上
  20. * ⇒ 透视行有真值;档案里没有该省该品牌运营商的那几条仍留在 `rows_unattributed`
  21. * (`available` 随透视行数走;0 行时页面显示后端 `message`,⛔ 把「取不到透视行」写成「空数据」)。
  22. * * `province_name` 取后端 `bos_region_meta.path_name` 叶子(如「上海市」),`null`=该节点未在册;
  23. * `operator_name` 取 `operator.name`。
  24. * * 写路径:`/cost_item/save@v1`、`/cost_item/delete@v1`、`/cost_amount/save@v1` 真实落库
  25. * (11102 乐观锁/11103 校验;删除前按 `plan_cost_item.item_name` 反查引用,命中 ⇒ KLT-E002);
  26. * `/cost_item/create|update@v1`、`/cost_amount/panel@v1` 经网关一律 `11001 接口未在 OpenAPI 声明`
  27. * ⇒ 录入面板⛔ 专用读接口,读侧用 `get_list` 透视结果。
  28. */
  29. import { costAmountGetList, costItemGetList } from "./api/bos.ts";
  30. import { COST_BILLING_UNIT, COST_CATEGORY, COST_CYCLE, PARTNER_STATUS, label } from "./api/labels.ts";
  31. import { WORD_NOT_PROVIDED } from "./api/lockedCopy.ts";
  32. import type { CostAmountRow } from "./api/types.ts";
  33. import { useLoad, type Load } from "./useLoad.ts";
  34. /** 三态文本单元(失败/未下发/真实值三者可区分)。 */
  35. export type FieldView = { text: string; pending: boolean };
  36. const pending = (v: unknown): boolean => v === null || v === undefined || v === "";
  37. function enumView(key: string | null | undefined, dict: Record<string, string>): FieldView {
  38. if (pending(key)) return { text: WORD_NOT_PROVIDED, pending: true };
  39. const text = label(dict, key ?? "");
  40. return { text: text || (key ?? ""), pending: false };
  41. }
  42. /** 一条成本项定义(后端 `cost_item` 真值;⛔ 本模块补任何演示定义)。 */
  43. export type CostItemView = {
  44. id: string;
  45. name: string;
  46. category: FieldView;
  47. /** 编辑表单回填用的后端原值(`null`=该列未下发,⛔ 猜一个类别)。 */
  48. categoryKey: string | null;
  49. /** 周期(设计册 分成§2.1:一期恒「每月」)。 */
  50. cycle: FieldView;
  51. cycleKey: string | null;
  52. /** 计算方式=计费单位。 */
  53. calc: FieldView;
  54. billingUnitKey: string | null;
  55. /** `null`=后端未下发该列(⛔ 当成「不分摊」)。 */
  56. amortized: boolean | null;
  57. amortizationMonths: number | null;
  58. /** `null`=后端未下发该列(⛔ 当成「不计入」)。 */
  59. deduct: boolean | null;
  60. remark: string | null;
  61. status: FieldView;
  62. rowVersion: number | string | null;
  63. /** 该成本项名下金额行总数(含缺省份/运营商轴的存量行);`null`=后端未下发该概况。 */
  64. configuredRowCount: number | null;
  65. /** 已配金额的「省份×运营商」组合数;`null`=后端未下发该概况。 */
  66. configuredComboCount: number | null;
  67. /** 已配金额的月合计(整数分)。 */
  68. configuredTotalCents: number | null;
  69. /** 被套餐档引用;`[]`=确实没有引用,`null`=后端未下发该概况。 */
  70. referringPlans: { id: string; name: string }[] | null;
  71. };
  72. /** 列表取数:一次取满成本项定义(后端按 `item_name` 排序)。 */
  73. export function useCostItems(): Load<CostItemView[]> {
  74. return useLoad(async () => {
  75. const res = await costItemGetList({ page: 0, page_size: 200 });
  76. return (res.items ?? []).map((row) => ({
  77. id: row.cost_item_id,
  78. name: row.item_name ?? row.cost_item_id,
  79. category: enumView(row.category, COST_CATEGORY),
  80. categoryKey: pending(row.category) ? null : row.category ?? null,
  81. cycle: enumView(row.cycle, COST_CYCLE),
  82. cycleKey: pending(row.cycle) ? null : row.cycle ?? null,
  83. calc: enumView(row.billing_unit, COST_BILLING_UNIT),
  84. billingUnitKey: pending(row.billing_unit) ? null : row.billing_unit ?? null,
  85. amortized: typeof row.is_amortized === "boolean" ? row.is_amortized : null,
  86. amortizationMonths: typeof row.amortization_months === "number" ? row.amortization_months : null,
  87. deduct: typeof row.deduct_from_base === "boolean" ? row.deduct_from_base : null,
  88. remark: row.remark ?? null,
  89. status: enumView(row.status, PARTNER_STATUS),
  90. rowVersion: row.row_version ?? null,
  91. configuredRowCount: typeof row.configured_row_count === "number" ? row.configured_row_count : null,
  92. configuredComboCount: typeof row.configured_combo_count === "number" ? row.configured_combo_count : null,
  93. configuredTotalCents: typeof row.configured_total_cents === "number" ? row.configured_total_cents : null,
  94. referringPlans: Array.isArray(row.referring_plans)
  95. ? row.referring_plans.map((plan) => ({ id: plan.plan_id, name: plan.plan_name ?? plan.plan_id }))
  96. : null,
  97. }));
  98. }, []);
  99. }
  100. /** 透视单元键:省份节点 × 运营商 × 成本项(与后端透视行三键一致)。 */
  101. export const costCellKey = (province: string, operator: string, itemId: string): string =>
  102. `${province}|${operator}|${itemId}`;
  103. /** 一个省份×运营商组合(后端透视行)。 */
  104. export type CostComboView = {
  105. provinceNodeId: string;
  106. operatorId: string;
  107. /** 后端 `bos_region_meta.path_name` 的叶子(如「上海市」);`null`=该节点未在册,页面回落区域树名称。 */
  108. provinceName: string | null;
  109. operatorName: string | null;
  110. /** 该组合下各成本项金额(元);`null`=后端下发了行但金额为空。 */
  111. amounts: Map<string, number | null>;
  112. };
  113. export type CostAmountView = {
  114. combos: CostComboView[];
  115. /** 快取:`province|operator|cost_item` → 金额(元,可为 `null`)。 */
  116. cells: Map<string, number | null>;
  117. /** `false`=透视行一条都配不出来(后端 `available`),`message` 是代价说明。 */
  118. available: boolean;
  119. message: string;
  120. /** 明细总行数与其中缺省份运营商轴的行数(后端直出)。 */
  121. rowsTotal: number;
  122. unattributedTotal: number;
  123. /** 明细里出现过的成本项名(透视表为空时用来兜出「哪些成本项有金额」)。 */
  124. itemNames: Map<string, string>;
  125. total: number;
  126. };
  127. /** 金额一览取数:透视行 + 未归属计数(一次调用,⛔ 二次拼表演示金额)。 */
  128. export function useCostAmounts(): Load<CostAmountView> {
  129. return useLoad(async () => {
  130. const res = await costAmountGetList({ page: 0, page_size: 200 });
  131. const combos: CostComboView[] = (res.items ?? []).map((row) => {
  132. const amounts = new Map<string, number | null>();
  133. for (const item of row.items ?? []) {
  134. amounts.set(item.cost_item_id, typeof item.amount_cents === "number" ? item.amount_cents / 100 : null);
  135. }
  136. return {
  137. provinceNodeId: row.province_node_id,
  138. operatorId: row.operator_id,
  139. provinceName: (row.province_name ?? "").trim() || null,
  140. operatorName: (row.operator_name ?? "").trim() || null,
  141. amounts,
  142. };
  143. });
  144. const cells = new Map<string, number | null>();
  145. const itemNames = new Map<string, string>();
  146. const note = (r: CostAmountRow) => {
  147. if (r.item_name) itemNames.set(r.cost_item_id, r.item_name);
  148. };
  149. for (const combo of combos) {
  150. for (const [itemId, cents] of combo.amounts) {
  151. cells.set(costCellKey(combo.provinceNodeId, combo.operatorId, itemId), cents);
  152. }
  153. }
  154. for (const row of [...(res.rows ?? []), ...(res.rows_unattributed ?? [])]) note(row);
  155. for (const row of res.items ?? []) for (const item of row.items ?? []) note(item);
  156. return {
  157. combos,
  158. cells,
  159. available: res.available === false ? false : combos.length > 0,
  160. message: res.message ?? "",
  161. rowsTotal: res.rows_total ?? (res.rows ?? []).length,
  162. unattributedTotal: res.rows_unattributed_total ?? (res.rows_unattributed ?? []).length,
  163. itemNames,
  164. total: res.total ?? combos.length,
  165. };
  166. }, []);
  167. }
  168. /** 是/否两态与「后端未下发」的三态文本(⛔ 把未下发写成「否」)。 */
  169. export function boolView(value: boolean | null, yes: string, no: string): FieldView {
  170. if (value === null) return { text: WORD_NOT_PROVIDED, pending: true };
  171. return { text: value ? yes : no, pending: false };
  172. }