package com.fdkankan.auth.schedul;

import com.alibaba.fastjson.JSON;
import com.fdkankan.auth.entity.TmDeveloper;
import com.fdkankan.auth.service.ITmDeveloperService;
import com.fdkankan.redis.constant.RedisKey;
import com.fdkankan.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class DeveloperSchedul {


    @Autowired
    private ITmDeveloperService tmDeveloperService;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 加载开发者信息到缓存，上一次执行完毕后五分钟执行
     */
    @Scheduled(fixedDelay=300000)
    public void loadTmDeveloperToRedisCache(){
        log.info("-----从mysql中加载开发者信息表到redis缓存定时任务开始执行------");
        List<TmDeveloper> list = tmDeveloperService.list();
        list.stream().forEach(tmDeveloper -> {
            String key = String.format(RedisKey.TM_DEVELOPER, tmDeveloper.getAppId());
            redisUtil.set(key, JSON.toJSONString(tmDeveloper));
        });
        log.info("-----从mysql中加载开发者信息表到redis缓存定时任务执行结束------");


    }

}
