ChatSessionService.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.oms.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.oms.converter.ChatSessionConverter;
  5. import com.oms.dto.ChatSessionDTO;
  6. import com.oms.entity.ChatSession;
  7. import com.oms.enums.ChatStatus;
  8. import com.oms.mapper.ChatSessionMapper;
  9. import lombok.RequiredArgsConstructor;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import java.time.LocalDateTime;
  13. import java.util.List;
  14. @Service
  15. @RequiredArgsConstructor
  16. public class ChatSessionService {
  17. private final ChatSessionMapper mapper;
  18. private final ChatSessionConverter converter;
  19. public Page<ChatSession> getPage(int page, int size) {
  20. Page<ChatSession> pageParam = new Page<>(page, size);
  21. return mapper.selectPage(pageParam, new LambdaQueryWrapper<ChatSession>()
  22. .orderByDesc(ChatSession::getCreatedAt));
  23. }
  24. public List<ChatSession> getAll() {
  25. return mapper.selectList(new LambdaQueryWrapper<ChatSession>()
  26. .orderByDesc(ChatSession::getCreatedAt));
  27. }
  28. public ChatSession getById(Long id) {
  29. return mapper.selectById(id);
  30. }
  31. public ChatSessionDTO getDtoById(Long id) {
  32. ChatSession entity = mapper.selectById(id);
  33. return entity == null ? null : converter.toDto(entity);
  34. }
  35. public List<ChatSession> getByStatus(String status) {
  36. return mapper.selectList(new LambdaQueryWrapper<ChatSession>()
  37. .eq(ChatSession::getStatus, status)
  38. .orderByDesc(ChatSession::getCreatedAt));
  39. }
  40. public List<ChatSession> getWaitingSessions() {
  41. return mapper.selectList(new LambdaQueryWrapper<ChatSession>()
  42. .eq(ChatSession::getStatus, ChatStatus.WAITING.getCode())
  43. .orderByAsc(ChatSession::getCreatedAt));
  44. }
  45. @Transactional
  46. public Long save(ChatSession entity) {
  47. if (entity.getStatus() == null) {
  48. entity.setStatus(ChatStatus.WAITING.getCode());
  49. }
  50. if (entity.getAiHandled() == null) {
  51. entity.setAiHandled(1);
  52. }
  53. entity.setLastMessageAt(LocalDateTime.now());
  54. mapper.insert(entity);
  55. return entity.getId();
  56. }
  57. @Transactional
  58. public void update(ChatSession entity) {
  59. mapper.updateById(entity);
  60. }
  61. @Transactional
  62. public void delete(Long id) {
  63. mapper.deleteById(id);
  64. }
  65. @Transactional
  66. public void closeSession(Long id) {
  67. ChatSession cs = mapper.selectById(id);
  68. if (cs == null) throw new IllegalArgumentException("Session not found");
  69. cs.setStatus(ChatStatus.CLOSED.getCode());
  70. cs.setEndedAt(LocalDateTime.now());
  71. mapper.updateById(cs);
  72. }
  73. @Transactional
  74. public void assignAgent(Long sessionId, Long agentId, String agentName) {
  75. ChatSession cs = mapper.selectById(sessionId);
  76. if (cs == null) throw new IllegalArgumentException("Session not found");
  77. cs.setAgentId(agentId);
  78. cs.setAgentName(agentName);
  79. cs.setStatus(ChatStatus.ASSIGNED.getCode());
  80. mapper.updateById(cs);
  81. }
  82. @Transactional
  83. public void updateLastMessage(Long sessionId) {
  84. ChatSession cs = mapper.selectById(sessionId);
  85. if (cs != null) {
  86. cs.setLastMessageAt(LocalDateTime.now());
  87. mapper.updateById(cs);
  88. }
  89. }
  90. @Transactional
  91. public void rateSession(Long sessionId, int rating) {
  92. ChatSession cs = mapper.selectById(sessionId);
  93. if (cs == null) throw new IllegalArgumentException("Session not found");
  94. cs.setSatisfaction(rating);
  95. cs.setStatus(ChatStatus.CLOSED.getCode());
  96. cs.setEndedAt(LocalDateTime.now());
  97. mapper.updateById(cs);
  98. }
  99. @Transactional
  100. public void transferToAgent(Long sessionId, Long agentId, String agentName) {
  101. ChatSession cs = mapper.selectById(sessionId);
  102. if (cs == null) throw new IllegalArgumentException("Session not found");
  103. cs.setAgentId(agentId);
  104. cs.setAgentName(agentName);
  105. cs.setStatus(ChatStatus.ASSIGNED.getCode());
  106. mapper.updateById(cs);
  107. }
  108. }