196 lines
6.7 KiB
TypeScript
196 lines
6.7 KiB
TypeScript
import type { FileInfo } from '@/types/api'
|
|
import type {
|
|
AfterSales,
|
|
Milestone,
|
|
Project,
|
|
ProjectBill,
|
|
ProjectStatus,
|
|
RequirementAfterSale,
|
|
RequirementAttachment,
|
|
RequirementBill,
|
|
RequirementBillPlan,
|
|
RequirementContract,
|
|
RequirementDetailBundle,
|
|
RequirementInvoice,
|
|
RequirementMember,
|
|
TeamMember,
|
|
} from '@/types/business/project'
|
|
import { toBusinessType } from '@/utils/format'
|
|
import { PROJECT_STATUS_LABEL } from '@/utils/projectMeta'
|
|
|
|
const PROJECT_STATUS_LABELS: string[] = Object.values(PROJECT_STATUS_LABEL)
|
|
|
|
function toProjectStatus(raw: string | null | undefined): ProjectStatus {
|
|
const value = (raw ?? '').trim()
|
|
if (!value) return '待立项'
|
|
const label = PROJECT_STATUS_LABEL[value]
|
|
?? (PROJECT_STATUS_LABELS.includes(value) ? value : undefined)
|
|
return (label as ProjectStatus | undefined) ?? '待立项'
|
|
}
|
|
|
|
function toMilestoneStatus(raw: string | null | undefined): Milestone['status'] {
|
|
const value = (raw ?? '').toUpperCase()
|
|
if (value.includes('COMPLET') || value.includes('DONE') || value.includes('FINISH')) return '已完成'
|
|
if (value.includes('PROGRESS') || value.includes('DOING')) return '进行中'
|
|
return '待开始'
|
|
}
|
|
|
|
function toBillStatus(raw: string | null | undefined): ProjectBill['status'] {
|
|
const value = (raw ?? '').toUpperCase()
|
|
if (value.includes('PAID') || value.includes('DONE') || value.includes('CLOSE')
|
|
|| value.includes('已支付')) return '已支付'
|
|
if (value.includes('ACTIVE') || value.includes('WAIT') || value.includes('PENDING')
|
|
|| value.includes('待支付')) return '待支付'
|
|
if (value.includes('UNSTART') || value.includes('NOT_DUE') || value.includes('未到期')
|
|
|| value.includes('未开始')) return '未到期'
|
|
return '未到期'
|
|
}
|
|
|
|
function toNumber(value: number | string | null | undefined, fallback = 0): number {
|
|
const num = Number(value)
|
|
return Number.isFinite(num) ? num : fallback
|
|
}
|
|
|
|
function toDate(value: string | null | undefined): string | undefined {
|
|
return value ? value.slice(0, 10) : undefined
|
|
}
|
|
|
|
function toRatioText(value: string | number | null | undefined): string {
|
|
const num = Number(value)
|
|
if (!Number.isFinite(num) || num <= 0) return ''
|
|
return Number.isInteger(num) ? String(num) : String(Math.round(num * 10) / 10)
|
|
}
|
|
|
|
function attachmentToFile(item: RequirementAttachment): FileInfo {
|
|
return {
|
|
name: item.originalName ?? '',
|
|
size: item.fileSize ?? 0,
|
|
url: item.url ?? '',
|
|
uploadTime: item.createTime ?? '',
|
|
ossId: item.ossId ?? undefined,
|
|
}
|
|
}
|
|
|
|
function invoiceToFile(item: RequirementInvoice): FileInfo {
|
|
return {
|
|
name: item.invoiceTitle ?? item.invoiceNo ?? '发票',
|
|
size: 0,
|
|
url: item.invoiceFile ?? '',
|
|
uploadTime: item.invoiceDate ?? item.createTime ?? '',
|
|
}
|
|
}
|
|
|
|
function contractToFile(item: RequirementContract): FileInfo {
|
|
return {
|
|
name: item.title ?? item.contractNo ?? '合同文件',
|
|
size: 0,
|
|
url: item.file ?? '',
|
|
uploadTime: toDate(item.signDate ?? item.createTime) ?? '',
|
|
}
|
|
}
|
|
|
|
function toProjectBill(item: RequirementBill): ProjectBill {
|
|
return {
|
|
id: String(item.id),
|
|
source: 'bill',
|
|
planName: item.periodName ?? '',
|
|
periodRatio: toRatioText(item.periodRatio),
|
|
billNo: item.billNo ?? '',
|
|
amount: toNumber(item.amount),
|
|
status: toBillStatus(item.status),
|
|
tradeNo: item.transactionNo ?? undefined,
|
|
projectName: item.projectName ?? '',
|
|
}
|
|
}
|
|
|
|
function planPeriodLabel(period: number, periods: number): string {
|
|
if (period <= 1) return '预付款'
|
|
if (period >= periods) return '尾款'
|
|
return '进度款'
|
|
}
|
|
|
|
function toPlanCard(item: RequirementBillPlan): ProjectBill {
|
|
const period = item.currentPeriod ?? 0
|
|
const periods = item.periods ?? 0
|
|
const rule = (item.periodRule ?? '').split(',').map((part) => Number(part.trim()))
|
|
const segment = rule[period - 1]
|
|
const ratio = Number.isFinite(segment) ? segment : periods > 0 ? 100 / periods : 0
|
|
const paid = (item.payStatus ?? '').toUpperCase() === 'SUCCESS'
|
|
return {
|
|
id: String(item.id),
|
|
source: 'plan',
|
|
planName: period > 0 && periods > 0
|
|
? `第${period}期·${planPeriodLabel(period, periods)}`
|
|
: (item.periodRule ?? '付款计划'),
|
|
periodRatio: toRatioText(ratio),
|
|
billNo: item.orderNo ?? '',
|
|
amount: Math.round(toNumber(item.totalAmount) * ratio) / 100,
|
|
status: paid ? '已支付' : toBillStatus(item.status),
|
|
tradeNo: item.transactionNo ?? undefined,
|
|
projectName: item.projectName ?? '',
|
|
}
|
|
}
|
|
|
|
function toTeamMember(item: RequirementMember): TeamMember {
|
|
const name = item.userName ?? ''
|
|
return {
|
|
role: item.roleName ?? (item.isOwner ? '项目负责人' : '项目成员'),
|
|
name,
|
|
avatarChar: name.slice(0, 1),
|
|
}
|
|
}
|
|
|
|
function toAfterSales(
|
|
item: RequirementAfterSale,
|
|
attachments: RequirementAttachment[] = [],
|
|
): AfterSales {
|
|
const value = (item.status ?? '').toUpperCase()
|
|
const closed = value.includes('CLOSE') || value.includes('CANCEL') || value.includes('WITHDRAW')
|
|
return {
|
|
status: closed ? '无' : '处理中',
|
|
type: item.type ?? '',
|
|
description: item.reason ?? '',
|
|
remark: item.remark ?? '',
|
|
attachments: attachments.map(attachmentToFile),
|
|
createTime: item.requestTime ?? '',
|
|
}
|
|
}
|
|
|
|
export function toProjectDetail(bundle: RequirementDetailBundle): Project {
|
|
const { progress, files, finance, afterSales } = bundle
|
|
const base = progress
|
|
const { projectStage: stage } = progress
|
|
const contract = finance.contracts?.[0]
|
|
return {
|
|
id: String(base.id),
|
|
projectNo: base.projectNo ?? '',
|
|
projectName: base.projectName ?? '',
|
|
businessType: toBusinessType(base.businessType),
|
|
businessTypeName: base.businessType ?? '',
|
|
progress: progress.developmentProgress ?? stage?.developmentProgress ?? 0,
|
|
status: toProjectStatus(base.status),
|
|
submitTime: base.createTime ?? '',
|
|
startTime: toDate(stage?.startTime),
|
|
endTime: toDate(stage?.finishTime ?? stage?.planTime),
|
|
milestones: [...(progress.projectMilestones ?? [])]
|
|
.sort((a, b) => (a.sortOrder ?? 0) - (b.sortOrder ?? 0))
|
|
.map((item) => ({
|
|
name: item.milestoneName ?? '',
|
|
status: toMilestoneStatus(item.status),
|
|
time: toDate(item.startTime),
|
|
})),
|
|
team: [...(progress.members ?? [])]
|
|
.sort((a, b) => Number(b.isOwner ?? false) - Number(a.isOwner ?? false))
|
|
.map(toTeamMember),
|
|
files: (files.requirementAttachments ?? []).map(attachmentToFile),
|
|
paymentPlans: (finance.billPlans ?? []).length > 0
|
|
? (finance.billPlans ?? []).map(toPlanCard)
|
|
: (finance.bills ?? []).map(toProjectBill),
|
|
invoiceRecords: (finance.invoices ?? []).map(invoiceToFile),
|
|
contractFile: contract ? contractToFile(contract) : undefined,
|
|
afterSales: afterSales.afterSale
|
|
? toAfterSales(afterSales.afterSale, afterSales.afterSaleAttachments ?? [])
|
|
: undefined,
|
|
}
|
|
}
|