RedisApplicationRunner.java 3.0 KB
package com.skua.redis.starter;

import com.skua.core.context.SpringContextUtils;
import com.skua.redis.util.CustomRedisUtil;
import com.skua.tool.javassist.JavassistFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

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

/**
 * @author sonin
 * @date 2021/9/30 14:03
 */
@Slf4j
@Component
public class RedisApplicationRunner implements ApplicationRunner {

    @Autowired
    private CustomRedisUtil customRedisUtil;

    @Override
    public void run(ApplicationArguments args) {
        this.loadSysDepartFunc();
        this.createPgClass();
    }

    /**
     * 厂区信息 id => departName 映射
     */
    private void loadSysDepartFunc() {
        try {
            JdbcTemplate masterDB = (JdbcTemplate) SpringContextUtils.getBean("master");
            String sysDepartSql = "select id, depart_name from sys_depart";
            List<Map<String, Object>> sysDepartMapList = masterDB.queryForList(sysDepartSql);
            Map<String, Object> redisMap = new HashMap<>();
            for (Map<String, Object> item : sysDepartMapList) {
                redisMap.put(item.get("id").toString(), item.get("depart_name"));
            }
            customRedisUtil.hsets("sys_depart", redisMap);
            redisMap.clear();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("init load厂区信息error: {}", e.getMessage());
        }
    }

    private void createPgClass() {
        try {
            // 创建Xsinsert基础类
            JavassistFactory.create()
                    .className("Xsinsert")
                    .field("id", Integer.class)
                    .field("nm", String.class)
                    .field("v", String.class)
                    .field("ts", String.class)
                    .field("createtime", Integer.class)
                    .field("factoryname", String.class)
                    .field("devicename", String.class)
                    .field("type", Integer.class)
                    .field("gatewaycode", String.class)
                    .buildClass();
            // 创建Count基础类
            JavassistFactory.create()
                    .className("Count")
                    .field("id", Integer.class)
                    .field("nm", String.class)
                    .field("v", String.class)
                    .field("ts", String.class)
                    .field("createtime", Integer.class)
                    .field("factoryname", String.class)
                    .field("devicename", String.class)
                    .field("type", String.class)
                    .field("gatewaycode", String.class)
                    .buildClass();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}