wlzboy
4 天以前 c098f1e3a3e052aa3d65584aae6dc003a70d75ad
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.ruoyi.system.service.impl;
 
import java.util.Date;
import java.util.List;
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 com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.NotifySendLog;
import com.ruoyi.system.mapper.NotifySendLogMapper;
import com.ruoyi.system.service.INotifySendLogService;
 
/**
 * 通知发送记录服务实现类
 * 
 * @author ruoyi
 * @date 2025-12-07
 */
@Service
public class NotifySendLogServiceImpl implements INotifySendLogService {
 
    private static final Logger log = LoggerFactory.getLogger(NotifySendLogServiceImpl.class);
 
    @Autowired
    private NotifySendLogMapper notifySendLogMapper;
 
    /**
     * 查询通知发送记录
     * 
     * @param id 通知发送记录主键
     * @return 通知发送记录
     */
    @Override
    public NotifySendLog selectNotifySendLogById(Long id) {
        return notifySendLogMapper.selectNotifySendLogById(id);
    }
 
    /**
     * 查询通知发送记录列表
     * 
     * @param notifySendLog 通知发送记录
     * @return 通知发送记录
     */
    @Override
    public List<NotifySendLog> selectNotifySendLogList(NotifySendLog notifySendLog) {
        return notifySendLogMapper.selectNotifySendLogList(notifySendLog);
    }
 
    /**
     * 根据任务ID和用户ID查询发送记录
     */
    @Override
    public NotifySendLog selectNotifySendLog(Long taskId, Long userId, String notifyType, String channel) {
        return notifySendLogMapper.selectNotifySendLog(taskId, userId, notifyType, channel);
    }
 
    /**
     * 检查是否已发送过通知(防重检查)
     * 
     * @param taskId 任务ID
     * @param userId 用户ID
     * @param notifyType 通知类型
     * @param channel 通知渠道
     * @return true=已发送过, false=未发送过
     */
    @Override
    public boolean hasNotified(Long taskId, Long userId, String notifyType, String channel) {
        int count = notifySendLogMapper.checkNotifySendLogExists(taskId, userId, notifyType, channel);
        return count > 0;
    }
 
    /**
     * 尝试创建发送记录(防重,如果已存在则返回null)
     * 使用数据库唯一索引保证防重
     * 
     * @param taskId 任务ID
     * @param userId 用户ID
     * @param userName 用户姓名
     * @param notifyType 通知类型
     * @param channel 通知渠道
     * @return 创建的记录,如果已存在则返回null
     */
    @Override
    @Transactional
    public NotifySendLog tryCreateSendLog(Long taskId, Long userId, String userName,
                                           String notifyType, String channel) {
        // 先检查是否已存在
        if (hasNotified(taskId, userId, notifyType, channel)) {
            log.debug("通知记录已存在,跳过创建,taskId={}, userId={}, notifyType={}, channel={}",
                    taskId, userId, notifyType, channel);
            return null;
        }
 
        try {
            // 创建新记录
            NotifySendLog sendLog = new NotifySendLog();
            sendLog.setTaskId(taskId);
            sendLog.setUserId(userId);
            sendLog.setUserName(userName);
            sendLog.setNotifyType(notifyType);
            sendLog.setChannel(channel);
            sendLog.setSendStatus(NotifySendLog.SEND_STATUS_PENDING);
            sendLog.setRetryCount(0);
            sendLog.setCreateTime(DateUtils.getNowDate());
            sendLog.setCreateBy("system");
 
            int result = notifySendLogMapper.insertNotifySendLog(sendLog);
            if (result > 0) {
                log.info("创建通知发送记录成功,id={}, taskId={}, userId={}, notifyType={}, channel={}",
                        sendLog.getId(), taskId, userId, notifyType, channel);
                return sendLog;
            }
        } catch (Exception e) {
            // 可能是并发插入导致的唯一索引冲突,视为已存在
            log.warn("创建通知发送记录失败(可能是并发重复),taskId={}, userId={}, notifyType={}, channel={}, error={}",
                    taskId, userId, notifyType, channel, e.getMessage());
        }
 
        return null;
    }
 
    /**
     * 更新发送状态为成功(向后兼容)
     * 
     * @param id 记录ID
     * @param result 发送结果信息
     */
    public void markSendSuccess(Long id, String result) {
        markSendSuccess(id, result, null);
    }
 
    /**
     * 更新发送状态为成功
     * 
     * @param id 记录ID
     * @param result 发送结果信息
     * @param content 发送内容
     */
    @Override
    public void markSendSuccess(Long id, String result, String content) {
        if (id == null) {
            return;
        }
        try {
            notifySendLogMapper.updateSendStatus(id, NotifySendLog.SEND_STATUS_SUCCESS, result, content);
            log.debug("更新通知发送状态为成功,id={}", id);
        } catch (Exception e) {
            log.error("更新通知发送状态失败,id={}", id, e);
        }
    }
 
    /**
     * 更新发送状态为失败(向后兼容)
     * 
     * @param id 记录ID
     * @param errorMsg 错误信息
     */
    public void markSendFailed(Long id, String errorMsg) {
        markSendFailed(id, errorMsg, null);
    }
 
    /**
     * 更新发送状态为失败
     * 
     * @param id 记录ID
     * @param errorMsg 错误信息
     * @param content 发送内容
     */
    @Override
    public void markSendFailed(Long id, String errorMsg, String content) {
        if (id == null) {
            return;
        }
        try {
            // 限制错误信息长度
            if (errorMsg != null && errorMsg.length() > 500) {
                errorMsg = errorMsg.substring(0, 500);
            }
            notifySendLogMapper.updateSendStatus(id, NotifySendLog.SEND_STATUS_FAILED, errorMsg, content);
            log.debug("更新通知发送状态为失败,id={}, error={}", id, errorMsg);
        } catch (Exception e) {
            log.error("更新通知发送状态失败,id={}", id, e);
        }
    }
 
    /**
     * 新增通知发送记录
     * 
     * @param notifySendLog 通知发送记录
     * @return 结果
     */
    @Override
    public int insertNotifySendLog(NotifySendLog notifySendLog) {
        notifySendLog.setCreateTime(DateUtils.getNowDate());
        return notifySendLogMapper.insertNotifySendLog(notifySendLog);
    }
 
    /**
     * 修改通知发送记录
     * 
     * @param notifySendLog 通知发送记录
     * @return 结果
     */
    @Override
    public int updateNotifySendLog(NotifySendLog notifySendLog) {
        notifySendLog.setUpdateTime(DateUtils.getNowDate());
        return notifySendLogMapper.updateNotifySendLog(notifySendLog);
    }
 
    /**
     * 批量删除通知发送记录
     * 
     * @param ids 需要删除的通知发送记录主键
     * @return 结果
     */
    @Override
    public int deleteNotifySendLogByIds(Long[] ids) {
        return notifySendLogMapper.deleteNotifySendLogByIds(ids);
    }
 
    /**
     * 删除通知发送记录信息
     * 
     * @param id 通知发送记录主键
     * @return 结果
     */
    @Override
    public int deleteNotifySendLogById(Long id) {
        return notifySendLogMapper.deleteNotifySendLogById(id);
    }
 
    /**
     * 查询待重试的失败记录
     * 
     * @param maxRetryCount 最大重试次数
     * @return 失败记录列表
     */
 
    public List<NotifySendLog> selectFailedNotifySendLogs(Integer maxRetryCount) {
        return notifySendLogMapper.selectFailedNotifySendLogs(maxRetryCount);
    }
}