package com.yida.data.system.configure;

import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.system.service.IDeptService;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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

import cc.mrbird.febs.common.redis.service.RedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RequiredArgsConstructor
@Component
@Order(10)
@Slf4j
public class InitializeRunner implements ApplicationRunner {

    private final RedisService redisService;
    private final IDeptService deptService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("初始化部门缓存");
        List<Dept> allDept = deptService.list();
        Map<Object, Object> items = new HashMap<>();

        for (Dept dept : allDept) {
            items.put(dept.getDeptId().toString(), dept);
        }
        redisService.hmset(CachePrefixConstant.SYS_DEPT_DATA, items);
    }
}
