@ -2,17 +2,14 @@ package com.biutag.supervision.service;
import cn.hutool.core.bean.BeanUtil ;
import cn.hutool.core.util.StrUtil ;
import com.alibaba.fastjson.JSONObject ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.biutag.supervision.constants.enums.* ;
import com.biutag.supervision.mapper.ModelClueMapper ;
import com.biutag.supervision.mapper.ModelMapper ;
import com.biutag.supervision.mapper.* ;
import com.biutag.supervision.pojo.dto.NegativeDto ;
import com.biutag.supervision.pojo.entity.Model ;
import com.biutag.supervision.pojo.entity.ModelClue ;
import com.biutag.supervision.pojo.entity.ModelClueTask ;
import com.biutag.supervision.pojo.entity.SupDepart ;
import com.biutag.supervision.pojo.entity.* ;
import com.biutag.supervision.pojo.model.ModelClueModel ;
import com.biutag.supervision.pojo.param.ModelClueQueryParam ;
import lombok.RequiredArgsConstructor ;
@ -20,10 +17,9 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional ;
import java.time.LocalDateTime ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Objects ;
import java.util.Set ;
import java.util.* ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import java.util.stream.Collectors ;
@RequiredArgsConstructor
@ -38,6 +34,14 @@ public class ModelClueService extends ServiceImpl<ModelClueMapper, ModelClue> {
public final SupDepartService departService ;
public final ModelClueMapper modelClueMapper ;
public final ModelClueDataMapper modelClueDataMapper ;
public final ModelClueRecordMapper modelClueRecordMapper ;
public final SupDepartMapper supDepartMapper ;
public Page < ModelClueModel > page ( ModelClueQueryParam param ) {
LambdaQueryWrapper < ModelClue > queryWrapper = new LambdaQueryWrapper < ModelClue > ( )
. eq ( Objects . nonNull ( param . getInvolveDepartId ( ) ) , ModelClue : : getInvolveDepartId , param . getInvolveDepartId ( ) )
@ -130,4 +134,71 @@ public class ModelClueService extends ServiceImpl<ModelClueMapper, ModelClue> {
return true ;
}
public void generate ( Integer modelId ) {
Model model = modelMapper . selectById ( modelId ) ;
List < ModelClue > modelClues = new ArrayList < > ( ) ;
if ( ! Objects . isNull ( model ) ) {
if ( model . getModelSql ( ) ! = null ) {
modelClues . addAll ( getModelClueBySql ( model . getModelSql ( ) ) ) ;
}
}
if ( ! modelClues . isEmpty ( ) ) {
for ( ModelClue modelClue : modelClues ) {
modelClue . setModelId ( modelId ) ;
modelClue . setCreateTime ( LocalDateTime . now ( ) ) ;
SupDepart supDepart = supDepartMapper . selectOne ( new LambdaQueryWrapper < SupDepart > ( ) . eq ( SupDepart : : getCode , modelClue . getInvolveDepartId ( ) ) ) ;
if ( supDepart ! = null & & supDepart . getShortName ( ) ! = null & & ! supDepart . getShortName ( ) . isEmpty ( ) ) {
modelClue . setInvolveDepartName ( supDepart . getShortName ( ) ) ;
modelClue . setInvolveDepartId ( supDepart . getId ( ) ) ;
}
}
modelClueMapper . insert ( modelClues ) ;
modelClueRecordMapper . insert ( new ModelClueRecord ( ) . setModelId ( modelId ) . setSize ( modelClues . size ( ) ) . setCreateTime ( LocalDateTime . now ( ) ) . setState ( "success" ) ) ;
} else {
modelClueRecordMapper . insert ( new ModelClueRecord ( ) . setModelId ( modelId ) . setSize ( 0 ) . setCreateTime ( LocalDateTime . now ( ) ) . setState ( "fail" ) . setErrMsg ( "数据为空" ) ) ;
}
}
public List < ModelClue > getModelClueBySql ( String sql ) {
List < ModelClue > modelClues = modelClueDataMapper . selectDataByDynamicSql ( sql ) ;
List < String > uniqueKeys = modelClues . stream ( ) . map ( ModelClue : : getUniqueKey ) . filter ( k - > k ! = null & & ! k . isEmpty ( ) ) . toList ( ) ;
String newSql = generateNewSql ( sql , uniqueKeys ) ;
List < Map < String , Object > > allData = modelClueDataMapper . selectByUniqueKeys ( newSql ) ;
String originalFieldName = getKeyFieldName ( sql ) ;
for ( ModelClue modelClue : modelClues ) {
for ( Map < String , Object > data : allData ) {
String keyColumn = ( String ) data . get ( originalFieldName ) ;
if ( Objects . equals ( keyColumn , modelClue . getUniqueKey ( ) ) ) {
modelClue . setData ( JSONObject . toJSONString ( data ) ) ;
break ;
}
}
}
return modelClues ;
}
public static String generateNewSql ( String originalSql , List < String > uniqueKeys ) {
Pattern tablePattern = Pattern . compile ( "FROM\\s+(\\w+)" , Pattern . CASE_INSENSITIVE ) ;
Matcher tableMatcher = tablePattern . matcher ( originalSql ) ;
String tableName = "" ;
if ( tableMatcher . find ( ) ) {
tableName = tableMatcher . group ( 1 ) ;
}
String originalFieldName = getKeyFieldName ( originalSql ) ;
return "SELECT * FROM " + tableName + " " + "WHERE " + originalFieldName + " IN " + "(" + uniqueKeys . stream ( ) . map ( k - > "'" + k + "'" ) . collect ( Collectors . joining ( "," ) ) + ");" ;
}
private static String getKeyFieldName ( String originalSql ) {
String patternString = "SELECT\\s+(\\w+)\\s+AS\\s+" + Pattern . quote ( "uniqueKey" ) ;
Pattern uniqueKeyPattern = Pattern . compile ( patternString , Pattern . CASE_INSENSITIVE ) ;
Matcher uniqueKeyMatcher = uniqueKeyPattern . matcher ( originalSql ) ;
String originalFieldName = "" ;
if ( uniqueKeyMatcher . find ( ) ) {
originalFieldName = uniqueKeyMatcher . group ( 1 ) ;
}
if ( originalFieldName . isEmpty ( ) ) {
throw new RuntimeException ( "未找到唯一键字段名" ) ;
}
return originalFieldName ;
}
}