项目中心-支付模块

This commit is contained in:
wangzzpeng
2026-09-30 08:04:16 +08:00
parent 6b88c3805f
commit ce8210fb3f
35 changed files with 2618 additions and 175 deletions

136
src/utils/billVo.ts Normal file
View File

@@ -0,0 +1,136 @@
import type { Bill, BillStatus, BillVo } from '@/types/business/finance'
/**
* 后端账单实体 → 页面视图模型的映射收口。
*
* <p>后端 `vm_bill` 用的是英文状态编码(`UNSTARTED/PENDING/PAID/CANCELLED`)与
* `periodName` / `transactionNo` 等字段名,页面用的是中文状态与 `planName` / `tradeNo`,
* 两侧口径的换算**只在这里做**,页面与 store 一律只认 `Bill`。</p>
*/
/** 后端状态编码 → 页面中文状态 */
const STATUS_TEXT: Record<string, BillStatus> = {
UNSTARTED: '未到期',
PENDING: '待支付',
PAID: '已支付',
CANCELLED: '已取消',
}
/**
* 后端状态编码 → 页面中文状态。
*
* <p>值域外(历史脏数据,如早期 `updateBillStatus` 硬编码写进去的 `'1'`)一律回落
* 「未到期」——宁可少标一个「待支付」按钮,也不要在页面上渲染出 `undefined`。</p>
*/
export function toBillStatus(code?: string): BillStatus {
if (!code) return '未到期'
return STATUS_TEXT[code] ?? '未到期'
}
/** 单条账单映射;入参为空时返回 null(便于 `?ids=` 查不到时直接判空) */
export function toBill(vo: BillVo): Bill {
return {
// 雪花 id 序列化成 string、小 id 是 number ⇒ 统一归一成 string
id: String(vo.id),
planName: vo.periodName ?? '',
billNo: vo.billNo ?? '',
tradeNo: vo.transactionNo,
projectName: vo.projectName ?? '',
amount: Number(vo.amount ?? 0),
status: toBillStatus(vo.status),
// 后端 Jackson 已按 yyyy-MM-dd HH:mm:ss 序列化,直接透出
createTime: vo.createTime ?? '',
payChannel: vo.payChannel,
payTime: vo.payTime,
}
}
/** 列表映射(过滤掉没有 id 的异常行) */
export function toBills(list: BillVo[] | null | undefined): Bill[] {
if (!list || list.length === 0) return []
return list.filter((item) => item != null && item.id != null).map(toBill)
}
/**
* 后端**计划**状态编码(`vm_bill_plan`)→ 页面中文状态。
*
* <p>为什么不能复用 {@link toBillStatus}:计划表用的是 `DONE/ACTIVE`,账单表用的是
* `PAID/PENDING`,值域不重合——代跑会把「已支付」「待支付」全落成「未到期」。
* (`utils/projectVo.ts` 里那份私有的 `toBillStatus` 是同一口径的另一种写法。)</p>
*/
export function toPlanStatus(code?: string | null): BillStatus {
const value = (code ?? '').toUpperCase()
if (value === 'DONE' || value === 'PAID' || value === 'COMPLETED') return '已支付'
if (value === 'ACTIVE' || value === 'PENDING') return '待支付'
if (value === 'CLOSED' || value === 'CANCELLED') return '已取消'
return '未到期'
}
/**
* 「付款计划」字段子集 —— 入参用**宽松结构**,两种声明都能喂进来。
*
* <p>同一张 `vm_bill_plan` 在前端有两份声明:`types/business/finance.d.ts#BillPlanVo`
* (字段可选)与 `types/business/project.d.ts#RequirementBillPlan`(字段可空 `| null`)。
* 后者赋给前者会被 TS 的可空检查拦下(`string | null` 不能给 `string | undefined`),
* 故映射函数统一收这个子集类型,两边都满足。</p>
*/
export interface PlanLike {
id?: number | string | null
projectName?: string | null
totalAmount?: number | string | null
periods?: number | null
currentPeriod?: number | null
periodRule?: string | null
status?: string | null
orderNo?: string | null
transactionNo?: string | null
payChannel?: string | null
payStatus?: string | null
payTime?: string | null
}
/** 期次名,与后端 `BillServiceImpl#buildPeriodName` 同口径:首期预付款、末期尾款、中间进度款 */
function periodLabel(period: number, periods: number): string {
if (period <= 1) return '预付款'
if (period >= periods) return '尾款'
return '进度款'
}
/**
* 分期计划(`vm_bill_plan`)→ 账单/订单条目。
*
* <p>两处都消费它:项目详情「合同财务」卡片(`utils/projectVo.ts#toPlanCard`)与收银台
* 「订单明细」(`api/finance.ts`,数据来自 `GET /web/requirementPool/get/finance/{需求id}`
* 下发的 `billPlans`)。期次名与金额按后端 `BillServiceImpl#resolveRatio/resolveAmount`
* 同口径折算;「账单号」取该期支付单的 `out_trade_no`(未支付为空 ⇒ 页面显示「—」)。</p>
*/
export function toPlanBill(vo: PlanLike): Bill {
const period = vo.currentPeriod ?? 0
const periods = vo.periods ?? 0
const rule = (vo.periodRule ?? '').split(',').map((part) => Number(part.trim()))
const segment = rule[period - 1]
const ratio = Number.isFinite(segment) ? segment : periods > 0 ? 100 / periods : 0
const total = Number(vo.totalAmount ?? 0)
// 支付成功以支付单为准:计划状态可能没被回调推到 DONE,但流水已经是 SUCCESS
const paid = (vo.payStatus ?? '').toUpperCase() === 'SUCCESS'
return {
id: String(vo.id ?? ''),
planName: period > 0 && periods > 0
? `第${period}期·${periodLabel(period, periods)}`
: (vo.periodRule ?? '付款计划'),
billNo: vo.orderNo ?? '',
tradeNo: vo.transactionNo ?? undefined,
projectName: vo.projectName ?? '',
amount: Math.round(total * ratio) / 100,
status: paid ? '已支付' : toPlanStatus(vo.status),
createTime: '',
payChannel: vo.payChannel ?? undefined,
payTime: vo.payTime ?? undefined,
}
}
/** 计划列表映射(过滤掉没有 id 的异常行) */
export function toPlanBills(list: readonly PlanLike[] | null | undefined): Bill[] {
if (!list || list.length === 0) return []
return list.filter((item) => item != null && item.id != null).map(toPlanBill)
}