diff --git a/src/main/java/cn/teammodel/common/ChatAppScopeEnum.java b/src/main/java/cn/teammodel/common/ChatAppScopeEnum.java new file mode 100644 index 0000000..485804e --- /dev/null +++ b/src/main/java/cn/teammodel/common/ChatAppScopeEnum.java @@ -0,0 +1,46 @@ +package cn.teammodel.common; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author winter + * @create 2024-02-01 15:30 + */ + +public enum ChatAppScopeEnum { + PUBLIC("public", "公共"), + SCHOOL("school", "学校"), + PRIVATE("private", "私人"); + private final String code; + private final String name; + public static List codes() { + return Arrays.stream(values()).map(ChatAppScopeEnum::getCode).collect(Collectors.toList()); + } + + /** + * 根据 name 获取 code + */ + public static String getCodeByName(String name) { + ChatAppScopeEnum chatAppScopeEnum = Arrays.stream(values()).filter(item -> item.getName().equals(name)).findFirst().orElse(null); + String res = null; + if (chatAppScopeEnum != null) { + res = chatAppScopeEnum.getCode(); + } + return res; + } + + ChatAppScopeEnum(String code, String name) { + this.code = code; + this.name = name; + } + + public String getCode() { + return code; + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/cn/teammodel/controller/frontend/AiController.java b/src/main/java/cn/teammodel/controller/frontend/AiController.java index 3bc8021..fdda18e 100644 --- a/src/main/java/cn/teammodel/controller/frontend/AiController.java +++ b/src/main/java/cn/teammodel/controller/frontend/AiController.java @@ -2,10 +2,7 @@ package cn.teammodel.controller.frontend; import cn.teammodel.common.IdRequest; import cn.teammodel.common.R; -import cn.teammodel.model.dto.ai.ChatCompletionReqDto; -import cn.teammodel.model.dto.ai.CreateChatAppDto; -import cn.teammodel.model.dto.ai.UpdateChatAppDto; -import cn.teammodel.model.dto.ai.UpdateSessionDto; +import cn.teammodel.model.dto.ai.*; import cn.teammodel.model.entity.ai.ChatApp; import cn.teammodel.model.entity.ai.ChatSession; import cn.teammodel.service.ChatAppService; @@ -106,10 +103,10 @@ public class AiController { return R.success(session); } - @GetMapping("app/list") + @PostMapping("app/list") @ApiOperation("查询聊天应用列表") - public R> listApp() { - List chatApps = chatAppService.listApp(); + public R> listApp(@RequestBody @Valid SearchAppDto searchAppDto) { + List chatApps = chatAppService.listApp(searchAppDto); return R.success(chatApps); } diff --git a/src/main/java/cn/teammodel/model/dto/ai/CreateChatAppDto.java b/src/main/java/cn/teammodel/model/dto/ai/CreateChatAppDto.java index 98e7241..d1438b4 100644 --- a/src/main/java/cn/teammodel/model/dto/ai/CreateChatAppDto.java +++ b/src/main/java/cn/teammodel/model/dto/ai/CreateChatAppDto.java @@ -13,13 +13,17 @@ public class CreateChatAppDto { @ApiModelProperty("应用名称") @NotBlank(message = "请输入应用名称") private String name; - + private String lang; + private String bizType; + @ApiModelProperty("应用域") + @NotBlank(message = "请输入应用域") + private String scope; + private String itemType;; + private String period; + private String subject; @ApiModelProperty("应用描述") private String description; - @ApiModelProperty("是否公开") - private boolean publicApp = false; - @NotBlank(message = "请输入应用提示词") @ApiModelProperty("应用提示词") private String prompt; diff --git a/src/main/java/cn/teammodel/model/dto/ai/SearchAppDto.java b/src/main/java/cn/teammodel/model/dto/ai/SearchAppDto.java new file mode 100644 index 0000000..64d0cc0 --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/ai/SearchAppDto.java @@ -0,0 +1,20 @@ +package cn.teammodel.model.dto.ai; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +/** + * @author winter + * @create 2024-02-01 15:40 + */ +@Data +public class SearchAppDto { + @NotBlank(message = "scope不能为空") + private String scope; + private String bizType; + private String lang; + private String itemType;; + private String period; + private String subject; +} diff --git a/src/main/java/cn/teammodel/model/dto/ai/UpdateChatAppDto.java b/src/main/java/cn/teammodel/model/dto/ai/UpdateChatAppDto.java index c69e70f..2040180 100644 --- a/src/main/java/cn/teammodel/model/dto/ai/UpdateChatAppDto.java +++ b/src/main/java/cn/teammodel/model/dto/ai/UpdateChatAppDto.java @@ -17,13 +17,17 @@ public class UpdateChatAppDto { @ApiModelProperty("应用名称") @NotBlank(message = "请输入应用名称") private String name; - + private String lang; + private String bizType; + @ApiModelProperty("应用域") + @NotBlank(message = "请输入应用域") + private String scope; + private String itemType;; + private String period; + private String subject; @ApiModelProperty("应用描述") private String description; - @ApiModelProperty("是否公开") - private boolean publicApp = false; - @NotBlank(message = "请输入应用提示词") @ApiModelProperty("应用提示词") private String prompt; diff --git a/src/main/java/cn/teammodel/model/entity/ai/ChatApp.java b/src/main/java/cn/teammodel/model/entity/ai/ChatApp.java index 1f728d9..31efd49 100644 --- a/src/main/java/cn/teammodel/model/entity/ai/ChatApp.java +++ b/src/main/java/cn/teammodel/model/entity/ai/ChatApp.java @@ -18,7 +18,7 @@ import lombok.EqualsAndHashCode; @JsonInclude(JsonInclude.Include.NON_NULL) public class ChatApp extends BaseItem { /** - * 租户 id ( public: 不允许修改 / telnet id) + * 租户 id (telnet id) */ private String schoolId; /** @@ -26,6 +26,24 @@ public class ChatApp extends BaseItem { */ private String icon; private String name; + private String bizType; + private String scope; + /** + * 语言 + */ + private String lang; + /** + * 题目类型 + */ + private String itemType; + /** + * 学段 + */ + private String period; + /** + * 学科 + */ + private String subject; private String description; /** * 面具提示词 diff --git a/src/main/java/cn/teammodel/repository/ChatAppRepository.java b/src/main/java/cn/teammodel/repository/ChatAppRepository.java index 8c06ab8..22a2775 100644 --- a/src/main/java/cn/teammodel/repository/ChatAppRepository.java +++ b/src/main/java/cn/teammodel/repository/ChatAppRepository.java @@ -13,8 +13,23 @@ import java.util.List; */ @Repository public interface ChatAppRepository extends CosmosRepository { - @Query("SELECT * FROM c WHERE c.code = 'ChatApp' AND (c.schoolId = @schoolId OR c.schoolId = 'public')") - List findAllByCodeAndSchoolId(String schoolId); + @Query("SELECT * FROM c WHERE c.code = 'ChatApp' " + + "AND (IS_NULL(@scope) or c.scope = @scope) " + + "AND (IS_NULL(@schoolId) or c.schoolId = @schoolId) " + + "AND (IS_NULL(@creatorId) or c.creatorId = @creatorId) " + + "AND (IS_NULL(@bizType) or c.bizType = @bizType) " + + "AND (IS_NULL(@lang) or c.lang = @lang) " + + "AND (IS_NULL(@itemType) or c.itemType = @itemType) " + + "AND (IS_NULL(@period) or c.period = @period) " + + "AND (IS_NULL(@subject) or c.subject = @subject)") + List findAllByConditon(String scope, + String schoolId, + String creatorId, + String bizType, + String lang, + String itemType, + String period, + String subject); @Query("SELECT c.id, c.schoolId, c.prompt FROM c WHERE c.code = 'ChatApp' AND c.id = @id") List findByAppId(String id); diff --git a/src/main/java/cn/teammodel/service/ChatAppService.java b/src/main/java/cn/teammodel/service/ChatAppService.java index 7180a94..cbfbf90 100644 --- a/src/main/java/cn/teammodel/service/ChatAppService.java +++ b/src/main/java/cn/teammodel/service/ChatAppService.java @@ -2,6 +2,7 @@ package cn.teammodel.service; import cn.teammodel.common.IdRequest; import cn.teammodel.model.dto.ai.CreateChatAppDto; +import cn.teammodel.model.dto.ai.SearchAppDto; import cn.teammodel.model.dto.ai.UpdateChatAppDto; import cn.teammodel.model.entity.ai.ChatApp; @@ -18,5 +19,5 @@ public interface ChatAppService { void deleteApp(IdRequest idRequest); - List listApp(); + List listApp(SearchAppDto searchAppDto); } diff --git a/src/main/java/cn/teammodel/service/impl/ChatAppServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatAppServiceImpl.java index b5024a0..973cf91 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatAppServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatAppServiceImpl.java @@ -1,10 +1,12 @@ package cn.teammodel.service.impl; +import cn.teammodel.common.ChatAppScopeEnum; import cn.teammodel.common.ErrorCode; import cn.teammodel.common.IdRequest; import cn.teammodel.common.PK; import cn.teammodel.config.exception.ServiceException; import cn.teammodel.model.dto.ai.CreateChatAppDto; +import cn.teammodel.model.dto.ai.SearchAppDto; import cn.teammodel.model.dto.ai.UpdateChatAppDto; import cn.teammodel.model.entity.User; import cn.teammodel.model.entity.ai.ChatApp; @@ -12,6 +14,8 @@ import cn.teammodel.repository.ChatAppRepository; import cn.teammodel.security.utils.SecurityUtil; import cn.teammodel.service.ChatAppService; import cn.teammodel.utils.RepositoryUtil; +import cn.teammodel.utils.StrUtil; +import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -30,17 +34,14 @@ public class ChatAppServiceImpl implements ChatAppService { @Override public ChatApp createApp(CreateChatAppDto createChatAppDto) { User user = SecurityUtil.getLoginUser(); - ChatApp newApp = new ChatApp(); - newApp.setName(createChatAppDto.getName()); - newApp.setIcon(createChatAppDto.getIcon()); - if (createChatAppDto.isPublicApp()) { - newApp.setSchoolId("public"); - } else { - newApp.setSchoolId(user.getSchoolId()); + String scope = createChatAppDto.getScope(); + if (!ChatAppScopeEnum.codes().contains(scope)) { + throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "scope参数不合法"); } - newApp.setDescription(createChatAppDto.getDescription()); - newApp.setPrompt(createChatAppDto.getPrompt()); + ChatApp newApp = new ChatApp(); + BeanUtils.copyProperties(createChatAppDto, newApp); newApp.setCreator(user.getName()); + newApp.setSchoolId(user.getSchoolId()); newApp.setCreatorId(user.getId()); newApp.setCreateTime(Instant.now().toEpochMilli()); newApp.setCode(PK.CHAT_APP); @@ -56,10 +57,7 @@ public class ChatAppServiceImpl implements ChatAppService { if (!userId.equals(chatApp.getCreatorId())) { throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您没有权限修改该应用"); } - chatApp.setIcon(updateChatAppDto.getIcon()); - chatApp.setName(updateChatAppDto.getName()); - chatApp.setDescription(updateChatAppDto.getDescription()); - chatApp.setPrompt(updateChatAppDto.getPrompt()); + BeanUtils.copyProperties(updateChatAppDto, chatApp); return chatAppRepostitory.save(chatApp); } @@ -76,10 +74,28 @@ public class ChatAppServiceImpl implements ChatAppService { } @Override - public List listApp() { + public List listApp(SearchAppDto searchAppDto) { + String scope = StrUtil.blankToNull(searchAppDto.getScope()); + String bizType = StrUtil.blankToNull(searchAppDto.getBizType()); + String lang = StrUtil.blankToNull(searchAppDto.getLang()); + String itemType = StrUtil.blankToNull(searchAppDto.getItemType()); + String period = StrUtil.blankToNull(searchAppDto.getPeriod()); + String subject = StrUtil.blankToNull(searchAppDto.getSubject()); + + if (!ChatAppScopeEnum.codes().contains(scope)) { + throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "scope参数不合法"); + } User user = SecurityUtil.getLoginUser(); - String schoolId = user.getSchoolId(); - List apps = chatAppRepostitory.findAllByCodeAndSchoolId(schoolId); + String schoolId = null; + String creatorId = null; + + if (scope.equals(ChatAppScopeEnum.PRIVATE.getCode())) { + creatorId = user.getId(); + } else if (scope.equals(ChatAppScopeEnum.SCHOOL.getCode())) { + schoolId = user.getSchoolId(); + } + + List apps = chatAppRepostitory.findAllByConditon(scope, schoolId, creatorId, bizType, lang, itemType, period, subject); return apps; } } diff --git a/src/main/java/cn/teammodel/utils/StrUtil.java b/src/main/java/cn/teammodel/utils/StrUtil.java new file mode 100644 index 0000000..c062645 --- /dev/null +++ b/src/main/java/cn/teammodel/utils/StrUtil.java @@ -0,0 +1,13 @@ +package cn.teammodel.utils; + +import org.apache.commons.lang3.StringUtils; + +/** + * @author winter + * @create 2024-02-01 16:18 + */ +public class StrUtil { + public static String blankToNull(String str) { + return StringUtils.isBlank(str) ? null : str; + } +}