Browse Source

重构编辑场景方法

wuweihao 3 years ago
parent
commit
7dba39d919

+ 2 - 0
gis_service/src/main/java/com/gis/service/SceneService.java

@@ -33,4 +33,6 @@ public interface SceneService extends IBaseService<SceneEntity, Long> {
     Result uploadVisionJsonToVisionModelData(MultipartFile file);
 
     Result addVisit(String sceneCode);
+
+    Result editScene_1(SceneDataDto param);
 }

+ 302 - 1
gis_service/src/main/java/com/gis/service/impl/SceneServiceImpl.java

@@ -209,6 +209,307 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, Long> implem
         return Result.failure("上传失败");
     }
 
+
+    /**
+     * by owen 2022-06-13 编辑场景接口
+     * @param param
+     * @return
+     */
+    @Override
+    public Result editScene_1(SceneDataDto param){
+        BaseRuntimeException.isBlank(param, null, "参数不能为空");
+
+        // 1. 处理SomeData.json
+        try {
+            doSomeData(param);
+            // 2. 处理data2.js
+            doData2Js(param);
+            // 3. 处理data.js
+            doDataJs(param);
+            return Result.success();
+        } catch (QiniuException e) {
+            log.error("七牛云上传出错");
+            e.printStackTrace();
+        }
+
+
+        return null;
+    }
+
+
+    private void doSomeData(SceneDataDto param) throws QiniuException {
+        String sceneCode = param.getSceneCode();
+
+
+        SceneEntity entity = this.findBySceneCode(sceneCode);
+        BaseRuntimeException.isTrue(entity == null, null, "场景不存在: " + sceneCode);
+
+        // 需要处理的参数
+        String info = param.getInfo();
+        String guides = param.getGuides();
+
+        // 处理someData.json, 网络下载someData.json
+        String someDataName = "someData.json";
+        // 注意网络下载会有缓存,必须加时间戳
+        String someDataUrl = OSS_DOMAIN + OSS_PATH + sceneCode + "/" + someDataName + "?m=" + System.currentTimeMillis();
+        log.info("网络下载文件地址: {}", someDataUrl);
+        String localBasePath = FILE_PATH + sceneCode;
+        FileUtils.downLoadFromUrl(someDataUrl, someDataName, localBasePath);
+        String someDataPath = entity.getPath() + "/someData.json";
+
+        BaseRuntimeException.isTrue(!FileUtil.isFile(someDataPath), null, "服务器someData.json文件不存在");
+
+        // 读取someDataJson
+        String someData = FileUtil.readUtf8String(someDataPath);
+        JSONObject someDataJson = JSONObject.parseObject(someData);
+
+
+
+        JSONArray guidesArray = new JSONArray();
+        if (guides != null) {
+            guidesArray = JSONObject.parseArray(guides);
+
+        }
+
+
+        if (info != null) {
+            JSONObject infoJson = JSONObject.parseObject(info);
+
+
+            // 处理model
+            JSONObject model = someDataJson.getJSONObject("model");
+            if (model != null) {
+                String name = infoJson.getString("name");
+                model.put("name", name);
+
+                // update 场景名称
+                if (!name.equals(entity.getSceneTitle())) {
+                    entity.setSceneTitle(name);
+                    entity.setUpdateTime(new Date());
+                    this.update(entity);
+                }
+
+                model.put("summary", infoJson.get("summary"));
+
+
+                if (guidesArray != null) {
+                    model.put("images", guidesArray);
+                } else {
+                    model.put("images", new JSONArray());
+                }
+
+            }
+
+            // 更新someDataJson
+            someDataJson.put("model", model);
+
+            // info信息添加到someDataJson最外层
+            Set<String> infoKey = infoJson.keySet();
+            for (String key : infoKey) {
+
+                someDataJson.put(key, infoJson.get(key));
+            }
+
+
+            // 删除旧someDataJson
+            FileUtil.del(someDataPath);
+            // 写入新someDataJson
+            FileUtil.writeUtf8String(someDataJson.toJSONString(), someDataPath);
+            // 2021-9-23 备份someData.json
+            String dateTime = DateUtils.getDateTime();
+            FileUtil.writeUtf8String(someDataJson.toJSONString(), entity.getPath() + "/someData_" + dateTime + ".json");
+
+            BaseRuntimeException.isTrue(!FileUtil.isFile(someDataPath), null, "上传七牛云的someData文件不存在");
+            long someDataSize = FileUtil.size(new File(someDataPath));
+            log.info("someData.json写入完成,文件大小:{} kb", someDataSize);
+
+            // 将新的someDataJson上传oss
+            QiniuOssUtil.upload(someDataPath, OSS_PATH + sceneCode + "/" + someDataName);
+            log.info(someDataName + "已上传到七牛云");
+            log.info("处理以完成: someData.json");
+        }
+    }
+
+    private void doData2Js(SceneDataDto param) throws QiniuException {
+        // 处理data2.js
+
+        // 出入参数,需要处理的
+        String sceneCode = param.getSceneCode();
+        String localBasePath = FILE_PATH + sceneCode;
+        String tourAudio = param.getTourAudio();
+        String overlays = param.getOverlays();
+        String guides = param.getGuides();
+        String hots = param.getHots();
+
+        String data2Name = "data2.js";
+        // 注意网络下载会有缓存,必须加时间戳
+        String data2Url = OSS_DOMAIN + OSS_PATH + sceneCode + "/" + data2Name+ "?m=" + System.currentTimeMillis();
+        log.info("网络下载文件地址: {}", data2Url);
+        FileUtils.downLoadFromUrl(data2Url, data2Name, localBasePath);
+
+        String data2Path = localBasePath + File.separator + data2Name;
+        log.info("data2.js文件位置: {}", data2Path);
+
+        BaseRuntimeException.isTrue(!FileUtil.exist(data2Path), null, "服务器data2.js文件不存在");
+
+
+        String data2 = FileUtil.readUtf8String(data2Path);
+        JSONObject data2Json = JSONObject.parseObject(data2);
+
+
+
+        if (tourAudio != null) {
+            data2Json.put("tourAudio", JSONObject.parseObject(tourAudio));
+        } else {
+            data2Json.put("tourAudio", new JSONObject());
+        }
+
+
+        if (overlays != null) {
+            data2Json.put("overlays", JSONObject.parseArray(overlays));
+        } else {
+            data2Json.put("overlays", new JSONArray());
+        }
+
+
+
+        // 处理guidesArray,将scan_id的值作为key, value:  time":40000
+        JSONObject audioJson = new JSONObject();
+        JSONObject timeJson = new JSONObject();
+        timeJson.put("time", 40000);
+
+
+        JSONArray guidesArray = new JSONArray();
+        if (guides != null) {
+            guidesArray = JSONObject.parseArray(guides);
+
+        }
+
+        if (guidesArray != null) {
+
+            // 将旧的audio字段删除
+            data2Json.remove("audio");
+
+            for (int i = 0; i < guidesArray.size() ; i++) {
+                JSONObject metadata = guidesArray.getJSONObject(i).getJSONObject("metadata");
+                if (metadata != null) {
+                    String scanId = metadata.getString("scan_id");
+                    BaseRuntimeException.isTrue(scanId == null, null , "guides.metadata.scan_id为空: " + i);
+                    // Fastjson-fastjson中$ref对象重复引用问题,拿不到想要的效果
+                    audioJson.put(scanId, JSON.toJSONString(timeJson, SerializerFeature.DisableCircularReferenceDetect));
+
+                }
+            }
+
+            // 新增audio
+            data2Json.put("audio", audioJson);
+        }
+
+
+        // host在data2.js、data.js都需要处理
+
+        if (hots != null) {
+            // 获取所有key
+            JSONObject hotJson = JSONObject.parseObject(hots);
+
+            Set<String> strings = hotJson.keySet();
+            for (String key: strings) {
+                JSONObject subJson = hotJson.getJSONObject(key);
+                String url  = "https://www.4dmodel.com/SuperTwo/hot_online1/index.html#/?m=" + key;
+                // 将link 添加进去
+                subJson.put("link", url);
+            }
+            data2Json.put("hots", hotJson);
+        } else {
+            data2Json.put("hots", new JSONObject());
+        }
+
+
+        // 删除旧data2.js
+        FileUtil.del(data2Path);
+        // 写入新data2.js
+        FileUtil.writeUtf8String(data2Json.toJSONString(), data2Path);
+
+        // 2021-09-23 备份data2.js
+        String dateTime = DateUtils.getDateTime();
+        FileUtil.writeUtf8String(data2Json.toJSONString(), localBasePath + File.separator + "data2_" + dateTime + ".js");
+
+
+        log.info("新data2.js写入完成");
+
+        BaseRuntimeException.isTrue(!FileUtil.isFile(data2Path), null, "上传七牛云的data2.js文件不存在");
+
+        // 将新的someDataJson上传oss
+        QiniuOssUtil.upload(data2Path, OSS_PATH + sceneCode + "/" + data2Name);
+        log.info(data2Name+ "已上传到七牛云");
+        log.info("处理以完成: data2.js");
+
+    }
+
+    private void doDataJs(SceneDataDto param) throws QiniuException {
+        // 因为data.js只是热点信息,所以直接创建上传oss
+
+        // 输入参数
+        String hots = param.getHots();
+        String sceneCode = param.getSceneCode();
+        String basePath = FILE_PATH + sceneCode;
+
+        JSONObject dataJsJson = new JSONObject();
+        if (hots != null) {
+            dataJsJson = JSONObject.parseObject(hots);
+
+            Set<String> strings = dataJsJson.keySet();
+            for (String key: strings) {
+                JSONObject subJson = dataJsJson.getJSONObject(key);
+                JSONObject infoAttribute = subJson.getJSONObject("infoAttribute");
+                if (infoAttribute != null) {
+                    Set<String> infoKey = infoAttribute.keySet();
+
+                    for (String s: infoKey) {
+                        Object val = null;
+                        // 添加到第一层, 空值不添加
+                        if ("images".equals(s) || "styleImg".equals(s) || "model".equals(s) || "video".equals(s) || "iframe".equals(s)) {
+                            JSONArray jsonArray = infoAttribute.getJSONArray(s);
+                            if (jsonArray.size() == 0) {
+                                continue;
+                            }
+                            val = jsonArray;
+
+                        } else {
+                            String a = infoAttribute.getString(s);
+                            if (StringUtils.isBlank(a)){
+                                continue;
+                            }
+                            val = a;
+                        }
+
+                        subJson.put(s, val);
+                    }
+                }
+
+                // 删除infoAttribute
+                subJson.remove("infoAttribute");
+
+            }
+
+        }
+        String dataPath = basePath + "/data.js";
+        FileUtil.writeUtf8String(dataJsJson.toJSONString(), dataPath);
+
+        // 2021-09-23 备份数据用
+        String dateTime = DateUtils.getDateTime();
+        FileUtil.writeUtf8String(dataJsJson.toJSONString(), basePath + "/data_"+ dateTime + ".js");
+
+        BaseRuntimeException.isTrue(!FileUtil.isFile(dataPath), null, "上传七牛云的data.js文件不存在");
+        //上传oss
+        String ossPath = OSS_PATH + sceneCode + "/hot/js/data.js";
+        QiniuOssUtil.upload(dataPath, ossPath);
+        log.info("data.js已上传到七牛云: {}", ossPath);
+        log.info("处理以完成: data.js");
+    }
+
+
+
     /**
      * 编辑场景
      * @param param
@@ -332,7 +633,7 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, Long> implem
 
         String data2 = FileUtil.readUtf8String(data2Path);
         JSONObject data2Json = JSONObject.parseObject(data2);
-        
+
 
         String tourAudio = param.getTourAudio();
         if (tourAudio != null) {

+ 26 - 2
gis_web/src/main/java/com/gis/web/controller/ApiController.java

@@ -34,6 +34,25 @@ public class ApiController extends BaseController {
     @Autowired
     private SceneService sceneService;
 
+//    /**
+//     * @param param
+//     * @param clientCode 用于验证,每次新增客户需要tb_client加上数据
+//     * @return
+//     * @throws QiniuException
+//     * @throws InterruptedException
+//     */
+//    @ApiOperation("编辑场景")
+//    @PostMapping("edit/{clientCode}")
+//    public Result edit(@Valid @RequestBody SceneDataDto param, @PathVariable String clientCode) throws QiniuException {
+//
+//        Map code = sceneMapper.findByCode(clientCode);
+//        if (code == null) {
+//            log.error("客户码有误");
+//            return Result.failure("客户码有误,请重新输入");
+//        }
+//        return sceneService.editScene(param);
+//    }
+
     /**
      * @param param
      * @param clientCode 用于验证,每次新增客户需要tb_client加上数据
@@ -43,16 +62,21 @@ public class ApiController extends BaseController {
      */
     @ApiOperation("编辑场景")
     @PostMapping("edit/{clientCode}")
-    public Result edit(@Valid @RequestBody SceneDataDto param, @PathVariable String clientCode) throws QiniuException {
+    public Result edit(@Valid @RequestBody SceneDataDto param, @PathVariable String clientCode) {
 
         Map code = sceneMapper.findByCode(clientCode);
         if (code == null) {
             log.error("客户码有误");
             return Result.failure("客户码有误,请重新输入");
         }
-        return sceneService.editScene(param);
+        return sceneService.editScene_1(param);
     }
 
+
+
+
+
+
     @ApiOperation("漫游可行")
     @PostMapping("roamViable/{clientCode}")
     public Result roamViable(@RequestBody RoamViableDto param, @PathVariable String clientCode) throws Exception {

+ 10 - 2
gis_web/src/main/java/com/gis/web/controller/SceneController.java

@@ -213,13 +213,21 @@ public class SceneController extends BaseController {
 
 
 
+//    @ApiOperation("编辑场景")
+//    @PostMapping("edit")
+//    public Result edit(@Valid @RequestBody SceneDataDto param) throws QiniuException, InterruptedException {
+//        return sceneService.editScene(param);
+//    }
+
     @ApiOperation("编辑场景")
     @PostMapping("edit")
-    public Result edit(@Valid @RequestBody SceneDataDto param) throws QiniuException, InterruptedException {
-        return sceneService.editScene(param);
+    public Result edit(@Valid @RequestBody SceneDataDto param)  {
+        return sceneService.editScene_1(param);
     }
 
 
+
+
     @ApiOperation("漫游可行")
     @PostMapping("roamViable")
     public Result roamViable(@RequestBody RoamViableDto param) throws Exception {