import request from '@/utils/request'; import { getProcessInstance } from '@/api/bpm/processInstance'; function unwrap(response) { const result = (response && response.data) || {}; if (Number(result.code) === 0) return result.data; const error = new Error(result.message || '服务调用失败'); if (response && response.__toastShown) error.__toastShown = true; return Promise.reject(error); } function hasId(value) { return value !== undefined && value !== null && String(value).trim() !== ''; } /** * 薪资核算工作台汇总:批次状态、同步状态、人数、完成率、异常计数、可用动作。 * GET /hr/payroll/batches/{batchId}/dashboard * 不含工资金额与来源快照正文。 */ export async function getPayrollBatchDashboard(batchId) { if (!hasId(batchId)) return null; const response = await request({ url: `/hr/payroll/batches/${encodeURIComponent(String(batchId))}/dashboard`, method: 'get' }); return unwrap(response); } /** * 批次未关闭异常:GET /hr/payroll/batches/{batchId}/exceptions */ export async function listPayrollBatchExceptions(batchId) { if (!hasId(batchId)) return []; const response = await request({ url: `/hr/payroll/batches/${encodeURIComponent(String(batchId))}/exceptions`, method: 'get' }); return unwrap(response) || []; } /** * 法人内批次摘要:GET /hr/payroll/batches?companyId=xxx * 含审批单据编码、模板版本、发起人、创建时间,不含金额。 */ export async function listPayrollBatches(companyId) { if (!hasId(companyId)) return []; const response = await request({ url: '/hr/payroll/batches', method: 'get', params: { companyId } }); return unwrap(response) || []; } /** * 法人可用薪资模板摘要:GET /hr/payroll/templates?companyId=xxx * 批次摘要只返回模板主键和版本号,名称需另取。 */ export async function listPayrollTemplates(companyId) { if (!hasId(companyId)) return []; const response = await request({ url: '/hr/payroll/templates', method: 'get', params: { companyId } }); return unwrap(response) || []; } /** * 按模板主键查模板名称,取不到时返回空字符串(不阻断审批页渲染)。 */ export async function resolvePayrollTemplateName(companyId, templateId) { if (!hasId(companyId) || !hasId(templateId)) return ''; try { const list = await listPayrollTemplates(companyId); const records = Array.isArray(list) ? list : []; const matched = records.find((item) => String(item.id) === String(templateId)); return (matched && matched.templateName) || ''; } catch (error) { return ''; } } /** * 在法人批次摘要列表中查找当前批次。 */ export async function getPayrollBatchSummary(companyId, batchId) { if (!hasId(companyId) || !hasId(batchId)) return {}; const list = await listPayrollBatches(companyId); const records = Array.isArray(list) ? list : []; return ( records.find((item) => String(item.id) === String(batchId)) || {} ); } /** * 审批详情取数(审批页用) * * 1. 待办传入的 businessId 即薪资批次主键,直接取工作台汇总; * 2. 汇总取不到时回退流程实例 formVariables(发起审批时透传的批次字段); * 3. 汇总可用时再并行取未关闭异常与批次摘要(单据编码/模板版本)。 * * 不经过薪资明细接口,审批页不取个人薪资数据。 */ export async function loadPayrollBatchApprovalDetail( businessId, processInstanceId = '' ) { const result = { dashboard: {}, summary: {}, exceptions: [], formVariables: {}, errorMessage: '' }; let batchId = hasId(businessId) ? businessId : ''; let dashboardError = ''; if (hasId(batchId)) { try { result.dashboard = (await getPayrollBatchDashboard(batchId)) || {}; } catch (error) { dashboardError = error.message || ''; } } if ( (!result.dashboard || !Object.keys(result.dashboard).length) && hasId(processInstanceId) ) { try { const instance = await getProcessInstance(processInstanceId); result.formVariables = (instance && instance.formVariables) || {}; const fallbackId = result.formVariables.businessId; if (hasId(fallbackId) && String(fallbackId) !== String(batchId)) { batchId = fallbackId; try { result.dashboard = (await getPayrollBatchDashboard(batchId)) || {}; } catch (error) { dashboardError = error.message || dashboardError; } } } catch (error) { /* 流程实例也取不到时保留流程表单摘要 */ } } if (result.dashboard && Object.keys(result.dashboard).length > 0) { const companyId = result.dashboard.companyId; await Promise.all([ listPayrollBatchExceptions(batchId) .then((list) => { result.exceptions = Array.isArray(list) ? list : []; }) .catch(() => { result.exceptions = []; }), getPayrollBatchSummary(companyId, batchId) .then(async (summary) => { result.summary = summary || {}; if (hasId(result.summary.templateId)) { const templateName = await resolvePayrollTemplateName( companyId, result.summary.templateId ); if (templateName) result.summary.templateName = templateName; } }) .catch(() => { result.summary = {}; }) ]); } if (dashboardError) { result.errorMessage = dashboardError; } return result; }