package com.m2pool.chat.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.m2pool.chat.entity.ChatMessage;
import com.m2pool.chat.entity.ChatMessageHistory;
import com.m2pool.chat.entity.ChatRoom;
import com.m2pool.chat.mapper.ChatMessageMapper;
import com.m2pool.chat.mapper.ChatRoomMapper;
import com.m2pool.chat.service.ChatMessageHistoryService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ChatTask
 * @Description chat 聊天室和消息定时任务
 * @Author yyb
 * @Date 2025/4/15 11:51
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "task")
@EnableScheduling
public class ChatTask {

    @Resource
    private ChatMessageMapper chatMessageMapper;

    @Resource
    private ChatMessageHistoryService chatMessageHistoryService;
    @Autowired
    private ChatRoomMapper chatRoomMapper;

    private boolean enable;

    //    @Scheduled(cron = "0 0/1 * * * ?")
    @Scheduled(cron = "0 15 1 * * ?")
    public void chatMessageDataSeparatedForHotAndCold(){
        if(!enable){
            System.out.println("ChatTask 定时任务已关闭，请在nacos修改配置");
            return;
        }
        int pageNum = 1;
        int pageSize = 1000; // 每批处理数量
        List<ChatMessage> chatMessages;
        do {
            Page<ChatMessage> page = new Page<>(pageNum, pageSize);
            chatMessageMapper.selectPage(page, new LambdaQueryWrapper<ChatMessage>()
                    .le(ChatMessage::getCreateTime, LocalDateTime.now().minusDays(7)));

            chatMessages = page.getRecords();

            if (!chatMessages.isEmpty()) {
                List<ChatMessageHistory> collect = chatMessages.stream()
                        .map(chatMessage ->
                             ChatMessageHistory.builder()
                                    .id(chatMessage.getId())
                                    .type(chatMessage.getType())
                                    .sendEmail(chatMessage.getSendEmail())
                                    .content(chatMessage.getContent())
                                    .roomId(chatMessage.getRoomId())
                                    .createTime(chatMessage.getCreateTime())
                                    .updateTime(chatMessage.getUpdateTime())
                                    .build()
                        )
                        .collect(Collectors.toList());

                // 批量插入
                chatMessageHistoryService.saveBatch(collect);
                chatMessageMapper.deleteBatchIds(chatMessages.stream().map(ChatMessage::getId).collect(Collectors.toList()));

            }

            pageNum++;
        } while (!chatMessages.isEmpty());
    }

    /**
     * 清理掉，因服务器异常，未发送到客服这边删除的游客聊天室和消息表数据
     *
     */
    @Scheduled(cron = "0 16 1 * * ?")
    //@Scheduled(cron = "0 0/1 * * * ?")
    public void clearTouristDatas(){
        if(!enable){
            return;
        }
        chatMessageMapper.delete(new LambdaUpdateWrapper<ChatMessage>()
                .like(ChatMessage::getSendEmail, "guest_"));
         chatRoomMapper.delete(new LambdaUpdateWrapper<ChatRoom>().like(ChatRoom::getUserOneEmail, "guest_"));
    }

}
