|
|
@@ -1,8 +1,15 @@
|
|
|
package cn.com.yusys.yusp.service.impl;
|
|
|
|
|
|
+import cn.com.yusys.yusp.commons.exception.BizException;
|
|
|
import cn.com.yusys.yusp.commons.util.StringUtils;
|
|
|
import cn.com.yusys.yusp.commons.util.date.DateUtils;
|
|
|
+import cn.com.yusys.yusp.config.FastApiConfig;
|
|
|
+import cn.com.yusys.yusp.domain.dto.TagImportDto;
|
|
|
+import cn.com.yusys.yusp.domain.entity.AitagTagCategory;
|
|
|
+import cn.com.yusys.yusp.domain.vo.GenerateRegexVo;
|
|
|
import cn.com.yusys.yusp.domain.vo.VersionRollbackVo;
|
|
|
+import cn.com.yusys.yusp.domain.vo.fastapivo.AiTaggingResponseVo;
|
|
|
+import cn.com.yusys.yusp.mapper.AitagTagCategoryMapper;
|
|
|
import cn.com.yusys.yusp.mapper.AitagTagInfoVersionDao;
|
|
|
import cn.com.yusys.yusp.domain.dto.TagInfoDto;
|
|
|
import cn.com.yusys.yusp.domain.entity.AitagTagInfoVersionEntity;
|
|
|
@@ -11,6 +18,9 @@ import cn.com.yusys.yusp.mapper.AitagTagInfoDao;
|
|
|
import cn.com.yusys.yusp.domain.entity.AitagTagInfoEntity;
|
|
|
import cn.com.yusys.yusp.domain.vo.AitagTagInfoQueryVo;
|
|
|
import cn.com.yusys.yusp.service.AitagTagInfoService;
|
|
|
+import cn.com.yusys.yusp.service.TagImportListener;
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
@@ -18,11 +28,22 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static cn.com.yusys.yusp.config.DataDictionary.*;
|
|
|
|
|
|
@@ -34,12 +55,19 @@ import static cn.com.yusys.yusp.config.DataDictionary.*;
|
|
|
*/
|
|
|
|
|
|
@Service("aitagTagInfoService")
|
|
|
+@Slf4j
|
|
|
public class AitagTagInfoServiceImpl extends ServiceImpl<AitagTagInfoDao, AitagTagInfoEntity> implements AitagTagInfoService {
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private AitagTagInfoVersionDao aitagTagInfoVersionDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private AitagTagCategoryMapper aiTagCategoryMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FastApiConfig fastApiConfig;
|
|
|
+
|
|
|
/**
|
|
|
* 分页查询
|
|
|
*
|
|
|
@@ -113,10 +141,14 @@ public class AitagTagInfoServiceImpl extends ServiceImpl<AitagTagInfoDao, AitagT
|
|
|
aitagTagInfo.setLevel(1);
|
|
|
}
|
|
|
aitagTagInfo.setRevisionTime(DateUtils.getCurrDateTimeStr());
|
|
|
- aitagTagInfo.setTagVersion("0.1");
|
|
|
+ aitagTagInfo.setTagVersion("1");
|
|
|
aitagTagInfo.setIsDelete(TAG_UNDELETED);
|
|
|
aitagTagInfo.setState(TAG_ENABLED);
|
|
|
this.save(aitagTagInfo);
|
|
|
+ HashMap<String, Object> body = new HashMap<>();
|
|
|
+ String[] ids = {aitagTagInfo.getId()};
|
|
|
+ body.put("tag_ids",ids);
|
|
|
+ callAiTag(body,"/api/aitag/admin/v1/synchronize_tag");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -153,8 +185,13 @@ public class AitagTagInfoServiceImpl extends ServiceImpl<AitagTagInfoDao, AitagT
|
|
|
aitagTagInfoVersionEntity.setId(StringUtils.getUUID());
|
|
|
this.aitagTagInfoVersionDao.insert(aitagTagInfoVersionEntity);
|
|
|
aitagTagInfoEntity.setRevisionTime(DateUtils.getCurrDateTimeStr());
|
|
|
- aitagTagInfoEntity.setTagVersion(Double.parseDouble(tagInfo.getTagVersion())+0.1+"");
|
|
|
+ aitagTagInfoEntity.setTagVersion(Integer.parseInt(tagInfo.getTagVersion())+1+"");
|
|
|
this.baseMapper.updateById(aitagTagInfoEntity);
|
|
|
+
|
|
|
+ HashMap<String, Object> body = new HashMap<>();
|
|
|
+ String[] ids = {aitagTagInfoEntity.getId()};
|
|
|
+ body.put("tag_ids",ids);
|
|
|
+ callAiTag(body,"/api/aitag/admin/v1/synchronize_tag");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -166,6 +203,9 @@ public class AitagTagInfoServiceImpl extends ServiceImpl<AitagTagInfoDao, AitagT
|
|
|
aitagTagInfoEntity.setIsDelete(TAG_DELETED);
|
|
|
this.baseMapper.updateById(aitagTagInfoEntity);
|
|
|
}
|
|
|
+ HashMap<String, Object> body = new HashMap<>();
|
|
|
+ body.put("tag_ids",list);
|
|
|
+ callAiTag(body,"/api/aitag/admin/v1/delete_tag");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -188,6 +228,11 @@ public class AitagTagInfoServiceImpl extends ServiceImpl<AitagTagInfoDao, AitagT
|
|
|
AitagTagInfoVersionEntity aitagTagInfoVersionEntity = new AitagTagInfoVersionEntity();
|
|
|
aitagTagInfoVersionEntity.setIsDelete(TAG_DELETED);
|
|
|
this.aitagTagInfoVersionDao.update(aitagTagInfoVersionEntity,updateTagVersion);
|
|
|
+
|
|
|
+ HashMap<String, Object> body = new HashMap<>();
|
|
|
+ String[] ids = {aitagTagInfoEntity.getId()};
|
|
|
+ body.put("tag_ids",ids);
|
|
|
+ callAiTag(body,"/api/aitag/admin/v1/synchronize_tag");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -234,6 +279,119 @@ public class AitagTagInfoServiceImpl extends ServiceImpl<AitagTagInfoDao, AitagT
|
|
|
return this.baseMapper.selectList(queryWrapper);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void batchImport(MultipartFile file, String reviser,String categoryId) throws IOException {
|
|
|
+ log.info("开始处理上传文件:{}", file.getOriginalFilename());
|
|
|
+
|
|
|
+ TagImportListener listener = new TagImportListener();
|
|
|
+ EasyExcel.read(file.getInputStream(), TagImportDto.class, listener)
|
|
|
+ .headRowNumber(1)
|
|
|
+ .sheet(1)
|
|
|
+ .doRead();
|
|
|
+ List<TagImportDto> tagImportDtos = listener.getDataList();
|
|
|
+ if (tagImportDtos.isEmpty()) {
|
|
|
+ throw BizException.of("E004");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, TagImportDto> nameToDtoMap = new HashMap<>();
|
|
|
+ for (TagImportDto d : tagImportDtos) {
|
|
|
+ if (nameToDtoMap.containsKey(d.getTagNm())) {
|
|
|
+ throw BizException.of("E005",d.getTagNm());
|
|
|
+ }
|
|
|
+ nameToDtoMap.put(d.getTagNm(), d);
|
|
|
+ }
|
|
|
+
|
|
|
+ validateParentTags(nameToDtoMap);
|
|
|
+ resolveHierarchy(nameToDtoMap);
|
|
|
+ saveToDatabase(tagImportDtos,reviser,categoryId);
|
|
|
+
|
|
|
+ HashMap<String, Object> body = new HashMap<>();
|
|
|
+ List<String> tagIds = tagImportDtos.stream().map(TagImportDto::getId).collect(Collectors.toList());
|
|
|
+ body.put("tag_ids",tagIds);
|
|
|
+ callAiTag(body,"/api/aitag/admin/v1/synchronize_tag");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String generateRegex(GenerateRegexVo regexVo) {
|
|
|
+ Map body = JSONObject.parseObject(JSONObject.toJSONString(regexVo), Map.class);
|
|
|
+ AiTaggingResponseVo taggingResponse = callAiTag(body, "/api/aitag/admin/v1/generate_reg");
|
|
|
+ if(StringUtils.equals(taggingResponse.getCode(),"200")){
|
|
|
+ return taggingResponse.getBody();
|
|
|
+ }
|
|
|
+ log.error("生成正则表达式异常:返回参数为:{}",JSON.toJSONString(taggingResponse));
|
|
|
+ throw BizException.of("E008");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveToDatabase(List<TagImportDto> tagImportDtos,String reviser,String categoryId) {
|
|
|
+ AitagTagCategory aitagTagCategory = aiTagCategoryMapper.selectById(categoryId);
|
|
|
+ String categoryNm = aitagTagCategory.getCategoryNm();
|
|
|
+ for (TagImportDto tagImportDto: tagImportDtos){
|
|
|
+ AitagTagInfoEntity aitagTagInfo = JSONObject.parseObject(JSONObject.toJSONString(tagImportDto),
|
|
|
+ AitagTagInfoEntity.class);
|
|
|
+ String tagPath = tagImportDto.getTagPath();
|
|
|
+ aitagTagInfo.setTagPath(categoryNm+"/"+tagPath);
|
|
|
+ aitagTagInfo.setLevel(aitagTagInfo.getTagPath().split("/").length);
|
|
|
+ aitagTagInfo.setRevisionTime(DateUtils.getCurrDateTimeStr());
|
|
|
+ aitagTagInfo.setReviser(reviser);
|
|
|
+ aitagTagInfo.setTagVersion("1");
|
|
|
+ aitagTagInfo.setIsDelete(TAG_UNDELETED);
|
|
|
+ aitagTagInfo.setState(TAG_ENABLED);
|
|
|
+ this.save(aitagTagInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验父标签存在性并将赋值parentId
|
|
|
+ * @param nameToDtoMap
|
|
|
+ */
|
|
|
+ private void validateParentTags(Map<String, TagImportDto> nameToDtoMap) {
|
|
|
+ Set<String> validNames = nameToDtoMap.keySet();
|
|
|
+ for (TagImportDto d : nameToDtoMap.values()) {
|
|
|
+ if (!StringUtils.isBlank(d.getParentName())) {
|
|
|
+ if(!validNames.contains(d.getParentName())){
|
|
|
+ throw BizException.of("E006",d.getTagNm(),d.getParentName());
|
|
|
+ }else{
|
|
|
+ d.setParentId(nameToDtoMap.get(d.getParentName()).getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建path路径
|
|
|
+ * @param nameToDtoMap
|
|
|
+ */
|
|
|
+ private void resolveHierarchy(Map<String, TagImportDto> nameToDtoMap) {
|
|
|
+ for (TagImportDto current : nameToDtoMap.values()) {
|
|
|
+ current.setTagPath(buildFullPath(current, new HashSet<>(),nameToDtoMap));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildFullPath(TagImportDto current, Set<String> visited,Map<String, TagImportDto> nameToDtoMap) {
|
|
|
+ if (current == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if (visited.contains(current.getTagNm())) {
|
|
|
+ throw BizException.of("E007", String.join("->", visited) + "->" + current.getTagNm());
|
|
|
+ }
|
|
|
+ visited.add(current.getTagNm());
|
|
|
+
|
|
|
+ StringBuilder path = new StringBuilder();
|
|
|
+ if (current.getParentName() != null && nameToDtoMap.containsKey(current.getParentName())) {
|
|
|
+ TagImportDto parent = nameToDtoMap.get(current.getParentName());
|
|
|
+ String parentPath = buildFullPath(parent, visited, nameToDtoMap);
|
|
|
+ if (!parentPath.isEmpty()) {
|
|
|
+ path.append(parentPath).append("/");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ path.append(current.getTagNm());
|
|
|
+ visited.remove(current.getTagNm());
|
|
|
+ return path.toString();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 将AitagTagInfoEntity列表转换为树形结构
|
|
|
* @param entities AitagTagInfoEntity列表
|
|
|
@@ -269,4 +427,40 @@ public class AitagTagInfoServiceImpl extends ServiceImpl<AitagTagInfoDao, AitagT
|
|
|
}
|
|
|
return rootNodes;
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ private AiTaggingResponseVo callAiTag(Map<String,Object> body, String path) {
|
|
|
+ String url = fastApiConfig.getUrl() + path;
|
|
|
+ log.info("调用AI打标接口: {}", url);
|
|
|
+
|
|
|
+ try (CloseableHttpClient httpClient = createHttpClient()) {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ // 构造请求体
|
|
|
+ String requestBody = JSON.toJSONString(body);
|
|
|
+ httpPost.setEntity(new StringEntity(requestBody, "UTF-8"));
|
|
|
+
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ log.info("AI打标接口响应: {}", responseBody);
|
|
|
+ return JSON.parseObject(responseBody, AiTaggingResponseVo.class);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用AI打标接口失败", e);
|
|
|
+ throw BizException.of("E008");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建HTTP客户端
|
|
|
+ */
|
|
|
+ private CloseableHttpClient createHttpClient() {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ .setConnectTimeout(fastApiConfig.getConnectTimeout())
|
|
|
+ .setSocketTimeout(fastApiConfig.getReadTimeout())
|
|
|
+ .build();
|
|
|
+ return HttpClients.custom()
|
|
|
+ .setDefaultRequestConfig(requestConfig)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|