package cn.com.ty.lift.business.framework.runner;

import cn.com.ty.lift.business.library.dao.entity.LiftBrand;
import cn.com.ty.lift.business.library.dao.mapper.LiftBrandMapper;
import cn.com.ty.lift.common.constants.RedisConstants;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author bieao
 * @date 2019/12/10
 * @description 业务模块启动项
 */
@Component
@Slf4j
public class BusinessServiceRunner implements ApplicationRunner {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private LiftBrandMapper liftBrandMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //初始化电梯品牌列表
        initLiftBrandList();
    }

    /**
     * @description 初始化电梯品牌列表
     * @date 2019/12/10 10:12 AM
     */
    public void initLiftBrandList() {
        List<LiftBrand> cacheList = (List<LiftBrand>) redisTemplate.opsForValue().get(RedisConstants.RK_LIFT_BRAND_LIST);
        if (cacheList == null) {
            //查询品牌列表
            List<LiftBrand> liftBrandList = liftBrandMapper.selectList(new QueryWrapper<>());
            //设置品牌列表缓存到redis
            redisTemplate.opsForValue().set(RedisConstants.RK_LIFT_BRAND_LIST, liftBrandList);
            log.info("初始化品牌列表{}", JSONUtil.parse(liftBrandList));
        }
    }
}
