package ink.wgink.mongo.login.init;

import ink.wgink.interfaces.init.IInitMongoData;
import ink.wgink.interfaces.user.mongo.IMongoLoginUserService;
import ink.wgink.properties.ServerProperties;
import ink.wgink.service.user.service.IUserService;
import ink.wgink.service.user.util.UserUtil;
import ink.wgink.util.thread.CachedThreadPoolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: InitLoginUser
 * @Description: 初始化登录用户
 * @Author: wanggeng
 * @Date: 2022/5/24 20:59
 * @Version: 1.0
 */
@Component
public class InitMongoLoginUser implements IInitMongoData {

    private static final Logger LOG = LoggerFactory.getLogger(InitMongoLoginUser.class);
    @Autowired
    private ServerProperties serverProperties;
    @Autowired
    private IMongoLoginUserService mongoLoginUserService;
    @Autowired
    private IUserService userService;

    @Override
    public void init() {
        if (serverProperties.getMasterNode()) {
            LOG.debug("MASTER-NODE: {}" + serverProperties.getMasterNode());
            CachedThreadPoolUtil.execute(() -> {
                // 初始化所有账号内容
                System.out.println("***************************");
                System.out.println("** LOGIN USER INIT START **");
                System.out.println("***************************");
                long startTime = System.currentTimeMillis();
                Map<String, Object> params = new HashMap<>(0);
                userService.listPO(params).forEach(userPO -> {
                    mongoLoginUserService.update(UserUtil.createLoginUser(userPO));
                });
                long endTime = System.currentTimeMillis();
                System.out.println("*************************");
                System.out.println("** LOGIN USER INIT END **");
                System.out.println("*************************");
                LOG.debug("LOGIN USER INIT USED TIME: {}ms", (endTime - startTime) / 1000L);
            });
        }
    }

}
