|
|
@@ -0,0 +1,111 @@
|
|
|
+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.PageQuery;
|
|
|
+import com.izouma.nineth.exception.BusinessException;
|
|
|
+import com.izouma.nineth.repo.MetaObjectMoveCoordinateRepo;
|
|
|
+import com.izouma.nineth.repo.MetaObjectMoveRepo;
|
|
|
+import com.izouma.nineth.service.MetaObjectMoveService;
|
|
|
+import com.izouma.nineth.utils.ObjUtils;
|
|
|
+import com.izouma.nineth.utils.excel.ExcelUtils;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+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 java.io.IOException;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.atomic.AtomicLong;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/metaObjectMove")
|
|
|
+@AllArgsConstructor
|
|
|
+public class MetaObjectMoveController extends BaseController {
|
|
|
+ private MetaObjectMoveService metaObjectMoveService;
|
|
|
+ private MetaObjectMoveRepo metaObjectMoveRepo;
|
|
|
+ private MetaObjectMoveCoordinateRepo metaObjectMoveCoordinateRepo;
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
+ //@PreAuthorize("hasRole('ADMIN')")
|
|
|
+ @PostMapping("/save")
|
|
|
+ public MetaObjectMove save(@RequestBody MetaObjectMove record) {
|
|
|
+ if (record.getId() != null) {
|
|
|
+ MetaObjectMove orig = metaObjectMoveRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
|
|
|
+ ObjUtils.merge(orig, record);
|
|
|
+ return metaObjectMoveRepo.save(orig);
|
|
|
+ }
|
|
|
+ return metaObjectMoveRepo.save(record);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //@PreAuthorize("hasRole('ADMIN')")
|
|
|
+ @PostMapping("/all")
|
|
|
+ public Page<MetaObjectMove> all(@RequestBody PageQuery pageQuery) {
|
|
|
+ return metaObjectMoveService.all(pageQuery);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/get/{id}")
|
|
|
+ public MetaObjectMove get(@PathVariable Long id) {
|
|
|
+ return metaObjectMoveRepo.findById(id).orElseThrow(new BusinessException("无记录"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/del/{id}")
|
|
|
+ public void del(@PathVariable Long id) {
|
|
|
+ metaObjectMoveRepo.softDelete(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/excel")
|
|
|
+ @ResponseBody
|
|
|
+ public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
|
|
|
+ List<MetaObjectMove> data = all(pageQuery).getContent();
|
|
|
+ ExcelUtils.export(response, data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/test")
|
|
|
+ public MetaObjectMoveCoordinate test(Long objectId) {
|
|
|
+ // 查询缓存-物体移动配置
|
|
|
+ String objectMoveKey = MetaConstants.META_OBJECT_MOVE_REDIS_KEY.concat(String.valueOf(objectId));
|
|
|
+ MetaObjectMove metaObjectMove = (MetaObjectMove) redisTemplate.opsForValue().get(objectMoveKey);
|
|
|
+ // 缓存不存在 查询数据库
|
|
|
+ if (Objects.isNull(metaObjectMove)) {
|
|
|
+ metaObjectMove = metaObjectMoveRepo.findByObjectIdAndDel(objectId, false);
|
|
|
+ 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()) {
|
|
|
+ index = metaObjectMove.getTotalTime() * 2L - index;
|
|
|
+ }
|
|
|
+ // 查询缓存坐标信息
|
|
|
+ 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);
|
|
|
+ if (Objects.isNull(metaObjectMoveCoordinate)) {
|
|
|
+ throw new BusinessException("坐标信息为空");
|
|
|
+ }
|
|
|
+ // 重新缓存坐标信息
|
|
|
+ redisTemplate.delete(coordinateKey);
|
|
|
+ redisTemplate.opsForValue().set(coordinateKey, metaObjectMoveCoordinate);
|
|
|
+ }
|
|
|
+ return metaObjectMoveCoordinate;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|