package com.ruoyi.web.controller.tool;

import com.ruoyi.system.domain.Users;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class InitUsernameToUserId {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * user表中id与userid的 映射关系
     */
    @PostConstruct
    public void init() {
        Map<String, String> map = new HashMap<>();
        List<Users> users = sysUserMapper.selectList();
        for (Users user : users) {
            map.put(user.getUserid(), user.getId());
        }
        stringRedisTemplate.opsForHash().putAll("sys_map", map);
    }
}
