package com.fjhx.mail.listener;

import com.fjhx.mail.util.MailHttpUtil;
import com.ruoyi.framework.config.ThreadPoolConfig;
import com.ruoyi.framework.event.LoginEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Component
public class LoginEventListeners {

    @Autowired
    @Qualifier(ThreadPoolConfig.threadPoolTaskExecutor)
    private ThreadPoolExecutor threadPoolExecutor;

    @EventListener
    public void loginEvent(LoginEvent loginEvent) {
        threadPoolExecutor.execute(() -> {
            Long userId = (Long) loginEvent.getSource();
            int num = 0;
            while (num < 3) {
                try {
                    MailHttpUtil.userLogin(userId);
                    num = 3;
                } catch (Exception e) {
                    log.error("同步邮件服务用户登录失败", e);
                    num++;
                }
            }
        });

        threadPoolExecutor.execute(() -> {
            Long userId = (Long) loginEvent.getSource();
            int num = 0;
            while (num < 3) {
                try {
                    MailHttpUtil.abroadUserLogin(userId);
                    num = 3;
                } catch (Exception e) {
                    log.error("同步邮件服务用户登录失败", e);
                    num++;
                }
            }
        });
    }

}
