import axios from 'axios' import { http, unwrapApiResponse, ApiResponseError, type ApiEnvelope } from '@/shared/api/http' import { ownedBusinessDecimal, ownedBusinessVersion, type OwnedBusinessOperations, type OwnedBusinessRow, type OwnedBusinessDetail, type OwnedBusinessList, type OwnedBusinessQuery, type OwnedBusinessSave, type OwnedBusinessBind, type OwnedBusinessPhoto, type OwnedBusinessPhotoSlot, type OwnedBusinessCommand, type OwnedBusinessHistory, type OwnedBusinessPage } from '../ownedBusinessTypes' const base = '/platform/v1/last-leg/fleet/owned-business' const path = (no: string) => `${base}/${encodeURIComponent(no)}` async function get(url: string, params?: object): Promise { return unwrapApiResponse(await http.get, ApiEnvelope>(url, { params })) } function count(value: unknown): number { const text = ownedBusinessVersion(value); const result = Number(text); if (!Number.isSafeInteger(result)) throw new Error('运营记录数量超出精确范围'); return result } function decimal(value: string | null, scale: number, precision: number): string | null { if (value == null) return null if (typeof value !== 'string' || ownedBusinessDecimal(value, scale, precision) !== value) throw new Error('运营金额或测量必须使用精确十进制字符串') return value } function operations(value: OwnedBusinessOperations): OwnedBusinessOperations { if (value.money && !['CNY','USD'].includes(value.money.currency)) throw new Error('运营金额币种无效') return { ...value, money: value.money ? { ...value.money, amount: decimal(value.money.amount, 2, 18)! } : null, actualDistanceMiles: decimal(value.actualDistanceMiles,3,12), fuelConsumptionLitresPer100Km: decimal(value.fuelConsumptionLitresPer100Km,3,12) } } function row(value: OwnedBusinessRow): OwnedBusinessRow { if (!value.dispatchNo || !['AFTER_LOADING','DELIVERING','DELIVERED'].includes(value.stage) || !['OWNED','DISPATCHED'].includes(value.vehicle?.ownership)) throw new Error('自有排车来源或阶段不完整,请重新读取') return { ...value, operations: operations(value.operations), unitPrice: decimal(value.unitPrice,2,18), recordVersion: ownedBusinessVersion(value.recordVersion), dispatchRecordVersion: ownedBusinessVersion(value.dispatchRecordVersion), actualPalletCount: value.actualPalletCount == null ? null : count(value.actualPalletCount), photoSummary: { loadingCount: count(value.photoSummary.loadingCount), fuelCount: count(value.photoSummary.fuelCount) } } } function detail(value: OwnedBusinessDetail): OwnedBusinessDetail { if (value.row.dispatchNo !== value.dispatch.dispatchNo) throw new Error('运营资料与原排车单不一致'); return { ...value, row: row(value.row) } } function command(value: OwnedBusinessCommand): OwnedBusinessCommand { return { ...value, recordVersion: value.recordVersion == null ? null : ownedBusinessVersion(value.recordVersion) } } export async function listOwnedBusiness(query: OwnedBusinessQuery): Promise { const value = await get(base, query) return { ...value, records: value.records.map(row), page: count(value.page), size: count(value.size), total: count(value.total), summary: { dispatchCount: count(value.summary.dispatchCount), afterLoadingCount: count(value.summary.afterLoadingCount), deliveringCount: count(value.summary.deliveringCount), deliveredCount: count(value.summary.deliveredCount) } } } export async function getOwnedBusiness(no: string) { const value = detail(await get(path(no))); if (value.row.dispatchNo !== no) throw new Error('运营资料与当前排车业务号不一致'); return value } export async function listOwnedBusinessHistory(no: string, page: number, size: number): Promise> { const value = await get>(`${path(no)}/history`, { page, size }) return { ...value, records: value.records.map(record => ({ ...record, operations: record.operations ? operations(record.operations) : null, recordVersion: record.recordVersion == null ? null : ownedBusinessVersion(record.recordVersion), dispatchRecordVersion: record.dispatchRecordVersion == null ? null : ownedBusinessVersion(record.dispatchRecordVersion) })), page: count(value.page), size: count(value.size), total: count(value.total) } } export const getOwnedBusinessCommand = (no: string, key: string) => get(`${path(no)}/commands/${encodeURIComponent(key)}`).then(command) export async function saveOwnedBusinessOperations(no: string, body: OwnedBusinessSave) { ownedBusinessVersion(body.recordVersion) const value = detail(unwrapApiResponse(await http.put, ApiEnvelope>(`${path(no)}/operations`, body))) if (value.row.dispatchNo !== no) throw new Error('运营保存结果与原排车单不一致') return value } export async function uploadOwnedBusinessPhoto(no: string, slot: OwnedBusinessPhotoSlot, file: File, key: string) { if (!['image/jpeg','image/png'].includes(file.type) || file.size <= 0) throw new Error('请选择有效的JPG或PNG图片') const form = new FormData(); form.append('file', file); form.append('idempotencyKey', key) const value = unwrapApiResponse(await http.post, ApiEnvelope>(`${path(no)}/photos/${slot}/uploads`, form)) if (value.slot !== slot || !value.fileId) throw new Error('上传文件与原照片分类不一致') return value } export async function bindOwnedBusinessPhoto(no: string, slot: OwnedBusinessPhotoSlot, body: OwnedBusinessBind) { ownedBusinessVersion(body.recordVersion) const value = detail(unwrapApiResponse(await http.put, ApiEnvelope>(`${path(no)}/photos/${slot}`, body))) if (value.row.dispatchNo !== no) throw new Error('照片绑定结果与原排车单不一致') return value } async function downloadError(payload: unknown, status: number) { let value = payload if (payload instanceof Blob && payload.type.includes('json')) { try { value = JSON.parse(await payload.text()) as unknown } catch { throw new Error('图片接口返回了无法识别的错误') } } if (value && typeof value === 'object' && 'code' in value) { const error = value as ApiEnvelope; throw new ApiResponseError(error.message || '图片读取失败', error.code, status, error.data, error.requestId, error.traceId) } } export async function getOwnedBusinessPhotoContent(no: string, photo: OwnedBusinessPhoto, revisionVersion?: string): Promise { if (revisionVersion != null && BigInt(ownedBusinessVersion(revisionVersion)) < 1n) throw new Error('历史图片版本必须大于零') try { const blob = await http.get(`${path(no)}/photos/${photo.slot}/files/${encodeURIComponent(photo.fileId)}/content`, { responseType: 'blob', params: revisionVersion == null ? undefined : { revisionVersion: ownedBusinessVersion(revisionVersion) } }) await downloadError(blob, 200) if (!(blob instanceof Blob) || !['image/jpeg','image/png'].includes(blob.type) || !blob.size) throw new Error('图片内容为空或格式无效') return blob } catch (cause) { if (axios.isAxiosError(cause)) await downloadError(cause.response?.data, cause.response?.status || 0); throw cause } }