BaiduAiServiceImpl.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.goafanti.baiduAI.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.goafanti.baiduAI.BaiduChatErrorEnums;
  4. import com.goafanti.baiduAI.bo.*;
  5. import com.goafanti.baiduAI.service.BaiduAiService;
  6. import com.goafanti.common.bo.SseEmitterUTF8;
  7. import com.goafanti.common.dao.ErnieBotCommonDao;
  8. import com.goafanti.common.error.BusinessException;
  9. import com.goafanti.common.model.ErnieBotCommon;
  10. import com.goafanti.common.utils.BaiduChatUtils;
  11. import com.goafanti.common.utils.HttpUtils;
  12. import com.goafanti.common.utils.LoggerUtils;
  13. import com.goafanti.common.utils.RedisUtil;
  14. import com.goafanti.core.shiro.cache.ShiroRedisCache;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.cache.annotation.CacheEvict;
  17. import org.springframework.cache.annotation.Cacheable;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
  20. import javax.annotation.Resource;
  21. import java.io.IOException;
  22. import java.util.Calendar;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. @Service
  27. public class BaiduAiServiceImpl implements BaiduAiService {
  28. @Autowired
  29. private ErnieBotCommonDao ernieBotCommonDao;
  30. @Autowired
  31. private BaiduChatUtils baiduChatUtils;
  32. @Override
  33. public SseEmitterUTF8 sendChat(InputSendChat in) {
  34. SseEmitterUTF8 sseEmitter = new SseEmitterUTF8(3600_000L);
  35. // if (clientId==null)clientId=UUID.randomUUID().toString();
  36. SseMap.sseEmitterMap.put(in.getUserId(), new SseResult(in.getUserId(), System.currentTimeMillis(), sseEmitter));
  37. try {
  38. // 先将数据返回,异步调用返回数据
  39. baiduChatUtils.sendBaiduAiStream(in);
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. throw new BusinessException("文言一心调用失败"+e.getLocalizedMessage());
  43. }
  44. return sseEmitter;
  45. }
  46. @Override
  47. @Cacheable(value = "selectErnieBotCommonAll#300",key = "'page:'+'All'")
  48. public List<ErnieBotCommon> selectErnieBotCommonAll(Integer count) {
  49. return ernieBotCommonDao.selectAll(count);
  50. }
  51. }