61 lines
2.3 KiB
Java
61 lines
2.3 KiB
Java
package com.template.util;
|
||
import com.template.domain.MessageTemplateType;
|
||
import com.template.domain.SysCar;
|
||
import com.template.domain.Template;
|
||
import com.template.domain.resp.CarTypeResp;
|
||
import com.template.service.CarService;
|
||
import com.template.service.TemplateService;
|
||
import lombok.extern.log4j.Log4j2;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.data.redis.core.ListOperations;
|
||
import org.springframework.data.redis.core.RedisTemplate;
|
||
import org.springframework.stereotype.Component;
|
||
import javax.annotation.PostConstruct;
|
||
import javax.annotation.Resource;
|
||
import java.util.List;
|
||
/**
|
||
* @Author:liuxinyue
|
||
* @Package:com.template.util
|
||
* @Project:cloud-server
|
||
* @name:SynchronizingTemplate
|
||
* @Date:2024/9/25 20:03
|
||
*/
|
||
@Component
|
||
@Log4j2
|
||
public class SynchronizingTemplate {
|
||
|
||
|
||
//调用报文模版列表接口
|
||
@Resource
|
||
private TemplateService templateService;
|
||
|
||
//redis
|
||
@Resource
|
||
private RedisTemplate redisTemplate;
|
||
|
||
@Autowired
|
||
private CarService carService;
|
||
|
||
|
||
@PostConstruct
|
||
public void synchronizeTemplate() {
|
||
//获取所有报文模版的ID
|
||
log.info("获取所有报文模版的ID");
|
||
List<Template> templates = templateService.templateList();
|
||
templates.forEach(template -> {
|
||
Integer templateId = template.getTemplateId(); //报文模版ID
|
||
List<MessageTemplateType> list=templateService.findTemplateById(templateId); //根据报文模版ID查询所有的报文模版
|
||
ListOperations<String,Object> listOperations = redisTemplate.opsForList(); //将报文信息存储到redis中
|
||
redisTemplate.delete(template.getTemplateName());//因为每一次添加缓存的时候不会覆盖之前的数据 所有将数据先清空
|
||
List<CarTypeResp> allCars = carService.findAllCars();//查询所有车辆 里面有模版名称
|
||
redisTemplate.opsForList().leftPushAll("VehicleType", allCars);//将车辆类型放入列表
|
||
listOperations.leftPushAll(template.getTemplateName(), list); //将报文信息存储到redis中
|
||
List range = redisTemplate.opsForList().range("VehicleType", 0, -1);
|
||
range.forEach(o -> {
|
||
log.info("数据为:"+o);
|
||
});
|
||
|
||
});
|
||
}
|
||
}
|