|
@@ -6,10 +6,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.management.platform.entity.*;
|
|
|
import com.management.platform.entity.bo.FormStorePageBO;
|
|
|
import com.management.platform.entity.bo.QuestionBO;
|
|
|
+import com.management.platform.entity.dto.TaskDto;
|
|
|
import com.management.platform.mapper.*;
|
|
|
-import com.management.platform.service.AIQuestionService;
|
|
|
-import com.management.platform.service.CusTableColumnService;
|
|
|
-import com.management.platform.service.ExcelExportService;
|
|
|
+import com.management.platform.service.*;
|
|
|
import com.management.platform.util.ExcelConverter;
|
|
|
import com.management.platform.util.HttpRespMsg;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
@@ -38,14 +37,17 @@ import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
-import java.sql.*;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.ResultSetMetaData;
|
|
|
+import java.sql.SQLException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.regex.Matcher;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class AIQuestionServiceImpl extends ServiceImpl<AIQuestionMapper, AIQuestion> implements AIQuestionService {
|
|
@@ -71,6 +73,30 @@ public class AIQuestionServiceImpl extends ServiceImpl<AIQuestionMapper, AIQuest
|
|
|
@Resource
|
|
|
private CusTableColumnMapper cusTableColumnMapper;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ClueService clueService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private BusinessOpportunityService businessOpportunityService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TaskService taskService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CustomService customService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ContactsService contactsService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ProductService productService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SalesOrderService salesOrderService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ContractService contractService;
|
|
|
+
|
|
|
@Resource
|
|
|
private ExcelExportService excelExportService;
|
|
|
|
|
@@ -208,56 +234,195 @@ public class AIQuestionServiceImpl extends ServiceImpl<AIQuestionMapper, AIQuest
|
|
|
|
|
|
String tableName = questionBO.getSourceContent();
|
|
|
|
|
|
- //排除relation表中的关联字段
|
|
|
- List<BusObjRelation> busObjRelations = busObjRelationMapper.selectList(new LambdaQueryWrapper<BusObjRelation>()
|
|
|
- .eq(BusObjRelation::getFromTbl, tableName)
|
|
|
- );
|
|
|
- List<CusTableColumn> checkColumns = cusTableColumnMapper.getStructByTableName(tableName,busObjRelations);
|
|
|
- //映射字段和comment
|
|
|
- Map<String, String> columnCommentMap = checkColumns.stream().collect(Collectors.toMap(CusTableColumn::getColumnName, CusTableColumn::getColumnComment));
|
|
|
-
|
|
|
- StringBuilder stringBuilder = new StringBuilder();
|
|
|
- stringBuilder.append(" select ");
|
|
|
- for (String key : columnCommentMap.keySet()) {
|
|
|
- stringBuilder.append(" ").append(key).append(",");
|
|
|
+ HttpRespMsg dataMsg = new HttpRespMsg();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ switch (tableName){
|
|
|
+ case "business_opportunity" :
|
|
|
+ BusinessOpportunity bo = new BusinessOpportunity();
|
|
|
+ if(null != questionBO.getStartDate() && null != questionBO.getEndDate()){
|
|
|
+ bo.setStartTime(sdf.format(questionBO.getStartDate())) ;
|
|
|
+ bo.setEndTime(sdf.format(questionBO.getEndDate())) ;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ dataMsg = businessOpportunityService.exportDataAI(bo,request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ dataMsg.setError("系统表生成数据有误,请联系管理员");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "clue" :
|
|
|
+ Clue clue = new Clue();
|
|
|
+ if(null != questionBO.getStartDate() && null != questionBO.getEndDate()){
|
|
|
+ clue.setStartTime(sdf.format(questionBO.getStartDate())) ;
|
|
|
+ clue.setEndTime(sdf.format(questionBO.getEndDate())) ;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ dataMsg = clueService.exportDataAI(clue,request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ dataMsg.setError("系统表生成数据有误,请联系管理员");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "custom" :
|
|
|
+ Custom custom = new Custom();
|
|
|
+ if(null != questionBO.getStartDate() && null != questionBO.getEndDate()){
|
|
|
+ custom.setStartTime(sdf.format(questionBO.getStartDate())) ;
|
|
|
+ custom.setEndTime(sdf.format(questionBO.getEndDate())) ;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ dataMsg = customService.exportDataAI(custom,request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ dataMsg.setError("系统表生成数据有误,请联系管理员");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "contacts" :
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ map.put("isDelete", 0);
|
|
|
+ map.put("companyId",user.getCompanyId());
|
|
|
+// if(null != questionBO.getStartDate() && null != questionBO.getEndDate()){
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+//
|
|
|
+// }
|
|
|
+ try {
|
|
|
+ dataMsg = contactsService.exportDataAI(map,request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ dataMsg.setError("系统表生成数据有误,请联系管理员");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "task" :
|
|
|
+ TaskDto task = new TaskDto();
|
|
|
+ if(null != questionBO.getStartDate() && null != questionBO.getEndDate()){
|
|
|
+ task.setStartDate(LocalDateTime.parse(sdf.format(questionBO.getStartDate()), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); ;
|
|
|
+ task.setEndDate(LocalDateTime.parse(sdf.format(questionBO.getEndDate()), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) ;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ dataMsg = taskService.exportDataAI(task,request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ dataMsg.setError("系统表生成数据有误,请联系管理员");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "product" :
|
|
|
+ try {
|
|
|
+ dataMsg = productService.exportDataAI(sdf.format(questionBO.getStartDate()),sdf.format(questionBO.getEndDate()),request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ dataMsg.setError("系统表生成数据有误,请联系管理员");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "sales_order" :
|
|
|
+ try {
|
|
|
+ dataMsg = salesOrderService.exportDataAI(sdf.format(questionBO.getStartDate()),sdf.format(questionBO.getEndDate()),request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ dataMsg.setError("系统表生成数据有误,请联系管理员");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "contract" :
|
|
|
+ try {
|
|
|
+ dataMsg = contractService.exportDataAI(sdf.format(questionBO.getStartDate()),sdf.format(questionBO.getEndDate()),request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ dataMsg.setError("系统表生成数据有误,请联系管理员");
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
- stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
|
|
|
- stringBuilder.append(" from ")
|
|
|
- .append(tableName)
|
|
|
- .append(" where company_id =")
|
|
|
- .append(user.getCompanyId());
|
|
|
- if(null != questionBO.getStartDate() && null != questionBO.getEndDate()){
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- stringBuilder.append(" and date_format(create_time,'%Y-%m-%d') between ")
|
|
|
- .append("'").append(sdf.format(questionBO.getStartDate())).append("'")
|
|
|
- .append(" and ")
|
|
|
- .append("'").append(sdf.format(questionBO.getEndDate())).append("'");
|
|
|
+
|
|
|
+ if(dataMsg.getCode().equals("error")){
|
|
|
+ return dataMsg;
|
|
|
}
|
|
|
|
|
|
- List<Map<String,Object>> columnList = new ArrayList<>();
|
|
|
- try (Connection connection = dataSource.getConnection()) {
|
|
|
- PreparedStatement preparedStatement = connection.prepareStatement(stringBuilder.toString());
|
|
|
- ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
- columnList = this.convertListWithComment(resultSet,columnCommentMap);
|
|
|
- }catch (SQLException e) {
|
|
|
+ String filePath = dataMsg.getMsg();
|
|
|
+
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ //转换为MultipartFile 准备表单数据
|
|
|
+ MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
+ body.add("question", questionBO.getContent());
|
|
|
+ ByteArrayResource byteArrayResource = null;
|
|
|
+ File f = new File(path,filePath);
|
|
|
+ try {
|
|
|
+ MultipartFile multipartFileFromPath = getMultipartFileFromPath(f.getAbsolutePath());
|
|
|
+ byteArrayResource = new ByteArrayResource(multipartFileFromPath.getBytes()) {
|
|
|
+ @Override
|
|
|
+ public String getFilename() {
|
|
|
+ return multipartFileFromPath.getOriginalFilename();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }catch (IOException e){
|
|
|
e.printStackTrace();
|
|
|
- httpRespMsg.setError("查询系统表有误,请联系管理员");
|
|
|
- return httpRespMsg;
|
|
|
}
|
|
|
+ body.add("file",byteArrayResource);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
|
|
|
- if(CollectionUtils.isNotEmpty(columnList)){
|
|
|
- ResponseEntity<String> responseEntity = getResponseEntityWithFileAI(columnList, questionBO.getContent());
|
|
|
- if(responseEntity.getStatusCode().is2xxSuccessful()){
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(responseEntity.getBody());
|
|
|
- System.out.println("jsonObject=== "+jsonObject);
|
|
|
- queryRes = jsonObject.getString("data");
|
|
|
- }else{
|
|
|
- queryRes = "AI分析有误,稍后再试";
|
|
|
- }
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> requestEntity =
|
|
|
+ new HttpEntity<>(body, headers);
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
+ aiFileAskUrl,
|
|
|
+ HttpMethod.POST,
|
|
|
+ requestEntity,
|
|
|
+ String.class);
|
|
|
+ if(response.getStatusCode().is2xxSuccessful()){
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response.getBody());
|
|
|
+ System.out.println("jsonObject=== "+jsonObject);
|
|
|
+ queryRes = jsonObject.getString("data");
|
|
|
}else{
|
|
|
- queryRes = "无数据";
|
|
|
+ queryRes = "AI分析有误,稍后再试";
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ //排除relation表中的关联字段
|
|
|
+// List<BusObjRelation> busObjRelations = busObjRelationMapper.selectList(new LambdaQueryWrapper<BusObjRelation>()
|
|
|
+// .eq(BusObjRelation::getFromTbl, tableName)
|
|
|
+// );
|
|
|
+// List<CusTableColumn> checkColumns = cusTableColumnMapper.getStructByTableName(tableName,busObjRelations);
|
|
|
+// //映射字段和comment
|
|
|
+// Map<String, String> columnCommentMap = checkColumns.stream().collect(Collectors.toMap(CusTableColumn::getColumnName, CusTableColumn::getColumnComment));
|
|
|
+//
|
|
|
+// StringBuilder stringBuilder = new StringBuilder();
|
|
|
+// stringBuilder.append(" select ");
|
|
|
+// for (String key : columnCommentMap.keySet()) {
|
|
|
+// stringBuilder.append(" ").append(key).append(",");
|
|
|
+// }
|
|
|
+// stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
|
|
|
+// stringBuilder.append(" from ")
|
|
|
+// .append(tableName)
|
|
|
+// .append(" where company_id =")
|
|
|
+// .append(user.getCompanyId());
|
|
|
+// if(null != questionBO.getStartDate() && null != questionBO.getEndDate()){
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+// stringBuilder.append(" and date_format(create_time,'%Y-%m-%d') between ")
|
|
|
+// .append("'").append(sdf.format(questionBO.getStartDate())).append("'")
|
|
|
+// .append(" and ")
|
|
|
+// .append("'").append(sdf.format(questionBO.getEndDate())).append("'");
|
|
|
+// }
|
|
|
+//
|
|
|
+// List<Map<String,Object>> columnList = new ArrayList<>();
|
|
|
+// try (Connection connection = dataSource.getConnection()) {
|
|
|
+// PreparedStatement preparedStatement = connection.prepareStatement(stringBuilder.toString());
|
|
|
+// ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
+// columnList = this.convertListWithComment(resultSet,columnCommentMap);
|
|
|
+// }catch (SQLException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// httpRespMsg.setError("查询系统表有误,请联系管理员");
|
|
|
+// return httpRespMsg;
|
|
|
+// }
|
|
|
+//
|
|
|
+// if(CollectionUtils.isNotEmpty(columnList)){
|
|
|
+// ResponseEntity<String> responseEntity = getResponseEntityWithFileAI(columnList, questionBO.getContent());
|
|
|
+// if(responseEntity.getStatusCode().is2xxSuccessful()){
|
|
|
+// JSONObject jsonObject = JSONObject.parseObject(responseEntity.getBody());
|
|
|
+// System.out.println("jsonObject=== "+jsonObject);
|
|
|
+// queryRes = jsonObject.getString("data");
|
|
|
+// }else{
|
|
|
+// queryRes = "AI分析有误,稍后再试";
|
|
|
+// }
|
|
|
+// }else{
|
|
|
+// queryRes = "无数据";
|
|
|
+// }
|
|
|
+
|
|
|
} else if (2 == questionBO.getQuestionDataSource()) {
|
|
|
/**
|
|
|
* 自定义报表
|