diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/ChatConversationController.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/ChatConversationController.java new file mode 100644 index 000000000..4fc1b7fd8 --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/ChatConversationController.java @@ -0,0 +1,58 @@ +package cn.iocoder.yudao.module.ai.controller; + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.ai.service.ChatConversationService; +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq; +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRes; +import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq; +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * ia 模块 + * + * @author fansili + * @time 2024/4/13 17:44 + * @since 1.0 + */ +@Tag(name = "A2聊天-对话") +@RestController +@RequestMapping("/ai/chat/conversation") +@Slf4j +@AllArgsConstructor +public class ChatConversationController { + + private final ChatConversationService chatConversationService; + + @Operation(summary = "创建 - 对话") + @PostMapping("/create") + public CommonResult create(@RequestBody @Validated ChatConversationCreateReq req) { + return CommonResult.success(chatConversationService.create(req)); + } + + @Operation(summary = "获取 - 获取对话") + @GetMapping("/getConversation") + public CommonResult getConversation(@RequestParam("id") Long id) { + return CommonResult.success(chatConversationService.getConversation(id)); + } + + @Operation(summary = "获取 - 获取对话list") + @GetMapping("/listConversation") + public CommonResult> listConversation(@ModelAttribute @Validated ChatConversationListReq req) { + return CommonResult.success(chatConversationService.listConversation(req)); + } + + @Operation(summary = "删除") + @DeleteMapping("/listConversation") + public CommonResult delete(@RequestParam("id") Long id) { + chatConversationService.delete(id); + return CommonResult.success(null); + } +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/ChatConversationConvert.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/ChatConversationConvert.java new file mode 100644 index 000000000..3ecabcc7c --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/ChatConversationConvert.java @@ -0,0 +1,37 @@ +package cn.iocoder.yudao.module.ai.convert; + +import cn.iocoder.yudao.module.ai.dataobject.AiChatConversationDO; +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +/** + * 聊天 对话 convert + * + * @author fansili + * @time 2024/4/18 16:39 + * @since 1.0 + */ +@Mapper +public interface ChatConversationConvert { + + ChatConversationConvert INSTANCE = Mappers.getMapper(ChatConversationConvert.class); + + /** + * 转换 - 多个 ChatConversationRes + * + * @param top100Conversation + * @return + */ + List covnertChatConversationResList(List top100Conversation); + + /** + * 转换 - 单个 ChatConversationRes + * + * @param aiChatConversationDO + * @return + */ + ChatConversationRes covnertChatConversationRes(AiChatConversationDO aiChatConversationDO); +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/ChatConversationService.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/ChatConversationService.java new file mode 100644 index 000000000..cb52f725c --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/ChatConversationService.java @@ -0,0 +1,48 @@ +package cn.iocoder.yudao.module.ai.service; + +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq; +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRes; +import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq; +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; + +import java.util.List; + +/** + * chat 对话 + * + * @fansili + * @since v1.0 + */ +public interface ChatConversationService { + + /** + * 对话 - 创建 + * + * @param req + * @return + */ + ChatConversationRes create(ChatConversationCreateReq req); + + /** + * 获取 - 对话 + * + * @param id + * @return + */ + ChatConversationRes getConversation(Long id); + + /** + * 获取 - 对话列表 + * + * @param req + * @return + */ + List listConversation(ChatConversationListReq req); + + /** + * 删除 - 根据id + * + * @param id + */ + void delete(Long id); +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/ChatConversationServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/ChatConversationServiceImpl.java new file mode 100644 index 000000000..2278df935 --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/ChatConversationServiceImpl.java @@ -0,0 +1,74 @@ +package cn.iocoder.yudao.module.ai.service.impl; + +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; +import cn.iocoder.yudao.module.ai.convert.ChatConversationConvert; +import cn.iocoder.yudao.module.ai.dataobject.AiChatConversationDO; +import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper; +import cn.iocoder.yudao.module.ai.service.ChatConversationService; +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq; +import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq; +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * chat 对话 + * + * @fansili + * @since v1.0 + */ +@Service +@Slf4j +@AllArgsConstructor +public class ChatConversationServiceImpl implements ChatConversationService { + + private final AiChatConversationMapper aiChatConversationMapper; + + @Override + public ChatConversationRes create(ChatConversationCreateReq req) { + // 获取用户id + Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); + // 查询最新的对话 + AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId); + // 如果有对话没有被使用过,那就返回这个 + if (latestConversation != null && latestConversation.getChatCount() <= 0) { + return ChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation); + } + // 创建新的 Conversation + AiChatConversationDO insertConversation = new AiChatConversationDO(); + insertConversation.setId(null); + insertConversation.setUserId(loginUserId); + insertConversation.setChatRoleId(null); + insertConversation.setChatRoleName(null); + insertConversation.setChatTitle(null); + insertConversation.setChatCount(0); + insertConversation.setChatType(req.getChatType()); + aiChatConversationMapper.insert(insertConversation); + // 转换 res + return ChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation); + } + + @Override + public ChatConversationRes getConversation(Long id) { + AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(id); + return ChatConversationConvert.INSTANCE.covnertChatConversationRes(aiChatConversationDO); + } + + @Override + public List listConversation(ChatConversationListReq req) { + // 获取用户id + Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); + // 查询前100对话 + List top100Conversation + = aiChatConversationMapper.selectTop100Conversation(loginUserId, req.getSearch()); + return ChatConversationConvert.INSTANCE.covnertChatConversationResList(top100Conversation); + } + + @Override + public void delete(Long id) { + aiChatConversationMapper.deleteById(id); + } +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationCreateReq.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationCreateReq.java new file mode 100644 index 000000000..a39c9e2ed --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationCreateReq.java @@ -0,0 +1,23 @@ +package cn.iocoder.yudao.module.ai.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 聊天对话 + * + * @author fansili + * @time 2024/4/18 16:24 + * @since 1.0 + */ +@Data +@Accessors(chain = true) +public class ChatConversationCreateReq { + + @Schema(description = "对话类型(roleChat、userChat)") + @NotNull(message = "聊天类型不能为空!") + private String chatType; + +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationCreateRes.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationCreateRes.java new file mode 100644 index 000000000..3117aad9d --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationCreateRes.java @@ -0,0 +1,11 @@ +package cn.iocoder.yudao.module.ai.vo; + +/** + * 聊天对话 res + * + * @author fansili + * @time 2024/4/18 16:24 + * @since 1.0 + */ +public class ChatConversationCreateRes { +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationListReq.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationListReq.java new file mode 100644 index 000000000..846c8b2bc --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationListReq.java @@ -0,0 +1,20 @@ +package cn.iocoder.yudao.module.ai.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 聊天对话 list req + * + * @author fansili + * @time 2024/4/18 16:24 + * @since 1.0 + */ +@Data +@Accessors(chain = true) +public class ChatConversationListReq { + + @Schema(description = "查询根据title") + private String search; +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationRes.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationRes.java new file mode 100644 index 000000000..95eca40e3 --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationRes.java @@ -0,0 +1,11 @@ +package cn.iocoder.yudao.module.ai.vo; + +/** + * 聊天对话 res + * + * @author fansili + * @time 2024/4/18 16:24 + * @since 1.0 + */ +public class ChatConversationRes { +}