Merge branch 'dev.parse' into dev

dev.gateway
ruyaxie 2024-09-30 18:09:42 +08:00
commit 443350c8f4
15 changed files with 143 additions and 113 deletions

View File

@ -4,6 +4,8 @@ import com.muyu.common.cache.CacheAbsBasic;
import com.muyu.domain.MessageValue;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @version 1.0
* @Author xie ya ru
@ -11,7 +13,7 @@ import org.springframework.stereotype.Component;
* @
*/
@Component
public class MessageValueCacheService extends CacheAbsBasic<String, MessageValue> {
public class MessageValueCacheService extends CacheAbsBasic<String, List<MessageValue>>{
/**
*

View File

@ -0,0 +1,30 @@
package com.muyu.enterpise.cache;
import com.muyu.common.cache.CacheAbsBasic;
import com.muyu.domain.SysCar;
import org.springframework.stereotype.Component;
/**
* @version 1.0
* @Author xie ya ru
* @Date 2024/9/30 11:06
* @
*/
@Component
public class SysCarCacheService extends CacheAbsBasic<String, SysCar> {
@Override
public String keyPre() {
return "sysCar:info:";
}
@Override
public String encode(String key) {
return super.encode(key);
}
@Override
public String decode(String key) {
return super.decode(key);
}
}

View File

@ -0,0 +1,29 @@
package com.muyu.enterpise.cache;
import com.muyu.common.cache.CacheAbsBasic;
import com.muyu.domain.SysCarType;
import org.springframework.stereotype.Component;
/**
* @version 1.0
* @Author xie ya ru
* @Date 2024/9/30 11:18
* @
*/
@Component
public class SysCarTypeCacheService extends CacheAbsBasic<String, SysCarType> {
@Override
public String keyPre() {
return "sysCarType:info:";
}
@Override
public String encode(String key) {
return super.encode(key);
}
@Override
public String decode(String key) {
return super.decode(key);
}
}

View File

@ -1 +1,2 @@
com.muyu.enterpise.cache.MessageValueCacheService
com.muyu.enterpise.cache.SysCarCacheService

View File

@ -31,7 +31,7 @@ public class SysCarType extends BaseEntity {
*/
@TableId(value = "id", type = IdType.AUTO)
@Schema(name = "车辆类型主键")
private String id;
private Long id;
/**
*

View File

@ -33,7 +33,7 @@ public class MessageValueController extends BaseController {
private MessageValueService messageValueService;
@Autowired
private MessageValueCacheService enterpiseCacheService;
private MessageValueCacheService messageValueCacheService;
/**
*
@ -56,7 +56,6 @@ public class MessageValueController extends BaseController {
@Operation(summary = "添加报文数据", description = "新增报文数据")
public Result<String> save(@RequestBody MessageValueAddReq messageValueAddReq){
messageValueService.save(MessageValue.addBuild(messageValueAddReq));
enterpiseCacheService.put(String.valueOf(messageValueAddReq.getTemplateId()),MessageValue.addBuild(messageValueAddReq));
return Result.success("添加成功");
}
@ -81,12 +80,4 @@ public class MessageValueController extends BaseController {
}
@PostMapping({"/findByTemplateId/{stringVin}"})
@Operation(
summary = "根据车辆类型查询报文模版ID",
description = "根据车辆类型查询报文模版ID"
)
public Result<Long> findByTemplateId(@PathVariable("stringVin") String stringVin) {
return Result.success(this.messageValueService.findByTemplateId(stringVin), "查询成功");
}
}

View File

@ -8,6 +8,7 @@ import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.domain.SysCar;
import com.muyu.domain.req.SysCarReq;
import com.muyu.domain.resp.SysCarResp;
import com.muyu.enterpise.cache.SysCarCacheService;
import com.muyu.enterpise.service.SysCarService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
@ -30,6 +31,9 @@ public class SysCarController extends BaseController
@Resource
private SysCarService sysCarService;
@Resource
private SysCarCacheService sysCarCacheService;
/**
*
*/
@ -63,7 +67,9 @@ public class SysCarController extends BaseController
if (sysCarService.checkIdUnique(sysCar)) {
return error("新增 车辆基础信息 '" + sysCar + "'失败,车辆基础信息已存在");
}
sysCar.setCreateBy(SecurityUtils.getUsername());
sysCarCacheService.put(sysCar.getCarVin(),sysCar);
return toAjax(sysCarService.save(sysCar));
}

View File

@ -4,6 +4,8 @@ import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.domain.SysCarType;
import com.muyu.enterpise.cache.SysCarCacheService;
import com.muyu.enterpise.cache.SysCarTypeCacheService;
import com.muyu.enterpise.service.SysTypeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
@ -29,6 +31,9 @@ public class SysTypeController extends BaseController {
@Resource
private SysTypeService sysTypeService;
@Resource
private SysCarTypeCacheService sysCarTypeCacheService;
/**
*
@ -37,6 +42,10 @@ public class SysTypeController extends BaseController {
public Result<TableDataInfo<SysCarType>> list() {
startPage();
List<SysCarType> list = sysTypeService.selectSysTypeList();
for (SysCarType sysCarType : list) {
sysCarTypeCacheService.put(String.valueOf(sysCarType.getId()),sysCarType);
}
return getDataTable(list);
}
}

View File

@ -14,6 +14,5 @@ import org.apache.ibatis.annotations.Select;
*/
@Mapper
public interface MessageValueMapper extends BaseMapper<MessageValue> {
@Select({"SELECT sct.message_template_id FROM sys_car sc LEFT JOIN sys_car_type sct on sct.id = sc.car_type WHERE sc.car_vin = ${stringVin}"})
Long findByTemplateId(String stringVin);
}

View File

@ -27,6 +27,6 @@ public interface MessageValueService extends IService<MessageValue> {
* @param str
*/
void test(String str);
Long findByTemplateId(String stringVin);
}

View File

@ -7,6 +7,7 @@ import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.MessageValue;
import com.muyu.domain.req.MessageValueReq;
import com.muyu.domain.resp.MessageValueListResp;
import com.muyu.enterpise.cache.MessageValueCacheService;
import com.muyu.enterpise.mapper.MessageValueMapper;
import com.muyu.enterpise.service.MessageValueService;
import org.springframework.data.redis.core.RedisTemplate;
@ -30,7 +31,8 @@ public class MessageValueServiceImpl
@Resource
private MessageValueMapper messageValueMapper;
@Resource
private MessageValueCacheService messageValueCacheService;
/**
*
@ -53,8 +55,7 @@ public class MessageValueServiceImpl
}
List<MessageValue> list = this.list(queryWrapper);
messageValueCacheService.put(String.valueOf(messageValueReq.getMessageTemplateId()),list);
return list.stream()
.map(messageValue -> MessageValueListResp.valueBuild(
messageValue
@ -77,10 +78,7 @@ public class MessageValueServiceImpl
}
}
@Override
public Long findByTemplateId(String stringVin) {
return messageValueMapper.findByTemplateId(stringVin);
}
}

View File

@ -106,12 +106,13 @@
<artifactId>enterpise-cache</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- kafka 公共配置 -->
<!-- <dependency>-->
<!-- <groupId>com.muyu</groupId>-->
<!-- <artifactId>cloud-common-kafka</artifactId>-->
<!-- <version>${muyu.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-kafka</artifactId>
<version>${muyu.version}</version>
</dependency>
</dependencies>

View File

@ -1,21 +1,24 @@
package com.muyu.parse.process;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.muyu.common.core.domain.Result;
import com.muyu.common.kafka.config.KafkaProducerConfig;
import com.muyu.domain.MessageValue;
import com.muyu.domain.req.MessageValueReq;
import com.muyu.domain.resp.MessageValueListResp;
import com.muyu.domain.SysCar;
import com.muyu.domain.SysCarType;
import com.muyu.enterpise.cache.MessageValueCacheService;
import com.muyu.enterpise.cache.SysCarCacheService;
import com.muyu.enterpise.cache.SysCarTypeCacheService;
import com.muyu.parse.uitl.DataParseUtil;
import com.muyu.remote.RemoteMessageValueService;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
/**
* @version 1.0
* @Author xie ya ru
@ -28,58 +31,39 @@ public class ProcessData {
@Resource
private static RemoteMessageValueService remoteMessageValueService;
//报文模版信息
@Resource
private static MessageValueCacheService enterpiseCacheService;
private static MessageValueCacheService messageValueCacheService;
// @Resource
// private static KafkaProducerConfig kafkaProducerConfig;
//车辆信息
@Resource
private static SysCarCacheService sysCarCacheService;
//车辆类型信息
@Resource
private static SysCarTypeCacheService sysCarTypeCacheService;
@Resource
private static KafkaProducerConfig kafkaProducerConfig;
private final static String topic = "sysCar_vin_topic";
public static void DataConversion(String jsonVin ) {
//设置数组存储车辆数据
JSONObject jsonObject = new JSONObject();
String vin = DataParseUtil.dataParsing(jsonVin);
System.out.println("车辆转换的vin是"+vin);
//从Redis中获取车辆信息
//判断vin是否存在缓存中
if(sysCarCacheService.hashKey(vin)){
//从Redis中获取车辆信息
SysCar sysCar = sysCarCacheService.get(vin);
//根据缓存车辆类型获取缓存报文
SysCarType sysCarType = sysCarTypeCacheService.get(String.valueOf(sysCar.getCarType()));
//根据车辆信息获取车辆类型
Result<Long> byTemplateId = remoteMessageValueService.findByTemplateId(vin);
Long templateId = byTemplateId.getData();
List<MessageValueListResp> templateList;
try{
//从Redis中获取报文模版信息
if ( enterpiseCacheService.hashKey(String.valueOf(templateId))) {
List<Object> list = Collections.singletonList(enterpiseCacheService.get(String.valueOf(templateId)));
templateList = list.stream()
.map(obj -> JSON.parseObject(obj.toString(), MessageValueListResp.class))
.toList();
log.info("Redis缓存查询成功");
} else {
Result<List<MessageValueListResp>> byTemplate = remoteMessageValueService.findAll(MessageValueReq.builder().messageTemplateId(templateId).build());
templateList = byTemplate.getData();
templateList.forEach(
listResp ->{
enterpiseCacheService.put(String.valueOf(listResp.getMessageId()), MessageValue.addRollback(listResp));
}
);
}
log.info("数据库查询成功");
} catch (Exception e) {
throw new RuntimeException("获取报文模板失败");
}
//判断报文模板列表不为空
if (templateList.isEmpty()) {
throw new RuntimeException("报文模版为空");
}
//存储报文模版解析后的数据
JSONObject jsonObject = new JSONObject();
for (MessageValueListResp messageValue : templateList) {
//获取报文模版信息
List<MessageValue> messageValues = messageValueCacheService.get(String.valueOf(sysCarType.getMessageTemplateId()));
for (MessageValue messageValue : messageValues) {
//起始位下标
Integer startIndex = messageValue.getMessageStartIndex() - 1;
//结束位下标
@ -90,9 +74,20 @@ public class ProcessData {
System.out.println("标签"+messageValue.getMessageLabel()+"值"+value);
jsonObject.put(messageValue.getMessageLabel(), value);
}
System.out.println(jsonObject);
sendKafkaMessage(jsonObject);
}
}
private static void sendKafkaMessage(JSONObject jsonObject){
ProducerRecord<Object, JSONObject> producerRecord = new ProducerRecord<>(topic, jsonObject);
try {
kafkaProducerConfig.kafkaProducer().send(new ProducerRecord<>(topic,jsonObject.toString()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -53,7 +53,9 @@ spring:
- application-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# # xxl-job 配置文件
# - application-xxl-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
#kafka共享配置
#application-kafka-config-dev.yml
- application-kafka-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
logging:
level:
com.muyu.system.mapper: DEBUG

View File

@ -53,39 +53,6 @@
<version>0.2.21</version>
</dependency>
<!-- &lt;!&ndash; Cloud Gateway &ndash;&gt;-->
<!-- &lt;!&ndash; SpringCloud Gateway &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-gateway</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; SpringCloud Alibaba Sentinel Gateway &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; Sentinel Datasource Nacos &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.csp</groupId>-->
<!-- <artifactId>sentinel-datasource-nacos</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; SpringCloud Loadbalancer &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-loadbalancer</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; MuYu Common Redis&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.muyu</groupId>-->
<!-- <artifactId>cloud-common-redis</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.github.xiaoymin</groupId>-->
<!-- <artifactId>knife4j-gateway-spring-boot-starter</artifactId>-->
<!-- <version>4.5.0</version>-->
<!-- </dependency>-->
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>