// Shared by query, historical-result and draft cards. These business priorities // mirror BusinessActionRegistry.businessFieldOrder, including older cached cards. const noteKeys = new Set(['remark', 'description', 'reply', 'progressRemark', 'acceptanceRemark', 'revokeRemark', 'noticeContent']) const compactKeys = new Set(['quantity', 'arrivalQuantity', 'defectiveQuantity', 'outNum', 'realNum', 'bookNum', 'stockNum', 'stockQuantity', 'available', 'remainingQuantity', 'outstandingQuantity', 'returnedQuantity', 'acceptedQuantity', 'unitQuantity', 'pageNum', 'pageSize', 'sortOrder', 'dictSort']) const inventoryOrder = ['productName', 'itemName', 'materialName', 'itemId', 'productId', 'spec', 'materialParams', 'brand', 'stockNum', 'inNum', 'quantity', 'realNum', 'outNum', 'arrivalQuantity', 'acceptedQuantity', 'remainingQuantity', 'unit', 'areaName', 'area_name', 'areaId', 'fromAreaId', 'toAreaId', 'projectName', 'projectId', 'sourceProjectName', 'sourceProjectId', 'usageProjectName', 'usageProjectId', 'inStatus', 'checkStatus', 'acceptanceStatus', 'status', 'averageUnitCost', 'stockAmount', 'categoryGroupName', 'category'] const productionOrder = ['sourceProductId', 'seriesName', 'originalEditionTag', 'editionTag', 'cloneItem', 'itemName', 'itemSpec', 'item_name', 'productName', 'itemId', 'productId', 'copyBomId', 'bomTag', 'projectName', 'project_name', 'projectId', 'spec', 'brand', 'category', 'unit', 'versionTag', 'version_tag', 'bomId', 'taskNo', 'task_no', 'taskId', 'status', 'quantity', 'planned_quantity', 'good_quantity', 'required', 'available', 'shortage', 'purchaseSuggestion', 'unitQuantity', 'unitCost', 'unit_cost', 'amount', 'material_cost', 'current_material_cost'] const budgetOrder = ['projectName', 'projectId', 'categoryName', 'expenseCategoryId', 'categoryCode', 'budgetAmount', 'spentAmount', 'committedAmount', 'availableAmount', 'alertPercent', 'status'] const dictionaryOrder = ['dictLabel', 'dictName', 'dictType', 'dictValue', 'parentCode', 'status', 'dictSort'] export const assistantBusinessColumnOrder = Object.freeze({ project: ['projectName', 'projectCode', 'projectType', 'projectStatus', 'totalAmount', 'committedAmount', 'availableAmount'], area: ['areaName', 'status'], supplier: ['name', 'contact', 'phone', 'email', 'active'], stock: inventoryOrder, stockCheck: inventoryOrder, material: inventoryOrder, receipt: inventoryOrder, arrival: inventoryOrder, purchase: ['materialName', 'productName', 'productId', 'quantity', 'unit', 'finalUnitPrice', 'estimatedUnitPrice', 'amount', 'materialParams', 'spec', 'materialBrand', 'brand', 'category', 'projectName', 'projectId', 'expenseCategoryId', 'purchaseBatchNo', 'purchaseBatchId', 'batchName', 'supplierName', 'supplierId', 'purchaseStatus', 'purchasePurpose'], batch: ['batchName', 'purchaseBatchNo', 'batchNo', 'supplierName', 'supplierId', 'purchaseStatus', 'quantity', 'amount', 'lockedAmount', 'unpriced'], item: productionOrder, product: productionOrder, projectProduct: productionOrder, bom: productionOrder, cost: productionOrder, production: productionOrder, loan: ['itemName', 'productName', 'itemId', 'spec', 'brand', 'unit', 'lenderProjectName', 'lenderProjectId', 'borrowerProjectName', 'borrowerProjectId', 'areaName', 'fromAreaId', 'toAreaId', 'loanNo', 'quantity', 'returnedQuantity', 'outstandingQuantity', 'returnType', 'active', 'amount'], budget: budgetOrder, expenseCategory: budgetOrder, dictionary: dictionaryOrder, dictionaryType: dictionaryOrder, feedback: ['title', 'type', 'description', 'status', 'reply', 'submitterName', 'pageTitle', 'pagePath'], notification: ['title', 'id', 'content', 'readAt', 'deliveredAt', 'count'], feedbackNotification: ['module', 'subject', 'recipients', 'body', 'roleNames', 'autoSend', 'mailState', 'analysisState', 'reason', 'missingEmails', 'recipientError'], notice: ['noticeTitle', 'noticeType', 'noticeContent', 'status'], user: ['checkedIdentity', 'matchResult', 'nickName', 'userName', 'deptName', 'deptId', 'mode', 'roleIds', 'previousRoleNames', 'resultRoleNames', 'roleNames', 'postIds', 'status', 'email', 'phonenumber'], role: ['roleName', 'roleKey', 'menuName', 'perms', 'menuType', 'mode', 'menuIds', 'previousPermissionNames', 'resultPermissionNames', 'permissionNames', 'dataScope', 'deptIds', 'status', 'roleSort'], menu: ['menuName', 'menuType', 'path', 'component', 'perms', 'status', 'visible'], department: ['deptName', 'parentId', 'leader', 'phone', 'email', 'status', 'orderNum'], post: ['postName', 'postCode', 'status', 'postSort'], audit: ['title', 'operName', 'userName', 'businessType', 'status', 'operTime', 'loginTime', 'costTime'] }) export function assistantFieldOrder(key, { action = '', editing = false } = {}) { if (editing && key === 'id') return 0 // These are labelled source-document selectors, not raw internal IDs. if (editing && ['purchaseRequestId', 'requestId', 'arrivalId'].includes(key)) return 5 const position = (assistantBusinessColumnOrder[action === 'role.users' ? 'user' : ['user.me', 'user.permissions'].includes(action) ? 'menu' : action.split('.')[0]] || []).indexOf(key) if (position >= 0) return 10 + position if (['name', 'itemName', 'item_name', 'productName', 'materialName', 'projectName', 'project_name', 'areaName', 'area_name', 'supplierName', 'categoryName'].includes(key)) return 150 if (['code', 'requestNo', 'inNo', 'arrivalNo', 'checkNo', 'taskNo', 'task_no', 'loanNo', 'eventNo', 'event_no', 'ledgerNo', 'bizNo', 'revision_no'].includes(key)) return 200 if (['spec', 'brand', 'category', 'unit', 'versionTag', 'version_tag'].includes(key)) return 250 if (/status/i.test(key) || ['active', 'kind', 'type', 'complete'].includes(key)) return 300 if (['pageNum', 'pageSize'].includes(key)) return 1100 if (/quantity|amount|cost|num|price|count|balance|delta/i.test(key) || ['required', 'available', 'shortage', 'issued', 'returned', 'outstanding'].includes(key)) return 350 if (['date', 'businessDate', 'business_date', 'beginDate', 'endDate'].includes(key)) return 650 if (['remark', 'description', 'reason', 'acceptanceRemark', 'revokeRemark', 'progressRemark', 'purchaseLink'].includes(key)) return 800 if (key === 'id' || key.endsWith('Id')) return editing ? 400 : 900 if (/time/i.test(key) || ['createBy', 'updateBy', 'create_by', 'operator', 'version'].includes(key)) return 1000 return 500 } export function assistantOrderedColumns(columns = [], options = {}) { const source = columns.filter(column => column?.key && !column.hidden && (options.editing || !['includeZero', 'pageNum', 'pageSize'].includes(column.key))) const rank = column => assistantFieldOrder(column.key, options) return source.map((column, index) => ({ column, index })).sort((a, b) => rank(a.column) - rank(b.column) || a.index - b.index).map(entry => entry.column) } // Keep audit/empty columns available without making them the default reading task. // Do not hide IDs in unrelated results where they are a business identifier. export function assistantQueryColumns(columns = [], rows = [], options = {}) { const ordered = assistantOrderedColumns(columns, options) if (!['stock', 'stockCheck', 'material', 'receipt', 'arrival'].includes((options.action || '').split('.')[0])) return { primary: ordered, details: [] } const audit = new Set(['id', 'version', 'createTime', 'updateTime', 'createBy', 'updateBy', 'remark']) const details = ordered.filter(column => audit.has(column.key) || (rows.length && rows.every(row => row[column.key] == null || row[column.key] === ''))) const extra = new Set(details) return { primary: ordered.filter(column => !extra.has(column)), details } } export function assistantColumnWidth(column = {}, { editing = false, compact = false, primary = false } = {}) { const key = column.key || '' if (column.type === 'authorizationChanges') return compact ? 340 : 440 if (primary) return editing ? compact ? 184 : 232 : compact ? 168 : 220 if (noteKeys.has(key) || column.multiline) return compact ? 240 : 300 if (key === 'unit') return editing ? 100 : 76 if (compactKeys.has(key) || ['number', 'decimal', 'integer'].includes(column.type)) return editing ? 126 : 104 if (/status|active|complete|isDefault|priceLocked/i.test(key) || column.type === 'boolean') return editing ? 150 : 116 if (column.type === 'date' || /Date$|Time$|_date$|_time$/.test(key)) return /Time$|_time$/.test(key) ? 174 : 164 if (/amount|price|cost|budget|balance|total/i.test(key)) return editing ? 132 : 136 if (/spec|materialParams/i.test(key)) return compact ? 164 : 210 if (/brand|category/i.test(key)) return editing ? 172 : 132 if (/areaName|area_name/i.test(key)) return compact ? 116 : 150 if (column.ref) return compact ? 192 : 230 if (/projectName|project_name|supplierName/i.test(key)) return compact ? 164 : 220 if (/No$|Code$|_no$/.test(key) || key === 'id') return compact ? 150 : 180 return editing ? compact ? 168 : 190 : compact ? 140 : 170 } // Freeze only one business-identity column. On a genuinely narrow panel an // unfrozen table is safer than fixed columns covering all editable content. export function assistantTableLayout(columns = [], { width = 600, editing = false, leadingWidth = 0, trailingWidth = 0, primaryKey } = {}) { const available = Number(width) > 0 ? Number(width) : 600 const compact = available < 760 const primary = primaryKey || columns[0]?.key const identityWidth = assistantColumnWidth(columns.find(column => column.key === primary), { editing, compact, primary: true }) const fixed = available >= 420 && available - leadingWidth - trailingWidth - identityWidth >= 160 return { compact, fixed, columns: columns.map(column => ({ ...column, displayWidth: column.key === primary ? identityWidth : undefined, displayMinWidth: assistantColumnWidth(column, { editing, compact, primary: column.key === primary }), fixed: fixed && column.key === primary ? 'left' : false, align: !editing && (compactKeys.has(column.key) || /amount|price|cost|budget|balance|total/i.test(column.key)) ? 'right' : undefined })) } } // Leave room below the table for its pagination/result text. Otherwise following // the newest message can scroll the entire table header outside the conversation. export function assistantTableMaxHeight(conversationHeight, mode = 'draft') { const available = Number(conversationHeight) > 0 ? Number(conversationHeight) : 400 const reserved = mode === 'query' ? 210 : 86 return Math.max(96, Math.min(mode === 'query' ? 360 : 400, Math.floor(available - reserved))) }