40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { fetchCellProductSpecList } from '@/api/admin'
|
||
|
||
/** 从产出/细胞产品行解析规格展示文案(兼容 specName / specText 等字段) */
|
||
export function resolveCellProductSpecDisplay(row: Record<string, any> | null | undefined): string {
|
||
if (!row || typeof row !== 'object') return ''
|
||
const raw =
|
||
row.specName ?? row.specText ?? row.spec_name ?? row.spec_text ?? row.cellProductSpecName
|
||
return raw == null ? '' : String(raw).trim()
|
||
}
|
||
|
||
/** 规范化细胞产品行:规格统一写入 specName / specText(文本,非主数据 ID) */
|
||
export function normalizeCellProductRow<T extends Record<string, any>>(row: T): T {
|
||
const specText = resolveCellProductSpecDisplay(row)
|
||
return {
|
||
...row,
|
||
specName: specText,
|
||
specText
|
||
}
|
||
}
|
||
|
||
/** 按规格名称精确匹配主数据,返回 specId(可选关联) */
|
||
export async function resolveCellProductSpecIdByName(
|
||
specName: string
|
||
): Promise<string | undefined> {
|
||
const name = specName.trim()
|
||
if (!name) return undefined
|
||
try {
|
||
const res = await fetchCellProductSpecList({
|
||
page: 1,
|
||
pageSize: 100,
|
||
keyword: name
|
||
})
|
||
const list = res?.list || res || []
|
||
const exact = list.find((item: { specName?: string }) => item.specName === name)
|
||
return exact?.id
|
||
} catch {
|
||
return undefined
|
||
}
|
||
}
|