package com.yfd.platform.config;

import cn.hutool.cache.Cache;
import cn.hutool.cache.impl.CacheObj;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.component.ServerSendEventServer;
import com.yfd.platform.constant.Constant;
import com.yfd.platform.system.domain.Message;
import com.yfd.platform.system.domain.SysUser;
import com.yfd.platform.system.service.IMessageService;
import com.yfd.platform.system.service.IUserService;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Iterator;

/**
 * @author LiMengNan
 * @Date: 2023/3/24 15:56
 * @Description:
 */
@Component
public class MessageConfig {

    @Resource
    private IMessageService messageService;

    @Resource
    private IUserService userService;

    @Resource
    private WebConfig webConfig;

    public void sendMessage() {
        long count =
                messageService.count(new LambdaQueryWrapper<Message>().eq(Message::getStatus, "1"));
        String userId = userService.getUserInfo().getId();
        String token = webConfig.loginuserCache().get(Constant.TOKEN + userId);
        ServerSendEventServer.sendMessage(token, count + "");
    }

    public void addMessage(Message message) {
        messageService.save(message);
        long count =
                messageService.count(new LambdaQueryWrapper<Message>().eq(Message::getStatus, "1"));
        ServerSendEventServer.sendMessage(count + "");
    }
}
