import { axiosInstance } from '@/api/rbac' import { assertBusinessOk, extractOmsRequestError, omsUrl, parseListPayload, unwrapData, } from '@/api/oms/common' export interface ApprovalDefinitionDto { id: number operationCode: string operationName: string isEnabled: boolean conditionFormula?: string | null approverType: string approverIds: string[] forbidSelfApprove: boolean remark?: string | null createTime?: string updateTime?: string } export interface ApprovalCatalogItem { code: string name: string module: string supportsCondition: boolean conditionHint: string } export interface ApprovalInstanceDto { id: number instanceNo: string operationCode: string bizKey: string title: string status: number statusName: string applicantUserId: number applicantUserGuid: string applicantUserName?: string | null payloadJson?: string | null summaryJson?: string | null definitionSnapshot?: string | null currentTaskId?: number | null createTime?: string completedTime?: string | null executedTime?: string | null executeMessage?: string | null concurrencyStamp?: string | null canApprove?: boolean canWithdraw?: boolean events?: ApprovalEventDto[] } export interface ApprovalEventDto { id: number eventType: string fromStatus?: number | null toStatus?: number | null operatorUserName?: string | null message?: string | null createTime?: string } function pickString(row: Record, ...keys: string[]) { for (const key of keys) { const value = row[key] if (value != null && String(value).trim()) return String(value).trim() } return '' } function pickBool(row: Record, ...keys: string[]) { for (const key of keys) { const value = row[key] if (typeof value === 'boolean') return value if (value === 1 || value === '1' || value === 'true') return true if (value === 0 || value === '0' || value === 'false') return false } return false } function pickNumber(row: Record, ...keys: string[]) { for (const key of keys) { const value = row[key] if (typeof value === 'number' && Number.isFinite(value)) return value const n = Number(value) if (Number.isFinite(n)) return n } return 0 } function pickIds(row: Record): string[] { const raw = row.approverIds ?? row.ApproverIds if (Array.isArray(raw)) return raw.map((x) => String(x).trim()).filter(Boolean) if (typeof raw === 'string') { return raw.split(/[,;,\s]+/).map((x) => x.trim()).filter(Boolean) } return [] } export function mapDefinitionRow(row: Record): ApprovalDefinitionDto { return { id: pickNumber(row, 'id', 'Id'), operationCode: pickString(row, 'operationCode', 'OperationCode'), operationName: pickString(row, 'operationName', 'OperationName'), isEnabled: pickBool(row, 'isEnabled', 'IsEnabled'), conditionFormula: pickString(row, 'conditionFormula', 'ConditionFormula') || null, approverType: pickString(row, 'approverType', 'ApproverType') || 'User', approverIds: pickIds(row), forbidSelfApprove: pickBool(row, 'forbidSelfApprove', 'ForbidSelfApprove'), remark: pickString(row, 'remark', 'Remark') || null, createTime: pickString(row, 'createTime', 'CreateTime') || undefined, updateTime: pickString(row, 'updateTime', 'UpdateTime') || undefined, } } export function mapInstanceRow(row: Record): ApprovalInstanceDto { const eventsRaw = (row.events ?? row.Events) as Record[] | undefined return { id: pickNumber(row, 'id', 'Id'), instanceNo: pickString(row, 'instanceNo', 'InstanceNo'), operationCode: pickString(row, 'operationCode', 'OperationCode'), bizKey: pickString(row, 'bizKey', 'BizKey'), title: pickString(row, 'title', 'Title'), status: pickNumber(row, 'status', 'Status'), statusName: pickString(row, 'statusName', 'StatusName'), applicantUserId: pickNumber(row, 'applicantUserId', 'ApplicantUserId'), applicantUserGuid: pickString(row, 'applicantUserGuid', 'ApplicantUserGuid'), applicantUserName: pickString(row, 'applicantUserName', 'ApplicantUserName') || null, payloadJson: pickString(row, 'payloadJson', 'PayloadJson') || null, summaryJson: pickString(row, 'summaryJson', 'SummaryJson') || null, definitionSnapshot: pickString(row, 'definitionSnapshot', 'DefinitionSnapshot') || null, currentTaskId: pickNumber(row, 'currentTaskId', 'CurrentTaskId') || null, createTime: pickString(row, 'createTime', 'CreateTime') || undefined, completedTime: pickString(row, 'completedTime', 'CompletedTime') || null, executedTime: pickString(row, 'executedTime', 'ExecutedTime') || null, executeMessage: pickString(row, 'executeMessage', 'ExecuteMessage') || null, concurrencyStamp: pickString(row, 'concurrencyStamp', 'ConcurrencyStamp') || null, canApprove: pickBool(row, 'canApprove', 'CanApprove'), canWithdraw: pickBool(row, 'canWithdraw', 'CanWithdraw'), events: Array.isArray(eventsRaw) ? eventsRaw.map((e) => ({ id: pickNumber(e, 'id', 'Id'), eventType: pickString(e, 'eventType', 'EventType'), fromStatus: pickNumber(e, 'fromStatus', 'FromStatus') || null, toStatus: pickNumber(e, 'toStatus', 'ToStatus') || null, operatorUserName: pickString(e, 'operatorUserName', 'OperatorUserName') || null, message: pickString(e, 'message', 'Message') || null, createTime: pickString(e, 'createTime', 'CreateTime') || undefined, })) : [], } } export async function getApprovalDefinitionList(params: { page?: number pageSize?: number keyword?: string isEnabled?: boolean | null }) { const res = await axiosInstance.get(omsUrl('/system/approval/definition/list'), { params }) const { items, total } = parseListPayload(res.data) return { items: (items || []).map((row) => mapDefinitionRow(row as Record)), total, } } export async function getApprovalCatalog() { const res = await axiosInstance.get(omsUrl('/system/approval/operation-catalog')) const payload = unwrapData[] | null>(res.data) if (!Array.isArray(payload)) return [] as ApprovalCatalogItem[] return payload.map((row) => ({ code: pickString(row, 'code', 'Code'), name: pickString(row, 'name', 'Name'), module: pickString(row, 'module', 'Module'), supportsCondition: pickBool(row, 'supportsCondition', 'SupportsCondition'), conditionHint: pickString(row, 'conditionHint', 'ConditionHint'), })) } export async function saveApprovalDefinition(body: { id?: number operationCode: string isEnabled: boolean conditionFormula?: string | null approverType: string approverIds: string[] forbidSelfApprove: boolean remark?: string | null }) { try { const res = await axiosInstance.post(omsUrl('/system/approval/definition/save'), body) assertBusinessOk(res.data) return mapDefinitionRow((unwrapData(res.data) || {}) as Record) } catch (e) { throw new Error(extractOmsRequestError(e)) } } export async function enableApprovalDefinition(id: number, isEnabled: boolean) { try { const res = await axiosInstance.post(omsUrl('/system/approval/definition/enable'), { id, isEnabled }) assertBusinessOk(res.data) } catch (e) { throw new Error(extractOmsRequestError(e)) } } export async function deleteApprovalDefinition(id: number) { try { const res = await axiosInstance.post(omsUrl('/system/approval/definition/delete'), { id }) assertBusinessOk(res.data) } catch (e) { throw new Error(extractOmsRequestError(e)) } } export async function getApprovalMyTodo(params: { page?: number pageSize?: number keyword?: string }) { const res = await axiosInstance.get(omsUrl('/system/approval/engine/my-todo'), { params }) const { items, total } = parseListPayload(res.data) return { items: (items || []).map((row) => mapInstanceRow(row as Record)), total, } } export async function getApprovalMySubmitted(params: { page?: number pageSize?: number keyword?: string status?: number | null }) { const res = await axiosInstance.get(omsUrl('/system/approval/engine/my-submitted'), { params }) const { items, total } = parseListPayload(res.data) return { items: (items || []).map((row) => mapInstanceRow(row as Record)), total, } } export async function getApprovalDetail(id: number) { const res = await axiosInstance.get(omsUrl('/system/approval/engine/detail'), { params: { id } }) return mapInstanceRow((unwrapData(res.data) || {}) as Record) } export async function approveApproval(body: { instanceId: number comment?: string concurrencyStamp?: string | null }) { try { const res = await axiosInstance.post(omsUrl('/system/approval/engine/approve'), body) assertBusinessOk(res.data) } catch (e) { throw new Error(extractOmsRequestError(e)) } } export async function rejectApproval(body: { instanceId: number comment: string concurrencyStamp?: string | null }) { try { const res = await axiosInstance.post(omsUrl('/system/approval/engine/reject'), body) assertBusinessOk(res.data) } catch (e) { throw new Error(extractOmsRequestError(e)) } } export async function withdrawApproval(body: { instanceId: number comment?: string concurrencyStamp?: string | null }) { try { const res = await axiosInstance.post(omsUrl('/system/approval/engine/withdraw'), body) assertBusinessOk(res.data) } catch (e) { throw new Error(extractOmsRequestError(e)) } } export async function simulateApprovalSubmit(body: { operationCode?: string bizKey?: string title?: string payload?: unknown summary?: unknown }) { try { const res = await axiosInstance.post(omsUrl('/system/approval/engine/simulate-submit'), body) assertBusinessOk(res.data) return unwrapData(res.data) as { decision?: string instanceId?: number instanceNo?: string message?: string } } catch (e) { throw new Error(extractOmsRequestError(e)) } }