lrj
22 小时以前 dc643ba44fd2a426263015491268a0f0d6b4671d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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<Player> 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<Message> getMessagesByUserId(Long userId) {
        log.info("Getting messages for user ID: {}", userId);
        return messageRepository.findByUserIdOrderByCreateTimeDesc(userId);
    }
 
 
}