fix: 微信支付,登录接口。
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
package com.intc.fishery.utils;
|
||||
|
||||
import com.intc.common.satoken.utils.LoginHelper;
|
||||
import com.intc.fishery.constant.OpRecordType;
|
||||
import com.intc.fishery.domain.MessageOpRecord;
|
||||
import com.intc.fishery.mapper.MessageOpRecordMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* 操作记录工具类
|
||||
* 提供异步记录用户操作的功能
|
||||
*
|
||||
* @author intc
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class MessageOpRecordUtil {
|
||||
|
||||
private static MessageOpRecordMapper messageOpRecordMapper;
|
||||
|
||||
/**
|
||||
* 操作记录队列,用于异步批量插入
|
||||
*/
|
||||
private static final BlockingQueue<MessageOpRecord> RECORD_QUEUE = new LinkedBlockingQueue<>(10000);
|
||||
|
||||
/**
|
||||
* 队列处理线程是否已启动
|
||||
*/
|
||||
private static volatile boolean queueThreadStarted = false;
|
||||
|
||||
@Autowired
|
||||
public void setMessageOpRecordMapper(MessageOpRecordMapper mapper) {
|
||||
MessageOpRecordUtil.messageOpRecordMapper = mapper;
|
||||
// 启动队列处理线程
|
||||
startQueueProcessThread();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加用户操作记录
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param opUserId 操作用户ID
|
||||
* @param title 操作标题
|
||||
* @param message 操作内容
|
||||
*/
|
||||
public static void addMessageOpRecordUser(Long userId, Long opUserId, String title, String message) {
|
||||
addMessageOpRecord(userId, OpRecordType.OP_BY_USER, opUserId, title, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加用户操作记录(自动获取当前登录用户作为操作用户)
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param title 操作标题
|
||||
* @param message 操作内容
|
||||
*/
|
||||
public static void addMessageOpRecordUser(Long userId, String title, String message) {
|
||||
Long currentUserId = null;
|
||||
try {
|
||||
currentUserId = LoginHelper.getUserId();
|
||||
} catch (Exception e) {
|
||||
log.warn("获取当前登录用户失败,使用userId作为opUserId: {}", e.getMessage());
|
||||
currentUserId = userId;
|
||||
}
|
||||
addMessageOpRecord(userId, OpRecordType.OP_BY_USER, currentUserId, title, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加物理按键操作记录
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param title 操作标题
|
||||
* @param message 操作内容
|
||||
*/
|
||||
public static void addMessageOpRecordHardware(Long userId, String title, String message) {
|
||||
addMessageOpRecord(userId, OpRecordType.OP_BY_HARDWARE, null, title, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加单次定时操作记录
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param title 操作标题
|
||||
* @param message 操作内容
|
||||
*/
|
||||
public static void addMessageOpRecordTimingOne(Long userId, String title, String message) {
|
||||
addMessageOpRecord(userId, OpRecordType.OP_BY_TIMING_CTRL_ONE, null, title, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加循环定时操作记录
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param title 操作标题
|
||||
* @param message 操作内容
|
||||
*/
|
||||
public static void addMessageOpRecordTimingLoop(Long userId, String title, String message) {
|
||||
addMessageOpRecord(userId, OpRecordType.OP_BY_TIMING_CTRL_LOOP, null, title, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加联动控制操作记录
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param title 操作标题
|
||||
* @param message 操作内容
|
||||
*/
|
||||
public static void addMessageOpRecordLinked(Long userId, String title, String message) {
|
||||
addMessageOpRecord(userId, OpRecordType.OP_BY_LINKED_CTRL, null, title, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加操作记录(通用方法)
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param opType 操作类型
|
||||
* @param opUserId 操作用户ID
|
||||
* @param title 操作标题
|
||||
* @param message 操作内容
|
||||
*/
|
||||
public static void addMessageOpRecord(Long userId, OpRecordType opType, Long opUserId,
|
||||
String title, String message) {
|
||||
if (userId == null) {
|
||||
log.warn("添加操作记录失败:userId为空");
|
||||
return;
|
||||
}
|
||||
|
||||
MessageOpRecord record = new MessageOpRecord();
|
||||
record.setUserId(userId);
|
||||
record.setOpType(opType.getCode());
|
||||
record.setOpUserId(opUserId);
|
||||
record.setTitle(title);
|
||||
record.setMessage(message);
|
||||
record.setCreateTime(new Date());
|
||||
|
||||
// 添加到队列
|
||||
boolean success = RECORD_QUEUE.offer(record);
|
||||
if (!success) {
|
||||
log.error("操作记录队列已满,无法添加记录: userId={}, title={}", userId, title);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动队列处理线程
|
||||
*/
|
||||
private static synchronized void startQueueProcessThread() {
|
||||
if (queueThreadStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
Thread processThread = new Thread(() -> {
|
||||
log.info("操作记录队列处理线程启动");
|
||||
while (true) {
|
||||
try {
|
||||
// 从队列中取出记录
|
||||
MessageOpRecord record = RECORD_QUEUE.take();
|
||||
|
||||
// 批量插入逻辑(这里简化为单条插入,可根据需要优化为批量)
|
||||
try {
|
||||
if (messageOpRecordMapper != null) {
|
||||
messageOpRecordMapper.insert(record);
|
||||
log.debug("成功插入操作记录: userId={}, title={}",
|
||||
record.getUserId(), record.getTitle());
|
||||
} else {
|
||||
log.error("MessageOpRecordMapper未初始化,无法插入记录");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("插入操作记录失败: userId={}, title={}",
|
||||
record.getUserId(), record.getTitle(), e);
|
||||
}
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
log.error("队列处理线程被中断", e);
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
log.error("处理操作记录队列异常", e);
|
||||
}
|
||||
}
|
||||
}, "MessageOpRecord-Queue-Processor");
|
||||
|
||||
processThread.setDaemon(true);
|
||||
processThread.start();
|
||||
queueThreadStarted = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前队列大小
|
||||
*
|
||||
* @return 队列中待处理的记录数
|
||||
*/
|
||||
public static int getQueueSize() {
|
||||
return RECORD_QUEUE.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空队列(谨慎使用)
|
||||
*/
|
||||
public static void clearQueue() {
|
||||
RECORD_QUEUE.clear();
|
||||
log.warn("操作记录队列已清空");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user