|
|
@@ -3,6 +3,7 @@ package com.izouma.nineth.web;
|
|
|
import com.izouma.nineth.config.MetaConstants;
|
|
|
import com.izouma.nineth.domain.MetaObjectMove;
|
|
|
import com.izouma.nineth.domain.MetaObjectMoveCoordinate;
|
|
|
+import com.izouma.nineth.dto.MetaObjectMoveCoordinateDTO;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.repo.MetaObjectMoveCoordinateRepo;
|
|
|
@@ -11,16 +12,17 @@ import com.izouma.nineth.service.MetaObjectMoveService;
|
|
|
import com.izouma.nineth.utils.ObjUtils;
|
|
|
import com.izouma.nineth.utils.excel.ExcelUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.transaction.Transactional;
|
|
|
import java.io.IOException;
|
|
|
import java.time.ZoneOffset;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.concurrent.atomic.AtomicLong;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("/metaObjectMove")
|
|
|
@@ -42,6 +44,81 @@ public class MetaObjectMoveController extends BaseController {
|
|
|
return metaObjectMoveRepo.save(record);
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
+ @PostMapping("/{id}/handleCoordinate")
|
|
|
+ public void handleCoordinate(@PathVariable Long id) {
|
|
|
+ MetaObjectMove metaObjectMove = metaObjectMoveRepo.findByIdAndDel(id, false);
|
|
|
+ if (Objects.isNull(metaObjectMove)) {
|
|
|
+ throw new BusinessException("物体移动配置为空");
|
|
|
+ }
|
|
|
+ List<MetaObjectMoveCoordinateDTO> metaObjectMoveCoordinateDTOS = metaObjectMove.getMetaObjectMoveCoordinateDTOS();
|
|
|
+ if (CollectionUtils.isEmpty(metaObjectMoveCoordinateDTOS)) {
|
|
|
+ throw new BusinessException("物体移动坐标配置为空");
|
|
|
+ }
|
|
|
+ int size = metaObjectMoveCoordinateDTOS.size();
|
|
|
+ if (size < 2) {
|
|
|
+ throw new BusinessException("物体移动最低配置两个坐标");
|
|
|
+ }
|
|
|
+ List<MetaObjectMoveCoordinate> list = new ArrayList<>();
|
|
|
+ for (int i = 0; i < size - 1; i++) {
|
|
|
+ MetaObjectMoveCoordinateDTO start = metaObjectMoveCoordinateDTOS.get(i);
|
|
|
+ // 开始时间(getTime取出来的值单位为秒)
|
|
|
+ int startTime = (int) start.getTime() * 24;
|
|
|
+ MetaObjectMoveCoordinateDTO end = metaObjectMoveCoordinateDTOS.get(i + 1);
|
|
|
+ // 到达时间(getTime取出来的值单位为秒)
|
|
|
+ int endTime = (int) end.getTime() * 24;
|
|
|
+ // 两个坐标间总共移动时间
|
|
|
+ int time = endTime - startTime;
|
|
|
+ if (time <= 0) {
|
|
|
+ throw new BusinessException("两个坐标间移动时间必须大于0");
|
|
|
+ }
|
|
|
+ float startAxisX = start.getAxisX();
|
|
|
+ float endAxisX = end.getAxisX();
|
|
|
+ float axisX = (endAxisX - startAxisX) / time;
|
|
|
+
|
|
|
+ float startAxisY = start.getAxisY();
|
|
|
+ float endAxisY = end.getAxisY();
|
|
|
+ float axisY = (endAxisY - startAxisY) / time;
|
|
|
+
|
|
|
+ float startAxisZ = start.getAxisZ();
|
|
|
+ float endAxisZ = end.getAxisZ();
|
|
|
+ float axisZ = (endAxisZ - startAxisZ) / time;
|
|
|
+
|
|
|
+ float startEulerX = start.getEulerX();
|
|
|
+ float endEulerX = end.getEulerX();
|
|
|
+ float eulerX = (endEulerX - startEulerX) / time;
|
|
|
+
|
|
|
+ float startEulerY = start.getEulerY();
|
|
|
+ float endEulerY = end.getEulerY();
|
|
|
+ float eulerY = (endEulerY - startEulerY) / time;
|
|
|
+
|
|
|
+ float startEulerZ = start.getEulerZ();
|
|
|
+ float endEulerZ = end.getEulerZ();
|
|
|
+ float eulerZ = (endEulerZ - startEulerZ) / time;
|
|
|
+
|
|
|
+ for (int a = startTime; a <= endTime; a++) {
|
|
|
+ MetaObjectMoveCoordinate dbExist = metaObjectMoveCoordinateRepo.findByObjectIdAndCoordinateIndexAndDel(metaObjectMove.getObjectId(), a, false);
|
|
|
+ if (Objects.isNull(dbExist)) {
|
|
|
+ dbExist = new MetaObjectMoveCoordinate();
|
|
|
+ dbExist.setObjectId(metaObjectMove.getObjectId());
|
|
|
+ dbExist.setCoordinateIndex(a);
|
|
|
+ dbExist.setAxisX(startAxisX + (a - startTime) * axisX);
|
|
|
+ dbExist.setAxisY(startAxisY + (a - startTime) * axisY);
|
|
|
+ dbExist.setAxisZ(startAxisZ + (a - startTime) * axisZ);
|
|
|
+ dbExist.setEulerX(startEulerX + (a - startTime) * eulerX);
|
|
|
+ dbExist.setEulerY(startEulerY + (a - startTime) * eulerY);
|
|
|
+ dbExist.setEulerZ(startEulerZ + (a - startTime) * eulerZ);
|
|
|
+ list.add(dbExist);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 多索引去重
|
|
|
+ List<MetaObjectMoveCoordinate> distinctList = list.stream()
|
|
|
+ .collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
|
|
|
+ new TreeSet<>(Comparator.comparing(MetaObjectMoveCoordinate::getCoordinateIndex))),
|
|
|
+ ArrayList::new));
|
|
|
+ metaObjectMoveCoordinateRepo.saveAll(distinctList);
|
|
|
+ }
|
|
|
|
|
|
//@PreAuthorize("hasRole('ADMIN')")
|
|
|
@PostMapping("/all")
|
|
|
@@ -66,8 +143,8 @@ public class MetaObjectMoveController extends BaseController {
|
|
|
ExcelUtils.export(response, data);
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/test")
|
|
|
- public MetaObjectMoveCoordinate test(Long objectId) {
|
|
|
+ @GetMapping("/{objectId}/test")
|
|
|
+ public MetaObjectMoveCoordinate queryCoordinate(@PathVariable Long objectId) {
|
|
|
// 查询缓存-物体移动配置
|
|
|
String objectMoveKey = MetaConstants.META_OBJECT_MOVE_REDIS_KEY.concat(String.valueOf(objectId));
|
|
|
MetaObjectMove metaObjectMove = (MetaObjectMove) redisTemplate.opsForValue().get(objectMoveKey);
|
|
|
@@ -77,27 +154,26 @@ public class MetaObjectMoveController extends BaseController {
|
|
|
if (Objects.isNull(metaObjectMove)) {
|
|
|
throw new BusinessException("物体配置不存在!");
|
|
|
}
|
|
|
- AtomicLong totalTime = new AtomicLong();
|
|
|
- metaObjectMove.getMetaObjectMoveCoordinateDTOS().forEach(metaObjectMoveCoordinateDTO -> {
|
|
|
- totalTime.set(totalTime.get() + metaObjectMoveCoordinateDTO.getTime());
|
|
|
- });
|
|
|
- metaObjectMove.setTotalTime(totalTime.get());
|
|
|
// 重新缓存-物体移动配置
|
|
|
redisTemplate.delete(objectMoveKey);
|
|
|
redisTemplate.opsForValue().set(objectMoveKey, metaObjectMove);
|
|
|
}
|
|
|
- long startTime = metaObjectMove.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
|
|
- long now = System.currentTimeMillis();
|
|
|
- long index = (now - startTime) % (metaObjectMove.getTotalTime() * 2L);
|
|
|
- if (index > metaObjectMove.getTotalTime()) {
|
|
|
+ // 开始时间戳(单位为秒)
|
|
|
+ long startTime = metaObjectMove.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli() / 1000;
|
|
|
+ // 当前时间戳(单位为秒)
|
|
|
+ long currentTime = System.currentTimeMillis() / 1000;
|
|
|
+ // 计算坐标索引
|
|
|
+ long index = (currentTime - startTime) % (metaObjectMove.getTotalTime() * 2L);
|
|
|
+ if (index > (metaObjectMove.getTotalTime())) {
|
|
|
index = metaObjectMove.getTotalTime() * 2L - index;
|
|
|
}
|
|
|
+ index = index * 24;
|
|
|
// 查询缓存坐标信息
|
|
|
String coordinateKey = MetaConstants.META_OBJECT_INDEX_REDIS_KEY.concat(String.valueOf(objectId)).concat("_").concat(String.valueOf(index));
|
|
|
MetaObjectMoveCoordinate metaObjectMoveCoordinate = (MetaObjectMoveCoordinate) redisTemplate.opsForValue().get(coordinateKey);
|
|
|
if (Objects.isNull(metaObjectMoveCoordinate)) {
|
|
|
// 查询数据库坐标信息
|
|
|
- metaObjectMoveCoordinate = metaObjectMoveCoordinateRepo.findByObjectIdAndCoordinateIndexAndDel(objectId, index, false);
|
|
|
+ metaObjectMoveCoordinate = metaObjectMoveCoordinateRepo.findByObjectIdAndCoordinateIndexAndDel(objectId, (int) index, false);
|
|
|
if (Objects.isNull(metaObjectMoveCoordinate)) {
|
|
|
throw new BusinessException("坐标信息为空");
|
|
|
}
|