Browse Source

fix:大模型sql执行接口

zhongwei
haungxing 5 months ago
parent
commit
948000e500
  1. 63
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/service/DataSourceService.java
  2. 11
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/HznlmInteractiveController.java
  3. 3
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/dto/AuthDataDTO.java

63
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/datasource/service/DataSourceService.java

@ -45,33 +45,44 @@ public class DataSourceService {
});
String sql = sqlVO.getSql();
String userAuthDataSQL = userAuthDataService.getUserAuthDataSQL(Long.parseLong(sqlVO.getUserId()));
List<Map<String,String>> tempViewList = Lists.newArrayList();
try {
for (TableAuthVO tableAuthVO : sqlVO.getTableAuthVOList()) {
// 创建视图语句
String viewName = "V_TEMP_" + UUID.randomUUID().toString().replace("-", "");
String createView = "CREATE VIEW " + viewName + " AS SELECT * FROM " + tableAuthVO.getTableName() + " where " + userAuthDataSQL;
this.updateOnSpecificDataSource(createView,tableAuthVO.getDatasourceName());
Map<String,String> viewMap = new HashMap(2);
viewMap.put("datasource",tableAuthVO.getDatasourceName());
viewMap.put("viewName",viewName);
tempViewList.add(viewMap);
sql = sql.replace(tableAuthVO.getTableName(),viewName);
}
log.info("执行sql:{}",sql);
return this.queryListOnSpecificDataSource(sql, sqlVO.getTableAuthVOList().get(0).getDatasourceName());
}
catch(Exception e) {
log.error("An Error occurred!",e);
throw new ServiceException("sql执行失败!");
}
finally {
if(CollectionUtil.isNotEmpty(tempViewList)) {
tempViewList.forEach(viewMap -> {
this.updateOnSpecificDataSource("DROP VIEW IF EXISTS `" + viewMap.get("viewName")+"`;",viewMap.get("datasource"));
});
}
for (TableAuthVO tableAuthVO : sqlVO.getTableAuthVOList()) {
String tableSubStr = "(SELECT * FROM " + tableAuthVO.getTableName() + " where" + userAuthDataSQL +") temp";
sql = sql.replace(tableAuthVO.getTableName(),tableSubStr);
}
return this.queryListOnSpecificDataSource(sql, sqlVO.getTableAuthVOList().get(0).getDatasourceName());
// 过滤更新、删除语句
// Assert.isTrue(!DataSourceService.isUpdateOrDelete(sqlVO.getSql()),() -> {
// throw new ServiceException("执行sql语句包含更新/删除操作,执行失败!");
// });
// String sql = sqlVO.getSql();
// String userAuthDataSQL = userAuthDataService.getUserAuthDataSQL(Long.parseLong(sqlVO.getUserId()));
// List<Map<String,String>> tempViewList = Lists.newArrayList();
// try {
// for (TableAuthVO tableAuthVO : sqlVO.getTableAuthVOList()) {
// // 创建视图语句
// String viewName = "V_TEMP_" + UUID.randomUUID().toString().replace("-", "");
// String createView = "CREATE VIEW " + viewName + " AS SELECT * FROM " + tableAuthVO.getTableName() + " where " + userAuthDataSQL;
// this.updateOnSpecificDataSource(createView,tableAuthVO.getDatasourceName());
// Map<String,String> viewMap = new HashMap(2);
// viewMap.put("datasource",tableAuthVO.getDatasourceName());
// viewMap.put("viewName",viewName);
// tempViewList.add(viewMap);
// sql = sql.replace(tableAuthVO.getTableName(),viewName);
// }
// log.info("执行sql:{}",sql);
// return this.queryListOnSpecificDataSource(sql, sqlVO.getTableAuthVOList().get(0).getDatasourceName());
// }
// catch(Exception e) {
// log.error("An Error occurred!",e);
// throw new ServiceException("sql执行失败!");
// }
// finally {
// if(CollectionUtil.isNotEmpty(tempViewList)) {
// tempViewList.forEach(viewMap -> {
// this.updateOnSpecificDataSource("DROP VIEW IF EXISTS `" + viewMap.get("viewName")+"`;",viewMap.get("datasource"));
// });
// }
// }
}
/**

11
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/HznlmInteractiveController.java

@ -2,6 +2,8 @@ package com.hnac.hzims.bigmodel.interactive.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hnac.hzims.bigmodel.BigModelConstants;
import com.hnac.hzims.bigmodel.datasource.service.DataSourceService;
import com.hnac.hzims.bigmodel.datasource.vo.SqlVO;
import com.hnac.hzims.bigmodel.interactive.dto.AuthDataDTO;
import com.hnac.hzims.bigmodel.interactive.req.ModelFunctionReq;
import com.hnac.hzims.bigmodel.interactive.service.IHznlmInteractiveService;
@ -17,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
/**
* @Author: huangxing
@ -30,6 +33,7 @@ import java.util.List;
public class HznlmInteractiveController {
private final IHznlmInteractiveService interactiveService;
private final DataSourceService dataSourceService;
@PostMapping(value = "/get_auth_data")
@ApiOperation("获取鉴权数据")
@ -44,4 +48,11 @@ public class HznlmInteractiveController {
public R<ExtraVO> resolve(@RequestBody ModelFunctionReq req) {
return R.data(interactiveService.resolve(req));
}
@PostMapping("/execute_query")
@ApiOperation("执行大模型sql")
@ApiOperationSupport(order = 1)
public R<List<Map<String, Object>>> executeQuery(@RequestBody @Valid SqlVO sqlVO) {
return R.data(dataSourceService.queryListOnSpecificDataSource(sqlVO));
}
}

3
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/dto/AuthDataDTO.java

@ -19,12 +19,11 @@ import java.io.Serializable;
@EqualsAndHashCode
public class AuthDataDTO implements Serializable {
@JsonProperty("chat_id")
@JsonProperty("chatId")
@ApiModelProperty("问答ID,用于获取前端发起问答传入缓存中数据")
@NotBlank
private String sessionId;
@JsonProperty("user_id")
@ApiModelProperty("提问用户ID")
@NotBlank
private String userId;

Loading…
Cancel
Save