share-party-view.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { initialShareParties } from "./settlement-demo";
  2. import { enterpriseTeamIds, regionPath, type OrganizationData, type ShareFields } from "./organization-demo";
  3. import { typeLabel } from "./api/ticket";
  4. export const currentSharePartyTypes = ["运营商", "合伙人", "运维团队"] as const;
  5. export type CurrentSharePartyType = typeof currentSharePartyTypes[number];
  6. export const historicalSharePartyIds: Record<string, string> = {
  7. "op-0": "SP-01", "op-2": "SP-02", "pt-0": "SP-03",
  8. "pt-1": "SP-04", "mt-0": "SP-05", "mt-1": "SP-06", "op-5": "SP-07",
  9. };
  10. export function sharePartiesFromOrganization(data: OrganizationData, options: {
  11. customerRegions?: ReadonlySet<string>;
  12. defaultDay?: number | null;
  13. operatorBilling?: Record<string, Pick<OrganizationData["operators"][number], "reconcileDay" | "payCycle">>;
  14. } = {}) {
  15. const { customerRegions } = options;
  16. const customers = customerRegions
  17. ? data.enterprises.filter((customer) => customerRegions.has(customer.region))
  18. : data.enterprises;
  19. const defaultDay = options.defaultDay ?? null;
  20. const rows = [
  21. ...data.operators.map((item) => {
  22. const { reconcileDay, payCycle } = options.operatorBilling?.[item.id] ?? item;
  23. const day = reconcileDay == null
  24. ? (defaultDay == null ? "跟随系统默认" : `跟随默认(${defaultDay} 日)`)
  25. : `每月 ${reconcileDay} 日`;
  26. return { ...item, displayName: item.name, name: `${regionPath(data, item.region)} / ${item.name}`, note: `对账日 ${day} · ${payCycle}`, type: "运营商" as const, customerCount: customers.filter((customer) => customer.operator === item.id).length };
  27. }),
  28. ...data.partners.map((item) => ({ ...item, displayName: item.name, note: item.title, type: "合伙人" as const, customerCount: customers.filter((customer) => customer.partner === item.id).length })),
  29. ...data.maintenanceTeams.map((item) => ({ ...item, displayName: item.name, note: `${item.mode}分派 · 承接 ${item.ticketTypes?.map(typeLabel).join("、") || "—"}`, type: "运维团队" as const, customerCount: customers.filter((customer) => enterpriseTeamIds(customer).includes(item.id)).length })),
  30. ];
  31. return rows.map(({ id, name, displayName, note, type, region, shareMode, shareValue, sharePending, customerCount }) => {
  32. const historyId = historicalSharePartyIds[id];
  33. return { id, historyId, name, displayName, note, type, region, regionLabel: regionPath(data, region), shareMode, shareValue, sharePending, customerCount, history: initialShareParties.find((party) => party.id === historyId)?.history ?? [] };
  34. });
  35. }
  36. /**
  37. * 「已配分成/未配分成」判据:档案两列为空(`sharePending`=`toShare` 派生的「按比例 0」占位)
  38. * ⇒ 未配分成。⛔ 把占位值当已配置(否则状态列「已配分成」与取值列「未配分成」同行矛盾,TC-FN-24)。
  39. */
  40. export function hasShareTerms(party: ShareFields) {
  41. return !party.sharePending && Boolean(party.shareMode) && Number.isFinite(party.shareValue) && party.shareValue >= 0;
  42. }
  43. export function formatShareTerms(party: ShareFields) {
  44. if (!hasShareTerms(party)) return "未配分成";
  45. if (party.shareMode === "按比例") return `${Math.round(party.shareValue * 10000) / 100}%`;
  46. const amount = party.shareValue.toLocaleString("zh-CN", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
  47. return `¥ ${amount}${party.shareMode === "按金额" ? "/月" : "/账号"}`;
  48. }