102 lines
3.8 KiB
TypeScript
102 lines
3.8 KiB
TypeScript
|
|
import type { MmsMapping } from '@/api/tools/mmsmapping/interface'
|
||
|
|
|
||
|
|
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
||
|
|
typeof value === 'object' && value !== null && !Array.isArray(value)
|
||
|
|
|
||
|
|
const normalizeRequiredString = (value: unknown, fieldPath: string) => {
|
||
|
|
if (typeof value !== 'string' || !value.trim()) {
|
||
|
|
throw new Error(`${fieldPath} 必须是非空字符串`)
|
||
|
|
}
|
||
|
|
|
||
|
|
return value.trim()
|
||
|
|
}
|
||
|
|
|
||
|
|
const normalizeOptionalString = (value: unknown) => (typeof value === 'string' ? value.trim() : '')
|
||
|
|
|
||
|
|
const normalizeBindings = (value: unknown, groupIndex: number): MmsMapping.IndexSelectionBinding[] => {
|
||
|
|
if (!Array.isArray(value) || !value.length) {
|
||
|
|
throw new Error(`request.indexSelection[${groupIndex}].bindings 必须是非空数组`)
|
||
|
|
}
|
||
|
|
|
||
|
|
return value.map((binding, bindingIndex) => {
|
||
|
|
if (!isRecord(binding)) {
|
||
|
|
throw new Error(`request.indexSelection[${groupIndex}].bindings[${bindingIndex}] 必须是对象`)
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
reportName: normalizeRequiredString(
|
||
|
|
binding.reportName,
|
||
|
|
`request.indexSelection[${groupIndex}].bindings[${bindingIndex}].reportName`
|
||
|
|
),
|
||
|
|
dataSetName: normalizeRequiredString(
|
||
|
|
binding.dataSetName,
|
||
|
|
`request.indexSelection[${groupIndex}].bindings[${bindingIndex}].dataSetName`
|
||
|
|
),
|
||
|
|
label: normalizeRequiredString(
|
||
|
|
binding.label,
|
||
|
|
`request.indexSelection[${groupIndex}].bindings[${bindingIndex}].label`
|
||
|
|
),
|
||
|
|
lnInst: normalizeRequiredString(
|
||
|
|
binding.lnInst,
|
||
|
|
`request.indexSelection[${groupIndex}].bindings[${bindingIndex}].lnInst`
|
||
|
|
)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
export const buildDefaultIndexSelection = (
|
||
|
|
candidateGroups: MmsMapping.IndexCandidateGroup[]
|
||
|
|
): MmsMapping.IndexSelectionGroup[] =>
|
||
|
|
candidateGroups
|
||
|
|
.filter(candidate => candidate.groupKey?.trim())
|
||
|
|
.map(candidate => {
|
||
|
|
const defaultReport = (candidate.reports || []).find(
|
||
|
|
report => report.reportName?.trim() && report.dataSetName?.trim()
|
||
|
|
)
|
||
|
|
const defaultLnInst = (defaultReport?.availableLnInstValues || []).find(item => item?.trim())?.trim() || ''
|
||
|
|
|
||
|
|
return {
|
||
|
|
groupKey: candidate.groupKey!.trim(),
|
||
|
|
groupDesc: candidate.groupDesc?.trim() || '',
|
||
|
|
bindings: (candidate.templateLabels || [])
|
||
|
|
.map(label => label?.trim() || '')
|
||
|
|
.filter(Boolean)
|
||
|
|
.map(label => ({
|
||
|
|
reportName: defaultReport?.reportName?.trim() || '',
|
||
|
|
dataSetName: defaultReport?.dataSetName?.trim() || '',
|
||
|
|
label,
|
||
|
|
lnInst: defaultLnInst
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
export const formatIndexSelectionJson = (value: MmsMapping.IndexSelectionGroup[]) => JSON.stringify(value, null, 4)
|
||
|
|
|
||
|
|
export const parseIndexSelectionJson = (source: string): MmsMapping.IndexSelectionGroup[] => {
|
||
|
|
let parsed: unknown
|
||
|
|
|
||
|
|
try {
|
||
|
|
parsed = JSON.parse(source)
|
||
|
|
} catch {
|
||
|
|
throw new Error('request.indexSelection 不是合法 JSON')
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!Array.isArray(parsed)) {
|
||
|
|
throw new Error('request.indexSelection 必须是数组')
|
||
|
|
}
|
||
|
|
|
||
|
|
return parsed.map((group, groupIndex) => {
|
||
|
|
if (!isRecord(group)) {
|
||
|
|
throw new Error(`request.indexSelection[${groupIndex}] 必须是对象`)
|
||
|
|
}
|
||
|
|
|
||
|
|
const groupDesc = normalizeOptionalString(group.groupDesc)
|
||
|
|
|
||
|
|
return {
|
||
|
|
groupKey: normalizeRequiredString(group.groupKey, `request.indexSelection[${groupIndex}].groupKey`),
|
||
|
|
groupDesc,
|
||
|
|
bindings: normalizeBindings(group.bindings, groupIndex)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|