| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package com.sckw.ai.biz.service;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.util.StrUtil;
- import com.alibaba.fastjson.JSONObject;
- import com.alibaba.fastjson.TypeReference;
- import com.sckw.ai.biz.core.CommonResult;
- import com.sckw.ai.biz.core.exception.BusinessException;
- import com.sckw.ai.biz.core.web.LoginUserHolder;
- import com.sckw.ai.biz.pojo.FeedbackVo;
- import com.sckw.ai.biz.pojo.MessageVo;
- import com.sckw.ai.biz.pojo.dto.ApiPage;
- import com.sckw.ai.biz.pojo.dto.MessageDto;
- import com.sckw.ai.biz.pojo.para.FeedbackPara;
- import com.sckw.ai.biz.pojo.para.MessagePara;
- import com.sckw.ai.biz.pojo.para.PagePara;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import java.time.Instant;
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- import java.time.ZoneOffset;
- import java.time.format.DateTimeFormatter;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Objects;
- import java.util.stream.Collectors;
- /**
- * @author xucaiqin
- * @date 2025-12-04 15:38:01
- */
- @Service
- @Slf4j
- public class MessageService {
- /**
- * 反馈
- *
- * @param feedbackPara
- * @return
- */
- public CommonResult<Object> feedbacks(FeedbackPara feedbackPara) {
- Long userId = LoginUserHolder.getUserId();
- if (Objects.isNull(userId)) {
- throw new BusinessException("未登录,请先登录");
- }
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("content", feedbackPara.getContent());
- jsonObject.put("rating", feedbackPara.getRating());
- jsonObject.put("user", userId);
- return CommonResult.ok("反馈成功", AiApiInvoker.invoke(AiApiEnum.LIKE_CHAT, new Object[]{feedbackPara.getMessageId()}, jsonObject.toJSONString()));
- }
- /**
- * 获取当前会话的历史聊天
- *
- * @param messagePara
- * @return
- */
- public CommonResult<ApiPage<MessageVo>> list(MessagePara messagePara) {
- Long userId = LoginUserHolder.getUserId();
- if (Objects.isNull(userId)) {
- throw new BusinessException("未登录,请先登录");
- }
- HashMap<String, String> para = new HashMap<>();
- if (StrUtil.isNotBlank(messagePara.getConversionId())) {
- para.put("conversation_id", messagePara.getConversionId());
- }
- para.put("user", String.valueOf(userId));
- String invoke = AiApiInvoker.invoke(AiApiEnum.MESSAGES, new Object[]{}, para);
- ApiPage<MessageDto> res = JSONObject.parseObject(invoke, new TypeReference<>() {
- });
- if (CollUtil.isEmpty(res.getData())) {
- ApiPage<MessageVo> objectApiPage = new ApiPage<>();
- objectApiPage.setData(new ArrayList<>());
- objectApiPage.setLimit(res.getLimit());
- objectApiPage.setHasMore(res.isHasMore());
- return CommonResult.ok("", objectApiPage);
- }
- List<MessageVo> collect = res.getData().stream().map(d -> {
- MessageVo bean = BeanUtil.toBean(d, MessageVo.class);
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
- .withZone(ZoneId.of("Asia/Shanghai")); // 或 ZoneId.systemDefault()
- String formattedDate = formatter.format(Instant.ofEpochSecond(d.getCreatedAt()));
- bean.setCreatedAt(formattedDate);
- return bean;
- }).collect(Collectors.toList());
- ApiPage<MessageVo> objectApiPage = new ApiPage<>();
- objectApiPage.setData(collect);
- objectApiPage.setLimit(res.getLimit());
- objectApiPage.setHasMore(res.isHasMore());
- return CommonResult.ok("", objectApiPage);
- }
- public CommonResult<ApiPage<FeedbackVo>> feedbackList(PagePara pagePara) {
- Long userId = LoginUserHolder.getUserId();
- if (Objects.isNull(userId)) {
- throw new BusinessException("未登录,请先登录");
- }
- HashMap<String, String> para = new HashMap<>();
- para.put("page", String.valueOf(pagePara.getPage()));
- para.put("limit", String.valueOf(pagePara.getPageSize()));
- String invoke = AiApiInvoker.invoke(AiApiEnum.FEEDBACKS, new Object[]{}, para);
- ApiPage<FeedbackVo> res = JSONObject.parseObject(invoke, new TypeReference<>() {
- });
- if (CollUtil.isEmpty(res.getData())) {
- ApiPage<FeedbackVo> objectApiPage = new ApiPage<>();
- objectApiPage.setData(new ArrayList<>());
- objectApiPage.setLimit(res.getLimit());
- objectApiPage.setHasMore(res.isHasMore());
- return CommonResult.ok("", objectApiPage);
- }
- res.getData().forEach(d -> {
- LocalDateTime dt = LocalDateTime.parse(d.getCreatedAt());
- dt.atZone(ZoneOffset.UTC);
- d.setCreatedAt(dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- LocalDateTime dt2 = LocalDateTime.parse(d.getUpdatedAt());
- dt2.atZone(ZoneOffset.UTC);
- d.setUpdatedAt(dt2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- });
- return CommonResult.ok("查询成功", res);
- }
- }
|