package com.example.demo.controller; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.example.demo.constant.CodeConstant; import com.example.demo.entity.SceneLightEntity; import com.example.demo.entity.SceneStyleEntity; import com.example.demo.mq.TopicRabbitConfig; import com.example.demo.service.*; import com.example.demo.util.*; import com.example.demo.vo.response.ResponseRenovationBuildDetail; import com.example.demo.vo.response.ResponseRenovationPartsDetail; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.net.URLDecoder; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; /** * Created by Hb_zzZ on 2020/6/19. */ @Slf4j @RestController @RequestMapping("/change") @Api(tags = "生成风格模块") public class ImportDataController extends BaseController { @Autowired private UploadToOssUtil uploadToOssUtil; @Autowired private ISceneStyleService sceneStyleService; @Autowired private ISceneLightService sceneLightService; @Autowired private IRenovationPartsDetailService partsDetailService; @Autowired private IRenovationBuildDetailService buildDetailService; @Autowired private IStatisticsService statisticsService; @Autowired RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方 @Value("${main.url}") private String mainUrl; @Value("${server.file.location}") private String buildPath; @Value("${build.url}") private String buildUrl; @Value("${upload.file.location}") private String path; /** * 导入数据生成风格场景 * @param sceneNum * @param data * @param styleName * @param styleNum * @param file * @return */ @ApiOperation("导入数据生成风格场景") @ApiImplicitParams({ @ApiImplicitParam(name = "sceneNum", value = "场景码", dataType = "String"), @ApiImplicitParam(name = "styleName", value = "风格名称", dataType = "String"), @ApiImplicitParam(name = "styleNum", value = "风格码", dataType = "String"), @ApiImplicitParam(name = "file", value = "文件流", dataType = "MultipartFile"), @ApiImplicitParam(name = "buildTye", value = "渲染类型,all(全屋),light(灯光预览),pano(全景图)", dataType = "String"), @ApiImplicitParam(name = "decoration", value = "decoration.json", dataType = "String"), @ApiImplicitParam(name = "rooms", value = "rooms的json内容", dataType = "String"), @ApiImplicitParam(name = "data", value = "json数据", dataType = "String")}) @RequestMapping(value = "/importData", method = RequestMethod.POST) public Result importData(String sceneNum, String data, String styleName, String decoration, String styleNum, @RequestParam("file") MultipartFile file, String proLight, String buildType, String rooms) throws Exception{ if(StringUtils.isEmpty(sceneNum)){ return Result.failure(CodeConstant.FAILURE_CODE_3001, CodeConstant.FAILURE_MSG_3001); } String resultData = OkHttpUtils.httpGet(mainUrl + "api/scene/getInfo?num=" + sceneNum); JSONObject resultJson = JSON.parseObject(resultData); if(resultJson == null || resultJson.getInteger("code") != 0){ return Result.failure(CodeConstant.FAILURE_CODE_4007, CodeConstant.FAILURE_MSG_4007); } String vrNum = styleNum; try{ if(StringUtils.isEmpty(styleNum)){ Integer num = sceneStyleService.findLastNum(sceneNum); if(num == null){ num = 1; }else { num ++; } vrNum = "vr-" + sceneNum + "-" + String.format("%3d", num).replace(" ", "0"); SceneStyleEntity sceneStyleEntity = new SceneStyleEntity(); sceneStyleEntity.setNum(num.longValue()); sceneStyleEntity.setSceneNum(sceneNum); sceneStyleEntity.setStyleName(styleName); sceneStyleEntity.setStyleNum(vrNum); //新生成的换装风格,未渲染 sceneStyleEntity.setStatus(2); sceneStyleService.save(sceneStyleEntity); }else { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(SceneStyleEntity::getStyleNum,styleNum); wrapper.orderByDesc(SceneStyleEntity::getNum); List list = sceneStyleService.list(wrapper); if(list != null && list.size() > 0) { SceneStyleEntity sceneStyleEntity = list.get(0); sceneStyleEntity.setStyleName(styleName); sceneStyleService.updateById(sceneStyleEntity); } } String filePath = buildPath + "Input/" + vrNum ; String dataPath = ""; String decorationPath = ""; //灯光预览放在light文件夹 if("light".equals(buildType)){ filePath = filePath + "light/"; }else if ("pano".equals(buildType)){ filePath = filePath + "pano/"; } else { filePath = filePath + File.separator; } //生成需要vision.txt FileUtils.downLoadFromUrl("https://4dkk.4dage.com/images/images" + sceneNum + "/vision.modeldata", "vision.modeldata", filePath); if(!new File(filePath + "vision.modeldata").exists()){ log.info("vision.modeldata文件不存在"); return Result.failure(CodeConstant.FAILURE_CODE_4001, CodeConstant.FAILURE_MSG_4001); } log.info("下载vision.modeldata完成,开始转换vision.txt"); CreateObjUtil.convertVisionmodeldataToTxt(filePath + "vision.modeldata", filePath + "vision.txt"); //计算translationAverage,和puckAverage并重新赋予值 Map averageMap = ConvertCadKjl.getAverageValue(filePath + "vision.txt"); setAverageValue(filePath + "vision.txt", averageMap.get("translationAverage"), averageMap.get("puckAverage")); FileUtils.writeFile(filePath + "proLight.json", proLight); dataPath = filePath + "objectPosition.json"; decorationPath = filePath + "decoration.json"; FileUtils.writeFile(dataPath, data); FileUtils.writeFile(decorationPath, decoration); File objFile = new File(filePath + "Obj" + File.separator + file.getOriginalFilename()); if(!objFile.getParentFile().exists()){ objFile.getParentFile().mkdirs(); } file.transferTo(objFile); //数据上传oss,让计算服务器下载资源 List uploadList = new ArrayList<>(); FileUtils.readfilePath(filePath, uploadList); for(String uploadFile : uploadList){ if(StringUtils.isEmpty(buildType) || "all".equals(buildType)){ uploadToOssUtil.upload(uploadFile, "data/data" + vrNum + uploadFile.replace(filePath, "/changeData/")); } } //保存每个风格码的户型数据 if(!new File(filePath + "house.json").exists()){ String path = ResourceUtils.getURL("classpath:").getPath(); path = URLDecoder.decode(path,"utf-8"); path = path + File.separator + "data/" + sceneNum; System.out.println("文件路径:" + path); FileUtils.copyFile(path + "/kjl.json", filePath + "house.json", true); uploadToOssUtil.upload(filePath + "house.json", "data/data" + vrNum + "/house.json"); } //有修改房间信息就更新户型数据 if(!StringUtils.isEmpty(rooms)){ JSONObject kjlJson = JSONObject.parseObject(FileUtils.readFile(filePath + "house.json")); if(kjlJson != null){ kjlJson.put("rooms", JSONObject.parseObject(rooms)); } FileUtils.writeFile(filePath + "house.json", kjlJson.toJSONString()); uploadToOssUtil.upload(filePath + "house.json", "data/data" + vrNum + "/house.json"); } }catch (Exception e){ e.printStackTrace(); } JSONObject result = new JSONObject(); result.put("num", vrNum); return Result.success(result); } /** * 保存proLight.json数据 * @param styleNum * @return */ @ApiOperation("保存proLight.json数据") @ApiImplicitParams({ @ApiImplicitParam(name = "styleNum", value = "风格名称", dataType = "String"), @ApiImplicitParam(name = "proLight", value = "proLight.json的数据", dataType = "String"), @ApiImplicitParam(name = "buildType", value = "渲染类型,all(全屋),light(灯光预览),pano(全景图)", dataType = "String")}) @RequestMapping(value = "/saveProLightJson", method = RequestMethod.POST) public Result importData(String styleNum, String proLight, String buildType) throws Exception{ if(StringUtils.isEmpty(styleNum)){ return Result.failure(CodeConstant.FAILURE_CODE_3001, CodeConstant.FAILURE_MSG_3001); } String vrNum = styleNum; try{ String filePath = buildPath + "Input/" + vrNum ; //灯光预览放在light文件夹 if("light".equals(buildType)){ filePath = filePath + "light/"; }else if ("pano".equals(buildType)){ filePath = filePath + "pano/"; } else { filePath = filePath + File.separator; } log.info("保存的类型是:" + buildType + ";位置在:" + filePath); FileUtils.writeFile(filePath + "proLight.json", proLight); //数据上传oss,让计算服务器下载资源 uploadToOssUtil.upload(filePath + "proLight.json", "data/data" + vrNum + "/changeData/proLight.json"); }catch (Exception e){ e.printStackTrace(); } return Result.success(); } @ApiOperation("渲染场景") @ApiImplicitParams({ @ApiImplicitParam(name = "sceneNum", value = "场景码", dataType = "String"), @ApiImplicitParam(name = "ratio", value = "字符串16:9", dataType = "String"), @ApiImplicitParam(name = "type", value = "分辨率,1024,2048", dataType = "String"), @ApiImplicitParam(name = "dateType", value = "day(白天),night(晚上)", dataType = "String"), @ApiImplicitParam(name = "buildType", value = "渲染类型,all(全屋),light(灯光预览),pano(全景图)", dataType = "String"), @ApiImplicitParam(name = "isShow", value = "灯光预览是否显示在相册,0不显示,1显示", dataType = "String"), @ApiImplicitParam(name = "quality", value = "0或1", dataType = "String"), @ApiImplicitParam(name = "styleNum", value = "风格码", dataType = "String")}) @RequestMapping(value = "/buildChangeScene", method = RequestMethod.POST) public Result buildChangeScene(String sceneNum, String styleNum, String type, String dateType, String buildType, String ratio, String isShow, String quality){ try{ if(StringUtils.isEmpty(sceneNum) || StringUtils.isEmpty(styleNum)){ return Result.failure(CodeConstant.FAILURE_CODE_3001, CodeConstant.FAILURE_MSG_3001); } LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(SceneStyleEntity::getStyleNum,styleNum); wrapper.orderByDesc(SceneStyleEntity::getNum); List list = sceneStyleService.list(wrapper); SceneStyleEntity sceneStyleEntity = null; if(StringUtils.isEmpty(buildType) || "all".equals(buildType)){ if(list != null && list.size() > 0){ sceneStyleEntity = list.get(0); if(sceneStyleEntity.getStatus().intValue() == 0){ return Result.failure(CodeConstant.FAILURE_CODE_4002, CodeConstant.FAILURE_MSG_4002); } sceneStyleEntity.setStatus(0); sceneStyleEntity.setProgress(0); sceneStyleService.updateById(sceneStyleEntity); }else { return Result.failure(CodeConstant.FAILURE_CODE_4003, CodeConstant.FAILURE_MSG_4003); } //判断是否有vr+场景吗的场景,有则上传数据,无则生成; Map map = new HashMap(); map.put("type", "change"); map.put("num", styleNum); map.put("oldNum", sceneNum); map.put("sceneName", sceneStyleEntity.getStyleName()); map.put("resolution", type); HttpRequestorUtil.doPost(mainUrl + "api/scene/createChangeScene", map); if(StringUtils.isEmpty(type)){ type = "1024"; } // Variable.queue.offer(styleNum + ":;" + sceneNum + ":;" + type); if(dateType == null){ dateType = ""; } rabbitTemplate.convertAndSend(TopicRabbitConfig.TOPICE, TopicRabbitConfig.CHANGE, styleNum + ":;" + sceneNum + ":;" + type + ":;" + dateType); } if("light".equals(buildType) || "pano".equals(buildType)){ if(StringUtils.isEmpty(type)){ type = "1024"; } if(dateType == null){ dateType = ""; } if(StringUtils.isEmpty(quality)){ quality = "0"; } SceneLightEntity sceneLightEntity = new SceneLightEntity(); if(list != null && list.size() > 0){ sceneStyleEntity = list.get(0); if("light".equals(buildType)){ log.info("灯光预览:" + styleNum); if(sceneStyleEntity.getLightStatus().intValue() == 0){ return Result.failure(CodeConstant.FAILURE_CODE_4004, CodeConstant.FAILURE_MSG_4004); } sceneStyleEntity.setLightStatus(0); } if("pano".equals(buildType)){ log.info("全景预览:" + styleNum); if(sceneStyleEntity.getLightStatus().intValue() == 0){ return Result.failure(CodeConstant.FAILURE_CODE_4016, CodeConstant.FAILURE_MSG_4016); } sceneStyleEntity.setPanoStatus(0); } sceneStyleService.updateById(sceneStyleEntity); sceneLightEntity.setStatus(0); sceneLightEntity.setProgress(0); if(StringUtils.isEmpty(isShow)){ isShow = "1"; } sceneLightEntity.setIsShow(Integer.valueOf(isShow)); sceneLightEntity.setSceneStyleId(sceneStyleEntity.getId()); sceneLightEntity.setPixel(type); //数据上传oss,让计算服务器下载资源 String filePath = buildPath + "Input/" + styleNum; if("light".equals(buildType)){ sceneLightEntity.setType(1); filePath = filePath + "light/"; } if("pano".equals(buildType)){ sceneLightEntity.setType(2); filePath = filePath + "pano/"; } sceneLightEntity.setQuality(quality); sceneLightService.save(sceneLightEntity); List uploadList = new ArrayList<>(); FileUtils.readfilePath(filePath, uploadList); for(String uploadFile : uploadList){ uploadToOssUtil.upload(uploadFile, "data/data" + styleNum + uploadFile.replace( filePath, "/proLightData/" + sceneLightEntity.getId().longValue() + "/")); } }else { return Result.failure(CodeConstant.FAILURE_CODE_4003, CodeConstant.FAILURE_MSG_4003); } rabbitTemplate.convertAndSend(TopicRabbitConfig.TOPICE, TopicRabbitConfig.LIGHT, styleNum + ":;" + sceneNum + ":;" + type + ":;" + dateType + ":;" + ratio + ":;" + sceneLightEntity.getId().longValue() + ":;" + quality + ":;" + buildType); } FileUtils.deleteDirectory(buildPath + "Output/" + styleNum); }catch (Exception e){ e.printStackTrace(); } JSONObject result = new JSONObject(); result.put("num", styleNum); result.put("url", mainUrl + "spc.html?t=" + styleNum); return Result.success(result); } /** * 测试运行命令 * @return */ @ApiOperation("测试运行命令") @ApiImplicitParams({ @ApiImplicitParam(name = "fbxPath", value = "fbx文件路径", dataType = "String"), @ApiImplicitParam(name = "objPath", value = "obj文件路径", dataType = "String")}) @RequestMapping(value = "/fbxToObj", method = RequestMethod.POST) public String fbxToObj(String fbxPath, String objPath) throws Exception{ // String command = "D:/OneKeyDecorate/StartDecorate.exe -s " + vrNum; // log.info("开始建模:" + command); // RunCmdUtil.callshell(command); // log.info("计算完毕:" + command); String filePth = fbxPath.substring(0, fbxPath.lastIndexOf(File.pathSeparator) + 1); String fbxName = fbxPath.substring(fbxPath.lastIndexOf(File.pathSeparator) + 1); CreateObjUtil.fbxToObj(filePth, fbxName, objPath); return "运行完成"; } /** * 生成房型数据 * @param num * @return */ @ApiOperation("生成房型数据") @ApiImplicitParams({ @ApiImplicitParam(name = "num", value = "四维看看场景码", dataType = "String"), @ApiImplicitParam(name = "styleNum", value = "风格码", dataType = "String")}) @RequestMapping(value = "/createJson", method = RequestMethod.GET) public String createJson(String num, String styleNum){ try{ if(StringUtils.isEmpty(num)){ return "参数不能为空"; } String resultData = OkHttpUtils.httpGet(mainUrl + "api/scene/getInfo?num=" + num); JSONObject getInfoJson = JSON.parseObject(resultData); //初始画面 String entry = null; if(getInfoJson != null || getInfoJson.getInteger("code") == 0){ entry = getInfoJson.getJSONObject("data").getString("entry"); } String basePath = URLDecoder.decode(path,"utf-8"); basePath = basePath + File.separator + "data/" + num; System.out.println("文件路径:" + basePath); if(!new File(basePath).exists()){ new File(basePath).mkdirs(); } JSONObject entryJson = new JSONObject(); if(StrUtil.isEmpty(entry)){ FileUtils.downLoadFromUrl("https://4dkk.4dage.com/images/images" + num + "/vision.modeldata?t=" + System.currentTimeMillis(), "vision.modeldata", basePath); if(!new File(basePath + "/vision.modeldata").exists()){ log.info("vision.modeldata文件不存在"); }else { log.info("下载vision.modeldata完成,开始转换vision.txt"); CreateObjUtil.convertVisionmodeldataToTxt(basePath + "/vision.modeldata", basePath + "/vision.txt"); JSONObject visionJson = JSON.parseObject(FileUtils.readFile(basePath + "/vision.txt")); JSONArray sweepArray = visionJson.getJSONArray("sweepLocations"); if(sweepArray != null && sweepArray.size() > 0){ entryJson.put("noSetting", sweepArray.getJSONObject(0)); } } }else { entryJson.put("setting", JSON.parseObject(entry)); } //下载原场景的scene.json if(!StringUtils.isEmpty(num)){ FileUtils.downLoadFromUrl(mainUrl + "scene/data/data" + num + "/scene.json?t=" + System.currentTimeMillis(), "scene.json", basePath); } //原场景码的户型版本号 int floorPublishVer = 0; String sceneStr = FileUtils.readFile(basePath + "/scene.json"); JSONObject sceneJson = null; if(sceneStr != null){ sceneJson = JSONObject.parseObject(sceneStr); if(sceneJson.containsKey("floorPublishVer")){ floorPublishVer = sceneJson.getIntValue("floorPublishVer"); } } //下载风格码保存过的house.json if(!StringUtils.isEmpty(styleNum)){ String filePath = buildPath + "Input/" + styleNum +File.separator; if(!new File(filePath).exists()){ new File(filePath).mkdirs(); } FileUtils.downLoadFromUrl("https://4dkk.4dage.com/data/data" + styleNum + "/house.json?t=" + System.currentTimeMillis(), "house.json", filePath); //oss上的户型数据 String houseStr = FileUtils.readFile(filePath + "/house.json"); int version = 0; if(houseStr != null){ JSONObject houseJson = JSONObject.parseObject(houseStr); if(houseJson.containsKey("version")){ version = houseJson.getIntValue("version"); } houseJson.put("version", version); houseJson.put("floorPublishVer", floorPublishVer); houseJson.put("entry", entryJson); return houseJson.toJSONString(); } } Map map = null; FileUtils.downLoadFromUrl("https://4dkk.4dage.com/data/data" + num + "/floor.json?t=" + System.currentTimeMillis(), "floor.json", basePath); ConvertCadKjl cad = new ConvertCadKjl(); String data = FileUtils.readFile(basePath + File.separator + "floor.json"); JSONObject inputJson = null; JSONArray inputArray = null; JSONObject resultJson = new JSONObject(); JSONArray resultArray = new JSONArray(); if(StrUtil.isNotEmpty(data)){ inputJson = JSON.parseObject(data); if(inputJson.containsKey("floors")) { inputArray = inputJson.getJSONArray("floors"); for (int i = 0, len = inputArray.size(); i < len; i++) { cad = new ConvertCadKjl(); cad.init(inputArray.getJSONObject(i)); cad.input = cad.preHandle(cad.input); JSONArray beams = cad.getBeams(); JSONArray walls = cad.getWalls(); //判断是否需要重复出去墙 AtomicBoolean removeAgain = new AtomicBoolean(false); //去除重复的墙 walls = cad.removeRepeat(walls); JSONArray rooms = cad.getRooms(); JSONArray flues = cad.getFlues(); JSONArray holes = cad.getHoles(); cad.output.put("holes", holes); cad.output.put("flues", flues); cad.output.put("beams", beams); cad.output.put("rooms", rooms); cad.output.put("walls", walls); map = ConvertCadKjl.getMaxOrMinValue(inputArray.getJSONObject(i)); if(map != null){ JSONObject scene = new JSONObject(); scene.put("bottom", map.get("translationAverage")); scene.put("top", map.get("puckAverage") == null? null : map.get("puckAverage")); cad.output.put("scene", scene); } cad.output.put("version", floorPublishVer); resultArray.add(cad.output); } resultJson.put("floors", resultArray); }else { cad.init(inputJson); cad.input = cad.preHandle(cad.input); JSONArray beams = cad.getBeams(); JSONArray walls = cad.getWalls(); //判断是否需要重复出去墙 AtomicBoolean removeAgain = new AtomicBoolean(false); //去除重复的墙 walls = cad.removeRepeat(walls); JSONArray rooms = cad.getRooms(); JSONArray flues = cad.getFlues(); JSONArray holes = cad.getHoles(); cad.output.put("holes", holes); cad.output.put("flues", flues); cad.output.put("beams", beams); cad.output.put("rooms", rooms); cad.output.put("walls", walls); map = ConvertCadKjl.getMaxOrMinValue(inputJson); if(map != null){ JSONObject scene = new JSONObject(); scene.put("bottom", map.get("translationAverage")); scene.put("top", map.get("puckAverage") == null? null : map.get("puckAverage")); cad.output.put("scene", scene); } cad.output.put("version", floorPublishVer); resultJson = cad.output; } cad.init(inputJson); } resultJson.put("entry", entryJson); cad.writeBinary(resultJson.toString().getBytes(), basePath + File.separator + "kjl.json"); return resultJson.toString(); }catch (Exception e){ e.printStackTrace(); return "生成json数据失败"; } } /** * 修改房型数据 * @param num * @return */ @ApiOperation("修改房型数据") @ApiImplicitParams({ @ApiImplicitParam(name = "num", value = "四维看看场景码", dataType = "String"), @ApiImplicitParam(name = "styleNum", value = "风格码", dataType = "String")}) @RequestMapping(value = "/updateJson", method = RequestMethod.GET) public String updateJson(String num, String styleNum){ try{ if(StringUtils.isEmpty(num) || StringUtils.isEmpty(styleNum)){ return "参数不能为空"; } String path = ResourceUtils.getURL("classpath:").getPath(); path = URLDecoder.decode(path,"utf-8"); path = path + File.separator + "data/" + num; System.out.println("文件路径:" + path); if(!new File(path).exists()){ new File(path).mkdirs(); } //下载原场景的scene.json if(!StringUtils.isEmpty(num)){ FileUtils.downLoadFromUrl(mainUrl + "data/data" + num + "/scene.json?t=" + System.currentTimeMillis(), "scene.json", path); } //原场景码的户型版本号 int floorPublishVer = 0; String sceneStr = FileUtils.readFile(path + "/scene.json"); JSONObject sceneJson = null; if(sceneStr != null){ sceneJson = JSONObject.parseObject(sceneStr); if(sceneJson.containsKey("floorPublishVer")){ floorPublishVer = sceneJson.getIntValue("floorPublishVer"); } } Map map = null; FileUtils.downLoadFromUrl("https://4dkk.4dage.com/images/images" + num + "/vision.modeldata?t=" + System.currentTimeMillis(), "vision.modeldata", path); if(!new File(path + "/vision.modeldata").exists()){ log.info("vision.modeldata文件不存在"); }else { log.info("下载vision.modeldata完成,开始转换vision.txt"); CreateObjUtil.convertVisionmodeldataToTxt(path + "/vision.modeldata", path + "/vision.txt"); } FileUtils.downLoadFromUrl("https://4dkk.4dage.com/data/data" + num + "/floor.json?t=" + System.currentTimeMillis(), "floor.json", path); ConvertCadKjl cad = new ConvertCadKjl(); String data = FileUtils.readFile(path + File.separator + "floor.json"); JSONObject inputJson = null; JSONArray inputArray = null; JSONObject resultJson = new JSONObject(); JSONArray resultArray = new JSONArray(); if(StrUtil.isNotEmpty(data)){ inputJson = JSON.parseObject(data); if(inputJson.containsKey("floors")) { inputArray = inputJson.getJSONArray("floors"); for (int i = 0, len = inputArray.size(); i < len; i++) { cad = new ConvertCadKjl(); cad.init(inputArray.getJSONObject(i)); cad.input = cad.preHandle(cad.input); JSONArray beams = cad.getBeams(); JSONArray walls = cad.getWalls(); //判断是否需要重复出去墙 AtomicBoolean removeAgain = new AtomicBoolean(false); //去除重复的墙 walls = cad.removeRepeat(walls); JSONArray rooms = cad.getRooms(); JSONArray flues = cad.getFlues(); JSONArray holes = cad.getHoles(); cad.output.put("holes", holes); cad.output.put("flues", flues); cad.output.put("beams", beams); cad.output.put("rooms", rooms); cad.output.put("walls", walls); map = ConvertCadKjl.getMaxOrMinValue(inputArray.getJSONObject(i)); if(map != null){ JSONObject scene = new JSONObject(); scene.put("bottom", map.get("translationAverage")); scene.put("top", map.get("puckAverage") == null? null : map.get("puckAverage")); cad.output.put("scene", scene); } cad.output.put("version", floorPublishVer); resultArray.add(cad.output); } resultJson.put("floors", resultArray); }else { cad.init(inputJson); cad.input = cad.preHandle(cad.input); JSONArray beams = cad.getBeams(); JSONArray walls = cad.getWalls(); //判断是否需要重复出去墙 AtomicBoolean removeAgain = new AtomicBoolean(false); //去除重复的墙 walls = cad.removeRepeat(walls); JSONArray rooms = cad.getRooms(); JSONArray flues = cad.getFlues(); JSONArray holes = cad.getHoles(); cad.output.put("holes", holes); cad.output.put("flues", flues); cad.output.put("beams", beams); cad.output.put("rooms", rooms); cad.output.put("walls", walls); map = ConvertCadKjl.getMaxOrMinValue(inputJson); if(map != null){ JSONObject scene = new JSONObject(); scene.put("bottom", map.get("translationAverage")); scene.put("top", map.get("puckAverage") == null? null : map.get("puckAverage")); cad.output.put("scene", scene); } cad.output.put("version", floorPublishVer); resultJson = cad.output; } } cad.writeBinary(resultJson.toString().getBytes(), path + File.separator + "kjl.json"); uploadToOssUtil.upload(path + File.separator + "kjl.json", "data/data" + styleNum + "/house.json"); return resultJson.toString(); }catch (Exception e){ e.printStackTrace(); return "生成json数据失败"; } } public static void setAverageValue(String visionPath, Double translationAverage, Double puckAverage) throws Exception{ Map map = new HashMap<>(); JSONObject visionJson = JSONObject.parseObject(FileUtils.readFile(visionPath)); JSONArray sweepLocations = visionJson.getJSONArray("sweepLocations"); for(int i = 0, len = sweepLocations.size(); i < len; i++){ sweepLocations.getJSONObject(i).getJSONObject("pose"). getJSONObject("translation").put("z", translationAverage); sweepLocations.getJSONObject(i).getJSONObject("puck").put("z", puckAverage); } visionJson.put("sweepLocations", sweepLocations); FileUtils.writeFile(visionPath, visionJson.toString()); } /** * 更新所有decoration.json数据 * @return */ @ApiOperation("更新所有decoration.json数据") @RequestMapping(value = "/updateDecorationJson", method = RequestMethod.GET) public Result updateDecorationJson() throws Exception{ List list = sceneStyleService.list(); String path = ""; String lightPath = ""; String data = ""; File file = null; File lightFile = null; JSONObject object = null; JSONObject lightObject = null; JSONArray furniture = null; JSONArray walls = null; JSONArray rooms = null; JSONArray lights = null; JSONArray windows = null; JSONArray doors = null; JSONObject assets = null; JSONArray materials = null; JSONObject wallpaper = null; JSONObject floor = null; JSONObject texture = null; String wallpaperStr = ""; String textureStr = ""; ResponseRenovationPartsDetail detail = null; ResponseRenovationBuildDetail buildDetail = null; for (SceneStyleEntity sceneStyleEntity : list) { path = buildPath + "Input/" + sceneStyleEntity.getStyleNum() + File.separator + "decoration.json"; lightPath = buildPath + "Input/" + sceneStyleEntity.getStyleNum() + "light/decoration.json"; file = new File(path); lightFile = new File(lightPath); if(file.exists()){ data = FileUtils.readFile(path); if(data.startsWith("{") && data.endsWith("}")){ object = JSON.parseObject(data); if(object != null){ furniture = object.getJSONArray("furniture"); walls = object.getJSONArray("walls"); rooms = object.getJSONArray("rooms"); lights = object.getJSONArray("lights"); windows = object.getJSONArray("windows"); doors = object.getJSONArray("doors"); if(furniture != null){ for(int i = 0, len = furniture.size(); i < len; i++){ if(furniture.getJSONObject(i).containsKey("assets")){ assets = furniture.getJSONObject(i).getJSONObject("assets"); if(assets.containsKey("name")){ detail = partsDetailService.findByName(assets.getString("name")); if(detail != null){ assets.put("previewUrl", detail.getImg()); assets.put("url", detail.getHighImg()); assets.put("path", detail.getPath()); } } } } } if(walls != null){ for(int i = 0, len = walls.size(); i < len; i++){ if(walls.getJSONObject(i).containsKey("materials")){ materials = walls.getJSONObject(i).getJSONArray("materials"); for(int j = 0; j < materials.size(); j++){ wallpaperStr = materials.getJSONObject(j).getString("wallpaper"); wallpaper = new JSONObject(); buildDetail = buildDetailService.findByName(wallpaperStr); // if(wallpaper.containsKey("name")){ if(buildDetail != null){ wallpaper.put("previewUrl", buildDetail.getThumbnail()); wallpaper.put("url", buildDetail.getThumbnail()); wallpaper.put("name", wallpaperStr); wallpaper.put("id", buildDetail.getId()); // wallpaper.put("path", buildDetail.getPath()); materials.getJSONObject(j).put("wallpaper", wallpaper); } // } } } } } if(rooms != null){ for(int i = 0, len = rooms.size(); i < len; i++){ if(rooms.getJSONObject(i).containsKey("floor")){ floor = rooms.getJSONObject(i).getJSONObject("floor"); textureStr = floor.getString("texture"); texture = new JSONObject(); buildDetail = buildDetailService.findByName(textureStr); // if(texture.containsKey("name")){ if(buildDetail != null){ texture.put("previewUrl", buildDetail.getThumbnail()); texture.put("url", buildDetail.getThumbnail()); texture.put("id", buildDetail.getId()); texture.put("name", textureStr); // texture.put("path", buildDetail.getPath()); floor.put("texture", texture); } // } } } } if(lights != null){ for(int i = 0, len = lights.size(); i < len; i++){ if(lights.getJSONObject(i).containsKey("assets")){ assets = lights.getJSONObject(i).getJSONObject("assets"); if(assets.containsKey("name")){ detail = partsDetailService.findByName(assets.getString("name")); if(detail != null){ assets.put("previewUrl", detail.getImg()); assets.put("url", detail.getHighImg()); assets.put("path", detail.getPath()); } } } } } if(windows != null){ for(int i = 0, len = windows.size(); i < len; i++){ if(windows.getJSONObject(i).containsKey("assets")){ assets = windows.getJSONObject(i).getJSONObject("assets"); if(assets.containsKey("name")){ detail = partsDetailService.findByName(assets.getString("name")); if(detail != null){ assets.put("previewUrl", detail.getImg()); assets.put("url", detail.getHighImg()); assets.put("path", detail.getPath()); } } } } } if(doors != null){ for(int i = 0, len = doors.size(); i < len; i++){ if(doors.getJSONObject(i).containsKey("assets")){ assets = doors.getJSONObject(i).getJSONObject("assets"); if(assets.containsKey("name")){ detail = partsDetailService.findByName(assets.getString("name")); if(detail != null){ assets.put("previewUrl", detail.getImg()); assets.put("url", detail.getHighImg()); assets.put("path", detail.getPath()); } } } } } FileUtils.writeFile(path.replace("decoration.json", "decorationBack.json"), JSON.toJSONString(object,SerializerFeature.WriteMapNullValue)); uploadToOssUtil.upload(path.replace("decoration.json", "decorationBack.json"), "data/data" + sceneStyleEntity.getStyleNum() + "/changeData/decoration.json"); } } } if(lightFile.exists()){ data = FileUtils.readFile(lightPath); if(data.startsWith("{") && data.endsWith("}")){ object = JSON.parseObject(data); if(object != null){ furniture = object.getJSONArray("furniture"); walls = object.getJSONArray("walls"); rooms = object.getJSONArray("rooms"); lights = object.getJSONArray("lights"); windows = object.getJSONArray("windows"); doors = object.getJSONArray("doors"); if(furniture != null){ for(int i = 0, len = furniture.size(); i < len; i++){ if(furniture.getJSONObject(i).containsKey("assets")){ assets = furniture.getJSONObject(i).getJSONObject("assets"); if(assets.containsKey("name")){ detail = partsDetailService.findByName(assets.getString("name")); if(detail != null){ assets.put("previewUrl", detail.getImg()); assets.put("url", detail.getHighImg()); assets.put("path", detail.getPath()); } } } } } // if(walls != null){ // // for(int i = 0, len = walls.size(); i < len; i++){ // if(walls.getJSONObject(i).containsKey("materials") && walls.getJSONObject(i).containsKey("wallpaper")){ // // materials = walls.getJSONObject(i).getJSONArray("materials"); // for(int j = 0; j < materials.size(); j++){ // wallpaper = materials.getJSONObject(j).getJSONObject("wallpaper"); // if(wallpaper.containsKey("name")){ // buildDetail = buildDetailService.findByName(wallpaper.getString("name")); // if(buildDetail != null){ // wallpaper.put("previewUrl", buildDetail.getThumbnail()); // wallpaper.put("url", buildDetail.getThumbnail()); //// wallpaper.put("path", buildDetail.getPath()); // } // } // } // // // } // } // } // // if(rooms != null){ // // for(int i = 0, len = rooms.size(); i < len; i++){ // if(rooms.getJSONObject(i).containsKey("floor") && rooms.getJSONObject(i).containsKey("texture")){ // // floor = rooms.getJSONObject(i).getJSONObject("floor"); // texture = floor.getJSONObject("texture"); // // if(texture.containsKey("name")){ // buildDetail = buildDetailService.findByName(texture.getString("name")); // if(buildDetail != null){ // texture.put("previewUrl", buildDetail.getThumbnail()); // texture.put("url", buildDetail.getThumbnail()); //// texture.put("path", buildDetail.getPath()); // } // } // } // } // } if(walls != null){ for(int i = 0, len = walls.size(); i < len; i++){ if(walls.getJSONObject(i).containsKey("materials")){ materials = walls.getJSONObject(i).getJSONArray("materials"); for(int j = 0; j < materials.size(); j++){ wallpaperStr = materials.getJSONObject(j).getString("wallpaper"); wallpaper = new JSONObject(); buildDetail = buildDetailService.findByName(wallpaperStr); // if(wallpaper.containsKey("name")){ if(buildDetail != null){ wallpaper.put("previewUrl", buildDetail.getThumbnail()); wallpaper.put("url", buildDetail.getThumbnail()); wallpaper.put("name", wallpaperStr); wallpaper.put("id", buildDetail.getId()); // wallpaper.put("path", buildDetail.getPath()); materials.getJSONObject(j).put("wallpaper", wallpaper); } // } } } } } if(rooms != null){ for(int i = 0, len = rooms.size(); i < len; i++){ if(rooms.getJSONObject(i).containsKey("floor") ){ floor = rooms.getJSONObject(i).getJSONObject("floor"); textureStr = floor.getString("texture"); texture = new JSONObject(); buildDetail = buildDetailService.findByName(textureStr); // if(texture.containsKey("name")){ if(buildDetail != null){ texture.put("previewUrl", buildDetail.getThumbnail()); texture.put("url", buildDetail.getThumbnail()); texture.put("id", buildDetail.getId()); texture.put("name", textureStr); // texture.put("path", buildDetail.getPath()); floor.put("texture", texture); } // } } } } if(lights != null){ for(int i = 0, len = lights.size(); i < len; i++){ if(lights.getJSONObject(i).containsKey("assets")){ assets = lights.getJSONObject(i).getJSONObject("assets"); if(assets.containsKey("name")){ detail = partsDetailService.findByName(assets.getString("name")); if(detail != null){ assets.put("previewUrl", detail.getImg()); assets.put("url", detail.getHighImg()); assets.put("path", detail.getPath()); } } } } } if(windows != null){ for(int i = 0, len = windows.size(); i < len; i++){ if(windows.getJSONObject(i).containsKey("assets")){ assets = windows.getJSONObject(i).getJSONObject("assets"); if(assets.containsKey("name")){ detail = partsDetailService.findByName(assets.getString("name")); if(detail != null){ assets.put("previewUrl", detail.getImg()); assets.put("url", detail.getHighImg()); assets.put("path", detail.getPath()); } } } } } if(doors != null){ for(int i = 0, len = doors.size(); i < len; i++){ if(doors.getJSONObject(i).containsKey("assets")){ assets = doors.getJSONObject(i).getJSONObject("assets"); if(assets.containsKey("name")){ detail = partsDetailService.findByName(assets.getString("name")); if(detail != null){ assets.put("previewUrl", detail.getImg()); assets.put("url", detail.getHighImg()); assets.put("path", detail.getPath()); } } } } } FileUtils.writeFile(lightPath.replace("decoration.json", "decorationBack.json"), JSON.toJSONString(object,SerializerFeature.WriteMapNullValue)); uploadToOssUtil.upload(lightPath.replace("decoration.json", "decorationBack.json"), "data/data" + sceneStyleEntity.getStyleNum() + "/proLightData/decoration.json"); } } } } return Result.success(); } /** * 获取预设灯光信息 * @return */ @ApiOperation("获取预设灯光信息") @ApiImplicitParams({ @ApiImplicitParam(name = "data", value = "数据(json字符串)", dataType = "String")}) @RequestMapping(value = "/getLightInfo", method = RequestMethod.POST) public Result getLightInfo(String data){ if(StringUtils.isEmpty(data)){ return Result.failure(CodeConstant.FAILURE_CODE_3001, CodeConstant.FAILURE_MSG_3001); } JSONObject jsonObject = new JSONObject(); jsonObject.put("obj", data); String result = OkHttpUtils.httpPostJson(buildUrl + "getLightInfo", jsonObject.toJSONString()); log.info("getLightInfo返回结果:" + result); JSONObject resultJson = JSON.parseObject(result); if(resultJson.containsKey("state") && "done".equals(resultJson.getString("state"))){ return Result.success(resultJson.getString("msg")); } return Result.failure(CodeConstant.FAILURE_CODE_4005, CodeConstant.FAILURE_MSG_4005); } /** * 保存json数据 * @return */ @ApiOperation("保存json数据") @ApiImplicitParams({ @ApiImplicitParam(name = "styleNum", value = "风格码", dataType = "String"), @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String"), @ApiImplicitParam(name = "jsonData", value = "数据(json字符串)", dataType = "String")}) @RequestMapping(value = "/saveJsonUploadOss", method = RequestMethod.POST) public Result saveJsonUploadOss(String jsonData, String fileName, String styleNum) throws Exception{ if(StringUtils.isEmpty(jsonData) || StringUtils.isEmpty(fileName) || StringUtils.isEmpty(styleNum)){ return Result.failure(CodeConstant.FAILURE_CODE_3001, CodeConstant.FAILURE_MSG_3001); } String filePath = buildPath + "data/data" + styleNum + File.separator + fileName; log.info("保存文件路径-filePath{}:" + filePath); FileUtils.writeFile(filePath, jsonData); uploadToOssUtil.upload(filePath, "data/data" + styleNum + File.separator + fileName); return Result.success((Object) ("data/data" + styleNum + File.separator + fileName)); } public static void main(String[] args) { try{ Map map = ConvertCadKjl.getAverageValue("G:\\javaProject\\changeing\\vision.txt"); System.out.println(map.get("translationAverage")); System.out.println(map.get("puckAverage")); setAverageValue("G:\\javaProject\\changeing\\vision.txt", map.get("translationAverage"), map.get("puckAverage")); }catch (Exception e){ e.printStackTrace(); } } }