package com.rongyichuang.message.service; import com.rongyichuang.message.entity.Message; import com.rongyichuang.message.entity.MessageType; import com.rongyichuang.message.repository.MessageRepository; import com.rongyichuang.player.entity.Player; import com.rongyichuang.player.repository.PlayerRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Optional; /** * 消息服务类 */ @Service public class MessageService { private static final Logger log = LoggerFactory.getLogger(MessageService.class); @Autowired private MessageRepository messageRepository; @Autowired private PlayerRepository playerRepository; /** * 创建审核通过消息 */ @Transactional public void createApprovalMessage(Long targetId, Long playerId, String projectName) { Long userId = getUserIdByPlayerId(playerId); if (userId == null) { log.error("无法获取选手对应的用户ID,选手ID: {}", playerId); return; } Message message = new Message(); message.setTargetType(MessageType.REVIEW_APPROVED.getValue()); message.setTargetId(targetId); message.setPlayerId(playerId); message.setUserId(userId); message.setContent(projectName + " 审核通过"); message.setState(1); // 有效状态 message.setWxMsgSuccess(false); // 默认未发送微信消息 message.setWxMsgErrCount(0); // 默认错误次数为0 messageRepository.save(message); log.info("创建审核通过消息成功,targetId: {}, playerId: {}, userId: {}", targetId, playerId, userId); } /** * 创建审核驳回消息 */ @Transactional public void createRejectionMessage(Long targetId, Long playerId, String projectName) { Long userId = getUserIdByPlayerId(playerId); if (userId == null) { log.error("无法获取选手对应的用户ID,选手ID: {}", playerId); return; } Message message = new Message(); message.setTargetType(MessageType.REVIEW_REJECTED.getValue()); message.setTargetId(targetId); message.setPlayerId(playerId); message.setUserId(userId); message.setContent(projectName + " 审核不通过"); message.setState(1); // 有效状态 message.setWxMsgSuccess(false); // 默认未发送微信消息 message.setWxMsgErrCount(0); // 默认错误次数为0 messageRepository.save(message); log.info("创建审核驳回消息成功,targetId: {}, playerId: {}, userId: {}", targetId, playerId, userId); } /** * 创建晋级消息 */ @Transactional public void createPromotionMessage(Long targetId, Long playerId, String projectName) { Long userId = getUserIdByPlayerId(playerId); if (userId == null) { log.error("无法获取选手对应的用户ID,选手ID: {}", playerId); return; } Message message = new Message(); message.setTargetType(MessageType.COMPETITION_PROMOTED.getValue()); message.setTargetId(targetId); message.setPlayerId(playerId); message.setUserId(userId); message.setContent(projectName + " 比赛晋级"); message.setState(1); // 有效状态 message.setWxMsgSuccess(false); // 默认未发送微信消息 message.setWxMsgErrCount(0); // 默认错误次数为0 messageRepository.save(message); log.info("创建晋级消息成功,targetId: {}, playerId: {}, userId: {}", targetId, playerId, userId); } /** * 通过选手ID获取对应的用户ID */ private Long getUserIdByPlayerId(Long playerId) { try { Optional playerOpt = playerRepository.findById(playerId); if (playerOpt.isPresent()) { Player player = playerOpt.get(); return player.getUserId(); } else { log.warn("未找到选手记录,选手ID: {}", playerId); return null; } } catch (Exception e) { log.error("查询选手记录时发生异常,选手ID: {}", playerId, e); return null; } } /** * 根据用户ID获取消息列表,按时间倒序 */ public List getMessagesByUserId(Long userId) { log.info("Getting messages for user ID: {}", userId); return messageRepository.findByUserIdOrderByCreateTimeDesc(userId); } }