|
@@ -5,16 +5,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.pharmacopoeia.entity.Drug;
|
|
import com.pharmacopoeia.entity.Drug;
|
|
|
import com.pharmacopoeia.entity.KnowledgePoint;
|
|
import com.pharmacopoeia.entity.KnowledgePoint;
|
|
|
import com.pharmacopoeia.entity.Question;
|
|
import com.pharmacopoeia.entity.Question;
|
|
|
|
|
+import com.pharmacopoeia.entity.QuickAsk;
|
|
|
|
|
+import com.pharmacopoeia.entity.QuickAskAuditLog;
|
|
|
import com.pharmacopoeia.repository.DrugRepository;
|
|
import com.pharmacopoeia.repository.DrugRepository;
|
|
|
-import com.pharmacopoeia.repository.DrugChunkRepository;
|
|
|
|
|
import com.pharmacopoeia.repository.KnowledgePointRepository;
|
|
import com.pharmacopoeia.repository.KnowledgePointRepository;
|
|
|
import com.pharmacopoeia.repository.QuestionRepository;
|
|
import com.pharmacopoeia.repository.QuestionRepository;
|
|
|
-import com.pharmacopoeia.repository.QuickAskRepository;
|
|
|
|
|
import com.pharmacopoeia.repository.QuickAskAuditLogRepository;
|
|
import com.pharmacopoeia.repository.QuickAskAuditLogRepository;
|
|
|
-import com.pharmacopoeia.entity.QuickAsk;
|
|
|
|
|
-import com.pharmacopoeia.entity.QuickAskAuditLog;
|
|
|
|
|
|
|
+import com.pharmacopoeia.repository.QuickAskRepository;
|
|
|
|
|
+import com.pharmacopoeia.util.IpUtils;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
-
|
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -22,9 +21,6 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
|
|
-import com.pharmacopoeia.util.IpUtils;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
@@ -32,7 +28,6 @@ import java.util.function.Consumer;
|
|
|
public class AdminKnowledgeService {
|
|
public class AdminKnowledgeService {
|
|
|
|
|
|
|
|
private final DrugRepository drugRepository;
|
|
private final DrugRepository drugRepository;
|
|
|
- private final DrugChunkRepository drugChunkRepository;
|
|
|
|
|
private final KnowledgePointRepository knowledgePointRepository;
|
|
private final KnowledgePointRepository knowledgePointRepository;
|
|
|
private final QuestionRepository questionRepository;
|
|
private final QuestionRepository questionRepository;
|
|
|
private final JdbcTemplate jdbc;
|
|
private final JdbcTemplate jdbc;
|
|
@@ -42,14 +37,12 @@ public class AdminKnowledgeService {
|
|
|
private final QuickAskAuditLogRepository auditLogRepository;
|
|
private final QuickAskAuditLogRepository auditLogRepository;
|
|
|
|
|
|
|
|
public AdminKnowledgeService(DrugRepository drugRepository,
|
|
public AdminKnowledgeService(DrugRepository drugRepository,
|
|
|
- DrugChunkRepository drugChunkRepository,
|
|
|
|
|
KnowledgePointRepository knowledgePointRepository,
|
|
KnowledgePointRepository knowledgePointRepository,
|
|
|
QuestionRepository questionRepository,
|
|
QuestionRepository questionRepository,
|
|
|
QuickAskRepository quickAskRepository,
|
|
QuickAskRepository quickAskRepository,
|
|
|
QuickAskAuditLogRepository auditLogRepository,
|
|
QuickAskAuditLogRepository auditLogRepository,
|
|
|
JdbcTemplate jdbc) {
|
|
JdbcTemplate jdbc) {
|
|
|
this.drugRepository = drugRepository;
|
|
this.drugRepository = drugRepository;
|
|
|
- this.drugChunkRepository = drugChunkRepository;
|
|
|
|
|
this.knowledgePointRepository = knowledgePointRepository;
|
|
this.knowledgePointRepository = knowledgePointRepository;
|
|
|
this.questionRepository = questionRepository;
|
|
this.questionRepository = questionRepository;
|
|
|
this.quickAskRepository = quickAskRepository;
|
|
this.quickAskRepository = quickAskRepository;
|
|
@@ -299,31 +292,36 @@ public class AdminKnowledgeService {
|
|
|
|
|
|
|
|
// ==================== 快捷提问标签管理 ====================
|
|
// ==================== 快捷提问标签管理 ====================
|
|
|
|
|
|
|
|
- /** 获取所有激活的标签,按 sort_order 排序(公开接口,无需鉴权) */
|
|
|
|
|
|
|
+ /** 获取所有激活的标签,按 sort_order 排序(公开接口,无需鉴权),最多返回6个 */
|
|
|
public List<QuickAsk> listQuickAsks() {
|
|
public List<QuickAsk> listQuickAsks() {
|
|
|
- return quickAskRepository.findByIsActiveTrueOrderBySortOrderAscIdAsc();
|
|
|
|
|
|
|
+ return quickAskRepository.findByIsActiveTrueOrderBySortOrderAscIdAsc(
|
|
|
|
|
+ org.springframework.data.domain.PageRequest.of(0, MAX_ACTIVE_QUICK_ASKS));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /** 管理端:获取所有标签(含禁用) */
|
|
|
|
|
- public List<QuickAsk> listAllQuickAsks() {
|
|
|
|
|
- return quickAskRepository.findAllByOrderBySortOrderAscIdAsc();
|
|
|
|
|
|
|
+ /** 管理端:分页获取所有标签(含禁用) */
|
|
|
|
|
+ public Map<String, Object> listAllQuickAsks(int page, int pageSize) {
|
|
|
|
|
+ var pageable = PageRequest.of(page - 1, pageSize);
|
|
|
|
|
+ var result = quickAskRepository.findAllByOrderBySortOrderAscIdAsc(pageable);
|
|
|
|
|
+ return Map.of(
|
|
|
|
|
+ "items", result.getContent(),
|
|
|
|
|
+ "page", page,
|
|
|
|
|
+ "page_size", pageSize,
|
|
|
|
|
+ "total", result.getTotalElements(),
|
|
|
|
|
+ "total_pages", result.getTotalPages()
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private static final int MAX_ACTIVE_QUICK_ASKS = 6;
|
|
|
|
|
+
|
|
|
/** 新增标签 */
|
|
/** 新增标签 */
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public Map<String, Object> createQuickAsk(String label, String question, int sortOrder) {
|
|
public Map<String, Object> createQuickAsk(String label, String question, int sortOrder) {
|
|
|
- if (label == null || label.isBlank()) {
|
|
|
|
|
- return Map.of("ok", false, "message", "请输入推荐位文字");
|
|
|
|
|
- }
|
|
|
|
|
String trimmedLabel = label.trim();
|
|
String trimmedLabel = label.trim();
|
|
|
- if (trimmedLabel.length() < 2 || trimmedLabel.length() > 20) {
|
|
|
|
|
- return Map.of("ok", false, "message", "推荐位文字长度需在2-20个字符之间");
|
|
|
|
|
- }
|
|
|
|
|
- if (quickAskRepository.count() >= 6) {
|
|
|
|
|
- return Map.of("ok", false, "message", "最多只能添加6个推荐位");
|
|
|
|
|
- }
|
|
|
|
|
if (quickAskRepository.countByLabel(trimmedLabel) > 0) {
|
|
if (quickAskRepository.countByLabel(trimmedLabel) > 0) {
|
|
|
- return Map.of("ok", false, "message", "推荐位文字不能重复");
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("推荐位文字不能重复");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (quickAskRepository.count() >= MAX_ACTIVE_QUICK_ASKS) {
|
|
|
|
|
+ throw new IllegalArgumentException("最多只能添加" + MAX_ACTIVE_QUICK_ASKS + "个推荐位");
|
|
|
}
|
|
}
|
|
|
String operator = getCurrentOperator();
|
|
String operator = getCurrentOperator();
|
|
|
QuickAsk qa = QuickAsk.builder()
|
|
QuickAsk qa = QuickAsk.builder()
|
|
@@ -343,13 +341,13 @@ public class AdminKnowledgeService {
|
|
|
public Map<String, Object> updateQuickAsk(int id, String label, String question, Integer sortOrder, Boolean isActive) {
|
|
public Map<String, Object> updateQuickAsk(int id, String label, String question, Integer sortOrder, Boolean isActive) {
|
|
|
var opt = quickAskRepository.findById(id);
|
|
var opt = quickAskRepository.findById(id);
|
|
|
if (opt.isEmpty()) {
|
|
if (opt.isEmpty()) {
|
|
|
- return Map.of("ok", false, "message", "未找到");
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("未找到");
|
|
|
}
|
|
}
|
|
|
QuickAsk qa = opt.get();
|
|
QuickAsk qa = opt.get();
|
|
|
if (label != null && !label.isBlank()) {
|
|
if (label != null && !label.isBlank()) {
|
|
|
String trimmedLabel = label.trim();
|
|
String trimmedLabel = label.trim();
|
|
|
if (quickAskRepository.countByLabelAndIdNot(trimmedLabel, id) > 0) {
|
|
if (quickAskRepository.countByLabelAndIdNot(trimmedLabel, id) > 0) {
|
|
|
- return Map.of("ok", false, "message", "推荐位文字不能重复");
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("推荐位文字不能重复");
|
|
|
}
|
|
}
|
|
|
qa.setLabel(trimmedLabel);
|
|
qa.setLabel(trimmedLabel);
|
|
|
}
|
|
}
|
|
@@ -360,7 +358,13 @@ public class AdminKnowledgeService {
|
|
|
qa.setUpdatedBy(operator);
|
|
qa.setUpdatedBy(operator);
|
|
|
quickAskRepository.save(qa);
|
|
quickAskRepository.save(qa);
|
|
|
writeAuditLog(id, "UPDATE", operator, qa.getLabel(), qa.getQuestion(), qa.getSortOrder(), qa.getIsActive());
|
|
writeAuditLog(id, "UPDATE", operator, qa.getLabel(), qa.getQuestion(), qa.getSortOrder(), qa.getIsActive());
|
|
|
- return Map.of("ok", true, "id", id, "message", "已更新");
|
|
|
|
|
|
|
+
|
|
|
|
|
+ long activeCount = quickAskRepository.countByIsActiveTrue();
|
|
|
|
|
+ String message = "已更新";
|
|
|
|
|
+ if (Boolean.TRUE.equals(qa.getIsActive()) && activeCount > MAX_ACTIVE_QUICK_ASKS) {
|
|
|
|
|
+ message = "已更新(当前上架推荐位" + activeCount + "个,超过" + MAX_ACTIVE_QUICK_ASKS + "个上限)";
|
|
|
|
|
+ }
|
|
|
|
|
+ return Map.of("ok", true, "id", id, "message", message);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/** 删除标签 */
|
|
/** 删除标签 */
|
|
@@ -368,7 +372,7 @@ public class AdminKnowledgeService {
|
|
|
public Map<String, Object> deleteQuickAsk(int id) {
|
|
public Map<String, Object> deleteQuickAsk(int id) {
|
|
|
var opt = quickAskRepository.findById(id);
|
|
var opt = quickAskRepository.findById(id);
|
|
|
if (opt.isEmpty()) {
|
|
if (opt.isEmpty()) {
|
|
|
- return Map.of("ok", false, "message", "未找到");
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("未找到");
|
|
|
}
|
|
}
|
|
|
QuickAsk qa = opt.get();
|
|
QuickAsk qa = opt.get();
|
|
|
String operator = getCurrentOperator();
|
|
String operator = getCurrentOperator();
|