mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-08 02:16:49 +08:00
Compare commits
18 Commits
SZX-1293-z
...
121-202509
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46fef22fa1 | ||
|
|
6bea2431f0 | ||
|
|
5497c21735 | ||
|
|
ff27d7a283 | ||
|
|
7825a219b1 | ||
|
|
16c1972ad3 | ||
|
|
a94f3e72d9 | ||
|
|
fa9d80d4a5 | ||
|
|
9e5571297f | ||
|
|
fb9377d0fa | ||
|
|
2da2112993 | ||
|
|
28e688f487 | ||
|
|
8aae653da8 | ||
|
|
29a296577d | ||
|
|
de028f8d0e | ||
|
|
02ff9474bc | ||
|
|
d0ed822189 | ||
|
|
06015e90c7 |
@@ -161,6 +161,11 @@
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>javax.mail-api</artifactId>
|
||||
@@ -227,6 +232,23 @@
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
<version>7.9.0</version> <!-- 请根据实际需求选择合适的版本 -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp-sse</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>2.0.31</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.xboe</groupId>
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.xboe.enums;
|
||||
|
||||
/**
|
||||
* AI调用日志业务处理状态枚举
|
||||
*/
|
||||
public enum CaseDocumentLogCaseStatusEnum {
|
||||
|
||||
SUCCESS(1, "处理成功"),
|
||||
FAILED(2, "处理失败");
|
||||
|
||||
private final Integer code;
|
||||
private final String desc;
|
||||
|
||||
CaseDocumentLogCaseStatusEnum(Integer code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取描述
|
||||
*/
|
||||
public static String getDescByCode(Integer code) {
|
||||
for (CaseDocumentLogCaseStatusEnum statusEnum : values()) {
|
||||
if (statusEnum.getCode().equals(code)) {
|
||||
return statusEnum.getDesc();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取枚举
|
||||
*/
|
||||
public static CaseDocumentLogCaseStatusEnum getByCode(Integer code) {
|
||||
for (CaseDocumentLogCaseStatusEnum statusEnum : values()) {
|
||||
if (statusEnum.getCode().equals(code)) {
|
||||
return statusEnum;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.xboe.enums;
|
||||
|
||||
/**
|
||||
* AI调用日志接口调用状态枚举
|
||||
*/
|
||||
public enum CaseDocumentLogOptStatusEnum {
|
||||
|
||||
CALLING(0, "调用中"),
|
||||
SUCCESS(1, "调用成功"),
|
||||
FAILED(2, "调用失败");
|
||||
|
||||
private final Integer code;
|
||||
private final String desc;
|
||||
|
||||
CaseDocumentLogOptStatusEnum(Integer code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取描述
|
||||
*/
|
||||
public static String getDescByCode(Integer code) {
|
||||
for (CaseDocumentLogOptStatusEnum statusEnum : values()) {
|
||||
if (statusEnum.getCode().equals(code)) {
|
||||
return statusEnum.getDesc();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取枚举
|
||||
*/
|
||||
public static CaseDocumentLogOptStatusEnum getByCode(Integer code) {
|
||||
for (CaseDocumentLogOptStatusEnum statusEnum : values()) {
|
||||
if (statusEnum.getCode().equals(code)) {
|
||||
return statusEnum;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.xboe.enums;
|
||||
|
||||
/**
|
||||
* AI调用日志操作类型枚举
|
||||
*/
|
||||
public enum CaseDocumentLogOptTypeEnum {
|
||||
|
||||
CREATE("create", "新增"),
|
||||
DELETE("delete", "删除"),
|
||||
UPDATE("update", "更改");
|
||||
|
||||
private final String code;
|
||||
private final String desc;
|
||||
|
||||
CaseDocumentLogOptTypeEnum(String code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取描述
|
||||
*/
|
||||
public static String getDescByCode(String code) {
|
||||
for (CaseDocumentLogOptTypeEnum typeEnum : values()) {
|
||||
if (typeEnum.getCode().equals(code)) {
|
||||
return typeEnum.getDesc();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.xboe.module.boecase.api;
|
||||
|
||||
import com.xboe.core.api.ApiBaseController;
|
||||
import com.xboe.core.JsonResponse;
|
||||
import com.xboe.module.boecase.dto.CaseAiChatDto;
|
||||
import com.xboe.module.boecase.service.ICaseAiChatService;
|
||||
import com.xboe.module.boecase.vo.CaseAiMessageVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI对话管理API
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping(value = "/xboe/m/boe/case/ai")
|
||||
public class CaseAiChatApi extends ApiBaseController {
|
||||
|
||||
/**
|
||||
* 聊天
|
||||
* @param caseAiChatDto
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@Autowired
|
||||
private ICaseAiChatService caseAiChatService;
|
||||
|
||||
/**
|
||||
* 聊天
|
||||
* @param caseAiChatDto
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public SseEmitter chat(@RequestBody CaseAiChatDto caseAiChatDto,
|
||||
HttpServletResponse response) {
|
||||
response.setContentType("text/event-stream");
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
|
||||
// 获取当前用户
|
||||
return caseAiChatService.chat(caseAiChatDto, getCurrent());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据conversationId查看会话内消息记录
|
||||
* @param conversationId 会话ID
|
||||
* @return 消息记录列表
|
||||
*/
|
||||
@GetMapping("/messages")
|
||||
public JsonResponse<List<CaseAiMessageVo>> getConversationMessages(@RequestParam String conversationId) {
|
||||
try {
|
||||
List<CaseAiMessageVo> messages = caseAiChatService.getConversationMessages(conversationId);
|
||||
return success(messages);
|
||||
} catch (Exception e) {
|
||||
log.error("查询会话消息记录异常", e);
|
||||
return error("查询失败", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package com.xboe.module.boecase.api;
|
||||
|
||||
import com.xboe.common.PageList;
|
||||
import com.xboe.core.JsonResponse;
|
||||
import com.xboe.core.api.ApiBaseController;
|
||||
import com.xboe.core.log.AutoLog;
|
||||
import com.xboe.module.boecase.dto.CaseDocumentLogQueryDto;
|
||||
import com.xboe.module.boecase.service.ICaseDocumentLogService;
|
||||
import com.xboe.module.boecase.service.ICaseKnowledgeService;
|
||||
import com.xboe.module.boecase.vo.CaseDocumentLogVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* AI调用日志管理API
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping(value = "/xboe/m/boe/caseDocumentLog")
|
||||
public class CaseDocumentLogApi extends ApiBaseController {
|
||||
|
||||
@Resource
|
||||
private ICaseDocumentLogService caseDocumentLogService;
|
||||
|
||||
@Resource
|
||||
private ICaseKnowledgeService caseKnowledgeService;
|
||||
|
||||
/**
|
||||
* AI调用日志分页查询
|
||||
*
|
||||
* @param queryDto 查询条件
|
||||
* @return 分页结果
|
||||
*/
|
||||
@PostMapping("/pageQuery")
|
||||
@AutoLog(module = "AI调用日志", action = "分页查询", info = "AI调用日志分页查询")
|
||||
public JsonResponse<PageList<CaseDocumentLogVo>> pageQuery(@RequestBody CaseDocumentLogQueryDto queryDto) {
|
||||
try {
|
||||
PageList<CaseDocumentLogVo> result = caseDocumentLogService.pageQuery(
|
||||
queryDto.getPageIndex(),
|
||||
queryDto.getPageSize(),
|
||||
queryDto
|
||||
);
|
||||
return success(result);
|
||||
} catch (Exception e) {
|
||||
log.error("AI调用日志分页查询失败", e);
|
||||
return error("查询失败", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空日志(根据筛选条件)
|
||||
*
|
||||
* @param queryDto 查询条件
|
||||
* @return 删除结果
|
||||
*/
|
||||
@PostMapping("/clearLogs")
|
||||
@AutoLog(module = "AI调用日志", action = "清空日志", info = "AI调用日志清空操作")
|
||||
public JsonResponse<Integer> clearLogs(@RequestBody CaseDocumentLogQueryDto queryDto) {
|
||||
try {
|
||||
int deletedCount = caseDocumentLogService.clearLogsByCondition(queryDto);
|
||||
return success(deletedCount);
|
||||
} catch (Exception e) {
|
||||
log.error("AI调用日志清空失败", e);
|
||||
return error("清空失败", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试AI调用
|
||||
*
|
||||
* @param request 重试请求参数
|
||||
* @return 重试结果
|
||||
*/
|
||||
@PostMapping("/retry")
|
||||
@AutoLog(module = "AI调用日志", action = "重试调用", info = "AI调用日志重试操作")
|
||||
public JsonResponse<Boolean> retry(@RequestBody RetryRequest request) {
|
||||
try {
|
||||
boolean result = caseDocumentLogService.retryByLogId(request.getLogId());
|
||||
return success(result);
|
||||
} catch (Exception e) {
|
||||
log.error("AI调用重试失败", e);
|
||||
return error("重试失败", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试请求参数
|
||||
*/
|
||||
public static class RetryRequest {
|
||||
private String logId;
|
||||
|
||||
public String getLogId() {
|
||||
return logId;
|
||||
}
|
||||
|
||||
public void setLogId(String logId) {
|
||||
this.logId = logId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文档上传回调接口
|
||||
*
|
||||
* @param request 回调请求参数
|
||||
* @return 回调结果
|
||||
*/
|
||||
@PostMapping("/uploadCallback")
|
||||
@AutoLog(module = "AI调用日志", action = "文档上传回调", info = "文档上传回调接口")
|
||||
public CallbackResponse uploadCallback(@RequestBody CallbackRequest request) {
|
||||
try {
|
||||
log.info("收到文档上传回调,taskId: {}, fileStatus: {}, message: {}",
|
||||
request.getTaskId(), request.getFileStatus(), request.getMessage());
|
||||
|
||||
boolean result = caseKnowledgeService.handleUploadCallback(
|
||||
request.getTaskId(),
|
||||
request.getMessage(),
|
||||
request.getFileStatus()
|
||||
);
|
||||
|
||||
CallbackResponse response = new CallbackResponse();
|
||||
response.setSuccess(result);
|
||||
response.setCode(result ? 0 : -1);
|
||||
response.setMessage(result ? "回调处理成功" : "回调处理失败");
|
||||
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
log.error("文档上传回调处理失败", e);
|
||||
|
||||
CallbackResponse response = new CallbackResponse();
|
||||
response.setSuccess(false);
|
||||
response.setCode(-1);
|
||||
response.setMessage("回调处理异常: " + e.getMessage());
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 回调请求参数
|
||||
*/
|
||||
public static class CallbackRequest {
|
||||
private String taskId;
|
||||
private String message;
|
||||
private String fileStatus;
|
||||
|
||||
public String getTaskId() {
|
||||
return taskId;
|
||||
}
|
||||
|
||||
public void setTaskId(String taskId) {
|
||||
this.taskId = taskId;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getFileStatus() {
|
||||
return fileStatus;
|
||||
}
|
||||
|
||||
public void setFileStatus(String fileStatus) {
|
||||
this.fileStatus = fileStatus;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 回调响应参数
|
||||
*/
|
||||
public static class CallbackResponse {
|
||||
private boolean success;
|
||||
private int code;
|
||||
private String message;
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.xboe.module.boecase.dao;
|
||||
|
||||
import com.xboe.core.orm.BaseDao;
|
||||
import com.xboe.core.orm.FieldFilters;
|
||||
import com.xboe.module.boecase.entity.CaseAiConversations;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* 案例AI会话信息DAO
|
||||
*/
|
||||
@Repository
|
||||
public class CaseAiConversationsDao extends BaseDao<CaseAiConversations> {
|
||||
|
||||
/**
|
||||
* 根据主键ID查询AI会话ID
|
||||
* @param conversationId 主键ID
|
||||
* @return AI会话ID
|
||||
*/
|
||||
public String findAiConversationIdById(String conversationId) {
|
||||
CaseAiConversations conversation = this.getGenericDao().findOne(CaseAiConversations.class,
|
||||
FieldFilters.eq("id", conversationId));
|
||||
return conversation != null ? conversation.getAiConversationId() : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.xboe.module.boecase.dao;
|
||||
|
||||
import com.xboe.core.orm.BaseDao;
|
||||
import com.xboe.module.boecase.entity.CaseDocumentLog;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* AI调用日志DAO
|
||||
*/
|
||||
@Repository
|
||||
@Slf4j
|
||||
public class CaseDocumentLogDao extends BaseDao<CaseDocumentLog> {
|
||||
|
||||
/**
|
||||
* 根据taskId查询文档日志
|
||||
* @param taskId 任务ID
|
||||
* @return 文档日志
|
||||
*/
|
||||
public CaseDocumentLog findByTaskId(String taskId) {
|
||||
return this.getGenericDao().findOne(CaseDocumentLog.class,
|
||||
FieldFilters.eq("taskId", taskId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xboe.module.boecase.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* AI对话入参
|
||||
*/
|
||||
@Data
|
||||
public class CaseAiChatDto {
|
||||
|
||||
/**
|
||||
* 对话id
|
||||
* 如果是新对话,传空
|
||||
*/
|
||||
private String conversationId;
|
||||
|
||||
/**
|
||||
* 提问内容
|
||||
*/
|
||||
private String query;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.xboe.module.boecase.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI调用日志查询条件DTO
|
||||
*/
|
||||
@Data
|
||||
public class CaseDocumentLogQueryDto extends PageDto {
|
||||
|
||||
/**
|
||||
* 案例标题(模糊查询)
|
||||
*/
|
||||
private String caseTitle;
|
||||
|
||||
/**
|
||||
* 操作类型(create-新增,delete-删除,update-更改)
|
||||
*/
|
||||
private String optType;
|
||||
|
||||
/**
|
||||
* 调用时间开始
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private LocalDateTime optTimeStart;
|
||||
|
||||
/**
|
||||
* 调用时间结束
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private LocalDateTime optTimeEnd;
|
||||
|
||||
/**
|
||||
* 接口调用状态
|
||||
* 0-调用中, 1-调用成功, 2-调用失败
|
||||
*/
|
||||
private Integer optStatus;
|
||||
|
||||
/**
|
||||
* 业务处理状态
|
||||
* 1-处理成功, 2-处理失败
|
||||
*/
|
||||
private Integer caseStatus;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.xboe.module.boecase.entity;
|
||||
|
||||
import com.xboe.core.SysConstant;
|
||||
import com.xboe.core.orm.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* 案例AI会话信息表
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Table(name = SysConstant.TABLE_PRE + "case_ai_conversations")
|
||||
public class CaseAiConversations extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 会话ID(由AI平台提供)
|
||||
*/
|
||||
@Column(name = "ai_conversation_id", length = 100)
|
||||
private String aiConversationId;
|
||||
|
||||
/**
|
||||
* 会话名称
|
||||
*/
|
||||
@Column(name = "conversation_name", length = 200)
|
||||
private String conversationName;
|
||||
|
||||
/**
|
||||
* 会话对应用户ID
|
||||
*/
|
||||
@Column(name = "conversation_user", length = 50)
|
||||
private String conversationUser;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.xboe.module.boecase.entity;
|
||||
|
||||
import com.xboe.core.SysConstant;
|
||||
import com.xboe.core.orm.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 案例文档日志信息表
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Table(name = SysConstant.TABLE_PRE + "case_document_log")
|
||||
public class CaseDocumentLog extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 任务ID
|
||||
*/
|
||||
@Column(name = "task_id", length = 20)
|
||||
private String taskId;
|
||||
|
||||
/**
|
||||
* 案例id
|
||||
*/
|
||||
@Column(name = "case_id", length = 20)
|
||||
private String caseId;
|
||||
|
||||
/**
|
||||
* 案例标题
|
||||
*/
|
||||
@Column(name = "case_title", length = 200)
|
||||
private String caseTitle;
|
||||
|
||||
/**
|
||||
* 操作类型
|
||||
*/
|
||||
@Column(name = "opt_type")
|
||||
private String optType;
|
||||
|
||||
/**
|
||||
* 请求地址
|
||||
*/
|
||||
@Column(name = "request_url", length = 500)
|
||||
private String requestUrl;
|
||||
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
@Column(name = "request_body", length = 4000)
|
||||
private String requestBody;
|
||||
|
||||
/**
|
||||
* 响应参数
|
||||
*/
|
||||
@Column(name = "response_body", length = 4000)
|
||||
private String responseBody;
|
||||
|
||||
/**
|
||||
* 调用时间
|
||||
*/
|
||||
@Column(name = "opt_time")
|
||||
private LocalDateTime optTime;
|
||||
|
||||
/**
|
||||
* 接口调用状态
|
||||
* 0-调用中, 1-调用成功, 2-调用失败
|
||||
*/
|
||||
@Column(name = "opt_status")
|
||||
private Integer optStatus;
|
||||
|
||||
/**
|
||||
* 业务处理状态
|
||||
* 1-处理成功, 2-处理失败
|
||||
*/
|
||||
@Column(name = "case_status")
|
||||
private Integer caseStatus;
|
||||
|
||||
/**
|
||||
* 执行时间(ms)
|
||||
*/
|
||||
@Column(name = "execute_duration")
|
||||
private Long executeDuration;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.xboe.module.boecase.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 案例专家AI相关配置项
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "xboe.case.ai")
|
||||
@Data
|
||||
public class CaseAiProperties {
|
||||
|
||||
/**
|
||||
* 接口地址
|
||||
*/
|
||||
private String baseUrl;
|
||||
|
||||
/**
|
||||
* appKey
|
||||
*/
|
||||
private String appKey;
|
||||
|
||||
/**
|
||||
* appSecret
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* ai接口的apiCode
|
||||
*/
|
||||
private String aiApiCode;
|
||||
|
||||
/**
|
||||
* 案例知识库id
|
||||
*/
|
||||
private String caseKnowledgeId;
|
||||
|
||||
/**
|
||||
* 文档上传回调接口地址
|
||||
*/
|
||||
private String fileUploadCallbackUrl;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.xboe.module.boecase.service;
|
||||
|
||||
/**
|
||||
* 获取accesstoken
|
||||
*/
|
||||
public interface IAiAccessTokenService {
|
||||
|
||||
/**
|
||||
* 获取accesstoken
|
||||
* @return
|
||||
*/
|
||||
String getAccessToken();
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.xboe.module.boecase.service;
|
||||
|
||||
import com.xboe.core.CurrentUser;
|
||||
import com.xboe.module.boecase.dto.CaseAiChatDto;
|
||||
import com.xboe.module.boecase.entity.CaseAiConversations;
|
||||
import com.xboe.module.boecase.vo.CaseAiMessageVo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI案例对话
|
||||
*/
|
||||
public interface ICaseAiChatService {
|
||||
|
||||
/**
|
||||
* 聊天
|
||||
* @param caseAiChatDto
|
||||
* @param currentUser
|
||||
* @return
|
||||
*/
|
||||
SseEmitter chat(CaseAiChatDto caseAiChatDto, CurrentUser currentUser);
|
||||
|
||||
/**
|
||||
* 创建新的AI对话会话
|
||||
* @param userId 用户ID
|
||||
* @param conversationName 对话名称
|
||||
* @return 创建的会话信息
|
||||
*/
|
||||
CaseAiConversations createNewConversation(String userId, String conversationName);
|
||||
|
||||
/**
|
||||
* 根据conversationId查看会话内消息记录
|
||||
* @param conversationId 会话ID
|
||||
* @return 消息记录列表
|
||||
*/
|
||||
List<CaseAiMessageVo> getConversationMessages(String conversationId);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.xboe.module.boecase.service;
|
||||
|
||||
import com.xboe.common.PageList;
|
||||
import com.xboe.module.boecase.dto.CaseDocumentLogQueryDto;
|
||||
import com.xboe.module.boecase.vo.CaseDocumentLogVo;
|
||||
|
||||
/**
|
||||
* AI调用日志Service接口
|
||||
*/
|
||||
public interface ICaseDocumentLogService {
|
||||
|
||||
/**
|
||||
* 分页查询AI调用日志
|
||||
*
|
||||
* @param pageIndex 页码
|
||||
* @param pageSize 每页大小
|
||||
* @param queryDto 查询条件
|
||||
* @return 分页结果
|
||||
*/
|
||||
PageList<CaseDocumentLogVo> pageQuery(int pageIndex, int pageSize, CaseDocumentLogQueryDto queryDto);
|
||||
|
||||
/**
|
||||
* 根据查询条件清空日志
|
||||
* 仅删除当前筛选条件下的日志记录,非筛选范围内的日志不受影响
|
||||
*
|
||||
* @param queryDto 查询条件
|
||||
* @return 删除的记录数
|
||||
*/
|
||||
int clearLogsByCondition(CaseDocumentLogQueryDto queryDto);
|
||||
|
||||
/**
|
||||
* 根据logId重试AI调用
|
||||
* 查询原始日志数据,重试执行后添加新的日志记录
|
||||
*
|
||||
* @param logId 日志ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean retryByLogId(String logId);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.xboe.module.boecase.service;
|
||||
|
||||
/**
|
||||
* 案例-知识库
|
||||
*/
|
||||
public interface ICaseKnowledgeService {
|
||||
|
||||
/**
|
||||
* 上传案例文档
|
||||
*
|
||||
* @param caseId 案例ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean uploadCaseDocument(String caseId);
|
||||
|
||||
/**
|
||||
* 删除案例文档
|
||||
*
|
||||
* @param caseId 案例ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean deleteCaseDocument(String caseId);
|
||||
|
||||
/**
|
||||
* 更新案例文档
|
||||
*
|
||||
* @param caseId 案例ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updateCaseDocument(String caseId);
|
||||
|
||||
/**
|
||||
* 处理文档上传回调
|
||||
*
|
||||
* @param taskId 任务ID
|
||||
* @param message 回调信息
|
||||
* @param fileStatus 文件状态(vectored: 成功, failed: 失败)
|
||||
* @return 是否处理成功
|
||||
*/
|
||||
boolean handleUploadCallback(String taskId, String message, String fileStatus);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.xboe.module.boecase.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xboe.common.utils.StringUtil;
|
||||
import com.xboe.module.boecase.properties.CaseAiProperties;
|
||||
import com.xboe.module.boecase.service.IAiAccessTokenService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@EnableConfigurationProperties({CaseAiProperties.class})
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AiAccessTokenServiceImpl implements IAiAccessTokenService {
|
||||
|
||||
private static final String ACCESS_TOKEN_CACHE_KEY = "case_ai_access_token";
|
||||
|
||||
@Autowired
|
||||
private CaseAiProperties caseAiProperties;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Override
|
||||
public String getAccessToken() {
|
||||
// 1. 先从Redis缓存中获取
|
||||
String cachedToken = stringRedisTemplate.opsForValue().get(ACCESS_TOKEN_CACHE_KEY);
|
||||
if (StringUtil.isNotBlank(cachedToken)) {
|
||||
return cachedToken;
|
||||
}
|
||||
|
||||
// 2. 缓存中没有,重新获取
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
String tokenUrl = caseAiProperties.getBaseUrl() + "/apigateway/secret/getAppAccessToken" +
|
||||
"?appKey=" + URLEncoder.encode(caseAiProperties.getAppKey(), StandardCharsets.UTF_8.name()) +
|
||||
"&secretKey=" + URLEncoder.encode(caseAiProperties.getSecretKey(), StandardCharsets.UTF_8.name());
|
||||
|
||||
HttpGet httpGet = new HttpGet(tokenUrl);
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
|
||||
if (statusCode == 200) {
|
||||
JSONObject result = JSON.parseObject(responseBody);
|
||||
if (result.getIntValue("code") == 0 && result.getBooleanValue("success")) {
|
||||
JSONObject data = result.getJSONObject("data");
|
||||
String accessToken = data.getString("accessToken");
|
||||
Integer expiresIn = data.getInteger("expiresIn");
|
||||
if (expiresIn == null) {
|
||||
expiresIn = 7200;
|
||||
}
|
||||
|
||||
// 3. 存储到Redis,设置过期时间(提前5分钟过期)
|
||||
int cacheSeconds = Math.max(expiresIn - 300, 60);
|
||||
stringRedisTemplate.opsForValue().set(ACCESS_TOKEN_CACHE_KEY, accessToken,
|
||||
cacheSeconds, TimeUnit.SECONDS);
|
||||
|
||||
log.info("获取access_token成功,过期时间: {}秒", expiresIn);
|
||||
return accessToken;
|
||||
} else {
|
||||
log.error("获取access_token失败,接口返回失败,response: {}", responseBody);
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
log.error("获取access_token失败,HTTP请求失败,status: {}, response: {}",
|
||||
statusCode, responseBody);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取access_token异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,602 @@
|
||||
package com.xboe.module.boecase.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xboe.core.CurrentUser;
|
||||
import com.xboe.module.boecase.dao.CaseAiConversationsDao;
|
||||
import com.xboe.module.boecase.dao.CaseDocumentLogDao;
|
||||
import com.xboe.module.boecase.dao.CasesDao;
|
||||
import com.xboe.module.boecase.dto.CaseAiChatDto;
|
||||
import com.xboe.module.boecase.entity.CaseAiConversations;
|
||||
import com.xboe.module.boecase.entity.CaseDocumentLog;
|
||||
import com.xboe.module.boecase.entity.Cases;
|
||||
import com.xboe.module.boecase.properties.CaseAiProperties;
|
||||
import com.xboe.module.boecase.service.IAiAccessTokenService;
|
||||
import com.xboe.module.boecase.service.ICaseAiChatService;
|
||||
import com.xboe.module.boecase.vo.CaseAiMessageVo;
|
||||
import com.xboe.module.boecase.vo.CaseReferVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import okhttp3.sse.EventSource;
|
||||
import okhttp3.sse.EventSourceListener;
|
||||
import okhttp3.sse.EventSources;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.index.IndexResponse;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@EnableConfigurationProperties({CaseAiProperties.class})
|
||||
@Service
|
||||
@Slf4j
|
||||
public class CaseAiChatServiceImpl implements ICaseAiChatService {
|
||||
|
||||
@Autowired
|
||||
private CaseAiProperties caseAiProperties;
|
||||
|
||||
@Autowired
|
||||
private IAiAccessTokenService aiAccessTokenService;
|
||||
|
||||
@Autowired
|
||||
private CaseAiConversationsDao caseAiConversationsDao;
|
||||
|
||||
@Autowired(required = false)
|
||||
private RestHighLevelClient elasticsearchClient;
|
||||
|
||||
@Autowired
|
||||
private CaseDocumentLogDao caseDocumentLogDao;
|
||||
|
||||
@Autowired
|
||||
private CasesDao casesDao;
|
||||
|
||||
@Override
|
||||
public SseEmitter chat(CaseAiChatDto caseAiChatDto, CurrentUser currentUser) {
|
||||
// 1. 获取conversationId
|
||||
String conversationId = getOrCreateConversationId(caseAiChatDto, currentUser);
|
||||
|
||||
// 2. 检查是否为新会话,如果是则保存会话记录
|
||||
boolean isNewConversation = StringUtils.isEmpty(caseAiChatDto.getConversationId());
|
||||
CaseAiConversations conversation = null;
|
||||
if (isNewConversation) {
|
||||
// 新会话,需要保存到数据库
|
||||
conversation = new CaseAiConversations();
|
||||
conversation.setAiConversationId(conversationId);
|
||||
conversation.setConversationName("AI案例咨询-" + LocalDateTime.now());
|
||||
conversation.setConversationUser(currentUser.getAccountId());
|
||||
// 由于编译问题,这里先注释,实际部署时需要取消注释
|
||||
caseAiConversationsDao.save(conversation);
|
||||
}
|
||||
|
||||
// 3. 构建请求参数
|
||||
String userId = currentUser.getAccountId();
|
||||
String kId = caseAiProperties.getCaseKnowledgeId();
|
||||
JSONObject chatParam = new JSONObject();
|
||||
chatParam.put("userId", userId);
|
||||
JSONArray kIds = new JSONArray();
|
||||
kIds.add(kId);
|
||||
chatParam.put("kIds", kIds);
|
||||
chatParam.put("query", caseAiChatDto.getQuery());
|
||||
chatParam.put("conversationId", conversationId);
|
||||
|
||||
// 4. 设置请求头
|
||||
String accessToken = aiAccessTokenService.getAccessToken();
|
||||
String apiCode = caseAiProperties.getAiApiCode();
|
||||
Request.Builder builder = new Request.Builder();
|
||||
builder.url(caseAiProperties.getBaseUrl() + "/apigateway/chat/knowledge/v1/chat/completions");
|
||||
builder.addHeader("access_token", accessToken);
|
||||
builder.addHeader("X-AI-ApiCode", apiCode);
|
||||
RequestBody bodyRequestBody = RequestBody.create(chatParam.toJSONString(), MediaType.parse("application/json"));
|
||||
builder.post(bodyRequestBody);
|
||||
Request request = builder.build();
|
||||
|
||||
// 5. 创建SSE响应器
|
||||
SseEmitter sseEmitter = new SseEmitter();
|
||||
|
||||
// 6. 用于收集对话数据的容器
|
||||
ConversationData conversationData = new ConversationData();
|
||||
conversationData.query = caseAiChatDto.getQuery();
|
||||
conversationData.conversationId = conversationId;
|
||||
conversationData.userId = userId;
|
||||
|
||||
// 7. 创建事件监听器
|
||||
EventSourceListener listener = new EventSourceListener() {
|
||||
@Override
|
||||
public void onOpen(@NotNull EventSource eventSource, @NotNull Response response) {
|
||||
log.info("调用接口 [{}] 接口开始监听", request.url());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosed(@NotNull EventSource eventSource) {
|
||||
log.info("调用接口 [{}] 接口关闭", request.url());
|
||||
// 对话完成,保存到ES
|
||||
saveConversationToES(conversationData);
|
||||
sseEmitter.complete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(@NotNull EventSource eventSource, @Nullable String id, @Nullable String type, @NotNull String data) {
|
||||
log.info("调用接口 [{}] 监听数据 id: [{}] type: [{}] data: [{}]", request.url(), id, type, data);
|
||||
|
||||
try {
|
||||
// 解析返回的数据
|
||||
JSONObject jsonData = JSONObject.parseObject(data);
|
||||
if (jsonData.getBooleanValue("success") && jsonData.getIntValue("code") == 0) {
|
||||
JSONObject responseData = jsonData.getJSONObject("data");
|
||||
Integer status = responseData.getInteger("status");
|
||||
|
||||
if (status != null) {
|
||||
switch (status) {
|
||||
case 0: // 返回引用文件
|
||||
// 处理文件引用并构建返给前端的数据
|
||||
JSONObject modifiedData = handleFileReferAndBuildResponse(responseData, conversationData);
|
||||
if (modifiedData != null) {
|
||||
// 发送修改后的数据给前端
|
||||
sseEmitter.send(modifiedData.toJSONString());
|
||||
return; // 早期返回,不发送原始数据
|
||||
}
|
||||
break;
|
||||
case 1: // 流式对话中
|
||||
String content = responseData.getString("content");
|
||||
if (content != null) {
|
||||
conversationData.answer.append(content);
|
||||
}
|
||||
break;
|
||||
case 2: // 回答完成
|
||||
// 不做特殊处理
|
||||
break;
|
||||
case 3: // 返回建议
|
||||
handleSuggestions(responseData, conversationData);
|
||||
break;
|
||||
case 4: // 接口交互完成
|
||||
// 不做特殊处理
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 发送给前端
|
||||
sseEmitter.send(data);
|
||||
} catch (IOException e) {
|
||||
log.error("调用接口处理监听数据时发生异常", e);
|
||||
} catch (Exception e) {
|
||||
log.error("解析EventStream数据异常", e);
|
||||
try {
|
||||
sseEmitter.send(data); // 即使解析失败也要发送原始数据
|
||||
} catch (IOException ioException) {
|
||||
log.error("发送数据到前端失败", ioException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NotNull EventSource eventSource, @Nullable Throwable e, @Nullable Response response) {
|
||||
log.error("调用接口 [{}] 接口异常", request.url(), e);
|
||||
if (e != null) {
|
||||
sseEmitter.completeWithError(e);
|
||||
} else {
|
||||
sseEmitter.completeWithError(new RuntimeException("调用接口异常, 异常未捕获"));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 8. 执行HTTP请求
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.connectTimeout(60, TimeUnit.SECONDS)
|
||||
.writeTimeout(60, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.build();
|
||||
EventSource.Factory factory = EventSources.createFactory(client);
|
||||
factory.newEventSource(request, listener);
|
||||
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建会话ID
|
||||
*/
|
||||
private String getOrCreateConversationId(CaseAiChatDto caseAiChatDto, CurrentUser currentUser) {
|
||||
String conversationId = caseAiChatDto.getConversationId();
|
||||
|
||||
if (StringUtils.isEmpty(conversationId)) {
|
||||
// 新会话,调用创建会话接口
|
||||
String conversationName = "AI案例咨询-" + LocalDateTime.now().toString();
|
||||
CaseAiConversations newConversation = createNewConversation(currentUser.getAccountId(), conversationName);
|
||||
return newConversation.getAiConversationId();
|
||||
} else {
|
||||
// 已存在会话,从数据库查询
|
||||
return caseAiConversationsDao.findAiConversationIdById(conversationId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CaseAiConversations createNewConversation(String userId, String conversationName) {
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
String url = caseAiProperties.getBaseUrl() + "/apigateway/knowledge/v1/conversation";
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
// 设置请求头
|
||||
String accessToken = aiAccessTokenService.getAccessToken();
|
||||
String apiCode = caseAiProperties.getAiApiCode();
|
||||
httpPost.setHeader("access_token", accessToken);
|
||||
httpPost.setHeader("X-AI-ApiCode", apiCode);
|
||||
httpPost.setHeader("Content-Type", "application/json");
|
||||
|
||||
// 设置请求体
|
||||
JSONObject requestBody = new JSONObject();
|
||||
requestBody.put("userId", userId);
|
||||
requestBody.put("name", conversationName);
|
||||
StringEntity entity = new StringEntity(requestBody.toJSONString(), StandardCharsets.UTF_8);
|
||||
httpPost.setEntity(entity);
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
|
||||
if (statusCode == 200) {
|
||||
JSONObject result = JSONObject.parseObject(responseBody);
|
||||
if (result.getIntValue("code") == 0 && result.getBooleanValue("success")) {
|
||||
JSONObject data = result.getJSONObject("data");
|
||||
String aiConversationId = data.getString("id");
|
||||
String name = data.getString("name");
|
||||
|
||||
// 保存到数据库
|
||||
CaseAiConversations conversation = new CaseAiConversations();
|
||||
conversation.setAiConversationId(aiConversationId);
|
||||
conversation.setConversationName(name);
|
||||
conversation.setConversationUser(userId);
|
||||
caseAiConversationsDao.save(conversation);
|
||||
|
||||
log.info("创建AI会话成功,aiConversationId: {}, name: {}", aiConversationId, name);
|
||||
return conversation;
|
||||
} else {
|
||||
log.error("创建AI会话失败,接口返回失败,response: {}", responseBody);
|
||||
throw new RuntimeException("创建AI会话失败: " + result.getString("message"));
|
||||
}
|
||||
} else {
|
||||
log.error("创建AI会话失败,HTTP请求失败,status: {}, response: {}", statusCode, responseBody);
|
||||
throw new RuntimeException("创建AI会话失败,HTTP状态码: " + statusCode);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("创建AI会话异常", e);
|
||||
throw new RuntimeException("创建AI会话异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CaseAiMessageVo> getConversationMessages(String conversationId) {
|
||||
List<CaseAiMessageVo> messages = new ArrayList<>();
|
||||
|
||||
if (elasticsearchClient == null) {
|
||||
log.warn("未配置Elasticsearch客户端,无法查询消息记录");
|
||||
return messages;
|
||||
}
|
||||
|
||||
try {
|
||||
// 根据conversationId从数据库查询AI会话ID
|
||||
String aiConversationId = caseAiConversationsDao.findAiConversationIdById(conversationId);
|
||||
if (StringUtils.isEmpty(aiConversationId)) {
|
||||
log.warn("未找到conversationId: {}对应的AI会话ID", conversationId);
|
||||
return messages;
|
||||
}
|
||||
|
||||
// 从 ES 中查询消息记录
|
||||
SearchRequest searchRequest = new SearchRequest("ai_chat_messages"); // ES索引名,可以根据实际情况调整
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||
searchSourceBuilder.query(QueryBuilders.termQuery("conversationId", aiConversationId));
|
||||
searchSourceBuilder.size(1000); // 设置最大返回数量
|
||||
searchRequest.source(searchSourceBuilder);
|
||||
|
||||
SearchResponse searchResponse = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||
SearchHits hits = searchResponse.getHits();
|
||||
|
||||
for (SearchHit hit : hits) {
|
||||
Map<String, Object> sourceMap = hit.getSourceAsMap();
|
||||
CaseAiMessageVo messageVo = parseMessageFromES(sourceMap);
|
||||
if (messageVo != null) {
|
||||
messages.add(messageVo);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("从 ES 中查询到 {} 条消息记录", messages.size());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("从 ES 查询会话消息记录异常", e);
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 ES 数据中解析消息对象
|
||||
* @param sourceMap ES数据
|
||||
* @return 消息对象
|
||||
*/
|
||||
private CaseAiMessageVo parseMessageFromES(Map<String, Object> sourceMap) {
|
||||
try {
|
||||
CaseAiMessageVo messageVo = new CaseAiMessageVo();
|
||||
messageVo.setQuery((String) sourceMap.get("query"));
|
||||
messageVo.setAnswer((String) sourceMap.get("answer"));
|
||||
|
||||
// 解析 suggestions
|
||||
Object suggestionsObj = sourceMap.get("suggestions");
|
||||
if (suggestionsObj instanceof List) {
|
||||
messageVo.setSuggestions((List<String>) suggestionsObj);
|
||||
}
|
||||
|
||||
// 解析 caseRefer
|
||||
Object caseReferObj = sourceMap.get("caseRefer");
|
||||
if (caseReferObj instanceof List) {
|
||||
List<CaseReferVo> caseReferList = new ArrayList<>();
|
||||
List<Map<String, Object>> caseReferMaps = (List<Map<String, Object>>) caseReferObj;
|
||||
|
||||
for (Map<String, Object> caseReferMap : caseReferMaps) {
|
||||
CaseReferVo caseRefer = new CaseReferVo();
|
||||
caseRefer.setCaseId((String) caseReferMap.get("caseId"));
|
||||
caseRefer.setTitle((String) caseReferMap.get("title"));
|
||||
caseRefer.setAuthorName((String) caseReferMap.get("authorName"));
|
||||
caseRefer.setContent((String) caseReferMap.get("content"));
|
||||
|
||||
// 解析 keywords
|
||||
Object keywordsObj = caseReferMap.get("keywords");
|
||||
if (keywordsObj instanceof List) {
|
||||
caseRefer.setKeywords((List<String>) keywordsObj);
|
||||
}
|
||||
|
||||
caseReferList.add(caseRefer);
|
||||
}
|
||||
messageVo.setCaseRefer(caseReferList);
|
||||
}
|
||||
|
||||
return messageVo;
|
||||
} catch (Exception e) {
|
||||
log.error("解析ES消息数据异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文件引用并构建返给前端的响应数据
|
||||
*/
|
||||
private JSONObject handleFileReferAndBuildResponse(JSONObject responseData, ConversationData conversationData) {
|
||||
try {
|
||||
// 先处理文件引用,收集CaseReferVo数据
|
||||
List<CaseReferVo> currentCaseRefers = new ArrayList<>();
|
||||
|
||||
JSONObject fileRefer = responseData.getJSONObject("fileRefer");
|
||||
if (fileRefer != null && fileRefer.containsKey("files")) {
|
||||
JSONArray files = fileRefer.getJSONArray("files");
|
||||
for (int i = 0; i < files.size(); i++) {
|
||||
JSONObject file = files.getJSONObject(i);
|
||||
String docId = file.getString("docId");
|
||||
if (docId != null) {
|
||||
// 根据docId从 case_document_log 表查询案例数据
|
||||
CaseReferVo caseRefer = getCaseReferByDocId(docId);
|
||||
if (caseRefer != null) {
|
||||
currentCaseRefers.add(caseRefer);
|
||||
conversationData.caseRefers.add(caseRefer); // 也添加到总的收集器中
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 构建返给前端的数据结构
|
||||
JSONObject modifiedResponse = new JSONObject();
|
||||
modifiedResponse.put("success", true);
|
||||
modifiedResponse.put("code", 0);
|
||||
modifiedResponse.put("message", "业务处理成功");
|
||||
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("status", 0);
|
||||
data.put("content", responseData.getString("content"));
|
||||
|
||||
// 添加处理后的案例引用数据
|
||||
JSONArray caseReferArray = new JSONArray();
|
||||
for (CaseReferVo caseRefer : currentCaseRefers) {
|
||||
JSONObject caseReferObj = new JSONObject();
|
||||
caseReferObj.put("caseId", caseRefer.getCaseId());
|
||||
caseReferObj.put("title", caseRefer.getTitle());
|
||||
caseReferObj.put("authorName", caseRefer.getAuthorName());
|
||||
caseReferObj.put("keywords", caseRefer.getKeywords());
|
||||
caseReferObj.put("content", caseRefer.getContent());
|
||||
caseReferArray.add(caseReferObj);
|
||||
}
|
||||
|
||||
// 构建新的fileRefer结构,包含案例引用
|
||||
JSONObject newFileRefer = new JSONObject();
|
||||
newFileRefer.put("caseRefers", caseReferArray);
|
||||
|
||||
// 保留原始的docs和files信息(如果需要)
|
||||
if (fileRefer != null) {
|
||||
if (fileRefer.containsKey("docs")) {
|
||||
newFileRefer.put("docs", fileRefer.get("docs"));
|
||||
}
|
||||
if (fileRefer.containsKey("files")) {
|
||||
newFileRefer.put("files", fileRefer.get("files"));
|
||||
}
|
||||
}
|
||||
|
||||
data.put("fileRefer", newFileRefer);
|
||||
data.put("suggestions", responseData.get("suggestions"));
|
||||
|
||||
modifiedResponse.put("data", data);
|
||||
|
||||
log.info("处理文件引用成功,返回 {} 个案例引用", currentCaseRefers.size());
|
||||
return modifiedResponse;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理文件引用并构建响应数据异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文件引用(原方法,保留用于数据收集)
|
||||
*/
|
||||
private void handleFileRefer(JSONObject responseData, ConversationData conversationData) {
|
||||
try {
|
||||
JSONObject fileRefer = responseData.getJSONObject("fileRefer");
|
||||
if (fileRefer != null && fileRefer.containsKey("files")) {
|
||||
JSONArray files = fileRefer.getJSONArray("files");
|
||||
for (int i = 0; i < files.size(); i++) {
|
||||
JSONObject file = files.getJSONObject(i);
|
||||
String docId = file.getString("docId");
|
||||
if (docId != null) {
|
||||
// 根据docId从 case_document_log 表查询案例数据
|
||||
CaseReferVo caseRefer = getCaseReferByDocId(docId);
|
||||
if (caseRefer != null) {
|
||||
conversationData.caseRefers.add(caseRefer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("处理文件引用异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理建议
|
||||
*/
|
||||
private void handleSuggestions(JSONObject responseData, ConversationData conversationData) {
|
||||
try {
|
||||
JSONArray suggestions = responseData.getJSONArray("suggestions");
|
||||
if (suggestions != null) {
|
||||
for (int i = 0; i < suggestions.size(); i++) {
|
||||
String suggestion = suggestions.getString(i);
|
||||
if (suggestion != null) {
|
||||
conversationData.suggestions.add(suggestion);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("处理建议异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据docId查询案例引用信息
|
||||
*/
|
||||
private CaseReferVo getCaseReferByDocId(String docId) {
|
||||
try {
|
||||
// 从 case_document_log 表查询案例信息(docId对应task_id)
|
||||
CaseDocumentLog docLog = caseDocumentLogDao.findByTaskId(docId);
|
||||
if (docLog == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 根据 case_id 查询案例详情
|
||||
Cases caseEntity = casesDao.get(docLog.getCaseId());
|
||||
if (caseEntity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 构建 CaseReferVo
|
||||
CaseReferVo caseRefer = new CaseReferVo();
|
||||
caseRefer.setCaseId(caseEntity.getId());
|
||||
caseRefer.setTitle(caseEntity.getTitle());
|
||||
caseRefer.setAuthorName(caseEntity.getAuthorName());
|
||||
caseRefer.setContent(caseEntity.getContent());
|
||||
|
||||
// 构建关键词列表
|
||||
List<String> keywords = new ArrayList<>();
|
||||
if (caseEntity.getKeyword1() != null) keywords.add(caseEntity.getKeyword1());
|
||||
if (caseEntity.getKeyword2() != null) keywords.add(caseEntity.getKeyword2());
|
||||
if (caseEntity.getKeyword3() != null) keywords.add(caseEntity.getKeyword3());
|
||||
if (caseEntity.getKeyword4() != null) keywords.add(caseEntity.getKeyword4());
|
||||
if (caseEntity.getKeyword5() != null) keywords.add(caseEntity.getKeyword5());
|
||||
caseRefer.setKeywords(keywords);
|
||||
|
||||
return caseRefer;
|
||||
} catch (Exception e) {
|
||||
log.error("根据docId查询案例引用信息异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存对话记录到ES
|
||||
*/
|
||||
private void saveConversationToES(ConversationData conversationData) {
|
||||
if (elasticsearchClient == null) {
|
||||
log.warn("未配置Elasticsearch客户端,无法保存对话记录");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 构建要保存的数据
|
||||
JSONObject esData = new JSONObject();
|
||||
esData.put("query", conversationData.query);
|
||||
esData.put("answer", conversationData.answer.toString());
|
||||
esData.put("conversationId", conversationData.conversationId);
|
||||
esData.put("userId", conversationData.userId);
|
||||
esData.put("timestamp", LocalDateTime.now().toString());
|
||||
|
||||
// 构建 caseRefer 数据
|
||||
JSONArray caseReferArray = new JSONArray();
|
||||
for (CaseReferVo caseRefer : conversationData.caseRefers) {
|
||||
JSONObject caseReferObj = new JSONObject();
|
||||
caseReferObj.put("caseId", caseRefer.getCaseId());
|
||||
caseReferObj.put("title", caseRefer.getTitle());
|
||||
caseReferObj.put("authorName", caseRefer.getAuthorName());
|
||||
caseReferObj.put("keywords", caseRefer.getKeywords());
|
||||
caseReferObj.put("content", caseRefer.getContent());
|
||||
caseReferArray.add(caseReferObj);
|
||||
}
|
||||
esData.put("caseRefer", caseReferArray);
|
||||
|
||||
// 添加建议
|
||||
esData.put("suggestions", conversationData.suggestions);
|
||||
|
||||
// 保存到ES
|
||||
IndexRequest indexRequest = new IndexRequest("ai_chat_messages");
|
||||
indexRequest.source(esData.toJSONString(), XContentType.JSON);
|
||||
|
||||
IndexResponse indexResponse = elasticsearchClient.index(indexRequest, RequestOptions.DEFAULT);
|
||||
log.info("保存对话记录到ES成功,文档ID: {}", indexResponse.getId());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("保存对话记录到ES异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对话数据容器
|
||||
*/
|
||||
private static class ConversationData {
|
||||
public String query;
|
||||
public StringBuilder answer = new StringBuilder();
|
||||
public List<CaseReferVo> caseRefers = new ArrayList<>();
|
||||
public List<String> suggestions = new ArrayList<>();
|
||||
public String conversationId;
|
||||
public String userId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
package com.xboe.module.boecase.service.impl;
|
||||
|
||||
import com.xboe.common.utils.StringUtil;
|
||||
import com.xboe.common.utils.IDGenerator;
|
||||
import com.xboe.common.OrderCondition;
|
||||
import com.xboe.common.PageList;
|
||||
import com.xboe.core.orm.FieldFilters;
|
||||
import com.xboe.core.orm.IFieldFilter;
|
||||
import com.xboe.core.orm.LikeMatchMode;
|
||||
import com.xboe.module.boecase.dao.CaseDocumentLogDao;
|
||||
import com.xboe.module.boecase.dto.CaseDocumentLogQueryDto;
|
||||
import com.xboe.module.boecase.entity.CaseDocumentLog;
|
||||
import com.xboe.module.boecase.service.ICaseDocumentLogService;
|
||||
import com.xboe.module.boecase.service.ICaseKnowledgeService;
|
||||
import com.xboe.module.boecase.vo.CaseDocumentLogVo;
|
||||
import com.xboe.enums.CaseDocumentLogOptTypeEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* AI调用日志Service实现类
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional
|
||||
public class CaseDocumentLogServiceImpl implements ICaseDocumentLogService {
|
||||
|
||||
@Resource
|
||||
private CaseDocumentLogDao caseDocumentLogDao;
|
||||
|
||||
@Resource
|
||||
private ICaseKnowledgeService caseKnowledgeService;
|
||||
|
||||
@Override
|
||||
public PageList<CaseDocumentLogVo> pageQuery(int pageIndex, int pageSize, CaseDocumentLogQueryDto queryDto) {
|
||||
// 构建查询条件
|
||||
List<IFieldFilter> filters = new ArrayList<>();
|
||||
|
||||
// 删除标识过滤
|
||||
filters.add(FieldFilters.eq("deleted", false));
|
||||
|
||||
// 接口调用状态
|
||||
if (queryDto.getOptStatus() != null) {
|
||||
filters.add(FieldFilters.eq("optStatus", queryDto.getOptStatus()));
|
||||
} else {
|
||||
filters.add(FieldFilters.ge("optStatus", 1));
|
||||
}
|
||||
|
||||
// 案例标题模糊查询
|
||||
if (StringUtil.isNotBlank(queryDto.getCaseTitle())) {
|
||||
filters.add(FieldFilters.like("caseTitle", LikeMatchMode.ANYWHERE, queryDto.getCaseTitle()));
|
||||
}
|
||||
|
||||
// 操作类型精确查询
|
||||
if (StringUtil.isNotBlank(queryDto.getOptType())) {
|
||||
filters.add(FieldFilters.eq("optType", queryDto.getOptType()));
|
||||
}
|
||||
|
||||
// 调用时间区间查询
|
||||
if (queryDto.getOptTimeStart() != null) {
|
||||
filters.add(FieldFilters.ge("optTime", queryDto.getOptTimeStart()));
|
||||
}
|
||||
if (queryDto.getOptTimeEnd() != null) {
|
||||
filters.add(FieldFilters.le("optTime", queryDto.getOptTimeEnd()));
|
||||
}
|
||||
|
||||
// 业务处理状态
|
||||
if (queryDto.getCaseStatus() != null) {
|
||||
filters.add(FieldFilters.eq("caseStatus", queryDto.getCaseStatus()));
|
||||
}
|
||||
|
||||
// 按创建时间降序排序
|
||||
OrderCondition order = OrderCondition.desc("sysCreateTime");
|
||||
|
||||
// 执行分页查询
|
||||
PageList<CaseDocumentLog> pageResult = caseDocumentLogDao.getGenericDao()
|
||||
.findPage(pageIndex, pageSize, CaseDocumentLog.class, filters, order);
|
||||
|
||||
// 转换为VO对象
|
||||
List<CaseDocumentLogVo> voList = pageResult.getList().stream()
|
||||
.map(this::convertToVo)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 构建返回结果
|
||||
PageList<CaseDocumentLogVo> result = new PageList<>();
|
||||
result.setList(voList);
|
||||
result.setCount(pageResult.getCount());
|
||||
result.setPageSize(pageResult.getPageSize());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clearLogsByCondition(CaseDocumentLogQueryDto queryDto) {
|
||||
// 构建查询条件(与分页查询相同的逻辑)
|
||||
List<IFieldFilter> filters = new ArrayList<>();
|
||||
|
||||
// 删除标识过滤
|
||||
filters.add(FieldFilters.eq("deleted", false));
|
||||
|
||||
// 接口调用状态
|
||||
if (queryDto.getOptStatus() != null) {
|
||||
filters.add(FieldFilters.eq("optStatus", queryDto.getOptStatus()));
|
||||
} else {
|
||||
filters.add(FieldFilters.ge("optStatus", 1));
|
||||
}
|
||||
|
||||
// 案例标题模糊查询
|
||||
if (StringUtil.isNotBlank(queryDto.getCaseTitle())) {
|
||||
filters.add(FieldFilters.like("caseTitle", LikeMatchMode.ANYWHERE, queryDto.getCaseTitle()));
|
||||
}
|
||||
|
||||
// 操作类型精确查询
|
||||
if (StringUtil.isNotBlank(queryDto.getOptType())) {
|
||||
filters.add(FieldFilters.eq("optType", queryDto.getOptType()));
|
||||
}
|
||||
|
||||
// 调用时间区间查询
|
||||
if (queryDto.getOptTimeStart() != null) {
|
||||
filters.add(FieldFilters.ge("optTime", queryDto.getOptTimeStart()));
|
||||
}
|
||||
if (queryDto.getOptTimeEnd() != null) {
|
||||
filters.add(FieldFilters.le("optTime", queryDto.getOptTimeEnd()));
|
||||
}
|
||||
|
||||
// 业务处理状态
|
||||
if (queryDto.getCaseStatus() != null) {
|
||||
filters.add(FieldFilters.eq("caseStatus", queryDto.getCaseStatus()));
|
||||
}
|
||||
|
||||
// 查询符合条件的所有记录
|
||||
List<CaseDocumentLog> logsToDelete = caseDocumentLogDao.getGenericDao()
|
||||
.findList(CaseDocumentLog.class, (IFieldFilter) filters);
|
||||
|
||||
if (logsToDelete.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 批量设置删除标识为true(逻辑删除)
|
||||
int deletedCount = 0;
|
||||
for (CaseDocumentLog log : logsToDelete) {
|
||||
log.setDeleted(true);
|
||||
caseDocumentLogDao.update(log);
|
||||
deletedCount++;
|
||||
}
|
||||
|
||||
log.info("清空日志操作完成,共删除{}条记录", deletedCount);
|
||||
return deletedCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retryByLogId(String logId) {
|
||||
if (StringUtil.isBlank(logId)) {
|
||||
log.error("重试失败,logId不能为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. 根据logId查询原始日志数据
|
||||
CaseDocumentLog originalLog = caseDocumentLogDao.get(logId);
|
||||
if (originalLog == null || originalLog.getDeleted()) {
|
||||
log.error("重试失败,未找到有效的日志记录,logId: {}", logId);
|
||||
return false;
|
||||
}
|
||||
|
||||
log.info("开始重试AI调用,原始日志ID: {}, 案例标题: {}, 操作类型: {}",
|
||||
logId, originalLog.getCaseTitle(), originalLog.getOptType());
|
||||
|
||||
// 2. 执行AI调用重试逻辑
|
||||
boolean retrySuccess = false;
|
||||
|
||||
try {
|
||||
// 根据操作类型调用对应的接口方法
|
||||
String optType = originalLog.getOptType();
|
||||
String caseId = originalLog.getCaseId();
|
||||
|
||||
if (StringUtil.isBlank(caseId)) {
|
||||
throw new IllegalArgumentException("案例ID不能为空");
|
||||
}
|
||||
|
||||
log.info("正在执行AI调用重试,操作类型: {}, caseId: {}", optType, caseId);
|
||||
|
||||
// 根据操作类型执行对应的方法(这些方法内部会自动创建日志记录)
|
||||
if (CaseDocumentLogOptTypeEnum.CREATE.getCode().equals(optType)) {
|
||||
// 上传案例文档
|
||||
retrySuccess = caseKnowledgeService.uploadCaseDocument(caseId);
|
||||
log.info("执行上传案例文档重试,caseId: {}, 结果: {}", caseId, retrySuccess);
|
||||
|
||||
} else if (CaseDocumentLogOptTypeEnum.DELETE.getCode().equals(optType)) {
|
||||
// 删除案例文档
|
||||
retrySuccess = caseKnowledgeService.deleteCaseDocument(caseId);
|
||||
log.info("执行删除案例文档重试,caseId: {}, 结果: {}", caseId, retrySuccess);
|
||||
|
||||
} else if (CaseDocumentLogOptTypeEnum.UPDATE.getCode().equals(optType)) {
|
||||
// 更新案例文档
|
||||
retrySuccess = caseKnowledgeService.updateCaseDocument(caseId);
|
||||
log.info("执行更新案例文档重试,caseId: {}, 结果: {}", caseId, retrySuccess);
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("不支持的操作类型: " + optType);
|
||||
}
|
||||
|
||||
if (retrySuccess) {
|
||||
log.info("AI调用重试成功,操作类型: {}, caseId: {}", optType, caseId);
|
||||
} else {
|
||||
log.warn("AI调用重试失败,操作类型: {}, caseId: {}", optType, caseId);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("AI调用重试异常,操作类型: {}, caseId: {}",
|
||||
originalLog.getOptType(), originalLog.getCaseId(), e);
|
||||
retrySuccess = false;
|
||||
}
|
||||
|
||||
return retrySuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实体转换为VO
|
||||
*/
|
||||
private CaseDocumentLogVo convertToVo(CaseDocumentLog entity) {
|
||||
CaseDocumentLogVo vo = new CaseDocumentLogVo();
|
||||
BeanUtils.copyProperties(entity, vo);
|
||||
|
||||
// 操作类型转换为中文描述
|
||||
vo.setOptType(CaseDocumentLogOptTypeEnum.getDescByCode(entity.getOptType()));
|
||||
|
||||
// 接口调用状态转换
|
||||
vo.setOptStatusText(getOptStatusText(entity.getOptStatus()));
|
||||
|
||||
// 业务处理状态转换
|
||||
vo.setCaseStatusText(getCaseStatusText(entity.getCaseStatus()));
|
||||
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取接口调用状态描述
|
||||
*/
|
||||
private String getOptStatusText(Integer optStatus) {
|
||||
if (optStatus == null) {
|
||||
return "";
|
||||
}
|
||||
switch (optStatus) {
|
||||
case 0:
|
||||
return "调用中";
|
||||
case 1:
|
||||
return "调用成功";
|
||||
case 2:
|
||||
return "调用失败";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取业务处理状态描述
|
||||
*/
|
||||
private String getCaseStatusText(Integer caseStatus) {
|
||||
if (caseStatus == null) {
|
||||
return "";
|
||||
}
|
||||
switch (caseStatus) {
|
||||
case 1:
|
||||
return "处理成功";
|
||||
case 2:
|
||||
return "处理失败";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,687 @@
|
||||
package com.xboe.module.boecase.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xboe.common.utils.IDGenerator;
|
||||
import com.xboe.common.utils.StringUtil;
|
||||
import com.xboe.common.OrderCondition;
|
||||
import com.xboe.core.orm.FieldFilters;
|
||||
import com.xboe.enums.CaseDocumentLogCaseStatusEnum;
|
||||
import com.xboe.enums.CaseDocumentLogOptStatusEnum;
|
||||
import com.xboe.module.boecase.dao.CaseDocumentLogDao;
|
||||
import com.xboe.module.boecase.dao.CasesDao;
|
||||
import com.xboe.module.boecase.entity.CaseDocumentLog;
|
||||
import com.xboe.module.boecase.entity.Cases;
|
||||
import com.xboe.module.boecase.properties.CaseAiProperties;
|
||||
import com.xboe.module.boecase.service.IAiAccessTokenService;
|
||||
import com.xboe.module.boecase.service.ICaseKnowledgeService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 案例-知识库Service实现类
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional
|
||||
@EnableConfigurationProperties({CaseAiProperties.class})
|
||||
public class CaseKnowledgeServiceImpl implements ICaseKnowledgeService {
|
||||
|
||||
@Autowired
|
||||
private CaseAiProperties caseAiProperties;
|
||||
|
||||
@Resource
|
||||
private CasesDao casesDao;
|
||||
|
||||
@Resource
|
||||
private CaseDocumentLogDao caseDocumentLogDao;
|
||||
|
||||
@Autowired
|
||||
private IAiAccessTokenService aiAccessTokenService;
|
||||
|
||||
private static final String ACCESS_TOKEN_CACHE_KEY = "case:ai:access_token";
|
||||
|
||||
@Override
|
||||
public boolean uploadCaseDocument(String caseId) {
|
||||
if (StringUtil.isBlank(caseId)) {
|
||||
log.error("上传案例文档失败,案例ID不能为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. 查询案例信息
|
||||
Cases caseEntity = casesDao.findOne(FieldFilters.eq("id", caseId));
|
||||
if (caseEntity == null || caseEntity.getDeleted()) {
|
||||
log.error("上传案例文档失败,未找到有效的案例记录,caseId: {}", caseId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 检查文件路径
|
||||
if (StringUtil.isBlank(caseEntity.getFilePath())) {
|
||||
log.error("上传案例文档失败,案例文件路径为空,caseId: {}", caseId);
|
||||
return false;
|
||||
}
|
||||
|
||||
File file = new File(caseEntity.getFilePath());
|
||||
if (!file.exists()) {
|
||||
log.error("上传案例文档失败,案例文件不存在,filePath: {}", caseEntity.getFilePath());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. 获取access_token
|
||||
String accessToken = aiAccessTokenService.getAccessToken();
|
||||
if (StringUtil.isBlank(accessToken)) {
|
||||
log.error("上传案例文档失败,获取access_token失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 4. 构建上传参数
|
||||
String fileName = caseEntity.getFileName();
|
||||
if (StringUtil.isBlank(fileName)) {
|
||||
fileName = file.getName();
|
||||
}
|
||||
|
||||
String fileType = getFileType(fileName);
|
||||
String userId = getCurrentUserId();
|
||||
String uploadUrl = caseAiProperties.getBaseUrl() + "/apigateway/knowledge/v1/file/upload";
|
||||
|
||||
// 5. 重试逻辑:最多3次机会
|
||||
int maxRetries = 3;
|
||||
for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
log.info("上传案例文档第{}次尝试,caseId: {}", attempt, caseId);
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpPost httpPost = new HttpPost(uploadUrl);
|
||||
httpPost.setHeader("X-AI-ApiCode", caseAiProperties.getAiApiCode());
|
||||
httpPost.setHeader("access_token", accessToken);
|
||||
|
||||
// 构建multipart/form-data请求体
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.addBinaryBody("file", file);
|
||||
builder.addTextBody("userId", userId, ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("kId", caseAiProperties.getCaseKnowledgeId(), ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("fileName", fileName, ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("fileType", fileType, ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("parseType", "AUTO", ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("callbackUrl", caseAiProperties.getFileUploadCallbackUrl(), ContentType.TEXT_PLAIN);
|
||||
|
||||
HttpEntity multipart = builder.build();
|
||||
httpPost.setEntity(multipart);
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
|
||||
if (statusCode == 200) {
|
||||
JSONObject result = JSON.parseObject(responseBody);
|
||||
if (result.getBooleanValue("success")) {
|
||||
// 业务处理成功
|
||||
JSONObject data = result.getJSONObject("data");
|
||||
String taskId = data.getString("taskId");
|
||||
|
||||
// 保存成功的CaseDocumentLog记录
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "create", uploadUrl,
|
||||
"上传案例文档", responseBody, CaseDocumentLogOptStatusEnum.SUCCESS.getCode(), CaseDocumentLogCaseStatusEnum.SUCCESS.getCode(), taskId);
|
||||
|
||||
log.info("上传案例文档成功,caseId: {}, taskId: {}, 尝试次数: {}", caseId, taskId, attempt);
|
||||
return true;
|
||||
} else {
|
||||
// 业务处理失败,不重试
|
||||
log.error("上传案例文档业务处理失败,不重试,response: {}", responseBody);
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "create", uploadUrl,
|
||||
"上传案例文档", responseBody, CaseDocumentLogOptStatusEnum.SUCCESS.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 接口调用失败
|
||||
log.warn("上传案例文档接口调用失败,第{}次尝试,status: {}, response: {}", attempt, statusCode, responseBody);
|
||||
if (attempt == maxRetries) {
|
||||
// 最后一次尝试仍然失败
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "create", uploadUrl,
|
||||
"上传案例文档", responseBody, CaseDocumentLogOptStatusEnum.FAILED.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
// 继续下一次重试
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 接口调用异常
|
||||
log.warn("上传案例文档接口调用异常,第{}次尝试", attempt, e);
|
||||
if (attempt == maxRetries) {
|
||||
// 最后一次尝试仍然异常
|
||||
log.error("上传案例文档最终失败,已重试{}次", maxRetries);
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "create", uploadUrl,
|
||||
"上传案例文档", "接口调用异常: " + e.getMessage(), CaseDocumentLogOptStatusEnum.FAILED.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
// 继续下一次重试
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteCaseDocument(String caseId) {
|
||||
if (StringUtil.isBlank(caseId)) {
|
||||
log.error("删除案例文档失败,案例ID不能为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. 查询案例信息
|
||||
Cases caseEntity = casesDao.findOne(FieldFilters.eq("id", caseId));
|
||||
if (caseEntity == null) {
|
||||
log.error("删除案例文档失败,未找到案例记录,caseId: {}", caseId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 根据案例ID查询最新一条CaseDocumentLog数据
|
||||
List<CaseDocumentLog> logList = caseDocumentLogDao.getGenericDao()
|
||||
.findList(CaseDocumentLog.class,
|
||||
FieldFilters.eq("caseId", caseId),
|
||||
OrderCondition.desc("sysCreateTime"));
|
||||
|
||||
if (logList.isEmpty()) {
|
||||
log.error("删除案例文档失败,未找到相关的日志记录,caseId: {}", caseId);
|
||||
return false;
|
||||
}
|
||||
|
||||
CaseDocumentLog latestLog = logList.get(0);
|
||||
String taskId = latestLog.getTaskId();
|
||||
if (StringUtil.isBlank(taskId)) {
|
||||
log.error("删除案例文档失败,日志记录中taskId为空,caseId: {}", caseId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. 获取access_token
|
||||
String accessToken = aiAccessTokenService.getAccessToken();
|
||||
if (StringUtil.isBlank(accessToken)) {
|
||||
log.error("删除案例文档失败,获取access_token失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
String deleteUrl = caseAiProperties.getBaseUrl() + "/apigateway/knowledge/v1/file/delete";
|
||||
|
||||
// 4. 重试逻辑:最多3次机会
|
||||
int maxRetries = 3;
|
||||
for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
log.info("删除案例文档第{}次尝试,caseId: {}, taskId: {}", attempt, caseId, taskId);
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpDelete httpDelete = new HttpDelete(deleteUrl);
|
||||
httpDelete.setHeader("X-AI-ApiCode", caseAiProperties.getAiApiCode());
|
||||
httpDelete.setHeader("access_token", accessToken);
|
||||
httpDelete.setHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
// 构建请求参数
|
||||
String params = "kId=" + URLEncoder.encode(caseAiProperties.getCaseKnowledgeId(), StandardCharsets.UTF_8.name()) +
|
||||
"&taskIds=" + URLEncoder.encode(taskId, StandardCharsets.UTF_8.name());
|
||||
|
||||
StringEntity entity = new StringEntity(params, StandardCharsets.UTF_8);
|
||||
entity.setContentType("application/x-www-form-urlencoded");
|
||||
httpDelete.setEntity(entity);
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpDelete)) {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
|
||||
if (statusCode == 200) {
|
||||
JSONObject result = JSON.parseObject(responseBody);
|
||||
if (result.getBooleanValue("success")) {
|
||||
// 接口调用成功,检查业务处理结果
|
||||
JSONObject data = result.getJSONObject("data");
|
||||
Boolean deleteSuccess = data.getBoolean(taskId);
|
||||
|
||||
int optStatus = CaseDocumentLogOptStatusEnum.SUCCESS.getCode();
|
||||
int caseStatus = (deleteSuccess != null && deleteSuccess) ?
|
||||
CaseDocumentLogCaseStatusEnum.SUCCESS.getCode() : CaseDocumentLogCaseStatusEnum.FAILED.getCode();
|
||||
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "delete", deleteUrl,
|
||||
"删除案例文档", responseBody, optStatus, caseStatus, null);
|
||||
|
||||
if (deleteSuccess != null && deleteSuccess) {
|
||||
log.info("删除案例文档成功,caseId: {}, taskId: {}, 尝试次数: {}", caseId, taskId, attempt);
|
||||
return true;
|
||||
} else {
|
||||
// 业务处理失败,不重试
|
||||
log.error("删除案例文档业务处理失败,不重试,caseId: {}, taskId: {}", caseId, taskId);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 业务处理失败,不重试
|
||||
log.error("删除案例文档业务处理失败,不重试,response: {}", responseBody);
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "delete", deleteUrl,
|
||||
"删除案例文档", responseBody, CaseDocumentLogOptStatusEnum.SUCCESS.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 接口调用失败
|
||||
log.warn("删除案例文档接口调用失败,第{}次尝试,status: {}, response: {}", attempt, statusCode, responseBody);
|
||||
if (attempt == maxRetries) {
|
||||
// 最后一次尝试仍然失败
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "delete", deleteUrl,
|
||||
"删除案例文档", responseBody, CaseDocumentLogOptStatusEnum.FAILED.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
// 继续下一次重试
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 接口调用异常
|
||||
log.warn("删除案例文档接口调用异常,第{}次尝试", attempt, e);
|
||||
if (attempt == maxRetries) {
|
||||
// 最后一次尝试仍然异常
|
||||
log.error("删除案例文档最终失败,已重试{}次", maxRetries);
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "delete", deleteUrl,
|
||||
"删除案例文档", "接口调用异常: " + e.getMessage(), CaseDocumentLogOptStatusEnum.FAILED.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
// 继续下一次重试
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateCaseDocument(String caseId) {
|
||||
if (StringUtil.isBlank(caseId)) {
|
||||
log.error("更新案例文档失败,案例ID不能为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. 查询案例信息
|
||||
Cases caseEntity = casesDao.findOne(FieldFilters.eq("id", caseId));
|
||||
if (caseEntity == null) {
|
||||
log.error("更新案例文档失败,未找到案例记录,caseId: {}", caseId);
|
||||
return false;
|
||||
}
|
||||
|
||||
log.info("开始更新案例文档,caseId: {}", caseId);
|
||||
|
||||
// 获取access_token
|
||||
String accessToken = aiAccessTokenService.getAccessToken();
|
||||
if (StringUtil.isBlank(accessToken)) {
|
||||
log.error("更新案例文档失败,获取access_token失败");
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "update", "",
|
||||
"更新案例文档", "获取access_token失败", CaseDocumentLogOptStatusEnum.FAILED.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 先调用删除接口
|
||||
boolean deleteSuccess = callDeleteInterface(caseId, caseEntity, accessToken);
|
||||
if (!deleteSuccess) {
|
||||
log.error("更新案例文档失败,删除接口调用失败,不继续执行上传操作,caseId: {}", caseId);
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "update", "",
|
||||
"更新案例文档", "删除接口:失败,上传接口:未执行", CaseDocumentLogOptStatusEnum.FAILED.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. 删除成功后,再调用上传接口
|
||||
String taskId = callUploadInterface(caseId, caseEntity, accessToken);
|
||||
boolean uploadSuccess = StringUtil.isNotBlank(taskId);
|
||||
|
||||
// 4. 根据结果保存一条CaseDocumentLog数据
|
||||
int optStatus = uploadSuccess ? CaseDocumentLogOptStatusEnum.SUCCESS.getCode() : CaseDocumentLogOptStatusEnum.FAILED.getCode();
|
||||
int caseStatus = uploadSuccess ? CaseDocumentLogCaseStatusEnum.SUCCESS.getCode() : CaseDocumentLogCaseStatusEnum.FAILED.getCode();
|
||||
String result = String.format("删除接口:成功,上传接口:%s",
|
||||
uploadSuccess ? "成功" : "失败");
|
||||
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "update", "",
|
||||
"更新案例文档", result, optStatus, caseStatus, taskId);
|
||||
|
||||
if (uploadSuccess) {
|
||||
log.info("更新案例文档成功,caseId: {}, taskId: {}", caseId, taskId);
|
||||
} else {
|
||||
log.error("更新案例文档失败,上传接口调用失败,caseId: {}", caseId);
|
||||
}
|
||||
|
||||
return uploadSuccess;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("更新案例文档异常", e);
|
||||
saveCaseDocumentLog(caseId, caseEntity.getTitle(), "update", "",
|
||||
"更新案例文档", "更新异常: " + e.getMessage(), CaseDocumentLogOptStatusEnum.FAILED.getCode(), CaseDocumentLogCaseStatusEnum.FAILED.getCode(), null);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleUploadCallback(String taskId, String message, String fileStatus) {
|
||||
if (StringUtil.isBlank(taskId)) {
|
||||
log.error("处理上传回调失败,taskId不能为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtil.isBlank(fileStatus)) {
|
||||
log.error("处理上传回调失败,fileStatus不能为空,taskId: {}", taskId);
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 根据taskId查询最新一条CaseDocumentLog数据
|
||||
List<CaseDocumentLog> logList = caseDocumentLogDao.getGenericDao()
|
||||
.findList(CaseDocumentLog.class,
|
||||
FieldFilters.eq("taskId", taskId),
|
||||
OrderCondition.desc("sysCreateTime"));
|
||||
|
||||
if (logList.isEmpty()) {
|
||||
log.error("处理上传回调失败,未找到对应的日志记录,taskId: {}", taskId);
|
||||
return false;
|
||||
}
|
||||
|
||||
CaseDocumentLog latestLog = logList.get(0);
|
||||
log.info("找到对应的日志记录,logId: {}, taskId: {}, 原状态 - optStatus: {}, caseStatus: {}",
|
||||
latestLog.getId(), taskId, latestLog.getOptStatus(), latestLog.getCaseStatus());
|
||||
|
||||
// 2. 根据fileStatus更新状态
|
||||
int newOptStatus;
|
||||
int newCaseStatus;
|
||||
|
||||
if ("vectored".equals(fileStatus)) {
|
||||
// 文档上传成功(向量化成功)
|
||||
newOptStatus = CaseDocumentLogOptStatusEnum.SUCCESS.getCode();
|
||||
newCaseStatus = CaseDocumentLogCaseStatusEnum.SUCCESS.getCode();
|
||||
log.info("文档上传成功(向量化成功),taskId: {}", taskId);
|
||||
} else if ("failed".equals(fileStatus)) {
|
||||
// 业务处理失败
|
||||
newOptStatus = CaseDocumentLogOptStatusEnum.SUCCESS.getCode(); // 接口调用成功
|
||||
newCaseStatus = CaseDocumentLogCaseStatusEnum.FAILED.getCode(); // 业务处理失败
|
||||
log.warn("文档上传业务处理失败,taskId: {}, message: {}", taskId, message);
|
||||
} else {
|
||||
log.error("未知的fileStatus: {}, taskId: {}", fileStatus, taskId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. 更新日志记录状态
|
||||
latestLog.setOptStatus(newOptStatus);
|
||||
latestLog.setCaseStatus(newCaseStatus);
|
||||
latestLog.setResponseBody(StringUtil.isNotBlank(message) ? message : latestLog.getResponseBody());
|
||||
latestLog.setSysUpdateTime(LocalDateTime.now());
|
||||
|
||||
caseDocumentLogDao.save(latestLog);
|
||||
|
||||
log.info("更新日志记录成功,logId: {}, taskId: {}, 新状态 - optStatus: {}, caseStatus: {}",
|
||||
latestLog.getId(), taskId, newOptStatus, newCaseStatus);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理上传回调异常,taskId: {}", taskId, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件名获取文件类型
|
||||
*/
|
||||
private String getFileType(String fileName) {
|
||||
if (StringUtil.isBlank(fileName)) {
|
||||
return "txt";
|
||||
}
|
||||
|
||||
String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
||||
switch (extension) {
|
||||
case "doc":
|
||||
case "docx":
|
||||
return "word";
|
||||
case "pdf":
|
||||
return "pdf";
|
||||
case "md":
|
||||
return "md";
|
||||
case "txt":
|
||||
return "txt";
|
||||
case "xls":
|
||||
case "xlsx":
|
||||
return "excel";
|
||||
case "xml":
|
||||
return "xml";
|
||||
case "ppt":
|
||||
case "pptx":
|
||||
return "ppt";
|
||||
default:
|
||||
return "txt";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户ID
|
||||
*/
|
||||
private String getCurrentUserId() {
|
||||
// TODO: 实现获取当前登录用户ID的逻辑
|
||||
// 这里需要根据实际的用户认证系统来实现
|
||||
return "defaultUserId";
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用删除接口(仅调用接口,不记录日志)
|
||||
*/
|
||||
private boolean callDeleteInterface(String caseId, Cases caseEntity, String accessToken) {
|
||||
// 1. 根据案例ID查询最新一条CaseDocumentLog数据
|
||||
List<CaseDocumentLog> logList = caseDocumentLogDao.getGenericDao()
|
||||
.findList(CaseDocumentLog.class,
|
||||
FieldFilters.eq("caseId", caseId),
|
||||
OrderCondition.desc("sysCreateTime"));
|
||||
|
||||
if (logList.isEmpty()) {
|
||||
log.warn("调用删除接口时未找到相关的日志记录,caseId: {}", caseId);
|
||||
return false;
|
||||
}
|
||||
|
||||
CaseDocumentLog latestLog = logList.get(0);
|
||||
String taskId = latestLog.getTaskId();
|
||||
if (StringUtil.isBlank(taskId)) {
|
||||
log.warn("调用删除接口时日志记录中taskId为空,caseId: {}", caseId);
|
||||
return false;
|
||||
}
|
||||
|
||||
String deleteUrl = caseAiProperties.getBaseUrl() + "/apigateway/knowledge/v1/file/delete";
|
||||
|
||||
// 2. 重试逻辑:最多3次机会
|
||||
int maxRetries = 3;
|
||||
for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
log.info("调用删除接口第{}次尝试,caseId: {}, taskId: {}", attempt, caseId, taskId);
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpDelete httpDelete = new HttpDelete(deleteUrl);
|
||||
httpDelete.setHeader("X-AI-ApiCode", caseAiProperties.getAiApiCode());
|
||||
httpDelete.setHeader("access_token", accessToken);
|
||||
httpDelete.setHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
// 构建请求参数
|
||||
String params = "kId=" + URLEncoder.encode(caseAiProperties.getCaseKnowledgeId(), StandardCharsets.UTF_8.name()) +
|
||||
"&taskIds=" + URLEncoder.encode(taskId, StandardCharsets.UTF_8.name());
|
||||
|
||||
StringEntity entity = new StringEntity(params, StandardCharsets.UTF_8);
|
||||
entity.setContentType("application/x-www-form-urlencoded");
|
||||
httpDelete.setEntity(entity);
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpDelete)) {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
|
||||
if (statusCode == 200) {
|
||||
JSONObject result = JSON.parseObject(responseBody);
|
||||
if (result.getBooleanValue("success")) {
|
||||
JSONObject data = result.getJSONObject("data");
|
||||
Boolean deleteSuccess = data.getBoolean(taskId);
|
||||
|
||||
if (deleteSuccess != null && deleteSuccess) {
|
||||
log.info("调用删除接口成功,caseId: {}, taskId: {}, 尝试次数: {}", caseId, taskId, attempt);
|
||||
return true;
|
||||
} else {
|
||||
// 业务处理失败,不重试
|
||||
log.warn("调用删除接口业务处理失败,不重试,caseId: {}, taskId: {}", caseId, taskId);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 业务处理失败,不重试
|
||||
log.warn("调用删除接口业务处理失败,不重试,response: {}", responseBody);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 接口调用失败
|
||||
log.warn("调用删除接口失败,第{}次尝试,status: {}, response: {}", attempt, statusCode, responseBody);
|
||||
if (attempt == maxRetries) {
|
||||
log.warn("调用删除接口最终失败,已重试{}次", maxRetries);
|
||||
return false;
|
||||
}
|
||||
// 继续下一次重试
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 接口调用异常
|
||||
log.warn("调用删除接口异常,第{}次尝试", attempt, e);
|
||||
if (attempt == maxRetries) {
|
||||
log.warn("调用删除接口最终失败,已重试{}次", maxRetries);
|
||||
return false;
|
||||
}
|
||||
// 继续下一次重试
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用上传接口(仅调用接口,不记录日志)
|
||||
*/
|
||||
private String callUploadInterface(String caseId, Cases caseEntity, String accessToken) {
|
||||
// 1. 检查文件路径
|
||||
if (StringUtil.isBlank(caseEntity.getFilePath())) {
|
||||
log.warn("调用上传接口失败,案例文件路径为空,caseId: {}", caseId);
|
||||
return null;
|
||||
}
|
||||
|
||||
File file = new File(caseEntity.getFilePath());
|
||||
if (!file.exists()) {
|
||||
log.warn("调用上传接口失败,案例文件不存在,filePath: {}", caseEntity.getFilePath());
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. 构建上传参数
|
||||
String fileName = caseEntity.getFileName();
|
||||
if (StringUtil.isBlank(fileName)) {
|
||||
fileName = file.getName();
|
||||
}
|
||||
|
||||
String fileType = getFileType(fileName);
|
||||
String userId = getCurrentUserId();
|
||||
String uploadUrl = caseAiProperties.getBaseUrl() + "/apigateway/knowledge/v1/file/upload";
|
||||
|
||||
// 3. 重试逻辑:最多3次机会
|
||||
int maxRetries = 3;
|
||||
for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
log.info("调用上传接口第{}次尝试,caseId: {}", attempt, caseId);
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpPost httpPost = new HttpPost(uploadUrl);
|
||||
httpPost.setHeader("X-AI-ApiCode", caseAiProperties.getAiApiCode());
|
||||
httpPost.setHeader("access_token", accessToken);
|
||||
|
||||
// 构建multipart/form-data请求体
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.addBinaryBody("file", file);
|
||||
builder.addTextBody("userId", userId, ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("kId", caseAiProperties.getCaseKnowledgeId(), ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("fileName", fileName, ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("fileType", fileType, ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("parseType", "AUTO", ContentType.TEXT_PLAIN);
|
||||
builder.addTextBody("callbackUrl", caseAiProperties.getFileUploadCallbackUrl(), ContentType.TEXT_PLAIN);
|
||||
|
||||
HttpEntity multipart = builder.build();
|
||||
httpPost.setEntity(multipart);
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
|
||||
if (statusCode == 200) {
|
||||
JSONObject result = JSON.parseObject(responseBody);
|
||||
if (result.getBooleanValue("success")) {
|
||||
JSONObject data = result.getJSONObject("data");
|
||||
String taskId = data.getString("taskId");
|
||||
|
||||
log.info("调用上传接口成功,caseId: {}, taskId: {}, 尝试次数: {}", caseId, taskId, attempt);
|
||||
return taskId;
|
||||
} else {
|
||||
// 业务处理失败,不重试
|
||||
log.warn("调用上传接口业务处理失败,不重试,response: {}", responseBody);
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
// 接口调用失败
|
||||
log.warn("调用上传接口失败,第{}次尝试,status: {}, response: {}", attempt, statusCode, responseBody);
|
||||
if (attempt == maxRetries) {
|
||||
log.warn("调用上传接口最终失败,已重试{}次", maxRetries);
|
||||
return null;
|
||||
}
|
||||
// 继续下一次重试
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 接口调用异常
|
||||
log.warn("调用上传接口异常,第{}次尝试", attempt, e);
|
||||
if (attempt == maxRetries) {
|
||||
log.warn("调用上传接口最终失败,已重试{}次", maxRetries);
|
||||
return null;
|
||||
}
|
||||
// 继续下一次重试
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存CaseDocumentLog记录
|
||||
*/
|
||||
private void saveCaseDocumentLog(String caseId, String caseTitle, String optType,
|
||||
String requestUrl, String requestBody, String responseBody,
|
||||
Integer optStatus, Integer caseStatus, String taskId) {
|
||||
try {
|
||||
CaseDocumentLog caseDocumentLog = new CaseDocumentLog();
|
||||
caseDocumentLog.setId(IDGenerator.generate());
|
||||
caseDocumentLog.setTaskId(taskId);
|
||||
caseDocumentLog.setCaseId(caseId);
|
||||
caseDocumentLog.setCaseTitle(caseTitle);
|
||||
caseDocumentLog.setOptType(optType);
|
||||
caseDocumentLog.setRequestUrl(requestUrl);
|
||||
caseDocumentLog.setRequestBody(requestBody);
|
||||
caseDocumentLog.setResponseBody(responseBody);
|
||||
caseDocumentLog.setOptTime(LocalDateTime.now());
|
||||
caseDocumentLog.setOptStatus(optStatus);
|
||||
caseDocumentLog.setCaseStatus(caseStatus);
|
||||
caseDocumentLog.setExecuteDuration(0L);
|
||||
|
||||
caseDocumentLogDao.save(caseDocumentLog);
|
||||
log.info("保存CaseDocumentLog成功,logId: {}", caseDocumentLog.getId());
|
||||
} catch (Exception e) {
|
||||
log.error("保存CaseDocumentLog失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.xboe.module.boecase.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* AI会话记录
|
||||
*/
|
||||
@Data
|
||||
public class CaseAiConversationVo {
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.xboe.module.boecase.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI会话消息记录VO
|
||||
*/
|
||||
@Data
|
||||
public class CaseAiMessageVo {
|
||||
|
||||
/**
|
||||
* 用户提问内容
|
||||
*/
|
||||
private String query;
|
||||
|
||||
/**
|
||||
* AI回答内容
|
||||
*/
|
||||
private String answer;
|
||||
|
||||
/**
|
||||
* 案例引用列表
|
||||
*/
|
||||
private List<CaseReferVo> caseRefer;
|
||||
|
||||
/**
|
||||
* 建议列表
|
||||
*/
|
||||
private List<String> suggestions;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.xboe.module.boecase.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* AI调用日志列表展示VO
|
||||
*/
|
||||
@Data
|
||||
public class CaseDocumentLogVo {
|
||||
|
||||
/**
|
||||
* 日志ID
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 案例标题
|
||||
*/
|
||||
private String caseTitle;
|
||||
|
||||
/**
|
||||
* 操作类型
|
||||
*/
|
||||
private String optType;
|
||||
|
||||
/**
|
||||
* 调用接口名称
|
||||
*/
|
||||
private String requestUrl;
|
||||
|
||||
/**
|
||||
* 调用时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private LocalDateTime optTime;
|
||||
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
private String requestBody;
|
||||
|
||||
/**
|
||||
* 响应参数
|
||||
*/
|
||||
private String responseBody;
|
||||
|
||||
/**
|
||||
* 接口调用结果
|
||||
* 0-调用中, 1-调用成功, 2-调用失败
|
||||
*/
|
||||
private Integer optStatus;
|
||||
|
||||
/**
|
||||
* 接口调用结果描述
|
||||
*/
|
||||
private String optStatusText;
|
||||
|
||||
/**
|
||||
* 业务处理结果
|
||||
* 1-处理成功, 2-处理失败
|
||||
*/
|
||||
private Integer caseStatus;
|
||||
|
||||
/**
|
||||
* 业务处理结果描述
|
||||
*/
|
||||
private String caseStatusText;
|
||||
|
||||
/**
|
||||
* 执行时间(ms)
|
||||
*/
|
||||
private Long executeDuration;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.xboe.module.boecase.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 案例引用信息VO
|
||||
*/
|
||||
@Data
|
||||
public class CaseReferVo {
|
||||
|
||||
/**
|
||||
* 案例ID
|
||||
*/
|
||||
private String caseId;
|
||||
|
||||
/**
|
||||
* 案例标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 作者姓名
|
||||
*/
|
||||
private String authorName;
|
||||
|
||||
/**
|
||||
* 关键词列表
|
||||
*/
|
||||
private List<String> keywords;
|
||||
|
||||
/**
|
||||
* 案例内容
|
||||
*/
|
||||
private String content;
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.xboe.api.ThirdApi;
|
||||
import com.xboe.data.outside.IOutSideDataService;
|
||||
import com.xboe.module.course.entity.CourseTag;
|
||||
import com.xboe.module.course.service.*;
|
||||
import com.xboe.module.course.vo.TeacherVo;
|
||||
import com.xboe.school.study.entity.StudyCourse;
|
||||
import com.xboe.school.study.service.IStudyCourseService;
|
||||
@@ -34,11 +36,6 @@ import com.xboe.module.course.dto.CourseTeacherDto;
|
||||
import com.xboe.module.course.entity.Course;
|
||||
import com.xboe.module.course.entity.CourseCrowd;
|
||||
import com.xboe.module.course.entity.CourseTeacher;
|
||||
import com.xboe.module.course.service.CourseToCourseFullText;
|
||||
import com.xboe.module.course.service.ICourseContentService;
|
||||
import com.xboe.module.course.service.ICourseFullTextSearch;
|
||||
import com.xboe.module.course.service.ICourseService;
|
||||
import com.xboe.module.course.service.ICourseTeacherService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -63,7 +60,8 @@ public class CourseFullTextApi extends ApiBaseController{
|
||||
ICourseFullTextSearch fullTextSearch;
|
||||
@Resource
|
||||
IOrganizationService organizationService;
|
||||
|
||||
@Autowired
|
||||
ICourseTagService courseTagService;
|
||||
@Resource
|
||||
IStudyCourseService IStudyCourseService;
|
||||
|
||||
@@ -310,7 +308,18 @@ public class CourseFullTextApi extends ApiBaseController{
|
||||
}
|
||||
|
||||
paras.setDevice(dto.getDevice());
|
||||
|
||||
String tagIds = dto.getTags();
|
||||
if (tagIds != null && tagIds != ""){
|
||||
paras.setTags(tagIds);
|
||||
}else {
|
||||
String tagName = paras.getKeywords();
|
||||
if (tagName != null && tagName != ""){
|
||||
CourseTag courseTag = courseTagService.getTagByName(tagName);
|
||||
if (courseTag != null){
|
||||
paras.setTags(courseTag.getId().toString()+",");
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
//后续会根据当前用户的资源归属查询
|
||||
PageList<CourseFullText> coursePageList = fullTextSearch.search(ICourseFullTextSearch.DEFAULT_INDEX_NAME,pager.getStartRow(), pager.getPageSize(),paras);
|
||||
|
||||
@@ -9,6 +9,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.xboe.api.ThirdApi;
|
||||
import com.xboe.module.course.dto.*;
|
||||
import com.xboe.module.course.entity.*;
|
||||
import com.xboe.module.course.service.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -28,19 +30,6 @@ import com.xboe.data.dto.UserOrgIds;
|
||||
import com.xboe.data.outside.IOutSideDataService;
|
||||
import com.xboe.data.service.IDataUserSyncService;
|
||||
import com.xboe.module.assistance.service.IEmailService;
|
||||
import com.xboe.module.course.entity.Course;
|
||||
import com.xboe.module.course.entity.CourseContent;
|
||||
import com.xboe.module.course.entity.CourseCrowd;
|
||||
import com.xboe.module.course.entity.CourseHRBPAudit;
|
||||
import com.xboe.module.course.entity.CourseSection;
|
||||
import com.xboe.module.course.entity.CourseTeacher;
|
||||
import com.xboe.module.course.entity.CourseUpdateLog;
|
||||
import com.xboe.module.course.service.ICourseContentService;
|
||||
import com.xboe.module.course.service.ICourseCrowdService;
|
||||
import com.xboe.module.course.service.ICourseHRBPAuditService;
|
||||
import com.xboe.module.course.service.ICourseSectionService;
|
||||
import com.xboe.module.course.service.ICourseService;
|
||||
import com.xboe.module.course.service.ICourseTeacherService;
|
||||
import com.xboe.module.excel.ExportsExcelSenderUtil;
|
||||
import com.xboe.standard.enums.BoedxContentType;
|
||||
import com.xboe.standard.enums.BoedxCourseType;
|
||||
@@ -91,10 +80,10 @@ public class CourseManageApi extends ApiBaseController{
|
||||
|
||||
@Resource
|
||||
private ICourseHRBPAuditService hrbpAuditService;
|
||||
|
||||
@Resource
|
||||
private ICourseTagService tagService;
|
||||
@Resource
|
||||
IOutSideDataService outSideDataService;
|
||||
|
||||
@Autowired
|
||||
IDataUserSyncService userSyncService;
|
||||
@Resource
|
||||
@@ -169,14 +158,18 @@ public class CourseManageApi extends ApiBaseController{
|
||||
List<CourseSection> sectionlist=sectionService.getByCourseId(id);
|
||||
List<CourseTeacher> teachers=courseService.findTeachersByCourseId(id);
|
||||
List<CourseCrowd> crowds=courseService.findCrowdByCourseId(id);
|
||||
|
||||
//
|
||||
if (StringUtils.isNotBlank(course.getTags())){
|
||||
List<CourseTag> tagList = tagService.getTagsByIds(course.getTags());
|
||||
rs.put("tagList", tagList);
|
||||
}
|
||||
|
||||
|
||||
rs.put("course",course);
|
||||
rs.put("contents",cclist);
|
||||
rs.put("sections",sectionlist);
|
||||
rs.put("teachers",teachers);
|
||||
rs.put("crowds",crowds);
|
||||
|
||||
|
||||
|
||||
return success(rs);
|
||||
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
package com.xboe.module.course.api;
|
||||
|
||||
import com.xboe.common.OrderCondition;
|
||||
import com.xboe.common.PageList;
|
||||
import com.xboe.common.Pagination;
|
||||
import com.xboe.core.CurrentUser;
|
||||
import com.xboe.core.JsonResponse;
|
||||
import com.xboe.core.api.ApiBaseController;
|
||||
import com.xboe.core.orm.FieldFilters;
|
||||
import com.xboe.core.orm.IFieldFilter;
|
||||
import com.xboe.module.article.entity.Article;
|
||||
import com.xboe.module.article.service.IArticleService;
|
||||
import com.xboe.module.course.dto.CourseTagQueryDto;
|
||||
import com.xboe.module.course.dto.CourseTagRelationDto;
|
||||
import com.xboe.module.course.entity.CourseTag;
|
||||
import com.xboe.module.course.entity.CourseTagRelation;
|
||||
import com.xboe.module.course.service.ICourseTagService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ClassName:CourseTagApi
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/2614:27
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping(value="/xboe/m/coursetag")
|
||||
public class CourseTagApi extends ApiBaseController {
|
||||
|
||||
@Resource
|
||||
ICourseTagService courseTagService;
|
||||
|
||||
/**
|
||||
* 标签列表:分页查询
|
||||
* @param pager
|
||||
* @param courseTagQueryDto
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value="/page",method= {RequestMethod.GET,RequestMethod.POST})
|
||||
public JsonResponse<PageList<CourseTag>> find(Pagination pager, CourseTagQueryDto courseTagQueryDto){
|
||||
List<IFieldFilter> filters=new ArrayList<IFieldFilter>();
|
||||
OrderCondition order = null;
|
||||
if (courseTagQueryDto != null){
|
||||
String tagId = courseTagQueryDto.getId();
|
||||
String tagName = courseTagQueryDto.getTagName();
|
||||
Boolean isHot = courseTagQueryDto.getIsHot();
|
||||
String orderField = courseTagQueryDto.getOrderField();
|
||||
Boolean isAsc = courseTagQueryDto.getOrderAsc();
|
||||
if (StringUtils.isNotBlank(tagId)){
|
||||
filters.add(FieldFilters.eq("id",tagId));
|
||||
}
|
||||
//课程标签名称:模糊查询
|
||||
if (StringUtils.isNotBlank(tagName)){
|
||||
filters.add(FieldFilters.like("tagName",tagName));
|
||||
}
|
||||
// 构建排序条件:支持先按lastSetHotTime降序,再按动态字段升/降序排列
|
||||
if (isHot !=null ){
|
||||
filters.add(FieldFilters.eq("isHot",isHot));
|
||||
//order = OrderCondition.desc("lastSetHotTime");//固定降序
|
||||
}
|
||||
// 动态排序处理
|
||||
if (StringUtils.isNotBlank(orderField)) {
|
||||
if (order == null) {
|
||||
order = isAsc ? OrderCondition.asc(orderField) : OrderCondition.desc(orderField);
|
||||
} else {
|
||||
order = isAsc ? order.asc(orderField) : order.desc(orderField); // 链式追加排序条件
|
||||
}
|
||||
}
|
||||
}
|
||||
PageList<CourseTag> list=courseTagService.query(pager.getPageIndex(),pager.getPageSize(),filters,order);
|
||||
return success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改指定id的课程标签的公共属性
|
||||
* @param id
|
||||
* @param isPublic
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value="/changePublicStatus",method= RequestMethod.POST)
|
||||
public JsonResponse<Void> changePublicStatus(Long id,Boolean isPublic){
|
||||
courseTagService.changePublicStatus(id,isPublic);
|
||||
return success(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改指定id的课程标签的热点属性
|
||||
* @param id
|
||||
* @param isHot
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value="/changeHotStatus",method= RequestMethod.POST)
|
||||
public JsonResponse<Boolean> changeHotStatus(Long id,Boolean isHot){
|
||||
return courseTagService.changeHotStatus(id,isHot);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询:指定id的标签关联的所有课程
|
||||
* @param courseTagQueryDto
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value="/showCourseByTag",method= RequestMethod.POST)
|
||||
public JsonResponse<PageList<CourseTagRelationDto>> showCourseByTag(Pagination pager, CourseTagQueryDto courseTagQueryDto){
|
||||
PageList<CourseTagRelationDto> list=null;
|
||||
if (courseTagQueryDto != null) {
|
||||
Long tagId = Long.valueOf(courseTagQueryDto.getId());
|
||||
Boolean isAsc = courseTagQueryDto.getOrderAsc()!=null?courseTagQueryDto.getOrderAsc():false;
|
||||
list=courseTagService.getCourseByTag(pager.getPageIndex(),pager.getPageSize(),tagId,isAsc);
|
||||
}
|
||||
return success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解除指定id的课程和某个标签之间的关联关系
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value="/unbind",method= RequestMethod.POST)
|
||||
public JsonResponse<Void> unbindCourseTagRelation(CourseTagRelationDto courseTagRelationDto){
|
||||
if (courseTagRelationDto!=null){
|
||||
courseTagService.unbind(courseTagRelationDto.getId());
|
||||
return success(null);
|
||||
}
|
||||
return error("解绑失败!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊检索标签
|
||||
* @return 符合检索条件的所有公共标签
|
||||
*/
|
||||
@RequestMapping(value="/searchTags",method= RequestMethod.POST)
|
||||
public JsonResponse<List<CourseTag>> searchTags(String tagName){
|
||||
if (StringUtils.isNotBlank(tagName)){
|
||||
List<CourseTag> courseTagList = courseTagService.searchTags(tagName);
|
||||
return success(courseTagList);
|
||||
}
|
||||
return error("服务器端异常!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新标签,并与当前课程绑定
|
||||
* @param courseTagRelationDto
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value="/createTag",method= RequestMethod.POST)
|
||||
public JsonResponse<CourseTag> createTag(CourseTagRelationDto courseTagRelationDto){
|
||||
if (courseTagRelationDto!=null){
|
||||
CourseTag courseTag = courseTagService.createTag(courseTagRelationDto);
|
||||
return success(courseTag);
|
||||
}
|
||||
return error("创建标签失败!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新标签,并与当前课程绑定
|
||||
* @param courseTagRelationDto
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value="/getHotTagList",method= RequestMethod.POST)
|
||||
public JsonResponse<List<CourseTag>> getHotTagList(CourseTagRelationDto courseTagRelationDto){
|
||||
List<CourseTag> hotTagList = courseTagService.getHotTagList(courseTagRelationDto);
|
||||
return success(hotTagList);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.xboe.module.course.dao;
|
||||
|
||||
import com.xboe.common.OrderCondition;
|
||||
import com.xboe.common.PageList;
|
||||
import com.xboe.core.SysConstant;
|
||||
import com.xboe.core.orm.BaseDao;
|
||||
import com.xboe.core.orm.FieldFilters;
|
||||
import com.xboe.core.orm.IFieldFilter;
|
||||
import com.xboe.core.orm.IQuery;
|
||||
import com.xboe.module.course.entity.Course;
|
||||
import com.xboe.module.course.entity.CourseFile;
|
||||
import com.xboe.module.course.entity.CourseTag;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ClassName:CourseTagDao
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/2516:50
|
||||
*/
|
||||
@Repository
|
||||
public class CourseTagDao extends BaseDao<CourseTag> {
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
/**
|
||||
* 获取热门标签列表(前10条)
|
||||
* @return 热门标签列表
|
||||
*/
|
||||
public List<CourseTag> getHotTagList() {
|
||||
// 原生SQL:注意表名和列名需与数据库实际一致
|
||||
String sql = "select t.*,COUNT(r.tag_id) AS relation_count\n" +
|
||||
"from boe_course_tag t\n" +
|
||||
"left join boe_course_tag_relation r\n" +
|
||||
"on t.id = r.tag_id\n" +
|
||||
"where t.is_hot = true\n" +
|
||||
"GROUP BY t.id\n" +
|
||||
"order by t.last_set_hot_time desc,relation_count desc"; // 数据库字段为last_set_hot_time
|
||||
|
||||
// 创建原生查询并指定结果映射到CourseTag实体
|
||||
javax.persistence.Query query = entityManager.createNativeQuery(sql, CourseTag.class);
|
||||
|
||||
// 分页:取前10条
|
||||
query.setFirstResult(0);
|
||||
query.setMaxResults(10);
|
||||
|
||||
// 执行查询并返回结果(已映射为CourseTag类型)
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据课程类型获取热门标签列表(前10条)
|
||||
* @param sysType1 系统类型1
|
||||
* @param sysType2 系统类型2
|
||||
* @param sysType3 系统类型3
|
||||
* @return 热门标签列表
|
||||
*/
|
||||
public List<CourseTag> getHotTagListBySysTypes(String sysType1, String sysType2, String sysType3) {
|
||||
// 原生SQL:注意表名和列名需与数据库实际一致(此处假设表名为course_tag、course_type_tag_relation)
|
||||
String sql = "SELECT DISTINCT c.* FROM boe_course_tag c " +
|
||||
"JOIN boe_course_type_tag_relation r ON c.id = r.tag_id " +
|
||||
"WHERE r.deleted = 0 " +
|
||||
"AND c.is_hot = true "; // 假设数据库字段为is_hot(与实体属性isHot对应)
|
||||
if (StringUtils.isNotBlank(sysType1)){
|
||||
sql += "AND r.sys_type1 = ?1 ORDER BY c.last_set_hot_time DESC";
|
||||
}else if(StringUtils.isNotBlank(sysType2)){
|
||||
sql += "AND r.sys_type2 = ?1 ORDER BY c.last_set_hot_time DESC";
|
||||
}else {
|
||||
sql += "AND r.sys_type3 = ?1 ORDER BY c.last_set_hot_time DESC";
|
||||
}
|
||||
// 创建原生查询并指定结果映射到CourseTag实体
|
||||
javax.persistence.Query query = entityManager.createNativeQuery(sql, CourseTag.class);
|
||||
|
||||
// 绑定参数(注意参数索引从1开始)
|
||||
if (StringUtils.isNotBlank(sysType1)){
|
||||
query.setParameter(1, sysType1);
|
||||
} else if (StringUtils.isNotBlank(sysType2)) {
|
||||
query.setParameter(1, sysType2);
|
||||
}else {
|
||||
query.setParameter(1, sysType3);
|
||||
}
|
||||
// 分页:取前10条
|
||||
query.setFirstResult(0);
|
||||
query.setMaxResults(10);
|
||||
|
||||
// 执行查询并返回结果(已映射为CourseTag类型)
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<CourseTag> getTagsByIds(String id) {
|
||||
String sql = "select * from " + SysConstant.TABLE_PRE + "course_tag where id in (" + id + "0)";
|
||||
// 创建原生查询并指定结果映射到CourseTag实体
|
||||
javax.persistence.Query query = entityManager.createNativeQuery(sql, CourseTag.class);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public CourseTag getTagByName(String tagName) {
|
||||
CourseTag courseTag = this.findOne((FieldFilters.eq("tag_name", tagName)));
|
||||
return courseTag;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.xboe.module.course.dao;
|
||||
|
||||
import com.xboe.common.PageList;
|
||||
import com.xboe.core.orm.BaseDao;
|
||||
import com.xboe.module.course.dto.CourseTagRelationDto;
|
||||
import com.xboe.module.course.entity.CourseTagRelation;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @ClassName:CourseTagRelationDao
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/2815:09
|
||||
*/
|
||||
@Repository
|
||||
public class CourseTagRelationDao extends BaseDao<CourseTagRelation> {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
private String sqlStr = "SELECT " +
|
||||
" r1.id as id, " +
|
||||
" c.id as courseId, " +
|
||||
" r1.tag_id as tagId, " +
|
||||
" c.`name` as courseName, " +
|
||||
" r1.sys_create_by as sysCreateBy, " +
|
||||
" r1.sys_create_time as sysCreateTime, " +
|
||||
" COALESCE(GROUP_CONCAT(DISTINCT t.tag_name ORDER BY t.tag_name), '') AS otherTags " +
|
||||
"FROM " +
|
||||
" boe_course c " +
|
||||
"JOIN " +
|
||||
" boe_course_tag_relation r1 ON c.id = r1.course_id " +
|
||||
"LEFT JOIN " +
|
||||
" ( " +
|
||||
" boe_course_tag_relation r2 " +
|
||||
" JOIN boe_course_tag t ON r2.tag_id = t.id AND t.deleted = 0 " +
|
||||
" ) " +
|
||||
" ON c.id = r2.course_id AND r2.tag_id != r1.tag_id " +
|
||||
"WHERE " +
|
||||
" r1.tag_id = :tagId AND r1.deleted = 0 " +
|
||||
" AND c.id IN ( " +
|
||||
" SELECT course_id " +
|
||||
" FROM boe_course_tag_relation " +
|
||||
" WHERE tag_id = :tagId " +
|
||||
" ) " +
|
||||
"GROUP BY " +
|
||||
" c.id, c.`name` ";
|
||||
|
||||
public PageList<CourseTagRelationDto> findCoursesWithRelatedTagsDesc(Integer pageIndex, Integer pageSize, Long tagId){
|
||||
String sql = sqlStr + " ORDER BY r1.sys_create_time DESC";
|
||||
Query query = entityManager.createNativeQuery(sql);
|
||||
query.setParameter("tagId", tagId);
|
||||
query.setFirstResult((pageIndex - 1) * pageSize); // 设置起始位置
|
||||
query.setMaxResults(pageSize); // 设置每页大小
|
||||
|
||||
Query countQuery = entityManager.createNativeQuery(sql);
|
||||
countQuery.setParameter("tagId", tagId);
|
||||
List<Object[]> totalresults = countQuery.getResultList();
|
||||
|
||||
List<Object[]> results = query.getResultList();
|
||||
List<CourseTagRelationDto> list = results.stream()
|
||||
.map(row -> {
|
||||
String id = String.valueOf(row[0]);
|
||||
String courseId = String.valueOf(row[1]);
|
||||
String tagId2 = String.valueOf(row[2]);
|
||||
return new CourseTagRelationDto(
|
||||
id,
|
||||
courseId,
|
||||
tagId2,
|
||||
(String) row[3],
|
||||
(String) row[4],
|
||||
(Date) row[5],
|
||||
(String) row[6]
|
||||
);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
return new PageList<CourseTagRelationDto>(list,totalresults!=null?totalresults.size():0);
|
||||
}
|
||||
|
||||
public PageList<CourseTagRelationDto> findCoursesWithRelatedTagsAsc(Integer pageIndex, Integer pageSize, Long tagId) {
|
||||
String sql = sqlStr + " ORDER BY r1.sys_create_time ASC";
|
||||
Query query = entityManager.createNativeQuery(sql);
|
||||
query.setParameter("tagId", tagId);
|
||||
query.setFirstResult((pageIndex - 1) * pageSize); // 设置起始位置
|
||||
query.setMaxResults(pageSize); // 设置每页大小
|
||||
|
||||
Query countQuery = entityManager.createNativeQuery(sql);
|
||||
countQuery.setParameter("tagId", tagId);
|
||||
List<Object[]> totalresults = countQuery.getResultList();
|
||||
|
||||
List<Object[]> results = query.getResultList();
|
||||
List<CourseTagRelationDto> list = results.stream()
|
||||
.map(row ->{
|
||||
String id = String.valueOf(row[0]);
|
||||
String courseId = String.valueOf(row[1]);
|
||||
String tagId2 = String.valueOf(row[2]);
|
||||
return new CourseTagRelationDto(
|
||||
id,
|
||||
courseId,
|
||||
tagId2,
|
||||
(String) row[3],
|
||||
(String) row[4],
|
||||
(Date) row[5],
|
||||
(String) row[6]
|
||||
);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
return new PageList<CourseTagRelationDto>(list,totalresults!=null?totalresults.size():0);
|
||||
}
|
||||
|
||||
public boolean countHotTags() {
|
||||
String sql = "SELECT COUNT(*) FROM boe_course_tag WHERE is_hot = 1";
|
||||
Query query = entityManager.createNativeQuery(sql);
|
||||
Object result = query.getSingleResult();
|
||||
long count = Long.parseLong(result.toString());
|
||||
return count >= 10;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.xboe.module.course.dao;
|
||||
|
||||
import com.xboe.core.orm.BaseDao;
|
||||
import com.xboe.module.course.entity.CourseTagRelation;
|
||||
import com.xboe.module.course.entity.CourseTypeTagRelation;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @ClassName:CourseTypeTagRelationDao
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/8/113:42
|
||||
*/
|
||||
@Repository
|
||||
public class CourseTypeTagRelationDao extends BaseDao<CourseTypeTagRelation> {
|
||||
|
||||
|
||||
}
|
||||
@@ -140,4 +140,5 @@ public class CourseQueryDto {
|
||||
*/
|
||||
private String userId;
|
||||
|
||||
private String tags;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.xboe.module.course.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 课程标签查询的条件对象
|
||||
* @ClassName:CourseTagQueryDto
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/2517:02
|
||||
*/
|
||||
@Data
|
||||
public class CourseTagQueryDto {
|
||||
|
||||
/**
|
||||
* 标签id
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 标签名称
|
||||
*/
|
||||
private String tagName;
|
||||
|
||||
|
||||
/**
|
||||
* 是否热点标签( 0-否(默认) 1-是)
|
||||
*/
|
||||
private Boolean isHot;
|
||||
|
||||
/**
|
||||
* 排序字段
|
||||
*/
|
||||
private String orderField;
|
||||
|
||||
/**
|
||||
* 排序顺序
|
||||
*/
|
||||
private Boolean orderAsc;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.xboe.module.course.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @ClassName:CourseTagRelationDto
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/2815:00
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class CourseTagRelationDto{
|
||||
|
||||
private String id;
|
||||
private String courseId;
|
||||
private String tagId;
|
||||
private String tagName;
|
||||
private String courseName;
|
||||
private String sysCreateBy;
|
||||
private Date sysCreateTime;
|
||||
private String otherTags; // 改为字符串类型,与 GROUP_CONCAT 结果匹配
|
||||
private String sysType1;
|
||||
private String sysType2;
|
||||
private String sysType3;
|
||||
|
||||
// 添加匹配查询字段顺序的构造函数
|
||||
public CourseTagRelationDto(
|
||||
String id,
|
||||
String courseId,
|
||||
String tagId,
|
||||
String courseName,
|
||||
String sysCreateBy,
|
||||
Date sysCreateTime,
|
||||
String otherTags
|
||||
) {
|
||||
this.id = id;
|
||||
this.courseId = courseId;
|
||||
this.tagId = tagId;
|
||||
this.courseName = courseName;
|
||||
this.sysCreateBy = sysCreateBy;
|
||||
this.sysCreateTime = sysCreateTime;
|
||||
this.otherTags = otherTags;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.xboe.module.course.entity;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.xboe.core.SysConstant;
|
||||
import com.xboe.core.orm.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 在线课程的标签类
|
||||
* @ClassName:CourseTag
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/25 16:37
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Entity
|
||||
@Table(name = SysConstant.TABLE_PRE+"course_tag")
|
||||
public class CourseTag extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 标签名称
|
||||
*/
|
||||
@Column(name = "tag_name",nullable=false, length = 50)
|
||||
private String tagName;
|
||||
|
||||
/**
|
||||
* 是否设置为公共标签
|
||||
*/
|
||||
@Column(name = "is_public",length = 1)
|
||||
private Boolean isPublic;
|
||||
|
||||
/**
|
||||
* 是否设置为热点标签
|
||||
*/
|
||||
@Column(name = "is_hot",length = 1)
|
||||
private Boolean isHot;
|
||||
|
||||
/**
|
||||
* 使用次数(关联课程数)
|
||||
*/
|
||||
@Column(name = "use_count",length = 1)
|
||||
private Integer useCount;
|
||||
|
||||
/**
|
||||
* 最近设置为公共标签的时间
|
||||
*/
|
||||
@Column(name = "last_set_public_time", nullable = true)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime lastSetPublicTime;
|
||||
|
||||
/**
|
||||
* 最近设置为热点标签的时间
|
||||
*/
|
||||
@Column(name = "last_set_hot_time", nullable = true)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime lastSetHotTime;
|
||||
|
||||
public CourseTag() {
|
||||
|
||||
}
|
||||
public CourseTag(Long id, Boolean isPublic,Boolean isHot) {
|
||||
this.setId(String.valueOf(id));
|
||||
this.isPublic=isPublic;
|
||||
this.isHot=isHot;
|
||||
}
|
||||
|
||||
|
||||
public CourseTag(String id,String tagName,String sysCreateBy,String sysCreateAid,LocalDateTime sysCreateTime,
|
||||
Boolean isPublic,Boolean isHot,Integer useCount,LocalDateTime lastSetPublicTime,LocalDateTime lastSetHotTime,Boolean deleted){
|
||||
this.setId(id);
|
||||
this.setTagName(tagName);
|
||||
super.setSysCreateBy(sysCreateBy);
|
||||
super.setSysCreateAid(sysCreateAid);
|
||||
super.setSysCreateTime(sysCreateTime);
|
||||
this.isPublic = isPublic;
|
||||
this.isHot = isHot;
|
||||
this.useCount = useCount;
|
||||
this.lastSetPublicTime = lastSetPublicTime;
|
||||
this.lastSetHotTime = lastSetHotTime;
|
||||
super.setDeleted(deleted);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.xboe.module.course.entity;
|
||||
|
||||
import com.xboe.core.SysConstant;
|
||||
import com.xboe.core.orm.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* @ClassName:CourseTagRelation
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/2814:54
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Entity
|
||||
@Table(name = SysConstant.TABLE_PRE+"course_tag_relation")
|
||||
public class CourseTagRelation extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 课程Id
|
||||
*/
|
||||
@Column(name = "course_id",length = 20)
|
||||
private Long courseId;
|
||||
|
||||
/**
|
||||
* 标签id
|
||||
*/
|
||||
@Column(name = "tag_id",length = 20)
|
||||
private Long tagId;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.xboe.module.course.entity;
|
||||
|
||||
import com.xboe.core.SysConstant;
|
||||
import com.xboe.core.orm.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* @ClassName:CourseTypeTagRelation
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/8/111:02
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Entity
|
||||
@Table(name = SysConstant.TABLE_PRE+"course_type_tag_relation")
|
||||
public class CourseTypeTagRelation extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Column(name = "sys_type1",length = 20)
|
||||
private String sysType1;
|
||||
|
||||
@Column(name = "sys_type2",length = 20)
|
||||
private String sysType2;
|
||||
|
||||
@Column(name = "sys_type3",length = 20)
|
||||
private String sysType3;
|
||||
|
||||
@Column(name = "tag_id",length = 20)
|
||||
private String tagId;
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -52,6 +52,7 @@ public class CourseToCourseFullText {
|
||||
cft.setTeacher("");
|
||||
cft.setTeacherCode("");
|
||||
cft.setType(c.getType());
|
||||
cft.setTags(c.getTags());
|
||||
if(c.getOpenCourse()==null) {
|
||||
cft.setOpenCourse(0);
|
||||
}else {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.xboe.module.course.service;
|
||||
|
||||
import com.xboe.common.OrderCondition;
|
||||
import com.xboe.common.PageList;
|
||||
import com.xboe.core.JsonResponse;
|
||||
import com.xboe.core.orm.IFieldFilter;
|
||||
import com.xboe.module.course.dto.CourseTagRelationDto;
|
||||
import com.xboe.module.course.entity.CourseTag;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @InterfaceName:ICourseTagService
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/2516:53
|
||||
*/
|
||||
public interface ICourseTagService {
|
||||
|
||||
/**
|
||||
* 分页查询标签列表,使用自定义filter
|
||||
* @param pageIndex
|
||||
* @param pageSize
|
||||
* @return
|
||||
*/
|
||||
PageList<CourseTag> query(Integer pageIndex, Integer pageSize, List<IFieldFilter> filters, OrderCondition order);
|
||||
|
||||
/**
|
||||
* 分页查询指定id标签关联的课程列表,使用自定义filter
|
||||
* @param pageIndex
|
||||
* @param pageSize
|
||||
* @return
|
||||
*/
|
||||
PageList<CourseTagRelationDto> getCourseByTag(Integer pageIndex, Integer pageSize, Long tagId, Boolean isAsc);
|
||||
|
||||
/**
|
||||
* 修改指定id的课程标签的公共属性
|
||||
* @param id
|
||||
* @param isPublic
|
||||
* @return
|
||||
*/
|
||||
void changePublicStatus(Long id,Boolean isPublic);
|
||||
|
||||
/**
|
||||
* 修改指定id的课程标签的热点属性
|
||||
*
|
||||
* @param id
|
||||
* @param isHot
|
||||
* @return
|
||||
*/
|
||||
JsonResponse<Boolean> changeHotStatus(Long id, Boolean isHot);
|
||||
|
||||
/**
|
||||
* 解除指定id的课程和某个标签之间的关联关系
|
||||
* @return
|
||||
*/
|
||||
void unbind(String id);
|
||||
|
||||
/**
|
||||
* 根据标签名称进行检索(模糊查询)
|
||||
* @param tagName
|
||||
* @return 符合检索条件的所有公共标签
|
||||
*/
|
||||
List<CourseTag> searchTags(String tagName);
|
||||
|
||||
/**
|
||||
* 创建新标签,并与当前课程绑定
|
||||
* @param courseTagRelationDto
|
||||
* @return
|
||||
*/
|
||||
CourseTag createTag(CourseTagRelationDto courseTagRelationDto);
|
||||
|
||||
/**
|
||||
* 根据课程类型获取热点标签
|
||||
* @param courseTagRelationDto
|
||||
* @return
|
||||
*/
|
||||
List<CourseTag> getHotTagList(CourseTagRelationDto courseTagRelationDto);
|
||||
|
||||
/**
|
||||
* 根据多个id获取标签
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
List<CourseTag> getTagsByIds(String id);
|
||||
|
||||
CourseTag getTagByName(String tagName);
|
||||
|
||||
void bindTag(String id, String tags);
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import javax.management.Query;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.xboe.api.ThirdApi;
|
||||
import com.xboe.core.orm.*;
|
||||
import com.xboe.module.course.service.ICourseTagService;
|
||||
import com.xboe.school.study.dao.StudyCourseDao;
|
||||
import com.xboe.school.study.entity.StudyCourse;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -98,7 +99,8 @@ public class CourseServiceImpl implements ICourseService {
|
||||
|
||||
@Resource
|
||||
private CourseHRBPAuditDao courseHRBPAuditDao;
|
||||
|
||||
@Resource
|
||||
private ICourseTagService courseTagService;
|
||||
|
||||
@Resource
|
||||
private SysLogAuditDao logAuditDao;//审核日志记录
|
||||
@@ -491,7 +493,7 @@ public class CourseServiceImpl implements ICourseService {
|
||||
String sql = "SELECT DISTINCT\n" +
|
||||
"rt.course_id\n" +
|
||||
"FROM\n" +
|
||||
"boe_new.student s INNER JOIN boe_new.router_task rt on s.pid=rt.router_id inner join boe_course c on c.id=rt.course_id\n" +
|
||||
"boe.student s INNER JOIN boe.router_task rt on s.pid=rt.router_id inner join boe_course c on c.id=rt.course_id\n" +
|
||||
"\n" +
|
||||
"WHERE\n" +
|
||||
"\n" +
|
||||
@@ -514,7 +516,7 @@ public class CourseServiceImpl implements ICourseService {
|
||||
String sql = "SELECT DISTINCT\n" +
|
||||
"pt.course_id\n" +
|
||||
"FROM\n" +
|
||||
"boe_new.student s INNER JOIN boe_new.project_task pt on s.pid=pt.project_id inner join boe_course c on c.id=pt.course_id\n" +
|
||||
"boe.student s INNER JOIN boe.project_task pt on s.pid=pt.project_id inner join boe_course c on c.id=pt.course_id\n" +
|
||||
"\n" +
|
||||
"WHERE\n" +
|
||||
"\n" +
|
||||
@@ -571,8 +573,8 @@ public class CourseServiceImpl implements ICourseService {
|
||||
String sql = "SELECT DISTINCT\n" +
|
||||
"\tc.id \n" +
|
||||
"FROM\n" +
|
||||
"\tboe_new.student s\n" +
|
||||
"\tINNER JOIN boe_new.grow_task gt ON s.pid = gt.grow_id\n" +
|
||||
"\tboe.student s\n" +
|
||||
"\tINNER JOIN boe.grow_task gt ON s.pid = gt.grow_id\n" +
|
||||
"\tINNER JOIN boe_course c ON gt.course_id = c.id \n" +
|
||||
"WHERE\n" +
|
||||
"\ts.type = 14 \n" +
|
||||
@@ -926,6 +928,7 @@ public class CourseServiceImpl implements ICourseService {
|
||||
courseCrowdDao.save(cc);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1022,7 +1025,9 @@ public class CourseServiceImpl implements ICourseService {
|
||||
publishUtil.removeByDocId(c.getFullTextId());
|
||||
|
||||
}
|
||||
|
||||
// 添加课程对应的标签
|
||||
String tags = full.getCourse().getTags();
|
||||
courseTagService.bindTag(c.getId(), tags);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,393 @@
|
||||
package com.xboe.module.course.service.impl;
|
||||
|
||||
import com.xboe.common.OrderCondition;
|
||||
import com.xboe.common.PageList;
|
||||
import com.xboe.core.JsonResponse;
|
||||
import com.xboe.core.orm.FieldFilters;
|
||||
import com.xboe.core.orm.IFieldFilter;
|
||||
import com.xboe.core.orm.QueryBuilder;
|
||||
import com.xboe.module.course.dao.CourseDao;
|
||||
import com.xboe.module.course.dao.CourseTagDao;
|
||||
import com.xboe.module.course.dao.CourseTagRelationDao;
|
||||
import com.xboe.module.course.dao.CourseTypeTagRelationDao;
|
||||
import com.xboe.module.course.dto.CourseTagRelationDto;
|
||||
import com.xboe.module.course.entity.Course;
|
||||
import com.xboe.module.course.entity.CourseTag;
|
||||
import com.xboe.module.course.entity.CourseTagRelation;
|
||||
import com.xboe.module.course.entity.CourseTypeTagRelation;
|
||||
import com.xboe.module.course.service.ICourseTagService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @ClassName:CourseTagServiceImpl
|
||||
* @author:zhengge@oracle.com
|
||||
* @since:2025/7/2516:55
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional
|
||||
public class CourseTagServiceImpl implements ICourseTagService {
|
||||
|
||||
@Resource
|
||||
private CourseTagDao courseTagDao;
|
||||
@Resource
|
||||
PublishCourseUtil publishUtil;
|
||||
@Resource
|
||||
private CourseTagRelationDao courseTagRelationDao;
|
||||
@Resource
|
||||
private CourseTypeTagRelationDao courseTypeTagRelationDao;
|
||||
@Resource
|
||||
private CourseDao courseDao;
|
||||
|
||||
/**
|
||||
* 课程标签分页查询
|
||||
* @param pageIndex
|
||||
* @param pageSize
|
||||
* @param filters
|
||||
* @param order
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PageList<CourseTag> query(Integer pageIndex, Integer pageSize, List<IFieldFilter> filters, OrderCondition order) {
|
||||
QueryBuilder query=QueryBuilder.from(CourseTag.class);
|
||||
query.setPageIndex(pageIndex);
|
||||
query.setPageSize(pageSize);
|
||||
filters.add(FieldFilters.eq("deleted",false));
|
||||
query.addFilters(filters);
|
||||
if(order!=null) {
|
||||
query.addOrder(order);
|
||||
}else {
|
||||
query.addOrder(OrderCondition.desc("sysCreateTime"));
|
||||
}
|
||||
return courseTagDao.findPage(query.builder());
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询指定id标签关联的课程
|
||||
* @param pageIndex
|
||||
* @param pageSize
|
||||
* @param tagId
|
||||
* @param isAsc
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PageList<CourseTagRelationDto> getCourseByTag(Integer pageIndex, Integer pageSize, Long tagId, Boolean isAsc) {
|
||||
PageList<CourseTagRelationDto> list = null;
|
||||
if(isAsc) {
|
||||
list = courseTagRelationDao.findCoursesWithRelatedTagsAsc(pageIndex,pageSize,tagId);
|
||||
}else {
|
||||
list = courseTagRelationDao.findCoursesWithRelatedTagsDesc(pageIndex,pageSize,tagId);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改指定id的课程标签的公共属性
|
||||
* @param id
|
||||
* @param isPublic
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void changePublicStatus(Long id, Boolean isPublic) {
|
||||
CourseTag courseTag = courseTagDao.findOne(FieldFilters.eq("id", String.valueOf(id)));
|
||||
if (courseTag!=null){
|
||||
courseTag.setIsPublic(isPublic);
|
||||
courseTag.setLastSetPublicTime(isPublic?LocalDateTime.now():null);
|
||||
courseTagDao.update(courseTag);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改指定id的课程标签的热点属性
|
||||
*
|
||||
* @param id
|
||||
* @param isHot
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public JsonResponse<Boolean> changeHotStatus(Long id, Boolean isHot) {
|
||||
// 当标签切换为热点标签时才会判断,超过十个热点标签则禁止设置
|
||||
JsonResponse<Boolean> objectJsonResponse = new JsonResponse<>();
|
||||
if (isHot){
|
||||
if (courseTagRelationDao.countHotTags()){
|
||||
objectJsonResponse.setStatus(400);
|
||||
objectJsonResponse.setMessage("超过十个热点标签,无法进行设置");
|
||||
objectJsonResponse.setResult(false);
|
||||
return objectJsonResponse;
|
||||
}
|
||||
}
|
||||
CourseTag courseTag = courseTagDao.findOne(FieldFilters.eq("id", String.valueOf(id)));
|
||||
if (courseTag!=null){
|
||||
courseTag.setIsHot(isHot);
|
||||
courseTag.setLastSetHotTime(isHot?LocalDateTime.now():null);
|
||||
courseTagDao.update(courseTag);
|
||||
}
|
||||
objectJsonResponse.setStatus(200);
|
||||
objectJsonResponse.setMessage("修改成功");
|
||||
return objectJsonResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解除指定id的课程和某个标签之间的关联关系
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void unbind(String id) {
|
||||
//根据主键查询关联关系
|
||||
CourseTagRelation courseTagRelation = courseTagRelationDao.findOne(FieldFilters.eq("id", id));
|
||||
if (courseTagRelation != null){
|
||||
//修改该标签关联课程数
|
||||
CourseTag courseTag = courseTagDao.findOne(FieldFilters.eq("id", String.valueOf(courseTagRelation.getTagId())));
|
||||
if (courseTag != null){
|
||||
courseTag.setUseCount(courseTag.getUseCount()>1?courseTag.getUseCount()-1:0);
|
||||
courseTagDao.updateFieldById(courseTag.getId(),"useCount",courseTag.getUseCount());
|
||||
}
|
||||
//解绑(删除关联关系)
|
||||
courseTagRelationDao.setDeleted(id);
|
||||
Course course = courseDao.get(courseTagRelation.getCourseId().toString());
|
||||
String tags = course.getTags();
|
||||
if (StringUtils.isNotBlank(tags)){
|
||||
String[] tagIds = tags.split(",");
|
||||
List<String> tagIdList = new ArrayList<>();
|
||||
for (String tagId : tagIds){
|
||||
if (!tagId.equals(courseTagRelation.getTagId().toString())){
|
||||
tagIdList.add(tagId);
|
||||
}
|
||||
}
|
||||
// 数据格式:1,2,3
|
||||
String s = StringUtils.join(tagIdList, ",");
|
||||
if (!"".equals(s)){
|
||||
s+=",";
|
||||
}
|
||||
course.setTags(s);
|
||||
}
|
||||
// 同步ES
|
||||
publishUtil.fullTextPublish(course);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标签名称进行检索(模糊查询)
|
||||
* @param tagName
|
||||
* @return 符合检索条件的所有公共标签
|
||||
*/
|
||||
@Override
|
||||
public List<CourseTag> searchTags(String tagName){
|
||||
QueryBuilder query=QueryBuilder.from(CourseTag.class);
|
||||
List<IFieldFilter> filters = new ArrayList<>();
|
||||
filters.add(FieldFilters.eq("deleted",false));//未删除
|
||||
filters.add(FieldFilters.eq("isPublic",true));//公共标签
|
||||
filters.add(FieldFilters.like("tagName",tagName));//模糊检索
|
||||
query.addFilters(filters);
|
||||
List<CourseTag> courseTagList = courseTagDao.findList(query.builder());
|
||||
return courseTagList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新标签,并与指定课程绑定
|
||||
* @param courseTagRelationDto
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public CourseTag createTag(CourseTagRelationDto courseTagRelationDto) {
|
||||
CourseTag courseTag = null;
|
||||
String tagName = courseTagRelationDto.getTagName();
|
||||
Long courseId = Long.valueOf(courseTagRelationDto.getCourseId());
|
||||
//1.创建标签:先判断是否已经存在该标签
|
||||
QueryBuilder query=QueryBuilder.from(CourseTag.class);
|
||||
List<IFieldFilter> filters = new ArrayList<>();
|
||||
filters.add(FieldFilters.eq("tagName",tagName));//精确匹配
|
||||
query.addFilters(filters);
|
||||
List<CourseTag> courseTagList = courseTagDao.findList(query.builder());
|
||||
if (courseTagList==null || courseTagList.size()==0){//1.1 如果该标签不存在,则新建标签
|
||||
courseTag = new CourseTag();
|
||||
courseTag.setTagName(tagName);
|
||||
courseTag.setIsPublic(false);
|
||||
courseTag.setIsHot(false);
|
||||
courseTag.setUseCount(1);
|
||||
courseTagDao.save(courseTag);
|
||||
//新建一条标签和课程的关联关系
|
||||
CourseTagRelation courseTagRelation = new CourseTagRelation();
|
||||
courseTagRelation.setTagId(Long.valueOf(courseTag.getId()));
|
||||
courseTagRelation.setCourseId(courseId);
|
||||
courseTagRelationDao.save(courseTagRelation);
|
||||
}else {//1.2 否则修改标签
|
||||
courseTag=courseTagList.get(0);
|
||||
// 当同一标签被3个及以上课管创建时,默认开启这个标签的公共化
|
||||
if(courseTag.getUseCount() >= 3){
|
||||
courseTag.setIsPublic(true);
|
||||
}
|
||||
courseTag.setDeleted(false);//有可能是之前被删除的标签,这里恢复为有效
|
||||
//查找改课程与这个标签是否已经建立关联关系
|
||||
query=QueryBuilder.from(CourseTagRelation.class);
|
||||
filters = new ArrayList<>();
|
||||
filters.add(FieldFilters.eq("tagId",Long.valueOf(courseTag.getId())));//精确匹配
|
||||
filters.add(FieldFilters.eq("courseId",courseId));//精确匹配
|
||||
query.addFilters(filters);
|
||||
List<CourseTagRelation> courseTagRelationList = courseTagRelationDao.findList(query.builder());
|
||||
//1.2.1 如果还未建立关联关系,则新建一条标签和课程的关联关系
|
||||
if (courseTagRelationList==null || courseTagRelationList.size()==0){
|
||||
CourseTagRelation courseTagRelation = new CourseTagRelation();
|
||||
courseTagRelation.setTagId(Long.valueOf(courseTag.getId()));
|
||||
courseTagRelation.setCourseId(courseId);
|
||||
courseTagRelationDao.save(courseTagRelation);
|
||||
//更新该标签的关联课程数量
|
||||
courseTag.setUseCount(courseTag.getUseCount()+1);
|
||||
}else {//1.2.2 否则修改该标签和课程的关联关系
|
||||
CourseTagRelation courseTagRelation = courseTagRelationList.get(0);
|
||||
if (courseTagRelation.getDeleted()){//之前"解绑",这里恢复为有效
|
||||
courseTagRelation.setDeleted(false);
|
||||
courseTagRelationDao.saveOrUpdate(courseTagRelation);
|
||||
//更新该标签的关联课程数量
|
||||
courseTag.setUseCount(courseTag.getUseCount()+1);
|
||||
}
|
||||
}
|
||||
courseTagDao.saveOrUpdate(courseTag);
|
||||
}
|
||||
//2.创建该标签和课程分类之间的关联关系
|
||||
courseTagRelationDto.setTagId(courseTag.getId());
|
||||
createCourseTypeAndTagRelation(courseTagRelationDto);
|
||||
return courseTag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindTag(String id, String tags) {
|
||||
// 将tags转换为数组
|
||||
String[] tagIds = tags.split(",");
|
||||
List<Long> tagIdList = new ArrayList<>();
|
||||
for (String tagId : tagIds){
|
||||
tagIdList.add(Long.valueOf(tagId));
|
||||
}
|
||||
for (Long tagId : tagIdList){
|
||||
QueryBuilder courseTagQuery=QueryBuilder.from(CourseTag.class);
|
||||
List<IFieldFilter> courseTagFilters = new ArrayList<>();
|
||||
courseTagFilters.add(FieldFilters.eq("id",tagId.toString()));//精确匹配
|
||||
courseTagQuery.addFilters(courseTagFilters);
|
||||
//修改该标签关联课程数
|
||||
CourseTag courseTag = courseTagDao.findOne(FieldFilters.eq("id", String.valueOf(tagId)));
|
||||
if (courseTag!=null){
|
||||
//更新该标签的关联课程数量
|
||||
courseTag.setUseCount(courseTag.getUseCount()+1);
|
||||
courseTagDao.saveOrUpdate(courseTag);
|
||||
}
|
||||
// 查询课程是否绑定了标签
|
||||
QueryBuilder query=QueryBuilder.from(CourseTagRelation.class);
|
||||
List<IFieldFilter> filters = new ArrayList<>();
|
||||
filters.add(FieldFilters.eq("courseId",Long.valueOf(id)));
|
||||
filters.add(FieldFilters.eq("tagId",Long.valueOf(tagId)));
|
||||
query.addFilters(filters);
|
||||
List<CourseTagRelation> courseTagRelationList = courseTagRelationDao.findList(query.builder());
|
||||
// 如果没有绑定标签,那么就进行绑定
|
||||
if (courseTagRelationList==null || courseTagRelationList.size()==0){
|
||||
CourseTagRelation courseTagRelation = new CourseTagRelation();
|
||||
courseTagRelation.setTagId(Long.valueOf(tagId));
|
||||
courseTagRelation.setCourseId(Long.valueOf(id));
|
||||
courseTagRelationDao.save(courseTagRelation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CourseTag getTagByName(String tagName) {
|
||||
CourseTag courseTag = courseTagDao.getTagByName(tagName);
|
||||
return courseTag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CourseTag> getTagsByIds(String id) {
|
||||
// id=17,18
|
||||
List<CourseTag> courseTagList = courseTagDao.getTagsByIds(id);
|
||||
return courseTagList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取热门标签
|
||||
* @param courseTagRelationDto
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<CourseTag> getHotTagList(CourseTagRelationDto courseTagRelationDto) {
|
||||
List<CourseTag> hotTagList = null;
|
||||
if (StringUtils.isNotBlank(courseTagRelationDto.getSysType1()) ||
|
||||
StringUtils.isNotBlank(courseTagRelationDto.getSysType2()) ||
|
||||
StringUtils.isNotBlank(courseTagRelationDto.getSysType3())){
|
||||
String sysType1 = courseTagRelationDto.getSysType1();
|
||||
String sysType2 = courseTagRelationDto.getSysType2();
|
||||
String sysType3 = courseTagRelationDto.getSysType3();
|
||||
hotTagList = courseTagDao.getHotTagListBySysTypes(sysType1,sysType2,sysType3);
|
||||
}else {
|
||||
hotTagList = courseTagDao.getHotTagList();
|
||||
}
|
||||
return hotTagList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建标签和课程分类之间的关联关系
|
||||
* @param courseTagRelationDto
|
||||
*/
|
||||
private void createCourseTypeAndTagRelation(CourseTagRelationDto courseTagRelationDto){
|
||||
String sysType1 = courseTagRelationDto!=null?courseTagRelationDto.getSysType1():null;
|
||||
String tagId = courseTagRelationDto!=null?courseTagRelationDto.getTagId():null;
|
||||
if (StringUtils.isNotBlank(sysType1) && StringUtils.isNotBlank(tagId)){
|
||||
String sysType2 = courseTagRelationDto.getSysType2();
|
||||
String sysType3 = courseTagRelationDto.getSysType3();
|
||||
//判断数据库中该课程分类和标签是否已经存在关联关系
|
||||
if (!isHadCourseTypeAndTagRelation(courseTagRelationDto,true)){//不存在,则新建
|
||||
CourseTypeTagRelation courseTypeTagRelation = new CourseTypeTagRelation();
|
||||
courseTypeTagRelation.setSysType1(sysType1);
|
||||
courseTypeTagRelation.setSysType2(StringUtils.isNotBlank(sysType2)?sysType2:"0");
|
||||
courseTypeTagRelation.setSysType3(StringUtils.isNotBlank(sysType3)?sysType3:"0");
|
||||
courseTypeTagRelation.setTagId(tagId);
|
||||
courseTypeTagRelationDao.save(courseTypeTagRelation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断数据库制定的课程分类和标签是否已经存在关联关系
|
||||
* @param courseTagRelationDto
|
||||
* @param clearFlag 清理标识 true:清理已存在的数据,只保留一条有效数据
|
||||
* @return true:已存在;false:不存在
|
||||
*/
|
||||
private Boolean isHadCourseTypeAndTagRelation(CourseTagRelationDto courseTagRelationDto,Boolean clearFlag){
|
||||
QueryBuilder query=QueryBuilder.from(CourseTypeTagRelation.class);
|
||||
List<IFieldFilter> filters = new ArrayList<>();
|
||||
filters.add(FieldFilters.eq("sysType1",courseTagRelationDto.getSysType1()));//一级分类
|
||||
filters.add(FieldFilters.eq("sysType2",courseTagRelationDto.getSysType1()));//二级分类
|
||||
filters.add(FieldFilters.eq("sysType3",courseTagRelationDto.getSysType1()));//三级分类
|
||||
filters.add(FieldFilters.eq("tagId",courseTagRelationDto.getTagId()));
|
||||
List<CourseTypeTagRelation> courseTypeTagRelList = courseTypeTagRelationDao.findList(query.addFilters(filters).builder());
|
||||
Boolean isExist = (courseTypeTagRelList!=null && courseTypeTagRelList.size()>0)?true:false;
|
||||
if (isExist && clearFlag ){
|
||||
List<CourseTypeTagRelation> toRemove = new ArrayList<>();
|
||||
for (CourseTypeTagRelation courseTypeTagRel : courseTypeTagRelList) {
|
||||
if (courseTypeTagRel.getDeleted()) {//如果是逻辑删的本次物理删除
|
||||
courseTypeTagRelationDao.getGenericDao().delete(courseTypeTagRel);
|
||||
toRemove.add(courseTypeTagRel);
|
||||
}
|
||||
}
|
||||
courseTypeTagRelList.removeAll(toRemove);//移除逻辑删的数据
|
||||
//如果还存在有效数据
|
||||
if (courseTypeTagRelList!=null && courseTypeTagRelList.size()>0){
|
||||
//只保留一条有效数据,其余物理删除
|
||||
for (int i = courseTypeTagRelList.size() - 1; i >= 1; i--) {
|
||||
CourseTypeTagRelation courseTypeTagRel = courseTypeTagRelList.get(i);
|
||||
if (courseTypeTagRel.getDeleted()) {
|
||||
courseTypeTagRelationDao.getGenericDao().delete(courseTypeTagRel);
|
||||
courseTypeTagRelList.remove(i); // 倒序删除不影响未遍历的索引
|
||||
}
|
||||
}
|
||||
isExist = true;//存在一条有效数据
|
||||
}else {
|
||||
isExist = false;//不存在有效数据了
|
||||
}
|
||||
}
|
||||
return isExist;
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,14 @@ xboe:
|
||||
image:
|
||||
course:
|
||||
default: http://192.168.0.253/pc/images/bgimg/course.png
|
||||
case:
|
||||
ai:
|
||||
base-url: http://10.10.181.114:30003
|
||||
app-key: 6e9be45319184ac793aa127c362b0f0b
|
||||
secret-key: db4d24279e3d6dbf1524af42cd0bedd2
|
||||
ai-api-code: 30800
|
||||
case-knowledge-id: de2e006e-82fb-4ace-8813-f25c316be4ff
|
||||
file-upload-callback-url: http://192.168.0.253:9090/xboe/m/boe/caseDocumentLog/uploadCallback
|
||||
xxl:
|
||||
job:
|
||||
accessToken: 65ddc683-22f5-83b4-de3a-3c97a0a29af0
|
||||
|
||||
@@ -111,6 +111,14 @@ xboe:
|
||||
image:
|
||||
course:
|
||||
default: http://10.251.132.75/pc/images/bgimg/course.png
|
||||
case:
|
||||
ai:
|
||||
base-url: http://10.10.181.114:30003
|
||||
app-key: 6e9be45319184ac793aa127c362b0f0b
|
||||
secret-key: db4d24279e3d6dbf1524af42cd0bedd2
|
||||
ai-api-code: 30800
|
||||
case-knowledge-id: de2e006e-82fb-4ace-8813-f25c316be4ff
|
||||
file-upload-callback-url: http://192.168.0.253:9090/xboe/m/boe/caseDocumentLog/uploadCallback
|
||||
jasypt:
|
||||
encryptor:
|
||||
algorithm: PBEWithMD5AndDES
|
||||
|
||||
@@ -77,6 +77,14 @@ xboe:
|
||||
image:
|
||||
course:
|
||||
default: https://u.boe.com/pc/images/bgimg/course.png
|
||||
case:
|
||||
ai:
|
||||
base-url: http://10.10.181.114:30003
|
||||
app-key: 6e9be45319184ac793aa127c362b0f0b
|
||||
secret-key: db4d24279e3d6dbf1524af42cd0bedd2
|
||||
ai-api-code: 30800
|
||||
case-knowledge-id: de2e006e-82fb-4ace-8813-f25c316be4ff
|
||||
file-upload-callback-url: http://192.168.0.253:9090/xboe/m/boe/caseDocumentLog/uploadCallback
|
||||
xxl:
|
||||
job:
|
||||
accessToken: 65ddc683-22f5-83b4-de3a-3c97a0a29af0
|
||||
|
||||
@@ -111,6 +111,14 @@ xboe:
|
||||
image:
|
||||
course:
|
||||
default: https://u-pre.boe.com/pc/images/bgimg/course.png
|
||||
case:
|
||||
ai:
|
||||
base-url: http://10.10.181.114:30003
|
||||
app-key: 6e9be45319184ac793aa127c362b0f0b
|
||||
secret-key: db4d24279e3d6dbf1524af42cd0bedd2
|
||||
ai-api-code: 30800
|
||||
case-knowledge-id: de2e006e-82fb-4ace-8813-f25c316be4ff
|
||||
file-upload-callback-url: http://192.168.0.253:9090/xboe/m/boe/caseDocumentLog/uploadCallback
|
||||
jasypt:
|
||||
encryptor:
|
||||
algorithm: PBEWithMD5AndDES
|
||||
|
||||
Reference in New Issue
Block a user