Merge remote-tracking branch 'origin/master'

This commit is contained in:
joylink_zhangsai 2023-06-26 09:33:35 +08:00
commit 90340d5897
16 changed files with 361 additions and 189 deletions

View File

@ -4,6 +4,7 @@ import club.joylink.xiannccda.ats.message.MessageData;
import club.joylink.xiannccda.ats.message.line3.device.DeviceStatus;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import club.joylink.xiannccda.dto.protos.DeviceStatusProto;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.GeneratedMessageV3;
import java.lang.reflect.Array;
@ -112,6 +113,15 @@ public class DeviceStatusConvertor {
return convert(deviceStatusEnum, statusBitMap);
}
public static void fillField(GeneratedMessageV3.Builder build, String attrName, Object vals) {
Optional<FieldDescriptor> fd = build.getDescriptorForType().getFields().stream().filter(d -> StringUtils.equals(d.getName(), attrName)).findFirst();
if (fd.isEmpty()) {
throw new RuntimeException(String.format("未找到对应的属性 build:[%s] attrName:[%s],val:[%s]", build.getClass().getName(), attrName, vals));
}
FieldDescriptor ffdd = fd.get();
build.setField(ffdd, vals);
}
public static void copyAttr(GeneratedMessageV3.Builder build, String attrName, Object... vals) {
Class targetClass = build.getClass();
Optional<Method> optional = Arrays.stream(targetClass.getMethods()).filter(d -> StringUtils.startsWith(d.getName(), "set") && StringUtils.endsWithIgnoreCase(d.getName(), attrName)).findFirst();

View File

@ -72,7 +72,8 @@ public class DeviceStatusBitmapResponse extends MessageResponse {
DeviceType dt = deviceTypeEntity.getType();
for (DeviceEntity deviceEntity : deviceTypeEntity.deviceList) {
GeneratedMessageV3.Builder builder = DeviceStatusConvertor.convert(dt, deviceEntity.status);
DeviceStatusConvertor.copyAttr(builder, "id", deviceEntity.getDevName());
DeviceStatusConvertor.fillField(builder, "id", deviceEntity.devName);
DeviceStatusConvertor.convertForTrack(deviceEntity.getStatus(), builder);
DeviceStatusConvertor.convertForPlatform(deviceEntity.getStatus(), (Objects.isNull(deviceEntity.getSpare()) ? 0 : deviceEntity.getSpare()), builder);
DeviceStatusConvertor.convertForSwitch(Objects.isNull(deviceEntity.getSpare()) ? 0 : deviceEntity.getSpare(), builder);

View File

@ -5,14 +5,18 @@ import club.joylink.xiannccda.ats.message.MessageResponse;
import club.joylink.xiannccda.ats.message.convertor.DeviceStatusConvertor;
import club.joylink.xiannccda.ats.message.line3.device.DeviceStatus;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import club.joylink.xiannccda.dto.protos.DeviceStatusProto;
import com.google.common.collect.Lists;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.GeneratedMessageV3.Builder;
import io.netty.buffer.ByteBuf;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
/**
* 2.7.5 设备状态变化消息
@ -79,10 +83,27 @@ public class DeviceStatusChangeResponse extends MessageResponse {
this.spare = buf.readInt();
}
public static void main(String[] args) {
GeneratedMessageV3.Builder rtuBuild = DeviceStatusProto.Rtu.newBuilder();
// rtuBuild.setId("asd");
rtuBuild.getDescriptorForType().getFields().forEach(d -> System.out.println(d.getName()));
Optional<FieldDescriptor> fd = rtuBuild.getDescriptorForType().getFields().stream().filter(d -> StringUtils.equals(d.getName(), "id")).findFirst();
if (fd.isEmpty()) {
}
FieldDescriptor ffdd = fd.get();
rtuBuild.setField(ffdd, "asdfasdf");
System.out.println(rtuBuild.getAllFields());
System.out.println(rtuBuild.getClass().getName());
}
@Override
public List<Builder> generateProto() throws Exception {
GeneratedMessageV3.Builder builder = DeviceStatusConvertor.convert(this.type, this.deviceStatus);
DeviceStatusConvertor.copyAttr(builder, "id", this.getDevName());
DeviceStatusConvertor.fillField(builder, "id", this.devName);
DeviceStatusConvertor.convertForTrack(this.deviceStatus, builder);
DeviceStatusConvertor.convertForPlatform(this.deviceStatus, Objects.isNull(this.spare) ? 0 : this.spare, builder);
DeviceStatusConvertor.convertForSwitch(Objects.isNull(this.spare) ? 0 : this.spare, builder);

View File

@ -1,6 +1,7 @@
package club.joylink.xiannccda.controller;
import club.joylink.xiannccda.dto.PublishedGIDTO;
import club.joylink.xiannccda.dto.PublishedGIDTO.LineType;
import club.joylink.xiannccda.dto.PublishedGIQueryDTO;
import club.joylink.xiannccda.entity.PublishedGi;
import club.joylink.xiannccda.repository.IPublishedGiRepository;
@ -17,9 +18,7 @@ import java.security.Principal;
import java.util.List;
/**
* <p>
* 发布图形界面 前端控制器
* </p>
*
* @author walker-sheng
* @since 2023-06-08
@ -29,65 +28,76 @@ import java.util.List;
@Tag(name = "发布图形管理接口")
public class PublishedGiController {
final IPublishedGiRepository publishedGiRepository;
final IPublishedGiRepository publishedGiRepository;
final PublishedGiService publishedGiService;
final PublishedGiService publishedGiService;
public PublishedGiController(IPublishedGiRepository publishedGiRepository, PublishedGiService publishedGiService) {
this.publishedGiRepository = publishedGiRepository;
this.publishedGiService = publishedGiService;
}
public PublishedGiController(
IPublishedGiRepository publishedGiRepository, PublishedGiService publishedGiService) {
this.publishedGiRepository = publishedGiRepository;
this.publishedGiService = publishedGiService;
}
@GetMapping("/paging")
@SecurityRequirement(name = "jwt")
@Operation(summary = "查询分页发布图形数据")
@ApiResponse(description = "查询发布图形数据")
public Page<PublishedGi> paging(PublishedGIQueryDTO query) {
return publishedGiRepository.paging(query);
}
@GetMapping("/paging")
@SecurityRequirement(name = "jwt")
@Operation(summary = "查询分页发布图形数据")
@ApiResponse(description = "查询发布图形数据")
public Page<PublishedGi> paging(PublishedGIQueryDTO query) {
return publishedGiRepository.paging(query);
}
@GetMapping("/list")
@SecurityRequirement(name = "jwt")
@Operation(summary = "查询发布图形数据列表")
@ApiResponse(description = "查询发布图形数据列表")
public List<PublishedGi> list(PublishedGIQueryDTO query) {
return publishedGiRepository.list(query);
}
@GetMapping("/list")
@SecurityRequirement(name = "jwt")
@Operation(summary = "查询发布图形数据列表")
@ApiResponse(description = "查询发布图形数据列表")
public List<PublishedGi> list(PublishedGIQueryDTO query) {
return publishedGiRepository.list(query);
}
@GetMapping("/{id}")
@SecurityRequirement(name = "jwt")
@Operation(summary = "根据主键获取发布图形数据详情")
@ApiResponse(description = "根据主键获取发布图形数据详情")
public PublishedGi queryById(@PathVariable Integer id) {
return publishedGiRepository.getById(id);
}
@GetMapping("/{id}")
@SecurityRequirement(name = "jwt")
@Operation(summary = "根据主键获取发布图形数据详情")
@ApiResponse(description = "根据主键获取发布图形数据详情")
public PublishedGi queryById(@PathVariable Integer id) {
return publishedGiRepository.getById(id);
}
@GetMapping("/{type}/{lineId}")
@SecurityRequirement(name = "jwt")
@Operation(summary = "根据线路号和类型获取发布图形数据详情")
@ApiResponse(description = "根据线路号和类型获取发布图形数据详情")
public PublishedGi queryByTypeAndLineId(@PathVariable String type, @PathVariable Integer lineId) {
PublishedGIQueryDTO dto = new PublishedGIQueryDTO();
dto.setType(type);
dto.setLineId(lineId);
return publishedGiRepository.queryByDto(dto);
}
@GetMapping("/{type}/{lineId}")
@SecurityRequirement(name = "jwt")
@Operation(summary = "根据线路号和类型获取发布图形数据详情")
@ApiResponse(description = "根据线路号和类型获取发布图形数据详情")
public PublishedGi queryByTypeAndLineId(@PathVariable String type, @PathVariable Integer lineId) {
PublishedGIQueryDTO dto = new PublishedGIQueryDTO();
dto.setType(type);
dto.setLineId(lineId);
return publishedGiRepository.queryByDto(dto);
}
@PostMapping("/publish")
@SecurityRequirement(name = "jwt")
@Operation(summary = "发布草稿图形数据")
@ApiResponse(description = "发布草稿图形数据")
public PublishedGi publish(
Principal user,
@RequestBody @Validated(PublishedGIDTO.Publish.class) PublishedGIDTO publishedDto) {
return publishedGiService.publish(user, publishedDto);
}
@PostMapping("/publish")
@SecurityRequirement(name = "jwt")
@Operation(summary = "发布草稿图形数据")
@ApiResponse(description = "发布草稿图形数据")
public PublishedGi publish(Principal user, @RequestBody @Validated(PublishedGIDTO.Publish.class) PublishedGIDTO publishedDto) {
return publishedGiService.publish(user, publishedDto);
}
@DeleteMapping("/{id}")
@SecurityRequirement(name = "jwt")
@Operation(summary = "删除发布图形数据")
@ApiResponse(description = "删除发布图形数据")
public boolean deleteById(@PathVariable Integer id) {
return publishedGiRepository.removeById(id);
}
@DeleteMapping("/{id}")
@SecurityRequirement(name = "jwt")
@Operation(summary = "删除发布图形数据")
@ApiResponse(description = "删除发布图形数据")
public boolean deleteById(@PathVariable Integer id) {
return publishedGiRepository.removeById(id);
}
@GetMapping("/lineNetwork")
@SecurityRequirement(name = "jwt")
@Operation(summary = "获取已发布的现网数据")
@ApiResponse(description = "获取已发布的现网数据")
public PublishedGi queryLineNetwork() {
PublishedGIQueryDTO query = new PublishedGIQueryDTO();
query.setType(LineType.LineNetwork.name());
return publishedGiRepository.queryByDto(query);
}
}

View File

@ -14,14 +14,18 @@ public class PublishedGIDTO {
private String name;
@Schema(description = "关联的线路号")
@NotNull(message = "关联线路不能为空", groups = { Publish.class })
private Integer lineId;
@Schema(description = "草稿数据主键")
@NotNull(message = "请选择草稿数据", groups = { Publish.class })
@NotNull(
message = "请选择草稿数据",
groups = {Publish.class})
private Integer draftingId;
public interface Publish {
public interface Publish {}
public enum LineType {
Line,
LineNetwork
}
}

View File

@ -1,5 +1,7 @@
package club.joylink.xiannccda.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.time.LocalDateTime;
import io.swagger.v3.oas.annotations.media.Schema;
@ -8,10 +10,6 @@ import lombok.Setter;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author walker-sheng
* @since 2023-06-08
*/
@ -22,6 +20,7 @@ import lombok.experimental.Accessors;
@Schema(name = "PublishedGi", description = "发布的图形界面数据")
public class PublishedGi {
@TableId(value = "id", type = IdType.AUTO)
@Schema(description = "id")
private Integer id;

View File

@ -44,8 +44,8 @@ public class NccMockData {
public static final String MOCK_RECEIVE_TIME = "mock_receive_time";
private static final String MSG_TYPE = "msg_type";
private static final String ACTION_TYPE = "ACTION_type";
public static final String MSG_TYPE = "msg_type";
public static final String ACTION_TYPE = "action_type";
public enum MsgTypeEnum {
REAL_TIME, UNREAL_TIME

View File

@ -32,13 +32,18 @@ public class NccMockDataService {
private static boolean FINISH = false;
private final static int PAGE_SIZE = 200;
private static int PAGE_SIZE = 200;
public void reset() {
LAST_ID = 0L;
FINISH = false;
}
public void reset(int pageSize) {
this.reset();
PAGE_SIZE = 1;
}
public boolean finish() {
return FINISH;
}
@ -94,6 +99,8 @@ public class NccMockDataService {
private List<NccMockData> findData() {
QueryWrapper<NccMockData> queryWrapper = Wrappers.query();
queryWrapper.gt(NccMockData.ID, LAST_ID);
queryWrapper.eq(NccMockData.MSG_TYPE, MsgTypeEnum.REAL_TIME.name());
queryWrapper.eq(NccMockData.ACTION_TYPE, ActionTypeEnum.UPDATES.name());
queryWrapper.last(String.format(" limit %s", PAGE_SIZE));
List<NccMockData> list = this.nccMockDataRepository.list(queryWrapper);
return list;

View File

@ -1,15 +1,41 @@
package club.joylink.xiannccda.mock.message.ws;
import club.joylink.xiannccda.ats.message.MessageData;
import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataOperate;
import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataRepository;
import club.joylink.xiannccda.dto.protos.WsMessageProto.WsLineMessage;
import club.joylink.xiannccda.dto.protos.WsMessageProto.WsLineNetMessage;
import club.joylink.xiannccda.mock.message.NccMockDataService;
import club.joylink.xiannccda.ws.IMessageServer;
import club.joylink.xiannccda.ws.TopicMessage;
import club.joylink.xiannccda.ws.WsMessageServerManager;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.protobuf.Descriptors.FieldDescriptor.Type;
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.GeneratedMessageV3.Builder;
import com.google.protobuf.Message;
import jakarta.annotation.PostConstruct;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PropertyPlaceholderHelper;
@Component
@RequiredArgsConstructor
@ -18,30 +44,121 @@ public class MockDeviceMessageServer implements IMessageServer {
private final WsMessageServerManager serverManager;
private final NccMockDataService nccMockDataService;
private final static String SUBSCRIPTION_PATH = "/queue/line/{lineId}";
private final Set<String> lineIdSet = Sets.newHashSet();
private static PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
"{", "}", ":", false);
@PostConstruct
public void init() {
List<MessageData> dataList = this.nccMockDataService.loadALLData();
try {
for (MessageData messageData : dataList) {
List<GeneratedMessageV3.Builder> builders = messageData.generateProto();
DeviceStatusDataRepository.addDeviceStatusDataList("3", builders);
}
} catch (Exception e) {
e.printStackTrace();
}
this.serverManager.registerMessageServer(this);
this.nccMockDataService.reset(1);
this.circleQueryMockData();
}
static final ScheduledExecutorService CIRCLE_QUERY_THREAD = Executors.newSingleThreadScheduledExecutor();
private void circleQueryMockData() {
CIRCLE_QUERY_THREAD.scheduleAtFixedRate(() -> {
if (lineIdSet.isEmpty()) {
return;
}
List<MessageData> messageDataList = this.nccMockDataService.getMessageData();
if (CollectionUtils.isEmpty(messageDataList)) {
System.out.println("没有新的数据");
}
for (MessageData messageData : messageDataList) {
try {
DeviceStatusDataRepository.addDeviceStatusDataList("3", messageData.generateProto());
} catch (Exception e) {
e.printStackTrace();
}
}
}, this.getInterval(), this.getInterval(), TimeUnit.MILLISECONDS);
}
@Override
public String getDestinationPattern() {
// return String.format("/queue/line/%s", this.lineId);
return null;
return SUBSCRIPTION_PATH;
}
@Override
public Object onSubscription(String destination, Map<String, String> paramMap) {
String lineId = paramMap.get("lineId");
lineIdSet.add(lineId);
log.info("线路lineId={}订阅,发布全量数据", lineId);
// byte[] bytes = DeviceStatusDataRepository.collectLineAllMessage(lineId).toByteArray();
// System.out.println(JSON.toJSONString(bytes));
// return bytes;
return null;
DeviceStatusDataRepository.getDeviceStatusData(lineId).getAllDeviceMap();
WsLineMessage.Builder builder = WsLineMessage.newBuilder();
fillBuilderFunction(fun -> {
Map<String, Builder> builderMap = DeviceStatusDataRepository.getDeviceStatusData(lineId).getAllDeviceMap().get(fun);
if (Objects.isNull(builderMap)) {
return Map.of();
}
Map<String, Message> messageMap = Maps.newHashMap();
builderMap.forEach((k, v) -> messageMap.put(k, v.build()));
return messageMap;
}, builder);
WsLineMessage buildMsg = builder.build();
return buildMsg.toByteArray();
// return new TopicMessage(destination, buildMsg);
}
@Override
public int getInterval() {
return 500;
return 1500;
}
@Override
public List<TopicMessage> onTick() {
return null;
if (this.lineIdSet.isEmpty()) {
return Collections.emptyList();
}
Properties properties = new Properties();
List<TopicMessage> messages = Lists.newArrayList();
for (String lineId : this.lineIdSet) {
WsLineMessage.Builder msg = WsLineMessage.newBuilder();
fillBuilderFunction((field) -> DeviceStatusDataRepository.getDeviceStatusData(lineId).getStatusVOMap().get(field), msg);
DeviceStatusDataOperate.clearStatusVOMap(DeviceStatusDataRepository.getDeviceStatusData(lineId));
properties.put("lineId", lineId);
String destination = helper.replacePlaceholders(SUBSCRIPTION_PATH, properties);
messages.add(new TopicMessage(destination, msg.build().toByteArray()));
properties.clear();
}
return messages;
}
public void fillBuilderFunction(Function<String, Map<String, Message>> compareFun, GeneratedMessageV3.Builder builder) {
// 消息体字段列表
builder.getDescriptorForType().getFields().stream()
.filter(f -> f.getType().equals(Type.MESSAGE))
.forEach(
field -> {
String fieldType = field.getMessageType().getName(); // 字段类型
Map<String, Message> allDeviceMap = compareFun.apply(fieldType);
if (!CollectionUtils.isEmpty(allDeviceMap)) {
builder.setField(field, new ArrayList<>(allDeviceMap.values()));
}
});
}
}

View File

@ -1,27 +0,0 @@
package club.joylink.xiannccda.mock.message.ws;
import club.joylink.xiannccda.ats.message.MessageData;
import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataRepository;
import club.joylink.xiannccda.mock.message.NccMockDataService;
import com.google.protobuf.GeneratedMessageV3;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class MockLoadDataService implements ApplicationRunner {
private final NccMockDataService dataService;
@Override
public void run(ApplicationArguments args) throws Exception {
List<MessageData> dataList = this.dataService.loadALLData();
for (MessageData messageData : dataList) {
List<GeneratedMessageV3.Builder> builders = messageData.generateProto();
DeviceStatusDataRepository.addDeviceStatusDataList("3", builders);
}
}
}

View File

@ -8,18 +8,16 @@ import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 服务类
* </p>
* 服务类
*
* @author walker-sheng
* @since 2023-06-08
*/
public interface IPublishedGiRepository extends IService<PublishedGi> {
Page<PublishedGi> paging(PublishedGIQueryDTO query);
Page<PublishedGi> paging(PublishedGIQueryDTO query);
List<PublishedGi> list(PublishedGIQueryDTO query);
List<PublishedGi> list(PublishedGIQueryDTO query);
PublishedGi queryByDto(PublishedGIQueryDTO query);
PublishedGi queryByDto(PublishedGIQueryDTO query);
}

View File

@ -1,5 +1,6 @@
package club.joylink.xiannccda.repository.impl;
import club.joylink.xiannccda.dto.PublishedGIDTO.LineType;
import club.joylink.xiannccda.dto.PublishedGIQueryDTO;
import club.joylink.xiannccda.entity.PublishedGi;
import club.joylink.xiannccda.mapper.PublishedGiMapper;
@ -14,47 +15,46 @@ import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
* 服务实现类
*
* @author walker-sheng
* @since 2023-06-08
*/
@Service
public class PublishedGiRepository extends ServiceImpl<PublishedGiMapper, PublishedGi> implements IPublishedGiRepository {
public class PublishedGiRepository extends ServiceImpl<PublishedGiMapper, PublishedGi>
implements IPublishedGiRepository {
@Override
public Page<PublishedGi> paging(PublishedGIQueryDTO query) {
LambdaQueryWrapper<PublishedGi> wrapper = getQueryWrapper(query);
wrapper.select(PublishedGi.class, publishedGi -> !publishedGi.getColumn().equals("proto"));
return page(query, wrapper);
}
@Override
public Page<PublishedGi> paging(PublishedGIQueryDTO query) {
LambdaQueryWrapper<PublishedGi> wrapper = getQueryWrapper(query);
wrapper.select(PublishedGi.class, publishedGi -> !publishedGi.getColumn().equals("proto"));
return page(query, wrapper);
}
@Override
public List<PublishedGi> list(PublishedGIQueryDTO query) {
LambdaQueryWrapper<PublishedGi> wrapper = getQueryWrapper(query);
wrapper.select(PublishedGi.class, publishedGi -> !publishedGi.getColumn().equals("proto"));
return list(wrapper);
}
@Override
public List<PublishedGi> list(PublishedGIQueryDTO query) {
LambdaQueryWrapper<PublishedGi> wrapper = getQueryWrapper(query);
wrapper.select(PublishedGi.class, publishedGi -> !publishedGi.getColumn().equals("proto"));
return list(wrapper);
}
@Override
public PublishedGi queryByDto(PublishedGIQueryDTO query) {
LambdaQueryWrapper<PublishedGi> wrapper = getQueryWrapper(query);
return getOne(wrapper);
}
@Override
public PublishedGi queryByDto(PublishedGIQueryDTO query) {
LambdaQueryWrapper<PublishedGi> wrapper = getQueryWrapper(query);
return getOne(wrapper);
}
private static LambdaQueryWrapper<PublishedGi> getQueryWrapper(PublishedGIQueryDTO query) {
LambdaQueryWrapper<PublishedGi> wrapper = Wrappers.lambdaQuery();
if (StringUtils.isNotEmpty(query.getName())) {
wrapper.like(PublishedGi::getName, query.getName());
}
if (StringUtils.isNotEmpty(query.getType())) {
wrapper.eq(PublishedGi::getType, query.getType());
}
if (query.getLineId() != null) {
wrapper.eq(PublishedGi::getLineId, query.getLineId());
}
return wrapper;
private static LambdaQueryWrapper<PublishedGi> getQueryWrapper(PublishedGIQueryDTO query) {
LambdaQueryWrapper<PublishedGi> wrapper = Wrappers.lambdaQuery();
if (StringUtils.isNotEmpty(query.getName())) {
wrapper.like(PublishedGi::getName, query.getName());
}
if (StringUtils.isNotEmpty(query.getType())) {
wrapper.eq(PublishedGi::getType, query.getType());
}
if (query.getLineId() != null) {
wrapper.eq(PublishedGi::getLineId, query.getLineId());
}
return wrapper;
}
}

View File

@ -1,6 +1,7 @@
package club.joylink.xiannccda.service;
import club.joylink.xiannccda.dto.PublishedGIDTO;
import club.joylink.xiannccda.dto.PublishedGIDTO.LineType;
import club.joylink.xiannccda.entity.Drafting;
import club.joylink.xiannccda.entity.LineInfo;
import club.joylink.xiannccda.entity.PublishedGi;
@ -10,6 +11,7 @@ import club.joylink.xiannccda.repository.ILineInfoRepository;
import club.joylink.xiannccda.repository.IPublishedGiRepository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -20,56 +22,69 @@ import java.time.LocalDateTime;
@Service
public class PublishedGiService {
final IPublishedGiRepository publishedGiRepository;
final IPublishedGiRepository publishedGiRepository;
final IDraftingRepository draftingRepository;
final IDraftingRepository draftingRepository;
final ILineInfoRepository lineInfoRepository;
final ILineInfoRepository lineInfoRepository;
public PublishedGiService(
IPublishedGiRepository publishedGiRepository,
IDraftingRepository draftingRepository,
ILineInfoRepository lineInfoRepository) {
this.publishedGiRepository = publishedGiRepository;
this.draftingRepository = draftingRepository;
this.lineInfoRepository = lineInfoRepository;
}
public PublishedGiService(IPublishedGiRepository publishedGiRepository, IDraftingRepository draftingRepository, ILineInfoRepository lineInfoRepository) {
this.publishedGiRepository = publishedGiRepository;
this.draftingRepository = draftingRepository;
this.lineInfoRepository = lineInfoRepository;
/**
* 发布草稿数据
*
* @param user 用户
* @param publishedDto 草稿数据
* @return 发布后的实体数据
*/
@Transactional(rollbackFor = Exception.class)
public PublishedGi publish(Principal user, PublishedGIDTO publishedDto) {
// 查询草稿信息
Drafting drafting = draftingRepository.getById(publishedDto.getDraftingId());
if (drafting == null) {
throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(
String.format("草稿[%s]信息不存在", publishedDto.getDraftingId()));
}
if (drafting.getProto() == null) {
throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(
String.format("草稿[%s]绘图数据信息为空", publishedDto.getDraftingId()));
}
/**
* 发布草稿数据
* @param user 用户
* @param publishedDto 草稿数据
* @return 发布后的实体数据
*/
@Transactional(rollbackFor = Exception.class)
public PublishedGi publish(Principal user, PublishedGIDTO publishedDto) {
// 查询草稿信息
Drafting drafting = draftingRepository.getById(publishedDto.getDraftingId());
if (drafting == null) {
throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(String.format("草稿[%s]信息不存在", publishedDto.getDraftingId()));
}
if (drafting.getProto() == null) {
throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(String.format("草稿[%s]绘图数据信息为空", publishedDto.getDraftingId()));
}
// 查询线路信息是否存在
LambdaQueryWrapper<LineInfo> lineInfoWrapper = Wrappers.lambdaQuery();
lineInfoWrapper.eq(LineInfo::getLineId, publishedDto.getLineId());
LineInfo lineInfo = lineInfoRepository.getOne(lineInfoWrapper);
if (lineInfo == null) {
throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(String.format("线路[%s]信息不存在", publishedDto.getLineId()));
}
// 删除已发布的绘图数据
LambdaQueryWrapper<PublishedGi> wrapper = Wrappers.lambdaQuery();
String name = StringUtils.isNotEmpty(publishedDto.getName()) ? publishedDto.getName() : drafting.getName();
wrapper.eq(PublishedGi::getType, drafting.getType()).eq(PublishedGi::getLineId, publishedDto.getLineId());
publishedGiRepository.remove(wrapper); // 删除发布数据
// 保存发布信息
PublishedGi publishedGi = new PublishedGi();
publishedGi.setName(name);
publishedGi.setType(drafting.getType());
publishedGi.setLineId(lineInfo.getLineId());
publishedGi.setProto(drafting.getProto());
publishedGi.setUserId(Integer.valueOf(user.getName()));
publishedGi.setPublishAt(LocalDateTime.now());
publishedGiRepository.save(publishedGi);
return publishedGi;
// 删除已发布的绘图数据
LambdaQueryWrapper<PublishedGi> wrapper = Wrappers.lambdaQuery();
wrapper.eq(PublishedGi::getType, drafting.getType());
if (Objects.equals(drafting.getType(), LineType.Line.name())) {
// 查询线路信息是否存在
LambdaQueryWrapper<LineInfo> lineInfoWrapper = Wrappers.lambdaQuery();
lineInfoWrapper.eq(LineInfo::getLineId, publishedDto.getLineId());
LineInfo lineInfo = lineInfoRepository.getOne(lineInfoWrapper);
if (lineInfo == null) {
throw BusinessExceptionAssertEnum.DATA_NOT_EXIST.exception(
String.format("线路[%s]信息不存在", publishedDto.getLineId()));
}
wrapper.eq(PublishedGi::getLineId, publishedDto.getLineId());
}
publishedGiRepository.remove(wrapper); // 删除发布数据
// 保存发布信息
PublishedGi publishedGi = new PublishedGi();
String name =
StringUtils.isNotEmpty(publishedDto.getName())
? publishedDto.getName()
: drafting.getName();
publishedGi.setName(name);
publishedGi.setType(drafting.getType());
publishedGi.setLineId(publishedDto.getLineId());
publishedGi.setProto(drafting.getProto());
publishedGi.setUserId(Integer.valueOf(user.getName()));
publishedGi.setPublishAt(LocalDateTime.now());
publishedGiRepository.save(publishedGi);
return publishedGi;
}
}

View File

@ -16,7 +16,9 @@ import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
import org.springframework.web.socket.messaging.SessionSubscribeEvent;
import org.springframework.web.socket.messaging.SessionUnsubscribeEvent;
@Component
@Slf4j
@ -80,4 +82,14 @@ public class WsMessageServerManager {
}
});
}
/* @EventListener
public void disconnect(SessionDisconnectEvent event) {
System.out.println(event + "-----------dis");
}
@EventListener
public void unSubecribe(SessionUnsubscribeEvent event) {
System.out.println(event + "------- UnsubscribeEvent");
}*/
}

View File

@ -5,6 +5,7 @@ import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
@ -17,7 +18,7 @@ import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
public class StompTest {
@Test
public void subscribe() {
StandardWebSocketClient socketClient = new StandardWebSocketClient();
@ -27,7 +28,7 @@ public class StompTest {
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
StompHeaders headers1 = new StompHeaders();
headers1.set("Authorization",
"Bearer eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoiNiIsImV4cCI6MTY4NzUwNzI0MiwiaWF0IjoxNjg3MjQ4MDQyfQ.SEMBj2lYxyy5ebLCLg6YKfOqi20cAOnEcEaLU9Xa_PAV7MJNY6iakXy0DmL2PshKGtsikEaEpDg0S353NuSmBBBthk2U14tc4aYvIGJ9GxlGWDx3vOI3DX4X0Q7ZROq921cxpWPPGi-KKSPMnp3dkzofIS8La77rNYynOp78dmFojeo6FwPGnaOT5ve_d2l72sN9KO5Bd_hjGxUk0SIphGBDjPWoQeSfPG-3tlIFxfNPeRifwgGjfmRG5Gg6VvQfLUsRR7yMwuxnDnvDuxRmQ-7cTx2p1QSo3nILyYMdLT5LUHuejWJteiMSz9SrFbyyqclyqlDRV-5jFdgqj1uHLQ");
"Bearer eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoiOSIsImV4cCI6MTY4NzkzODcwNCwiaWF0IjoxNjg3Njc5NTA0fQ.W__1N-IyXIJ4SIPsFviK-1q_0H9lqQsr0uI6VhdpZsPME63Aes8Fr23XItIlVqb14pgWVeVxTnRMKnG8gKzqFj8FTJs6c2jo5wDYX2UTBED3GPBIdzPTNETQATwh2vQNoItxyGcepVzToN4jmjtIScmtly3ppIXmCgYUNtwniWRz_5FAbDyy18cawd7-rr9ex37uahUewdohgrQFDOHDRzxC0M5AGTpfxKIiXHYs-rGdyfqhvhpoy26bM0TTMr8_EpssODLY8EvSLFfQgqtzhlXbrOQWuOjG7fQ4RQa1cKFHzFtQPosIkE3gsvyCRw2BCLERlqmGoAor-3RZ5Mqv_w");
CompletableFuture<StompSession> future = stompClient.connectAsync("ws://127.0.0.1:9081/ws-default", headers, headers1, handler);
@ -37,7 +38,7 @@ public class StompTest {
} catch (Exception e) {
e.printStackTrace();
}
stompSession.subscribe("/queue/lineNet", new StompFrameHandler() {
stompSession.subscribe("/queue/line/3", new StompFrameHandler() {
@Override
public Type getPayloadType(StompHeaders stompHeaders) {
return byte[].class;
@ -46,6 +47,10 @@ public class StompTest {
@Override
public void handleFrame(StompHeaders stompHeaders, Object o) {
String recv = null;
if (Objects.isNull(o)) {
System.out.println("接收数据为空");
return;
}
try {
recv = new String((byte[]) o, "UTF-8");
} catch (UnsupportedEncodingException e) {

@ -1 +1 @@
Subproject commit 3bd4f023cd1810dd1ec9611c3714d626a08377d2
Subproject commit f42a98cd93767b1fddec263181cb6bed53729665