MessageService.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.sckw.ai.biz.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.alibaba.fastjson.TypeReference;
  7. import com.sckw.ai.biz.core.CommonResult;
  8. import com.sckw.ai.biz.core.exception.BusinessException;
  9. import com.sckw.ai.biz.core.web.LoginUserHolder;
  10. import com.sckw.ai.biz.pojo.FeedbackVo;
  11. import com.sckw.ai.biz.pojo.MessageVo;
  12. import com.sckw.ai.biz.pojo.dto.ApiPage;
  13. import com.sckw.ai.biz.pojo.dto.MessageDto;
  14. import com.sckw.ai.biz.pojo.para.FeedbackPara;
  15. import com.sckw.ai.biz.pojo.para.MessagePara;
  16. import com.sckw.ai.biz.pojo.para.PagePara;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.stereotype.Service;
  19. import java.time.Instant;
  20. import java.time.LocalDateTime;
  21. import java.time.ZoneId;
  22. import java.time.ZoneOffset;
  23. import java.time.format.DateTimeFormatter;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Objects;
  28. import java.util.stream.Collectors;
  29. /**
  30. * @author xucaiqin
  31. * @date 2025-12-04 15:38:01
  32. */
  33. @Service
  34. @Slf4j
  35. public class MessageService {
  36. /**
  37. * 反馈
  38. *
  39. * @param feedbackPara
  40. * @return
  41. */
  42. public CommonResult<Object> feedbacks(FeedbackPara feedbackPara) {
  43. Long userId = LoginUserHolder.getUserId();
  44. if (Objects.isNull(userId)) {
  45. throw new BusinessException("未登录,请先登录");
  46. }
  47. JSONObject jsonObject = new JSONObject();
  48. jsonObject.put("content", feedbackPara.getContent());
  49. jsonObject.put("rating", feedbackPara.getRating());
  50. jsonObject.put("user", userId);
  51. return CommonResult.ok("反馈成功", AiApiInvoker.invoke(AiApiEnum.LIKE_CHAT, new Object[]{feedbackPara.getMessageId()}, jsonObject.toJSONString()));
  52. }
  53. /**
  54. * 获取当前会话的历史聊天
  55. *
  56. * @param messagePara
  57. * @return
  58. */
  59. public CommonResult<ApiPage<MessageVo>> list(MessagePara messagePara) {
  60. Long userId = LoginUserHolder.getUserId();
  61. if (Objects.isNull(userId)) {
  62. throw new BusinessException("未登录,请先登录");
  63. }
  64. HashMap<String, String> para = new HashMap<>();
  65. if (StrUtil.isNotBlank(messagePara.getConversionId())) {
  66. para.put("conversation_id", messagePara.getConversionId());
  67. }
  68. para.put("user", String.valueOf(userId));
  69. String invoke = AiApiInvoker.invoke(AiApiEnum.MESSAGES, new Object[]{}, para);
  70. ApiPage<MessageDto> res = JSONObject.parseObject(invoke, new TypeReference<>() {
  71. });
  72. if (CollUtil.isEmpty(res.getData())) {
  73. ApiPage<MessageVo> objectApiPage = new ApiPage<>();
  74. objectApiPage.setData(new ArrayList<>());
  75. objectApiPage.setLimit(res.getLimit());
  76. objectApiPage.setHasMore(res.isHasMore());
  77. return CommonResult.ok("", objectApiPage);
  78. }
  79. List<MessageVo> collect = res.getData().stream().map(d -> {
  80. MessageVo bean = BeanUtil.toBean(d, MessageVo.class);
  81. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
  82. .withZone(ZoneId.of("Asia/Shanghai")); // 或 ZoneId.systemDefault()
  83. String formattedDate = formatter.format(Instant.ofEpochSecond(d.getCreatedAt()));
  84. bean.setCreatedAt(formattedDate);
  85. return bean;
  86. }).collect(Collectors.toList());
  87. ApiPage<MessageVo> objectApiPage = new ApiPage<>();
  88. objectApiPage.setData(collect);
  89. objectApiPage.setLimit(res.getLimit());
  90. objectApiPage.setHasMore(res.isHasMore());
  91. return CommonResult.ok("", objectApiPage);
  92. }
  93. public CommonResult<ApiPage<FeedbackVo>> feedbackList(PagePara pagePara) {
  94. Long userId = LoginUserHolder.getUserId();
  95. if (Objects.isNull(userId)) {
  96. throw new BusinessException("未登录,请先登录");
  97. }
  98. HashMap<String, String> para = new HashMap<>();
  99. para.put("page", String.valueOf(pagePara.getPage()));
  100. para.put("limit", String.valueOf(pagePara.getPageSize()));
  101. String invoke = AiApiInvoker.invoke(AiApiEnum.FEEDBACKS, new Object[]{}, para);
  102. ApiPage<FeedbackVo> res = JSONObject.parseObject(invoke, new TypeReference<>() {
  103. });
  104. if (CollUtil.isEmpty(res.getData())) {
  105. ApiPage<FeedbackVo> objectApiPage = new ApiPage<>();
  106. objectApiPage.setData(new ArrayList<>());
  107. objectApiPage.setLimit(res.getLimit());
  108. objectApiPage.setHasMore(res.isHasMore());
  109. return CommonResult.ok("", objectApiPage);
  110. }
  111. res.getData().forEach(d -> {
  112. LocalDateTime dt = LocalDateTime.parse(d.getCreatedAt());
  113. dt.atZone(ZoneOffset.UTC);
  114. d.setCreatedAt(dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  115. LocalDateTime dt2 = LocalDateTime.parse(d.getUpdatedAt());
  116. dt2.atZone(ZoneOffset.UTC);
  117. d.setUpdatedAt(dt2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  118. });
  119. return CommonResult.ok("查询成功", res);
  120. }
  121. }