| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package com.oms.service;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.oms.converter.ChatSessionConverter;
- import com.oms.dto.ChatSessionDTO;
- import com.oms.entity.ChatSession;
- import com.oms.enums.ChatStatus;
- import com.oms.mapper.ChatSessionMapper;
- import lombok.RequiredArgsConstructor;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.time.LocalDateTime;
- import java.util.List;
- @Service
- @RequiredArgsConstructor
- public class ChatSessionService {
- private final ChatSessionMapper mapper;
- private final ChatSessionConverter converter;
- public Page<ChatSession> getPage(int page, int size) {
- Page<ChatSession> pageParam = new Page<>(page, size);
- return mapper.selectPage(pageParam, new LambdaQueryWrapper<ChatSession>()
- .orderByDesc(ChatSession::getCreatedAt));
- }
- public List<ChatSession> getAll() {
- return mapper.selectList(new LambdaQueryWrapper<ChatSession>()
- .orderByDesc(ChatSession::getCreatedAt));
- }
- public ChatSession getById(Long id) {
- return mapper.selectById(id);
- }
- public ChatSessionDTO getDtoById(Long id) {
- ChatSession entity = mapper.selectById(id);
- return entity == null ? null : converter.toDto(entity);
- }
- public List<ChatSession> getByStatus(String status) {
- return mapper.selectList(new LambdaQueryWrapper<ChatSession>()
- .eq(ChatSession::getStatus, status)
- .orderByDesc(ChatSession::getCreatedAt));
- }
- public List<ChatSession> getWaitingSessions() {
- return mapper.selectList(new LambdaQueryWrapper<ChatSession>()
- .eq(ChatSession::getStatus, ChatStatus.WAITING.getCode())
- .orderByAsc(ChatSession::getCreatedAt));
- }
- @Transactional
- public Long save(ChatSession entity) {
- if (entity.getStatus() == null) {
- entity.setStatus(ChatStatus.WAITING.getCode());
- }
- if (entity.getAiHandled() == null) {
- entity.setAiHandled(1);
- }
- entity.setLastMessageAt(LocalDateTime.now());
- mapper.insert(entity);
- return entity.getId();
- }
- @Transactional
- public void update(ChatSession entity) {
- mapper.updateById(entity);
- }
- @Transactional
- public void delete(Long id) {
- mapper.deleteById(id);
- }
- @Transactional
- public void closeSession(Long id) {
- ChatSession cs = mapper.selectById(id);
- if (cs == null) throw new IllegalArgumentException("Session not found");
- cs.setStatus(ChatStatus.CLOSED.getCode());
- cs.setEndedAt(LocalDateTime.now());
- mapper.updateById(cs);
- }
- @Transactional
- public void assignAgent(Long sessionId, Long agentId, String agentName) {
- ChatSession cs = mapper.selectById(sessionId);
- if (cs == null) throw new IllegalArgumentException("Session not found");
- cs.setAgentId(agentId);
- cs.setAgentName(agentName);
- cs.setStatus(ChatStatus.ASSIGNED.getCode());
- mapper.updateById(cs);
- }
- @Transactional
- public void updateLastMessage(Long sessionId) {
- ChatSession cs = mapper.selectById(sessionId);
- if (cs != null) {
- cs.setLastMessageAt(LocalDateTime.now());
- mapper.updateById(cs);
- }
- }
- @Transactional
- public void rateSession(Long sessionId, int rating) {
- ChatSession cs = mapper.selectById(sessionId);
- if (cs == null) throw new IllegalArgumentException("Session not found");
- cs.setSatisfaction(rating);
- cs.setStatus(ChatStatus.CLOSED.getCode());
- cs.setEndedAt(LocalDateTime.now());
- mapper.updateById(cs);
- }
- @Transactional
- public void transferToAgent(Long sessionId, Long agentId, String agentName) {
- ChatSession cs = mapper.selectById(sessionId);
- if (cs == null) throw new IllegalArgumentException("Session not found");
- cs.setAgentId(agentId);
- cs.setAgentName(agentName);
- cs.setStatus(ChatStatus.ASSIGNED.getCode());
- mapper.updateById(cs);
- }
- }
|