Browse Source

20250415 dify功能修改

lrj 5 months ago
parent
commit
8ec980ef25

+ 1 - 0
src/main/java/cn/cslg/pas/common/model/dify/ChatMessageDTO.java

@@ -12,4 +12,5 @@ public class ChatMessageDTO {
     private String query;
     private String conversationId;
     private Integer projectId;
+    private Integer confessionSessionId;
 }

+ 8 - 0
src/main/java/cn/cslg/pas/common/model/dify/confessionSession/AddConfessionSessionDTO.java

@@ -0,0 +1,8 @@
+package cn.cslg.pas.common.model.dify.confessionSession;
+
+import lombok.Data;
+
+@Data
+public class AddConfessionSessionDTO {
+    private String fileGuid;
+}

+ 10 - 0
src/main/java/cn/cslg/pas/common/model/dify/confessionSession/DeleteConfessionSessionDTO.java

@@ -0,0 +1,10 @@
+package cn.cslg.pas.common.model.dify.confessionSession;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class DeleteConfessionSessionDTO {
+    private List<Integer> confessionSessionIds ;
+}

+ 10 - 0
src/main/java/cn/cslg/pas/common/model/dify/confessionSession/QueryConfessionSessionDTO.java

@@ -0,0 +1,10 @@
+package cn.cslg.pas.common.model.dify.confessionSession;
+
+import lombok.Data;
+
+@Data
+public class QueryConfessionSessionDTO {
+    private Integer confessionSessionId;
+    private long current;
+    private long size;
+}

+ 17 - 0
src/main/java/cn/cslg/pas/common/model/dify/confessionSession/QueryConfessionSessionVO.java

@@ -0,0 +1,17 @@
+package cn.cslg.pas.common.model.dify.confessionSession;
+
+import cn.cslg.pas.common.model.cronModel.SystemFile;
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class QueryConfessionSessionVO {
+    private SystemFile systemFile;
+    private Integer id;
+    private String conversationId;
+    private String conversationName;
+    private String inventionPoint;
+    private String createId;
+    private Date createTime;
+}

+ 11 - 0
src/main/java/cn/cslg/pas/common/model/dify/confessionSession/UpdateConfessionSessionDTO.java

@@ -0,0 +1,11 @@
+package cn.cslg.pas.common.model.dify.confessionSession;
+
+import lombok.Data;
+
+@Data
+public class UpdateConfessionSessionDTO {
+    private Integer confessionSessionId;
+    private String inventionPoint;
+    private String conversationName;
+    private String conversationId;
+}

+ 74 - 0
src/main/java/cn/cslg/pas/controller/common/ConfessionSessionController.java

@@ -0,0 +1,74 @@
+package cn.cslg.pas.controller.common;
+
+import cn.cslg.pas.common.core.base.Constants;
+import cn.cslg.pas.common.model.dify.ChatMessageDTO;
+import cn.cslg.pas.common.model.dify.DifyHistoryMessageDTO;
+import cn.cslg.pas.common.model.dify.confessionSession.*;
+import cn.cslg.pas.common.utils.CacheUtils;
+import cn.cslg.pas.common.utils.LoginUtils;
+import cn.cslg.pas.common.utils.Response;
+import cn.cslg.pas.service.common.DifyService;
+import cn.cslg.pas.service.dify.ConfessionSessionService;
+import cn.cslg.pas.service.dify.DifySessionService;
+import com.alibaba.fastjson2.JSONObject;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Lazy;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Flux;
+
+import java.io.IOException;
+import java.util.List;
+
+
+@SuppressWarnings({"all"})
+@Tag(name = "外部人员调用接口")
+@RestController
+@RequestMapping(Constants.API_XiaoSHI + "/confessionSession")
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
+
+public class ConfessionSessionController {
+    private final DifyService difyService;
+    private final ConfessionSessionService confessionSessionService;
+    @Autowired
+    private CacheUtils cacheUtils;
+    @Autowired
+    private LoginUtils loginUtils;
+
+    //
+    @RequestMapping(value = "/addConfessionSession", method = RequestMethod.POST)
+    @Operation(summary = "生成交底书会话记录")
+    public Response addConfessionSession(@RequestBody AddConfessionSessionDTO addConfessionSessionDTO) throws IOException {
+        Integer id = confessionSessionService.addConfessionSession(addConfessionSessionDTO);
+        return Response.success(id);
+    }
+
+
+    @RequestMapping(value = "/updateConfessionSession", method = RequestMethod.POST)
+    @Operation(summary = "更新技术交底书会话记录")
+    public Response updateConfessionSession(@RequestBody UpdateConfessionSessionDTO updateConfessionSessionDTO) throws IOException {
+        Integer id = confessionSessionService.updateConfessionSession(updateConfessionSessionDTO);
+        return Response.success(id);
+    }
+
+    @RequestMapping(value = "/queryConfessionSession", method = RequestMethod.POST)
+    @Operation(summary = "查询技术交底书会话记录")
+    public Response queryConfessionSession(@RequestBody QueryConfessionSessionDTO queryConfessionSessionDTO) throws IOException {
+        List<QueryConfessionSessionVO> queryConfessionSessions=confessionSessionService.queryConfessionSession(queryConfessionSessionDTO);
+        return Response.success(queryConfessionSessions);
+    }
+
+    @RequestMapping(value = "/deleteConfessionSessions", method = RequestMethod.POST)
+    @Operation(summary = "删除技术交底书会话记录")
+    public Response deleteConfessionSessions(@RequestBody DeleteConfessionSessionDTO deleteConfessionSessionDTO) throws IOException {
+        List<Integer> ids =confessionSessionService.deleteConfessionSessions(deleteConfessionSessionDTO);
+        return Response.success(ids);
+    }
+
+}

+ 32 - 0
src/main/java/cn/cslg/pas/domain/dify/ConfessionSession.java

@@ -0,0 +1,32 @@
+package cn.cslg.pas.domain.dify;
+
+import cn.cslg.pas.domain.BaseEntity;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import java.util.Date;
+import lombok.Data;
+
+/**
+ * @TableName confession_session
+ */
+@TableName(value ="confession_session")
+@Data
+public class ConfessionSession extends BaseEntity<DifySession> {
+    private Integer id;
+
+    private String conversationId;
+
+    private String conversationName;
+
+    private String inventionPoint;
+
+    private String createId;
+
+    private String guid;
+
+    private Date createTime;
+
+}

+ 18 - 0
src/main/java/cn/cslg/pas/mapper/dify/ConfessionSessionMapper.java

@@ -0,0 +1,18 @@
+package cn.cslg.pas.mapper.dify;
+
+import cn.cslg.pas.domain.dify.ConfessionSession;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+* @author admin
+* @description 针对表【confession_session(交底书会话记录)】的数据库操作Mapper
+* @createDate 2025-04-16 15:48:57
+* @Entity cn.cslg.pas.domain.novelty.domain.ConfessionSession
+*/
+public interface ConfessionSessionMapper extends BaseMapper<ConfessionSession> {
+
+}
+
+
+
+

+ 49 - 10
src/main/java/cn/cslg/pas/service/common/DifyService.java

@@ -5,12 +5,15 @@ import cn.cslg.pas.common.model.dify.ChatMessageDTO;
 import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
 import cn.cslg.pas.common.model.dify.DifyHistoryMessageDTO;
 
+import cn.cslg.pas.common.model.dify.confessionSession.UpdateConfessionSessionDTO;
 import cn.cslg.pas.common.utils.CacheUtils;
 import cn.cslg.pas.common.utils.DataUtils;
 import cn.cslg.pas.common.utils.LoginUtils;
 import cn.cslg.pas.domain.business.TechnicalCase;
+import cn.cslg.pas.domain.dify.ConfessionSession;
 import cn.cslg.pas.domain.report.AssoProjectConfession;
 import cn.cslg.pas.service.business.TechnicalCaseService;
+import cn.cslg.pas.service.dify.ConfessionSessionService;
 import cn.cslg.pas.service.dify.DifySessionService;
 import cn.cslg.pas.service.report.AssoProjectConfessionService;
 import com.alibaba.fastjson.JSON;
@@ -63,6 +66,7 @@ public class DifyService {
     private CacheUtils cacheUtils;
     @Autowired
     private LoginUtils loginUtils;
+    private final ConfessionSessionService confessionSessionService;
 
     /**
      * 调用文件系统删除文件接口
@@ -109,6 +113,7 @@ public class DifyService {
 
     public Flux<String> getOkhttp(ChatMessageDTO chatMessageDTO) {
         Integer projectId = chatMessageDTO.getProjectId();
+        Integer confessionSessionId = chatMessageDTO.getConfessionSessionId();
         String conversationId = chatMessageDTO.getConversationId();
         String userId = loginUtils.getId().toString();
         String query = chatMessageDTO.getQuery();
@@ -125,8 +130,17 @@ public class DifyService {
 
         DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
         Map<String, Object> map = new HashMap<>();
-        String fileContent = this.getConfression(projectId);
-        String inventionPoint = this.getInventPoint(projectId);
+        String fileContent = "";
+        String inventionPoint = "";
+        if (projectId != null) {
+            fileContent = this.getConfression(projectId);
+            inventionPoint = this.getInventPoint(projectId);
+        } else if (confessionSessionId != null) {
+            ConfessionSession confessionSession = confessionSessionService.getById(confessionSessionId);
+            fileContent = fileDownloadUrl +confessionSession.getGuid();
+            inventionPoint = confessionSession.getInventionPoint();
+        }
+
         map.put("file_path", fileContent);
         map.put("invention_point", inventionPoint);
         difyChatMessageDTO.setInputs(map);
@@ -148,6 +162,7 @@ public class DifyService {
 
                 @Override
                 public void onResponse(Call call, Response response) throws IOException {
+
                     if (!response.isSuccessful()) {
                         emitter.error(new IOException("Unexpected code: " + response));
                         return;
@@ -165,11 +180,20 @@ public class DifyService {
                             if (line.startsWith(prefixToRemove)) {
                                 line = line.substring(prefixToRemove.length());
                             }
-                            if (temConversationId2 == null) {
+                            if (temConversationId2 == null || temConversationId2.isEmpty()) {
                                 try {
                                     JSONObject jsonObject = JSON.parseObject(line);
                                     temConversationId2 = jsonObject.get("conversation_id").toString();
-                                    difySessionService.addDifySession(projectId, userId, temConversationId2);
+                                    if (projectId != null) {
+                                        difySessionService.addDifySession(projectId, userId, temConversationId2);
+                                    }
+                                    if (confessionSessionId != null) {
+                                        UpdateConfessionSessionDTO updateConfessionSessionDTO = new UpdateConfessionSessionDTO();
+                                        updateConfessionSessionDTO.setConversationName("新会话");
+                                        updateConfessionSessionDTO.setConfessionSessionId(confessionSessionId);
+                                        updateConfessionSessionDTO.setConversationId(temConversationId2);
+                                        confessionSessionService.updateConfessionSession(updateConfessionSessionDTO);
+                                    }
                                 } catch (Exception e) {
                                 }
 
@@ -240,12 +264,20 @@ public class DifyService {
     @Transactional(rollbackFor = Exception.class)
     public Map<String, Object> generateInventionPoint(ChatMessageDTO chatMessageDTO) throws IOException {
         Integer projectId = chatMessageDTO.getProjectId();
+        Integer confessionSessionId = chatMessageDTO.getConfessionSessionId();
         String query = "生成发明点";
         String userId = loginUtils.getId().toString();
         String conversationId = chatMessageDTO.getConversationId();
         DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
         Map<String, Object> map = new HashMap<>();
-        String fileContent = this.getConfression(projectId);
+        String fileContent="";
+         if(confessionSessionId!=null){
+             ConfessionSession confessionSession=confessionSessionService.getById(confessionSessionId);
+             fileContent=fileDownloadUrl+confessionSession.getGuid();
+         }
+         else if(projectId!=null) {
+             fileContent = this.getConfression(projectId);
+         }
         map.put("file_path", fileContent);
         difyChatMessageDTO.setInputs(map);
         difyChatMessageDTO.setConversationId(conversationId);
@@ -267,14 +299,21 @@ public class DifyService {
         JSONObject jsonObject = JSONObject.parseObject(res);
         String inventionPoint = jsonObject.get("answer").toString();
         inventionPoint = DataUtils.getMarkDownText(inventionPoint);
-//        if (conversationId == null) {
-//            conversationId = jsonObject.get("conversation_id").toString();
-//            difySessionService.addDifySession(projectId, userId, conversationId);
-//        }
         Map<String, Object> map1 = new HashMap<>();
         map1.put("invention_point", inventionPoint);
-        technicalCaseService.updateInventionPoint(projectId, inventionPoint);
+        if (projectId != null) {
+            technicalCaseService.updateInventionPoint(projectId, inventionPoint);
+        }
+        if (confessionSessionId != null) {
+            UpdateConfessionSessionDTO updateConfessionSessionDTO = new UpdateConfessionSessionDTO();
+            updateConfessionSessionDTO.setConfessionSessionId(confessionSessionId);
+            updateConfessionSessionDTO.setInventionPoint(inventionPoint);
+            confessionSessionService.updateConfessionSession(updateConfessionSessionDTO);
+        }
         map1.put("conversation_id", conversationId);
+
+        map1.put("projectId",projectId);
+        map1.put("confessionSessionId",confessionSessionId);
         return map1;
     }
 }

+ 101 - 0
src/main/java/cn/cslg/pas/service/dify/ConfessionSessionService.java

@@ -0,0 +1,101 @@
+package cn.cslg.pas.service.dify;
+
+import cn.cslg.pas.common.model.cronModel.SystemFile;
+import cn.cslg.pas.common.model.dify.confessionSession.*;
+import cn.cslg.pas.common.utils.CacheUtils;
+import cn.cslg.pas.common.utils.FormatUtil;
+import cn.cslg.pas.common.utils.LoginUtils;
+import cn.cslg.pas.exception.ExceptionEnum;
+import cn.cslg.pas.exception.XiaoShiException;
+import cn.cslg.pas.service.common.FileManagerService;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import cn.cslg.pas.domain.dify.ConfessionSession;
+import cn.cslg.pas.mapper.dify.ConfessionSessionMapper;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author admin
+ * @description 针对表【confession_session(交底书会话记录)】的数据库操作Service实现
+ * @createDate 2025-04-16 15:48:57
+ */
+@Service
+@RequiredArgsConstructor
+public class ConfessionSessionService extends ServiceImpl<ConfessionSessionMapper, ConfessionSession> {
+    private final CacheUtils cacheUtils;
+    private final LoginUtils loginUtils;
+    private final FileManagerService fileManagerService;
+
+    public Integer addConfessionSession(AddConfessionSessionDTO addConfessionSessionDTO) {
+        String guid = addConfessionSessionDTO.getFileGuid();
+        ConfessionSession confessionSession = new ConfessionSession();
+        confessionSession.setGuid(guid);
+        String id = loginUtils.getId().toString();
+        confessionSession.setCreateId(id);
+        confessionSession.insert();
+        return confessionSession.getId();
+    }
+
+    public Integer updateConfessionSession(UpdateConfessionSessionDTO updateConfessionSessionDTO) {
+        Integer id = updateConfessionSessionDTO.getConfessionSessionId();
+        ConfessionSession confessionSession = this.getById(id);
+        if (confessionSession == null) {
+            throw new XiaoShiException(ExceptionEnum.BUSINESS_CHECK, "未查询到交底书会话记录");
+        }
+        BeanUtils.copyProperties(updateConfessionSessionDTO, confessionSession, FormatUtil.getNullPropertyNames(updateConfessionSessionDTO));
+        confessionSession.updateById();
+        return confessionSession.getId();
+    }
+
+    public List<QueryConfessionSessionVO> queryConfessionSession(QueryConfessionSessionDTO queryConfessionSessionDTO) {
+        Integer userId = loginUtils.getId();
+        Integer confessionSessionId = queryConfessionSessionDTO.getConfessionSessionId();
+        LambdaQueryWrapper<ConfessionSession> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(ConfessionSession::getCreateId, userId)
+                .orderByDesc(ConfessionSession::getCreateTime);
+        if (confessionSessionId != null) {
+            queryWrapper.eq(ConfessionSession::getId, confessionSessionId);
+        }
+        List<ConfessionSession> confessionSessions = this.list(queryWrapper);
+        List<QueryConfessionSessionVO> queryConfessionSessionVOS = new ArrayList<>();
+        if (confessionSessions == null || confessionSessions.size() == 0) {
+            return queryConfessionSessionVOS;
+        }
+        List<String> fileGuids = confessionSessions.stream().map(ConfessionSession::getGuid).collect(Collectors.toList());
+        List<SystemFile> systemFiles = new ArrayList<>();
+        try {
+            String res = fileManagerService.getSystemFileFromFMS(fileGuids);
+            systemFiles = JSONObject.parseArray(res, SystemFile.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        for (ConfessionSession confessionSession : confessionSessions) {
+            QueryConfessionSessionVO queryConfessionSessionVO = new QueryConfessionSessionVO();
+            BeanUtils.copyProperties(confessionSession, queryConfessionSessionVO);
+            String guid = confessionSession.getGuid();
+            SystemFile systemFile = systemFiles.stream().filter(item -> item.getGuid().equals(guid)).findFirst().orElse(null);
+            queryConfessionSessionVO.setSystemFile(systemFile);
+            queryConfessionSessionVOS.add(queryConfessionSessionVO);
+        }
+        return queryConfessionSessionVOS;
+    }
+
+    public List<Integer> deleteConfessionSessions(DeleteConfessionSessionDTO deleteConfessionSessionDTO) {
+        List<Integer> ids = deleteConfessionSessionDTO.getConfessionSessionIds();
+        this.removeBatchByIds(ids);
+        return ids;
+    }
+}
+
+
+
+

+ 15 - 0
src/test/java/cn/cslg/pas/service/common/DifyServiceTest.java

@@ -2,6 +2,9 @@ package cn.cslg.pas.service.common;
 
 import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
 import cn.cslg.pas.common.model.dify.DifyHistoryMessageDTO;
+import cn.cslg.pas.common.utils.FormatUtil;
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.bean.copier.CopyOptions;
 import com.vladsch.flexmark.ext.tables.TablesExtension;
 import com.vladsch.flexmark.html.HtmlRenderer;
 import com.vladsch.flexmark.parser.Parser;
@@ -9,6 +12,7 @@ import com.vladsch.flexmark.util.ast.Node;
 import com.vladsch.flexmark.util.data.MutableDataSet;
 import org.jsoup.Jsoup;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
@@ -74,4 +78,15 @@ private DifyService difyService;
         System.out.println(html);
 
     }
+
+    @Test
+    public  void  test(){
+        DifyHistoryMessageDTO difyHistoryMessageDTO =new DifyHistoryMessageDTO();
+        difyHistoryMessageDTO.setUser("张三");
+        DifyHistoryMessageDTO difyHistoryMessageDTO1 =new DifyHistoryMessageDTO();
+        difyHistoryMessageDTO1.setUser("李四");
+        difyHistoryMessageDTO1.setLimit(10);
+        BeanUtils.copyProperties(difyHistoryMessageDTO, difyHistoryMessageDTO1, FormatUtil.getNullPropertyNames(difyHistoryMessageDTO));        System.out.println(difyHistoryMessageDTO1);
+        System.out.println(difyHistoryMessageDTO1);
+    }
 }