119 lines
4.0 KiB
TypeScript
119 lines
4.0 KiB
TypeScript
|
|
import type { SteadyDataView } from '@/api/steady/steadyDataView/interface'
|
||
|
|
|
||
|
|
export const MAX_TREND_SERIES_COUNT = 24
|
||
|
|
export const MAX_HARMONIC_ORDER_COUNT = 6
|
||
|
|
|
||
|
|
export const collectSelectedLineIds = (nodes: SteadyDataView.SteadyLedgerNode[]) => {
|
||
|
|
const lineIds = new Set<string>()
|
||
|
|
|
||
|
|
const collect = (node: SteadyDataView.SteadyLedgerNode) => {
|
||
|
|
if (node.level === 3 || node.selectable) {
|
||
|
|
lineIds.add(node.id)
|
||
|
|
}
|
||
|
|
|
||
|
|
node.children?.forEach(collect)
|
||
|
|
}
|
||
|
|
|
||
|
|
nodes.forEach(collect)
|
||
|
|
|
||
|
|
return Array.from(lineIds)
|
||
|
|
}
|
||
|
|
|
||
|
|
export const collectLeafIndicators = (nodes: SteadyDataView.SteadyIndicatorNode[]) => {
|
||
|
|
const indicators: SteadyDataView.SteadyIndicatorNode[] = []
|
||
|
|
|
||
|
|
const collect = (node: SteadyDataView.SteadyIndicatorNode) => {
|
||
|
|
if (node.children?.length) {
|
||
|
|
node.children.forEach(collect)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if (node.indicatorCode) {
|
||
|
|
indicators.push(node)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
nodes.forEach(collect)
|
||
|
|
|
||
|
|
return indicators
|
||
|
|
}
|
||
|
|
|
||
|
|
export const hasHarmonicIndicator = (indicators: SteadyDataView.SteadyIndicatorNode[]) => {
|
||
|
|
return indicators.some(item => item.harmonic || Boolean(item.harmonicOrderStart || item.harmonicOrderEnd))
|
||
|
|
}
|
||
|
|
|
||
|
|
export const resolveAvailablePhases = (indicators: SteadyDataView.SteadyIndicatorNode[]) => {
|
||
|
|
const phaseSet = new Set<string>()
|
||
|
|
|
||
|
|
indicators.forEach(indicator => {
|
||
|
|
indicator.phaseCodes?.forEach(phase => phaseSet.add(phase))
|
||
|
|
})
|
||
|
|
|
||
|
|
return Array.from(phaseSet)
|
||
|
|
}
|
||
|
|
|
||
|
|
export const resolveAvailableStats = (indicators: SteadyDataView.SteadyIndicatorNode[]) => {
|
||
|
|
const statSet = new Set<SteadyDataView.SteadyTrendStatType>()
|
||
|
|
|
||
|
|
indicators.forEach(indicator => {
|
||
|
|
indicator.supportStats?.forEach(stat => statSet.add(stat))
|
||
|
|
})
|
||
|
|
|
||
|
|
return Array.from(statSet)
|
||
|
|
}
|
||
|
|
|
||
|
|
export const estimateTrendSeriesCount = (
|
||
|
|
lineIds: string[],
|
||
|
|
indicators: SteadyDataView.SteadyIndicatorNode[],
|
||
|
|
phases: string[],
|
||
|
|
statTypes: SteadyDataView.SteadyTrendStatType[],
|
||
|
|
harmonicOrders: number[]
|
||
|
|
) => {
|
||
|
|
const harmonicMultiplier = hasHarmonicIndicator(indicators) ? Math.max(harmonicOrders.length, 1) : 1
|
||
|
|
|
||
|
|
return indicators.reduce((count, indicator) => {
|
||
|
|
const indicatorPhases = indicator.phaseCodes?.length ? indicator.phaseCodes : phases
|
||
|
|
const selectedPhaseCount = indicatorPhases.filter(phase => phases.includes(phase)).length || indicatorPhases.length || 1
|
||
|
|
const fieldCount = Math.max(indicator.seriesFields?.length || indicator.baseFields?.length || 1, 1)
|
||
|
|
|
||
|
|
return count + lineIds.length * selectedPhaseCount * Math.max(statTypes.length, 1) * fieldCount * harmonicMultiplier
|
||
|
|
}, 0)
|
||
|
|
}
|
||
|
|
|
||
|
|
export const validateHarmonicOrders = (
|
||
|
|
indicators: SteadyDataView.SteadyIndicatorNode[],
|
||
|
|
harmonicOrders: number[]
|
||
|
|
) => {
|
||
|
|
if (!hasHarmonicIndicator(indicators)) return ''
|
||
|
|
if (!harmonicOrders.length) return '谐波指标必须选择谐波次数'
|
||
|
|
if (harmonicOrders.length > MAX_HARMONIC_ORDER_COUNT) return '谐波次数最多选择 6 个'
|
||
|
|
|
||
|
|
return ''
|
||
|
|
}
|
||
|
|
|
||
|
|
export const validateTrendSelection = (params: {
|
||
|
|
lineIds: string[]
|
||
|
|
indicators: SteadyDataView.SteadyIndicatorNode[]
|
||
|
|
phases: string[]
|
||
|
|
statTypes: SteadyDataView.SteadyTrendStatType[]
|
||
|
|
harmonicOrders: number[]
|
||
|
|
}) => {
|
||
|
|
const { lineIds, indicators, phases, statTypes, harmonicOrders } = params
|
||
|
|
|
||
|
|
if (!lineIds.length) return '请选择监测点'
|
||
|
|
if (!indicators.length) return '请选择趋势指标'
|
||
|
|
if (lineIds.length > 1 && indicators.length > 1) return '多监测点查询时只能选择 1 个指标'
|
||
|
|
if (!statTypes.length) return '请选择统计类型'
|
||
|
|
if (!phases.length) return '请选择相别'
|
||
|
|
|
||
|
|
const harmonicError = validateHarmonicOrders(indicators, harmonicOrders)
|
||
|
|
if (harmonicError) return harmonicError
|
||
|
|
|
||
|
|
const seriesCount = estimateTrendSeriesCount(lineIds, indicators, phases, statTypes, harmonicOrders)
|
||
|
|
if (seriesCount > MAX_TREND_SERIES_COUNT) {
|
||
|
|
return '趋势曲线数量不能超过 24 条,请缩小监测点、指标、相别或统计类型范围'
|
||
|
|
}
|
||
|
|
|
||
|
|
return ''
|
||
|
|
}
|