@@ -1,12 +1,19 @@
package com.njcn.gather.result.service.impl ;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.util.ObjectUtil ;
import com.njcn.db.mybatisplus.handler.DynamicTableNameHandler ;
import cn.hutool.core.util.StrUtil ;
import com.github.yulichang.wrapper.MPJLambdaWrapper ;
import com.njcn.common.pojo.enums.common.DataStateEnum ;
import com.njcn.gather.device.device.service.IPqDevService ;
import com.njcn.gather.device.script.mapper.PqScriptMapper ;
import com.njcn.gather.device.script.pojo.po.PqScriptDtls ;
import com.njcn.gather.device.script.service.IPqScriptDtlsService ;
import com.njcn.gather.plan.pojo.po.AdPlan ;
import com.njcn.gather.plan.service.IAdPlanService ;
import com.njcn.gather.result.pojo.enums.ResultUnitEnum ;
import com.njcn.gather.result.pojo.param.ResultParam ;
import com.njcn.gather.result.pojo.vo.DetectionInfoVO ;
import com.njcn.gather.result.pojo.vo.FormContentVO ;
import com.njcn.gather.result.service.IResultService ;
import com.njcn.gather.storage.pojo.po.AdBaseResult ;
@@ -20,6 +27,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service ;
import java.util.* ;
import java.util.function.Function ;
import java.util.stream.Collectors ;
/**
@@ -37,9 +45,12 @@ public class ResultServiceImpl implements IResultService {
private final AdNonHarmonicService adNonHarmonicService ;
private final AdHarmonicService adHarmonicService ;
private final IPqScriptDtlsService pqScriptDtlsService ;
private final PqScriptMapper scriptMapper ;
private final IDictTreeService dictTreeService ;
// 谐波类code, 取树形字典表中的code
/**
* 谐波类code, 取树形字典表中的code
*/
private final List < String > HARMONIC_TYPE_CODE = Arrays . asList ( " HV " , " HI " , " HP " , " HSV " , " HSI " ) ;
@@ -60,35 +71,22 @@ public class ResultServiceImpl implements IResultService {
List < Map < String , String > > chnList = new ArrayList < > ( ) ;
List < AdBaseResult > allResultList = new ArrayList < > ( ) ;
if ( ObjectUtil . isNotNull ( queryParam . getScriptType ( ) ) ) { //只查询指定的脚本类型
//只查询指定的脚本类型
if ( ObjectUtil . isNotNull ( queryParam . getScriptType ( ) ) ) {
List < Integer > indexList = pqScriptDtlsService . getIndexList ( queryParam . getScriptType ( ) , scriptId ) ;
DictTree dictTree = dictTreeService . getById ( queryParam . getScriptType ( ) ) ;
if ( HARMONIC_TYPE_CODE . contains ( dictTree . getCode ( ) ) ) {
String prefix = " ad_harmonic_result_ " ;
DynamicTableNameHandler . setTableName ( prefix + plan . getCode ( ) ) ;
allResultList . addAll ( adHarmonicService . get ( scriptId , indexList , queryParam . getDeviceId ( ) , queryParam . getChnNum ( ) ) ) ;
DynamicTableNameHandler . remove ( ) ;
allResultList . addAll ( adHarmonicService . get ( scriptId , indexList , queryParam . getDeviceId ( ) , queryParam . getChnNum ( ) , plan . getCode ( ) ) ) ;
} else {
String prefix = " ad_non_harmonic_result_ " ;
DynamicTableNameHandler . setTableName ( prefix + plan . getCode ( ) ) ;
allResultList . addAll ( adNonHarmonicService . get ( scriptId , indexList , queryParam . getDeviceId ( ) , queryParam . getChnNum ( ) ) ) ;
DynamicTableNameHandler . remove ( ) ;
allResultList . addAll ( adNonHarmonicService . get ( scriptId , indexList , queryParam . getDeviceId ( ) , queryParam . getChnNum ( ) , plan . getCode ( ) ) ) ;
}
} else { //查询所有的脚本类型
String prefix = " ad_harmonic_result_ " ;
DynamicTableNameHandler . s etTableName ( prefix + plan . getCode ( ) ) ;
allResultList . addAll ( adHarmonicService . get ( scriptId , null , queryParam . getDeviceId ( ) , queryParam . getChnNum ( ) ) ) ;
DynamicTableNameHandler . remove ( ) ;
prefix = " ad_non_harmonic_result_ " ;
DynamicTableNameHandler . setTableName ( prefix + plan . getCode ( ) ) ;
allResultList . addAll ( adNonHarmonicService . get ( scriptId , null , queryParam . getDeviceId ( ) , queryParam . getChnNum ( ) ) ) ;
DynamicTableNameHandler . remove ( ) ;
allResultList . addAll ( adHarmonicService . get ( scriptId , null , queryParam . getDeviceId ( ) , queryParam . getChnNum ( ) , plan . getCode ( ) ) ) ;
allResultList . addAll ( adNonHarmonicService . g et( scriptId , null , queryParam . getDeviceId ( ) , queryParam . getChnNum ( ) , plan . getCode ( ) ) ) ;
}
if ( ObjectUtil . isNotEmpty ( allResultList ) ) {
if ( ObjectUtil . isNotEmpty ( allResultList ) ) {
Map < String , List < AdBaseResult > > chnMap = allResultList . stream ( ) . collect (
Collectors . groupingBy ( obj - > obj . getMonitorId ( ) . substring ( obj . getMonitorId ( ) . lastIndexOf ( " _ " ) + 1 ) )
) ;
@@ -102,8 +100,198 @@ public class ResultServiceImpl implements IResultService {
}
chnList . sort ( Comparator . comparingInt ( o - > Integer . parseInt ( o . get ( " value " ) ) ) ) ;
formContentVO . setChnList ( chnList ) ;
return formContentVO ;
}
@Override
public List < DetectionInfoVO > treeData ( ResultParam param ) {
//根据所有的检测脚本查询出检测信息
List < PqScriptDtls > dtlsList = pqScriptDtlsService . list ( new MPJLambdaWrapper < PqScriptDtls > ( )
. selectAll ( PqScriptDtls . class )
. eq ( StrUtil . isNotBlank ( param . getScriptId ( ) ) , PqScriptDtls : : getScriptId , param . getScriptId ( ) )
. eq ( StrUtil . isNotBlank ( param . getScriptType ( ) ) , PqScriptDtls : : getScriptType , param . getScriptType ( ) )
. eq ( PqScriptDtls : : getIndex , 81 )
. ne ( PqScriptDtls : : getIndex , - 1 )
. eq ( PqScriptDtls : : getEnable , DataStateEnum . ENABLE . getCode ( ) ) ) ;
List < AdBaseResult > allResultList = new ArrayList < > ( ) ;
List < Integer > indexList = new ArrayList < > ( ) ;
if ( StrUtil . isNotBlank ( param . getScriptType ( ) ) ) {
indexList = pqScriptDtlsService . getIndexList ( param . getScriptType ( ) , param . getScriptId ( ) ) ;
}
allResultList . addAll ( adNonHarmonicService . get ( param . getScriptId ( ) , indexList , param . getDevId ( ) , param . getDevNum ( ) , param . getCode ( ) ) ) ;
allResultList . addAll ( adHarmonicService . get ( param . getScriptId ( ) , indexList , param . getDevId ( ) , param . getDevNum ( ) , param . getCode ( ) ) ) ;
Map < Integer , Set < Integer > > resultMap = new HashMap < > ( 5 ) ;
if ( CollUtil . isNotEmpty ( allResultList ) ) {
resultMap = allResultList . stream ( ) . collect ( Collectors . groupingBy ( AdBaseResult : : getIndex , Collectors . mapping ( AdBaseResult : : getResultFlag , Collectors . toSet ( ) ) ) ) ;
}
Map < String , List < PqScriptDtls > > dtlsSortMap = dtlsList . stream ( )
. sorted ( Comparator . comparing ( PqScriptDtls : : getIndex ) )
. collect ( Collectors . groupingBy ( PqScriptDtls : : getScriptType , LinkedHashMap : : new , Collectors . toList ( ) ) ) ;
List < DictTree > dictTreeById = dictTreeService . getDictTreeById ( new ArrayList < > ( dtlsSortMap . keySet ( ) ) ) ;
Map < String , DictTree > dictTreeMap = dictTreeById . stream ( ) . collect ( Collectors . toMap ( DictTree : : getId , Function . identity ( ) ) ) ;
Map < String , String > subName = new LinkedHashMap < > ( ) ;
subName . put ( " Base " , " 额定工作条件下的测量 " ) ;
subName . put ( " VOL " , " 电压对XX测量的影响 " ) ;
subName . put ( " Freq " , " 频率对XX测量的影响 " ) ;
subName . put ( " Harm " , " 谐波对XX测量的影响 " ) ;
Boolean isValueType = scriptMapper . selectScriptIsValueType ( param . getScriptId ( ) ) ;
List < DetectionInfoVO > infoVOS = new ArrayList < > ( ) ;
Map < Integer , Set < Integer > > finalResultMap = resultMap ;
dtlsSortMap . forEach ( ( key , value ) - > {
DictTree dictTree = dictTreeMap . get ( key ) ;
if ( ObjectUtil . isNotNull ( dictTree ) ) {
List < DetectionInfoVO > scriptSubList = new ArrayList < > ( ) ;
LinkedHashMap < String , List < PqScriptDtls > > subTypeMap = value . stream ( )
. sorted ( Comparator . comparing ( PqScriptDtls : : getIndex ) )
. collect ( Collectors . groupingBy ( PqScriptDtls : : getScriptSubType , LinkedHashMap : : new , Collectors . toList ( ) ) ) ;
subTypeMap . forEach ( ( subKey , subValue ) - > {
List < DetectionInfoVO > scriptDlsList = new ArrayList < > ( ) ;
//根据index进行分组统计小相检测信息
LinkedHashMap < Integer , List < PqScriptDtls > > indexMap = subValue . stream ( )
. sorted ( Comparator . comparing ( PqScriptDtls : : getIndex ) )
. collect ( Collectors . groupingBy ( PqScriptDtls : : getIndex , LinkedHashMap : : new , Collectors . toList ( ) ) ) ;
indexMap . forEach ( ( index , scriptDtlIndex ) - > {
if ( finalResultMap . containsKey ( index ) ) {
Set < Integer > nums = finalResultMap . get ( index ) ;
DetectionInfoVO childInfo = new DetectionInfoVO ( ) ;
StringBuffer buffer = new StringBuffer ( ) ;
buffer . append ( " 输入: " ) ;
List < PqScriptDtls > freq = scriptDtlIndex . stream ( ) . filter ( x - > " Freq " . equals ( x . getValueType ( ) ) ) . collect ( Collectors . toList ( ) ) ;
if ( CollUtil . isNotEmpty ( freq ) ) {
buffer . append ( " 频率= " + freq . get ( 0 ) . getValue ( ) + ResultUnitEnum . FREQ_UNIT . getUnit ( ) + " " ) ;
}
List < PqScriptDtls > vol = scriptDtlIndex . stream ( ) . filter ( x - > " VOL " . equals ( x . getValueType ( ) ) )
. sorted ( Comparator . comparing ( PqScriptDtls : : getPhase ) ) . collect ( Collectors . toList ( ) ) ;
if ( CollUtil . isNotEmpty ( vol ) ) {
String unit ;
if ( isValueType ) {
unit = ResultUnitEnum . V_RELATIVE_UNIT . getUnit ( ) ;
} else {
unit = ResultUnitEnum . V_ABSOLUTELY_UNIT . getUnit ( ) ;
}
for ( PqScriptDtls dtls : vol ) {
buffer . append ( " U " + dtls . getPhase ( ) . toLowerCase ( ) + " = " + dtls . getValue ( ) + unit + " " ) ;
}
}
List < PqScriptDtls > cur = scriptDtlIndex . stream ( ) . filter ( x - > " CUR " . equals ( x . getValueType ( ) ) )
. sorted ( Comparator . comparing ( PqScriptDtls : : getPhase ) )
. collect ( Collectors . toList ( ) ) ;
if ( CollUtil . isNotEmpty ( cur ) ) {
String unit ;
if ( isValueType ) {
unit = ResultUnitEnum . I_RELATIVE_UNIT . getUnit ( ) ;
} else {
unit = ResultUnitEnum . I_ABSOLUTELY_UNIT . getUnit ( ) ;
}
for ( PqScriptDtls dtls : vol ) {
buffer . append ( " I " + dtls . getPhase ( ) . toLowerCase ( ) + " = " + dtls . getValue ( ) + unit + " " ) ;
}
}
dtlsSetBuffer ( scriptDtlIndex , vol , buffer , " Harm_V " , " 次谐波电压幅值 " , ResultUnitEnum . HV_UNIT . getUnit ( ) , true ) ;
dtlsSetBuffer ( scriptDtlIndex , vol , buffer , " Harm_I " , " 次谐波电流幅值 " , ResultUnitEnum . HI_UNIT . getUnit ( ) , true ) ;
dtlsSetBuffer ( scriptDtlIndex , vol , buffer , " InHarm_V " , " 次间谐波电压幅值 " , ResultUnitEnum . HSV_UNIT . getUnit ( ) , false ) ;
dtlsSetBuffer ( scriptDtlIndex , vol , buffer , " InHarm_I " , " 次间谐波电流幅值 " , ResultUnitEnum . HSI_UNIT . getUnit ( ) , false ) ;
dtlsSetBufferDip ( scriptDtlIndex , vol , buffer , " Dip " , " 暂态 " , ResultUnitEnum . HSI_UNIT . getUnit ( ) ) ;
dtlsSetBufferFlicker ( scriptDtlIndex , vol , buffer , " Flicker " , " 闪变 " ) ;
childInfo . setIndex ( index ) ;
childInfo . setFly ( conform ( nums ) ) ;
childInfo . setScriptTypeName ( buffer . toString ( ) ) ;
scriptDlsList . add ( childInfo ) ;
}
} ) ;
if ( CollUtil . isNotEmpty ( scriptDlsList ) ) {
DetectionInfoVO sub = new DetectionInfoVO ( ) ;
sub . setScriptTypeName ( subName . get ( subKey ) . replace ( " XX " , dictTree . getName ( ) ) ) ;
sub . setChildren ( scriptDlsList ) ;
scriptSubList . add ( sub ) ;
}
} ) ;
if ( CollUtil . isNotEmpty ( scriptSubList ) ) {
//大相检测信息
DetectionInfoVO infoVO = new DetectionInfoVO ( ) ;
infoVO . setScriptTypeName ( dictTree . getName ( ) ) ;
infoVO . setChildren ( scriptSubList ) ;
infoVOS . add ( infoVO ) ;
}
}
} ) ;
return infoVOS ;
}
private static void dtlsSetBuffer ( List < PqScriptDtls > scriptDtlIndex , List < PqScriptDtls > vol , StringBuffer buffer , String valueType , String name , String unit , Boolean fly ) {
LinkedHashMap < Double , List < PqScriptDtls > > harmNumMap = scriptDtlIndex . stream ( )
. filter ( x - > valueType . equals ( x . getValueType ( ) ) )
. sorted ( Comparator . comparing ( PqScriptDtls : : getIndex ) )
. collect ( Collectors . groupingBy ( PqScriptDtls : : getHarmNum , LinkedHashMap : : new , Collectors . toList ( ) ) ) ;
harmNumMap . forEach ( ( harmNum , value ) - > {
if ( fly ) {
buffer . append ( String . format ( " %.0f " , harmNum ) + name + " ( " ) ;
} else {
buffer . append ( harmNum + name + " ( " ) ;
}
List < PqScriptDtls > list = scriptDtlIndex . stream ( ) . filter ( x - > valueType . equals ( x . getValueType ( ) ) )
. sorted ( Comparator . comparing ( PqScriptDtls : : getPhase ) )
. collect ( Collectors . toList ( ) ) ;
if ( CollUtil . isNotEmpty ( list ) ) {
for ( PqScriptDtls dtls : vol ) {
buffer . append ( dtls . getPhase ( ) + " 相= " + dtls . getValue ( ) + unit + " " ) ;
}
buffer . append ( " ) " ) ;
}
} ) ;
}
private static void dtlsSetBufferDip ( List < PqScriptDtls > scriptDtlIndex , List < PqScriptDtls > vol , StringBuffer buffer , String valueType , String name , String unit ) {
List < PqScriptDtls > list = scriptDtlIndex . stream ( ) . filter ( x - > valueType . equals ( x . getValueType ( ) ) )
. sorted ( Comparator . comparing ( PqScriptDtls : : getPhase ) )
. collect ( Collectors . toList ( ) ) ;
if ( CollUtil . isNotEmpty ( list ) ) {
buffer . append ( name + " " ) ;
for ( PqScriptDtls dtls : vol ) {
buffer . append ( dtls . getPhase ( ) + " 相(暂态深度= " + dtls . getTransValue ( ) + ResultUnitEnum . VOLTAGE_DUR_UNIT . getUnit ( )
+ " 暂态持续时间= " + dtls . getRetainTime ( ) + ResultUnitEnum . VOLTAGE_DUR_UNIT . getUnit ( ) + " ) " ) ;
}
}
}
private static void dtlsSetBufferFlicker ( List < PqScriptDtls > scriptDtlIndex , List < PqScriptDtls > vol , StringBuffer buffer , String valueType , String name ) {
List < PqScriptDtls > list = scriptDtlIndex . stream ( ) . filter ( x - > valueType . equals ( x . getValueType ( ) ) )
. sorted ( Comparator . comparing ( PqScriptDtls : : getPhase ) )
. collect ( Collectors . toList ( ) ) ;
if ( CollUtil . isNotEmpty ( list ) ) {
buffer . append ( name + " " ) ;
for ( PqScriptDtls dtls : vol ) {
buffer . append ( dtls . getPhase ( ) + " 相(变动频度= " + dtls . getTransValue ( )
+ " 次/min 变动量= " + dtls . getRetainTime ( ) + ResultUnitEnum . VOLTAGE_DUR_UNIT . getUnit ( ) + " ) " ) ;
}
}
}
private Integer conform ( Set < Integer > numbers ) {
if ( numbers . size ( ) > 2 ) {
return 4 ;
} else if ( numbers . size ( ) > 1 ) {
if ( numbers . contains ( 4 ) ) {
return 4 ;
} else if ( numbers . contains ( 2 ) ) {
return 2 ;
}
}
return new ArrayList < > ( numbers ) . get ( 0 ) ;
}
}