调整ncc 数据发布

This commit is contained in:
tiger_zhou 2023-03-13 13:42:09 +08:00
parent 6838e18531
commit 55a5b31dee
4 changed files with 68 additions and 16 deletions

View File

@ -1,9 +1,11 @@
package club.joylink.rtss.simulation.ncc.dto;
import club.joylink.rtss.simulation.ncc.entity.RtsNccMapData;
import club.joylink.rtss.simulation.ncc.entity.RtsNccMapDataPublish;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public class NccMapDataDto implements Serializable {
@ -26,6 +28,18 @@ public class NccMapDataDto implements Serializable {
mapData.setState(this.getState());
return mapData;
}
public RtsNccMapDataPublish convertPublish(){
RtsNccMapDataPublish dataPublish = new RtsNccMapDataPublish();
dataPublish.setName(this.name);
dataPublish.setMapData(this.data);
LocalDateTime dateTime = LocalDateTime.now();
dataPublish.setCreateTime(dateTime);
dataPublish.setUpdateTime(dateTime);
dataPublish.setState(0);
return dataPublish;
}
public enum MapDataEnum{
PUBLISH,EDIT,DELETE;
}

View File

@ -19,7 +19,7 @@ import lombok.Setter;
@Getter
@Setter
@TableName("rts_ncc_map_data_publish")
public class RtsNccMapDataPublish implements Serializable {
public class RtsNccMapDataPublish implements Serializable,Cloneable {
private static final long serialVersionUID = 1L;
@ -32,7 +32,7 @@ public class RtsNccMapDataPublish implements Serializable {
private LocalDateTime createTime;
private Integer version;
private Float version;
private LocalDateTime updateTime;
@ -40,4 +40,13 @@ public class RtsNccMapDataPublish implements Serializable {
* 0=启用1=禁用
*/
private Integer state;
public RtsNccMapDataPublish clone(){
try {
return (RtsNccMapDataPublish) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -7,8 +7,13 @@ import club.joylink.rtss.simulation.ncc.exception.NccExceptionAssertEnum;
import club.joylink.rtss.simulation.ncc.mapper.RtsNccMapDataMapper;
import club.joylink.rtss.simulation.ncc.repository.IRtsNccMapDataPublishRepository;
import club.joylink.rtss.simulation.ncc.repository.IRtsNccMapDataRepository;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.util.concurrent.AtomicDouble;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.RegEx;
import javax.annotation.Resource;
@ -25,6 +30,7 @@ import java.util.Optional;
* @since 2023-02-27
*/
@Service
@Transactional
public class RtsNccMapDataRepository extends ServiceImpl<RtsNccMapDataMapper, RtsNccMapData> implements IRtsNccMapDataRepository {
@Resource
@ -42,17 +48,33 @@ public class RtsNccMapDataRepository extends ServiceImpl<RtsNccMapDataMapper, Rt
RtsNccMapData newData = this.byId(id);
NccExceptionAssertEnum.StateNotMatch.assertTrue(newData.getState() == NccMapDataDto.MapDataEnum.PUBLISH,"已经发布的数据不能修改");
NccExceptionAssertEnum.StateNotMatch.assertTrue(newData.getState() == NccMapDataDto.MapDataEnum.DELETE,"已经删除的数据不能修改");
newData.setId(id);
newData.setState(NccMapDataDto.MapDataEnum.PUBLISH);
newData.setUpdateTime(LocalDateTime.now());
this.getBaseMapper().updateById(newData);
UpdateWrapper<RtsNccMapData> mapDataWrapper = new UpdateWrapper<>();
mapDataWrapper.eq("id",id);
mapDataWrapper.set("state",NccMapDataDto.MapDataEnum.PUBLISH);
mapDataWrapper.set("update_time",LocalDateTime.now());
this.update(mapDataWrapper);
Optional<RtsNccMapDataPublish> dataPublishOptional = this.publishRepository.findLast();
RtsNccMapDataPublish dataPublish = dataPublishOptional.orElse(null);
int version = 0;
if(Objects.isNull(dataPublish)){
version = dataPublishOptional.get().getVersion() + 1;
}
AtomicDouble ad = new AtomicDouble(0.0);
if(Objects.nonNull(dataPublish)){
UpdateWrapper<RtsNccMapDataPublish> dataPublishWrapper = new UpdateWrapper<>();
dataPublishWrapper.eq("id",dataPublish.getId());
dataPublishWrapper.set("state",1);
ad.set(dataPublish.getVersion().doubleValue());
this.publishRepository.update(dataPublishWrapper);
}
ad.addAndGet(0.1F);
RtsNccMapDataPublish cloneDataPublish = dataPublish.clone();
cloneDataPublish.setId(null);
cloneDataPublish.setMapData(newData.getMapData());
cloneDataPublish.setState(0);
cloneDataPublish.setName(newData.getName());
cloneDataPublish.setCreateTime(newData.getCreateTime());
cloneDataPublish.setUpdateTime(newData.getUpdateTime());
publishRepository.save(cloneDataPublish);
}
@Override

View File

@ -3,9 +3,11 @@ package club.joylink.rtss.simulation.ncc.simulation;
import club.joylink.ecs.World;
import club.joylink.ecs.WorldManage;
import club.joylink.rtss.simulation.ncc.dto.NccMapDataDto;
import club.joylink.rtss.simulation.ncc.entity.RtsNccMapDataPublish;
import club.joylink.rtss.simulation.ncc.exception.NccExceptionAssertEnum;
import club.joylink.rtss.simulation.ncc.module.proto.StorageProto;
import club.joylink.rtss.simulation.ncc.module.proto.TrainStateProto;
import club.joylink.rtss.simulation.ncc.repository.IRtsNccMapDataPublishRepository;
import club.joylink.rtss.simulation.ncc.repository.IRtsNccMapDataRepository;
import club.joylink.rtss.simulation.ncc.simulation.event.NCCCreateEvent;
import club.joylink.rtss.simulation.ncc.simulation.event.NCCDestroyEvent;
@ -16,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Optional;
@ -25,18 +28,22 @@ import java.util.Optional;
@Component
public class NCCManagerService {
@Autowired
private IRtsNccMapDataRepository rtsNccMapDataRepository;
@Resource
private IRtsNccMapDataPublishRepository dataPublishRepository;
/**
* 创建world
*/
@EventListener
public void createWorld(NCCCreateEvent event) {
NccMapDataDto nccMapData = rtsNccMapDataRepository.findById(event.getMapId());
StorageProto.Storage storage = null;
// NccMapDataDto nccMapData = rtsNccMapDataRepository.findById(event.getMapId());
Optional<RtsNccMapDataPublish> dataPublishOptional = dataPublishRepository.findLast();
if(dataPublishOptional.isEmpty()){
//TODO
}
try {
storage = StorageProto.Storage.parseFrom(nccMapData.getData());
// storage = StorageProto.Storage.parseFrom(nccMapData.getData());
StorageProto.Storage storage = StorageProto.Storage.parseFrom(dataPublishOptional.get().getMapData());
World world = WorldManage.create(generateWorldId(event.getSimulationId()));
// 将构建信息放入world
world.getStorage().insertSingleton(storage);