package com.zanxiang.game.module.manage.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zanxiang.game.module.manage.service.IGameUserChatService;
import com.zanxiang.game.module.mybatis.entity.GameUserChat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author : lingfeng
 * @time : 2025-03-13
 * @description : 聊天记录定时删除任务
 */
@Slf4j
@Component
@RefreshScope
public class GameUserChatClearTask {

    @Autowired
    private IGameUserChatService gameUserChatService;

    /**
     * 每天凌晨2点清理一次7天前的聊天记录
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void execute() {
        try {
            gameUserChatService.remove(new LambdaQueryWrapper<GameUserChat>()
                    .lt(GameUserChat::getChatTime, LocalDateTime.now().minusDays(7))
            );
        } catch (Exception e) {
            log.error("每天凌晨2点清理一次7天前的聊天记录异常, e : {}", e.getMessage(), e);
        }
    }
}
