import { axiosInstance } from '@/api/rbac' import { assertBusinessOk, omsUrl, parseListPayload, unwrapData } from '@/api/oms/common' import { ENABLE_STATUS, isEnableStatusEnabled, toEnableStatus } from '@/utils/enableStatus' /** 渠道下拉:全部名称,不分页 */ export async function getServiceChannelOptions() { const res = await axiosInstance.get(omsUrl('/channel/service-channel/options')) const body = res.data if (Array.isArray(body)) return body assertBusinessOk(body) const raw = body?.data ?? body?.Data ?? body?.result ?? body?.Result ?? body if (Array.isArray(raw)) return raw if (raw && typeof raw === 'object') { const items = (raw as any).items ?? (raw as any).Items ?? (raw as any).list ?? (raw as any).List if (Array.isArray(items)) return items } return [] } export async function getServiceChannelList(params: { page?: number pageSize?: number groupId?: number keyword?: string isActive?: number }) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/list'), { params }) return parseListPayload(res.data) } /** 服务商分组列表 GET /channel/service-channel/group/list */ export async function getServiceChannelGroupList(params?: { keyword?: string appId?: number apiServiceProviderId?: number }) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/group/list'), { params }) const body = res.data // 网关偶发直接返回数组;也兼容 { code, data } / { items } if (Array.isArray(body)) return body assertBusinessOk(body) const raw = body?.data ?? body?.Data ?? body?.result ?? body?.Result ?? body if (Array.isArray(raw)) return raw if (raw && typeof raw === 'object') { const items = (raw as any).items ?? (raw as any).Items ?? (raw as any).list ?? (raw as any).List if (Array.isArray(items)) return items } return [] } /** 兼容 { code, data } / { Code, Data } / { code, result } 以及「业务字段与 code 同在根上」等响应 */ function parseServiceChannelDetailBody(body: any): any { assertBusinessOk(body) let inner = body?.data ?? body?.Data ?? body?.result ?? body?.Result if (Array.isArray(inner) && inner.length === 1) inner = inner[0] if (inner != null && typeof inner === 'object' && !Array.isArray(inner)) { return inner } if ( body && typeof body === 'object' && !Array.isArray(body) && (body.id != null || body.Id != null) && (body.channelCode != null || body.ChannelCode != null || body.channelName != null || body.ChannelName != null) ) { return body } return inner } export async function getServiceChannelDetail(id: number | string) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/detail'), { params: { id } }) return parseServiceChannelDetailBody(res.data) } export async function createServiceChannel(body: Record) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/create'), body) assertBusinessOk(res.data) } /** 与文档「POST /channel/service-channel/update」请求体字段一致,避免把详情响应中的审计字段一并提交 */ export type ServiceChannelUpdatePayload = { id: number | string groupId: number channelCode: string channelName: string minTime: number maxTime: number forecastType: string forecastCode: string description: string preScript: string isActive: number remoteAreaId?: number | null blacklistAreaId?: number | null weightRuleId?: number | null weightRoundingId?: number | null } export function buildServiceChannelUpdatePayload( detail: Record, patch: Partial< Pick< ServiceChannelUpdatePayload, | 'channelCode' | 'channelName' | 'minTime' | 'maxTime' | 'forecastType' | 'forecastCode' | 'description' | 'preScript' | 'isActive' | 'remoteAreaId' | 'blacklistAreaId' | 'weightRuleId' | 'weightRoundingId' > > & { groupId?: number } ): ServiceChannelUpdatePayload { const id = detail.id ?? detail.Id const groupId = Number( patch.groupId ?? detail.groupId ?? detail.GroupId ?? 0 ) const num = (v: unknown, fallback: number) => { if (v === '' || v == null) return fallback const n = Number(v) return Number.isFinite(n) ? n : fallback } const str = (v: unknown) => (v == null ? '' : String(v)) const activeRaw = patch.isActive ?? detail.isActive ?? detail.IsActive ?? ENABLE_STATUS.enabled const isActive = typeof activeRaw === 'boolean' ? toEnableStatus(activeRaw) : isEnableStatusEnabled(activeRaw) ? ENABLE_STATUS.enabled : ENABLE_STATUS.disabled const resolveOptionalPositiveId = ( patchVal: unknown, detailCamel: string, detailPascal: string ): number | null => { if (patchVal !== undefined) { if (patchVal == null || patchVal === '') return null const n = Number(patchVal) return Number.isFinite(n) && n > 0 ? n : null } const raw = detail[detailCamel] ?? detail[detailPascal] if (raw == null || raw === '') return null const n = Number(raw) return Number.isFinite(n) && n > 0 ? n : null } return { id, groupId: Number.isFinite(groupId) ? groupId : 0, channelCode: str(patch.channelCode ?? detail.channelCode ?? detail.ChannelCode), channelName: str(patch.channelName ?? detail.channelName ?? detail.ChannelName), minTime: patch.minTime !== undefined ? num(patch.minTime, 0) : num(detail.minTime ?? detail.MinTime, 0), maxTime: patch.maxTime !== undefined ? num(patch.maxTime, 0) : num(detail.maxTime ?? detail.MaxTime, 0), forecastType: str( patch.forecastType ?? detail.forecastType ?? detail.ForecastType ?? '' ) || 'waybill', forecastCode: str(patch.forecastCode ?? detail.forecastCode ?? detail.ForecastCode), description: str(patch.description ?? detail.description ?? detail.Description), preScript: str(patch.preScript ?? detail.preScript ?? detail.PreScript), isActive, remoteAreaId: resolveOptionalPositiveId( patch.remoteAreaId, 'remoteAreaId', 'RemoteAreaId' ), blacklistAreaId: resolveOptionalPositiveId( patch.blacklistAreaId, 'blacklistAreaId', 'BlacklistAreaId' ), weightRuleId: resolveOptionalPositiveId( patch.weightRuleId, 'weightRuleId', 'WeightRuleId' ), weightRoundingId: resolveOptionalPositiveId( patch.weightRoundingId, 'weightRoundingId', 'WeightRoundingId' ), } } export async function updateServiceChannel(body: ServiceChannelUpdatePayload) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/update'), body) assertBusinessOk(res.data) } export async function deleteServiceChannel(id: number | string) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/delete'), { id }) assertBusinessOk(res.data) } /** 服务商分组(t_service_channel_group) */ export async function createServiceChannelGroup(body: { name: string appId: number apiServiceProviderId: number }) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/group/create'), body) assertBusinessOk(res.data) } export async function updateServiceChannelGroup(body: { id: number | string name: string appId: number apiServiceProviderId: number }) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/group/update'), body) assertBusinessOk(res.data) } export async function deleteServiceChannelGroup(id: number | string) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/group/delete'), { id }) assertBusinessOk(res.data) } /** 买价列表;列表页请传 includeDetails:false,避免拉回整张价格矩阵导致卡顿 */ export async function getServiceChannelPriceList( serviceChannelId: number | string, opts?: { includeDetails?: boolean }, ) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/price/list'), { params: { serviceChannelId, includeDetails: opts?.includeDetails ?? true, }, }) // 与 parseListPayload 一致:兼容 data 为数组、{ items }、{ list } 等 const { items } = parseListPayload(res.data) return items } /** 单条买价(含 factors/details),编辑弹窗按需加载 */ export async function getServiceChannelPrice(id: number | string) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/price/get'), { params: { id }, }) return unwrapData(res.data) ?? res.data?.data ?? res.data } export async function createServiceChannelPrice(body: Record) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/price/create'), body) assertBusinessOk(res.data) } export async function updateServiceChannelPrice(body: Record & { id: number | string }) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/price/update'), body) assertBusinessOk(res.data) } export async function deleteServiceChannelPrice(id: number | string) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/price/delete'), { id }) assertBusinessOk(res.data) } export async function toggleServiceChannelPriceStatus(id: number | string, status: number) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/price/toggleStatus'), { id, status }) assertBusinessOk(res.data) } /** 整树替换买价;可选 publish=true 同步发布(与卖价 price/save 对称) */ export async function saveServiceChannelPriceAll(body: { serviceChannelId: number | string publish?: boolean prices: Record[] }) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/price/save'), body) assertBusinessOk(res.data) } export async function getServiceChannelPublishPriceList(params: { serviceChannelId: number | string page?: number pageSize?: number }) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/publish/price/list'), { params }) return parseListPayload(res.data) } export async function getServiceChannelPublishRulesList(params: { serviceChannelId: number | string page?: number pageSize?: number }) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/publish/rules/list'), { params }) return parseListPayload(res.data) } /** 预览即将发布的快照(不落库),用于发布弹窗与发布记录核对 */ export async function getServiceChannelPublishPreview(params: { serviceChannelId: number | string operatorType: 'rules' | 'price' | string }) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/publish/preview'), { params }) const body = res.data assertBusinessOk(body) return (body?.data ?? body?.Data ?? body) as Record } /** 发布同步态(轻量,不含快照正文),与产品卖价对称 */ export async function getServiceChannelPublishSyncStatus(params: { serviceChannelId: number | string operatorType: 'rules' | 'price' | string }) { const res = await axiosInstance.get(omsUrl('/channel/service-channel/publish/sync-status'), { params }) const body = res.data assertBusinessOk(body) return (body?.data ?? body?.Data ?? body) as { pending?: boolean Pending?: boolean onlineVersion?: string | null OnlineVersion?: string | null onlinePublishTime?: string | null OnlinePublishTime?: string | null pendingContentHash?: string | null onlineContentHash?: string | null } } export async function publishServiceChannel(body: { serviceChannelId: number | string operatorType: string description?: string }) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/publish'), body) assertBusinessOk(res.data) } export async function rollbackServiceChannelPublish(body: { serviceChannelId: number | string recordId: number | string operatorType: string }) { const res = await axiosInstance.post(omsUrl('/channel/service-channel/publish/rollback'), body) assertBusinessOk(res.data) }