瀏覽代碼

ai交换日志

zhangchen 1 月之前
父節點
當前提交
5f1c3473d6

+ 186 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/controller/LawyerAiInteractionLogController.java

@@ -0,0 +1,186 @@
+package shop.alien.lawyer.controller;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import io.swagger.annotations.*;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.*;
+import shop.alien.entity.result.R;
+import shop.alien.entity.store.LawyerAiInteractionLog;
+import shop.alien.lawyer.service.LawyerAiInteractionLogService;
+import shop.alien.util.myBaticsPlus.QueryBuilder;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * AI交互日志 前端控制器
+ *
+ * @author system
+ * @since 2025-01-XX
+ */
+@Slf4j
+@Api(tags = {"律师平台-AI交互日志"})
+@ApiSort(21)
+@CrossOrigin
+@RestController
+@RequestMapping("/lawyer/aiInteractionLog")
+@RequiredArgsConstructor
+public class LawyerAiInteractionLogController {
+
+    private final LawyerAiInteractionLogService aiInteractionLogService;
+
+    @ApiOperation("新增AI交互日志")
+    @ApiOperationSupport(order = 1)
+    @PostMapping("/addAiInteractionLog")
+    public R<LawyerAiInteractionLog> addAiInteractionLog(@RequestBody LawyerAiInteractionLog aiInteractionLog) {
+        log.info("LawyerAiInteractionLogController.addAiInteractionLog?aiInteractionLog={}", aiInteractionLog);
+        return aiInteractionLogService.addAiInteractionLog(aiInteractionLog);
+    }
+
+    @ApiOperation("编辑AI交互日志")
+    @ApiOperationSupport(order = 2)
+    @PostMapping("/editAiInteractionLog")
+    public R<LawyerAiInteractionLog> editAiInteractionLog(@RequestBody LawyerAiInteractionLog aiInteractionLog) {
+        log.info("LawyerAiInteractionLogController.editAiInteractionLog?aiInteractionLog={}", aiInteractionLog);
+        return aiInteractionLogService.editAiInteractionLog(aiInteractionLog);
+    }
+
+    @ApiOperation("删除AI交互日志")
+    @ApiOperationSupport(order = 3)
+    @DeleteMapping("/deleteAiInteractionLog")
+    public R<Boolean> deleteAiInteractionLog(@RequestParam(value = "id") Integer id) {
+        log.info("LawyerAiInteractionLogController.deleteAiInteractionLog?id={}", id);
+        return aiInteractionLogService.deleteAiInteractionLog(id);
+    }
+
+    @ApiOperation("保存或更新AI交互日志")
+    @ApiOperationSupport(order = 4)
+    @PostMapping("/saveOrUpdate")
+    public R<LawyerAiInteractionLog> saveOrUpdate(@RequestBody LawyerAiInteractionLog aiInteractionLog) {
+        log.info("LawyerAiInteractionLogController.saveOrUpdate?aiInteractionLog={}", aiInteractionLog);
+        boolean result = aiInteractionLogService.saveOrUpdate(aiInteractionLog);
+        if (result) {
+            return R.data(aiInteractionLog);
+        }
+        return R.fail("操作失败");
+    }
+
+    @ApiOperation("通用列表查询")
+    @ApiOperationSupport(order = 5)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
+            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
+            @ApiImplicitParam(name = "conversationId", value = "会话ID(支持模糊查询)", dataType = "String", paramType = "query"),
+            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
+            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始(范围查询)", dataType = "String", paramType = "query"),
+            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束(范围查询)", dataType = "String", paramType = "query")
+    })
+    @GetMapping("/getList")
+    public R<List<LawyerAiInteractionLog>> getList(@ModelAttribute LawyerAiInteractionLog aiInteractionLog) {
+        log.info("LawyerAiInteractionLogController.getList?aiInteractionLog={}", aiInteractionLog);
+        List<LawyerAiInteractionLog> list = QueryBuilder.of(aiInteractionLog)
+                .likeFields("conversationId")  // 会话ID支持模糊查询
+                .build()
+                .list(aiInteractionLogService);
+        return R.data(list);
+    }
+
+    @ApiOperation("通用分页查询")
+    @ApiOperationSupport(order = 6)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "page", value = "页数(默认1)", dataType = "int", paramType = "query"),
+            @ApiImplicitParam(name = "size", value = "页容(默认10)", dataType = "int", paramType = "query"),
+            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
+            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
+            @ApiImplicitParam(name = "conversationId", value = "会话ID(支持模糊查询)", dataType = "String", paramType = "query"),
+            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
+            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始(范围查询)", dataType = "String", paramType = "query"),
+            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束(范围查询)", dataType = "String", paramType = "query")
+    })
+    @GetMapping("/getPage")
+    public R<IPage<LawyerAiInteractionLog>> getPage(@ModelAttribute LawyerAiInteractionLog aiInteractionLog,
+                                                     @RequestParam(defaultValue = "1") int page,
+                                                     @RequestParam(defaultValue = "10") int size) {
+        log.info("LawyerAiInteractionLogController.getPage?aiInteractionLog={},page={},size={}", aiInteractionLog, page, size);
+        int pageNum = page > 0 ? page : 1;
+        int pageSize = size > 0 ? size : 10;
+        IPage<LawyerAiInteractionLog> pageResult = QueryBuilder.of(aiInteractionLog)
+                .likeFields("conversationId")  // 会话ID支持模糊查询
+                .page(pageNum, pageSize)
+                .build()
+                .page(aiInteractionLogService);
+        return R.data(pageResult);
+    }
+
+    @ApiOperation("AI聊天接口")
+    @ApiOperationSupport(order = 7)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "message", value = "用户发送的消息内容", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "id", value = "会话ID(AI)", dataType = "String", paramType = "query"),
+            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID(可选)", dataType = "Integer", paramType = "query")
+    })
+    @PostMapping("/chat")
+    public R<Map<String, Object>> sendAIMessage(
+            @RequestParam String message,
+            @RequestParam(required = false) String sessionId,
+            @RequestParam(required = false) Integer clientUserId) {
+        log.info("LawyerAiInteractionLogController.sendAIMessage?message={},sessionId={},clientUserId={}", message, sessionId, clientUserId);
+        return aiInteractionLogService.sendAIMessage(message, sessionId, clientUserId);
+    }
+
+
+    @ApiOperation("AI聊天记录保存")
+    @ApiOperationSupport(order = 8)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "queryText", value = "用户发送的消息内容", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "responseText", value = "AI回复内容", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query", required = true)
+    })
+    @PostMapping("/saveChatLog")
+    public R<Map<String, Object>> saveChatLog(
+            @RequestParam String queryText,
+            @RequestParam String responseText,
+            @RequestParam Integer clientUserId) {
+        log.info("LawyerAiInteractionLogController.sendAIMessage?message={},responseText={},clientUserId={}", queryText, responseText, clientUserId);
+        return aiInteractionLogService.saveChatLog(queryText, responseText, clientUserId);
+    }
+
+    @ApiOperation("删除聊天记录")
+    @ApiOperationSupport(order = 9)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "id", value = "AI交互日志表id", dataType = "Integer", paramType = "query", required = true),
+    })
+    @PostMapping("/updatedLog")
+    public R<Map<String, Object>> updatedLog(
+            @RequestParam Integer id) {
+        log.info("LawyerAiInteractionLogController.updatedLog?id={}", id);
+        return aiInteractionLogService.updatedLog(id);
+    }
+
+    @ApiOperation("分页查询对话历史")
+    @ApiOperationSupport(order = 10)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "page", value = "页数(默认1)", dataType = "int", paramType = "query"),
+            @ApiImplicitParam(name = "size", value = "页容(默认10)", dataType = "int", paramType = "query"),
+//            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
+            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query", required = true)
+//            @ApiImplicitParam(name = "conversationId", value = "会话ID(支持模糊查询)", dataType = "String", paramType = "query"),
+//            @ApiImplicitParam(name = "problemScenarId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
+//            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始(范围查询)", dataType = "String", paramType = "query"),
+//            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束(范围查询)", dataType = "String", paramType = "query")
+    })
+    @GetMapping("/getLogList")
+    public R<IPage<LawyerAiInteractionLog>> getLogList(
+                                                    @RequestParam Integer clientUserId,
+                                                    @RequestParam(defaultValue = "0") int page,
+                                                    @RequestParam(defaultValue = "10") int size
+                                                    ) {
+        log.info("LawyerAiInteractionLogController.getLogList?,page={},size={},clientUserId{}", clientUserId, page, size);
+
+        return aiInteractionLogService.getLogList( clientUserId,page, size);
+    }
+
+
+}
+

+ 88 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/service/LawyerAiInteractionLogService.java

@@ -0,0 +1,88 @@
+package shop.alien.lawyer.service;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.service.IService;
+import shop.alien.entity.result.R;
+import shop.alien.entity.store.LawyerAiInteractionLog;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * AI交互日志 服务类
+ *
+ * @author system
+ * @since 2025-01-XX
+ */
+public interface LawyerAiInteractionLogService extends IService<LawyerAiInteractionLog> {
+
+    /**
+     * 分页查询AI交互日志列表
+     *
+     * @param pageNum         页码
+     * @param pageSize        页容
+     * @param clientUserId    客户端用户ID
+     * @param conversationId  会话ID
+     * @param problemScenarioId 法律问题场景ID
+     * @return IPage<LawyerAiInteractionLog>
+     */
+    R<IPage<LawyerAiInteractionLog>> getAiInteractionLogList(int pageNum, int pageSize, Integer clientUserId,
+                                                        String conversationId, Integer problemScenarioId);
+
+    /**
+     * 根据会话ID查询交互日志列表
+     *
+     * @param conversationId 会话ID
+     * @return List<LawyerAiInteractionLog>
+     */
+    List<LawyerAiInteractionLog> getListByConversationId(String conversationId);
+
+    /**
+     * 新增AI交互日志
+     *
+     * @param aiInteractionLog AI交互日志
+     * @return R<LawyerAiInteractionLog>
+     */
+    R<LawyerAiInteractionLog> addAiInteractionLog(LawyerAiInteractionLog aiInteractionLog);
+
+    /**
+     * 编辑AI交互日志
+     *
+     * @param aiInteractionLog AI交互日志
+     * @return R<LawyerAiInteractionLog>
+     */
+    R<LawyerAiInteractionLog> editAiInteractionLog(LawyerAiInteractionLog aiInteractionLog);
+
+    /**
+     * 删除AI交互日志
+     *
+     * @param id 主键
+     * @return R<Boolean>
+     */
+    R<Boolean> deleteAiInteractionLog(Integer id);
+
+    /**
+     * AI聊天接口
+     *
+     * @param message     用户发送的消息内容
+     * @param sessionId   会话ID(可选)
+     * @param clientUserId 客户端用户ID(可选)
+     * @return R<Map<String, Object>>
+     */
+    R<Map<String, Object>> sendAIMessage(String message, String sessionId, Integer clientUserId);
+
+    /**
+     * AI聊天接口
+     *
+     * @param queryText     用户发送的消息内容
+     * @param responseText   AI回复内容
+     * @param clientUserId 客户端用户ID
+     * @return R<Map<String, Object>>
+     */
+    R<Map<String, Object>> saveChatLog(String queryText, String responseText, Integer clientUserId);
+
+    R<Map<String, Object>> updatedLog(Integer id);
+
+    R<IPage<LawyerAiInteractionLog>> getLogList(Integer clientUserId,Integer page, Integer size );
+}
+

+ 186 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerAiInteractionLogServiceImpl.java

@@ -0,0 +1,186 @@
+package shop.alien.lawyer.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+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.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
+import shop.alien.entity.result.R;
+import shop.alien.entity.store.LawyerAiInteractionLog;
+import shop.alien.entity.store.vo.LawyerAiInteractionLogVo;
+import shop.alien.lawyer.service.LawyerAiInteractionLogService;
+import shop.alien.mapper.LawyerAiInteractionLogMapper;
+import shop.alien.util.common.ListToPage;
+
+import java.util.*;
+
+/**
+ * AI交互日志 服务实现类
+ *
+ * @author system
+ * @since 2025-01-XX
+ */
+@Slf4j
+@Transactional
+@Service
+@RequiredArgsConstructor
+public class LawyerAiInteractionLogServiceImpl extends ServiceImpl<LawyerAiInteractionLogMapper, LawyerAiInteractionLog> implements LawyerAiInteractionLogService {
+
+    private final LawyerAiInteractionLogMapper aiInteractionLogMapper;
+
+    @Override
+    public R<IPage<LawyerAiInteractionLog>> getAiInteractionLogList(int pageNum, int pageSize, Integer clientUserId,
+                                                               String conversationId, Integer problemScenarioId) {
+        log.info("LawyerAiInteractionLogServiceImpl.getAiInteractionLogList?pageNum={},pageSize={},clientUserId={},conversationId={},problemScenarioId={}",
+                pageNum, pageSize, clientUserId, conversationId, problemScenarioId);
+        Page<LawyerAiInteractionLog> page = new Page<>(pageNum, pageSize);
+        LambdaQueryWrapper<LawyerAiInteractionLog> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(LawyerAiInteractionLog::getDeleteFlag, 0);
+        if (clientUserId != null) {
+            queryWrapper.eq(LawyerAiInteractionLog::getClientUserId, clientUserId);
+        }
+        if (StringUtils.hasText(conversationId)) {
+            queryWrapper.eq(LawyerAiInteractionLog::getConversationId, conversationId);
+        }
+        if (problemScenarioId != null) {
+            queryWrapper.eq(LawyerAiInteractionLog::getProblemScenarioId, problemScenarioId);
+        }
+        queryWrapper.orderByDesc(LawyerAiInteractionLog::getInteractionTime);
+        IPage<LawyerAiInteractionLog> pageResult = this.page(page, queryWrapper);
+        return R.data(pageResult);
+    }
+
+    @Override
+    public List<LawyerAiInteractionLog> getListByConversationId(String conversationId) {
+        log.info("LawyerAiInteractionLogServiceImpl.getListByConversationId?conversationId={}", conversationId);
+        LambdaQueryWrapper<LawyerAiInteractionLog> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(LawyerAiInteractionLog::getConversationId, conversationId)
+                .eq(LawyerAiInteractionLog::getDeleteFlag, 0)
+                .orderByAsc(LawyerAiInteractionLog::getInteractionTime);
+        return this.list(queryWrapper);
+    }
+
+    @Override
+    public R<LawyerAiInteractionLog> addAiInteractionLog(LawyerAiInteractionLog aiInteractionLog) {
+        log.info("LawyerAiInteractionLogServiceImpl.addAiInteractionLog?aiInteractionLog={}", aiInteractionLog);
+        boolean result = this.save(aiInteractionLog);
+        if (result) {
+            return R.data(aiInteractionLog);
+        }
+        return R.fail("新增失败");
+    }
+
+    @Override
+    public R<LawyerAiInteractionLog> editAiInteractionLog(LawyerAiInteractionLog aiInteractionLog) {
+        log.info("LawyerAiInteractionLogServiceImpl.editAiInteractionLog?aiInteractionLog={}", aiInteractionLog);
+        boolean result = this.updateById(aiInteractionLog);
+        if (result) {
+            return R.data(aiInteractionLog);
+        }
+        return R.fail("修改失败");
+    }
+
+    @Override
+    public R<Boolean> deleteAiInteractionLog(Integer id) {
+        log.info("LawyerAiInteractionLogServiceImpl.deleteAiInteractionLog?id={}", id);
+        boolean result = this.removeById(id);
+        if (result) {
+            return R.success("删除成功");
+        }
+        return R.fail("删除失败");
+    }
+
+    @Override
+    public R<Map<String, Object>> sendAIMessage(String message, String sessionId, Integer clientUserId) {
+        log.info("LawyerAiInteractionLogServiceImpl.sendAIMessage?message={},sessionId={},clientUserId={}", message, sessionId, clientUserId);
+
+        // 如果没有sessionId,生成一个新的
+        if (sessionId == null || sessionId.isEmpty()) {
+            sessionId = UUID.randomUUID().toString().replace("-", "");
+        }
+
+        // TODO: 这里需要调用实际的AI服务接口,目前返回模拟数据
+        String aiReply = "您好!我是AI助手UBAO,正在为您分析问题...";
+
+        // 保存AI交互日志
+        LawyerAiInteractionLog log = new LawyerAiInteractionLog();
+        log.setClientUserId(clientUserId);
+        log.setConversationId(sessionId);
+        log.setQueryText(message);
+        log.setResponseText(aiReply);
+        log.setInteractionTime(new Date());
+        this.save(log);
+
+        Map<String, Object> result = new HashMap<>();
+        result.put("reply", aiReply);
+        result.put("sessionId", sessionId);
+        result.put("hasRecommendLawyers", false);
+        result.put("recommendLawyerIds", new ArrayList<>());
+
+        return R.data(result);
+    }
+
+    @Override
+    public R<Map<String, Object>> saveChatLog(String queryText, String responseText, Integer clientUserId) {
+        log.info("LawyerAiInteractionLogServiceImpl.sendAIMessage?message={},responseText={},clientUserId={}", queryText, responseText, clientUserId);
+        Map<String, Object> result = new HashMap<>();
+//        LawyerAiInteractionLogVo log = new LawyerAiInteractionLogVo();
+//        log.setClientUserId(clientUserId);
+//        log.setQueryText(queryText);
+//        log.setResponseText(responseText);
+//        log.setInteractionTime(new Date());
+//        log.setDeleteFlag(0);
+//        log.setCreatedTime(new Date());
+//        log.setUpdatedTime(new Date());
+//        int num =aiInteractionLogMapper.insertLog(log);
+        LawyerAiInteractionLog log = new LawyerAiInteractionLog();
+        log.setClientUserId(clientUserId);
+        log.setQueryText(queryText);
+        log.setResponseText(responseText);
+        log.setInteractionTime(new Date());
+        log.setDeleteFlag(0);
+        log.setCreatedTime(new Date());
+        log.setUpdatedTime(new Date());
+        //向表中插入一条新的数据,插入成功后获取插入该条数据的id
+        boolean A=this.save(log);
+        if (A) {
+            result.put("result","聊天记录保存成功");
+            result.put("log",log);
+            return R.data(result);
+        }
+        result.put("result","聊天记录保存失败");
+        return R.data(result);
+    }
+
+    @Override
+    public R<Map<String, Object>> updatedLog(Integer id) {
+        log.info("LawyerAiInteractionLogController.updatedLog?id={}", id);
+        Map<String, Object> result = new HashMap<>();
+        LawyerAiInteractionLogVo log = new LawyerAiInteractionLogVo();
+        log.setId(id);
+        log.setDeleteFlag(1);
+        log.setUpdatedTime(new Date());
+        int num=aiInteractionLogMapper.updateLog(log);
+        if (num>0) {
+            result.put("result","删除成功");
+            result.put("log",log);
+            return R.data(result);
+        }
+        result.put("result","删除失败");
+        return R.data(result);
+    }
+
+    @Override
+    public R<IPage<LawyerAiInteractionLog>> getLogList(Integer clientUserId,Integer page, Integer size) {
+        List<LawyerAiInteractionLog> list = aiInteractionLogMapper.getLogList(clientUserId,page,size);
+        IPage<LawyerAiInteractionLog> iPag =ListToPage.setPage(list,page, size);
+        return R.data(iPag);
+    }
+
+
+}
+