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 getPage(int page, int size) { Page pageParam = new Page<>(page, size); return mapper.selectPage(pageParam, new LambdaQueryWrapper() .orderByDesc(ChatSession::getCreatedAt)); } public List getAll() { return mapper.selectList(new LambdaQueryWrapper() .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 getByStatus(String status) { return mapper.selectList(new LambdaQueryWrapper() .eq(ChatSession::getStatus, status) .orderByDesc(ChatSession::getCreatedAt)); } public List getWaitingSessions() { return mapper.selectList(new LambdaQueryWrapper() .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); } }