package com.fdkankan.scene.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amazonaws.services.devicefarm.model.UploadType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.*;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.common.util.ComputerUtil;
import com.fdkankan.common.util.CreateObjUtil;
import com.fdkankan.common.util.FileMd5Util;
import com.fdkankan.common.util.FileUtil;
import com.fdkankan.common.util.FileUtils;
import com.fdkankan.fyun.constant.StorageType;
import com.fdkankan.fyun.oss.UploadToOssUtil;
import com.fdkankan.platform.api.feign.PlatformGoodsClient;
import com.fdkankan.platform.api.feign.PlatformUserClient;
import com.fdkankan.platform.api.vo.Camera;
import com.fdkankan.platform.api.vo.CameraDetail;
import com.fdkankan.platform.api.vo.Company;
import com.fdkankan.redis.constant.RedisKey;
import com.fdkankan.redis.constant.RedisLockKey;
import com.fdkankan.redis.util.RedisLockUtil;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.scene.bean.SceneJsonBean;
import com.fdkankan.scene.entity.SceneDataDownload;
import com.fdkankan.scene.entity.SceneEditControls;
import com.fdkankan.scene.entity.SceneEditInfo;
import com.fdkankan.scene.entity.SceneEditInfoExt;
import com.fdkankan.scene.entity.ScenePro;
import com.fdkankan.scene.entity.SceneProExt;
import com.fdkankan.scene.factory.FloorLogoHandlerFactory;
import com.fdkankan.scene.factory.MusicHandlerFactory;
import com.fdkankan.scene.factory.ScreenshotHandlerFactory;
import com.fdkankan.scene.mapper.ISceneEditInfoMapper;
import com.fdkankan.scene.service.ISceneDataDownloadService;
import com.fdkankan.scene.service.ISceneEditControlsService;
import com.fdkankan.scene.service.ISceneEditInfoExtService;
import com.fdkankan.scene.service.ISceneEditInfoService;
import com.fdkankan.scene.service.ISceneProExtService;
import com.fdkankan.scene.service.ISceneProService;
import com.fdkankan.scene.vo.*;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.Var;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Calendar;
import java.util.Objects;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-01-18
 */
@Slf4j
@Service
public class SceneEditInfoServiceImpl extends ServiceImpl<ISceneEditInfoMapper, SceneEditInfo> implements ISceneEditInfoService {

    @Value("${oss.prefix.ali}")
    private String prefixAli;
    @Value("${oss.prefix.url}")
    private String ossUrlPrefix;
    @Value("${upload.type}")
    private String type;
    @Value("${oss.bucket:4dkankan}")
    private String bucket;
    @Value("${main.url}")
    private String mainUrl;
    @Autowired
    private ISceneEditControlsService sceneEditControlsService;
    @Autowired
    private ISceneProService sceneProService;
    @Autowired
    private UploadToOssUtil uploadToOssUtil;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private ISceneProExtService sceneProExtService;
    @Autowired
    private RedisLockUtil redisLockUtil;
    @Autowired
    ISceneDataDownloadService sceneDataDownloadService;
    @Autowired
    PlatformGoodsClient platformGoodsClient;
    @Autowired
    PlatformUserClient platformUserClient;
    @Autowired
    private ISceneEditInfoExtService sceneEditInfoExtService;


    @Transactional
    @Override
    public SceneEditInfoVO saveScene(SceneEditInfoParamVO param) {

        ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
        if(Objects.isNull(scenePro)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }
        SceneEditInfo sceneEditInfoDb = this.getBySceneProId(scenePro.getId());
        SceneEditControls sceneEditControlsDb = null;
        if(Objects.nonNull(sceneEditInfoDb)){
            sceneEditControlsDb = sceneEditControlsService.getBySceneEditId(sceneEditInfoDb.getId());
        }

        SceneEditInfo sceneEditInfo = BeanUtil.copyProperties(param, SceneEditInfo.class);
        sceneEditInfo.setSceneProId(scenePro.getId());
        if(Objects.isNull(sceneEditInfoDb)){
            this.save(sceneEditInfo);
        }else{
            sceneEditInfo.setId(sceneEditInfoDb.getId());
            sceneEditInfo.setUpdateTime(Calendar.getInstance().getTime());
            this.updateById(sceneEditInfo);
        }

        if(Objects.nonNull(param.getControls())){
            SceneEditControls sceneEditControls = BeanUtil.copyProperties(param.getControls(), SceneEditControls.class);
            sceneEditControls.setEditInfoId(sceneEditInfo.getId());
            if(Objects.isNull(sceneEditControlsDb)){
                sceneEditControlsService.save(sceneEditControls);
            }else{
                sceneEditControls.setId(sceneEditControlsDb.getId());
                sceneEditControlsService.updateById(sceneEditControls);
            }
        }

        SceneEditInfoVO result = BeanUtil.copyProperties(param, SceneEditInfoVO.class);
        result.setCreateTime(sceneEditInfo.getCreateTime());

        return result;
    }

    @Override
    public SceneEditInfo getBySceneProId(long sceneProId) {
        return this.getOne(new LambdaQueryWrapper<SceneEditInfo>()
                .eq(SceneEditInfo::getTbStatus, TbStatus.VALID.code())
                .eq(SceneEditInfo::getSceneProId, sceneProId));
    }

    @Transactional
    @Override
    public ResultData publicScene(SceneEditInfoParamVO param)  throws Exception{
        String sceneNum = param.getNum();
        if(StrUtil.isEmpty(sceneNum)){
            throw new BusinessException(ErrorCode.PARAM_REQUIRED);
        }

        //根据场景码查询数据库
        ScenePro scenePro = sceneProService.findBySceneNum(sceneNum);
        SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
        SceneEditInfo sceneEditInfo = this.getBySceneProId(scenePro.getId());
        SceneEditInfoExt sceneEditInfoExt = null;
            SceneEditControls sceneEditControls = null;
        if(sceneEditInfo == null){
            sceneEditInfo = new SceneEditInfo();
            sceneEditInfo.setSceneProId(scenePro.getId());
        }else{
            sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
            sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
        }
        if(sceneEditInfoExt == null){
            sceneEditInfoExt = new SceneEditInfoExt();
            sceneEditInfoExt.setFloorPlanAngle(0);
            sceneEditInfoExt.setFloorPlanCompass(0);
        }

//        if(sceneEditControls == null){
//            sceneEditControls = new SceneEditControls();
//        }

        //生成sceneJson
        SceneJsonBean sceneJson = new SceneJsonBean();
        BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
        BeanUtil.copyProperties(sceneEditInfo, sceneJson);
        SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
        sceneJson.setControls(sceneEditControlsVO);
        sceneJson.setNum(sceneNum);
//        if(StrUtil.isNotEmpty(sceneEditInfo.getFloorPlanPath())){
//            sceneJson.setFloorPlanPaths(sceneEditInfo.getFloorPlanPath().split(","));
//        }
        sceneJson.setCreateTime(scenePro.getCreateTime());

        sceneJson.setSceneResolution(sceneProExt.getSceneResolution());
        sceneJson.setSceneFrom(sceneProExt.getSceneFrom());
        if(StrUtil.isNotEmpty(scenePro.getVideos())){
            sceneJson.setVideos(JSON.parseObject(scenePro.getVideos()));
        }

        //处理热点数据，生成hot.json
        this.publicHotData(sceneNum, sceneJson, sceneEditInfo);

        // TODO: 2022/3/2 这里的逻辑放在上传球幕视频接口中做了，这里先暂时注释掉，以后要删除
        //处理球幕视频
//        this.buildVideo(sceneEditInfo, sceneProExt.getDataSource(), sceneNum);

        //本地写sceneJson文件
        String localSceneJsonPath = String.format(ConstantFilePath.SCENE_PATH_FORMAT, sceneNum);
        FileUtils.writeFile(localSceneJsonPath, JSON.toJSONString(sceneJson));
        //上传sceneJson文件
        String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", sceneNum);
        uploadToOssUtil.upload(JSON.toJSONBytes(sceneJson), sceneJsonPath);

        //sceneJson放入缓存
        String key = String.format(RedisKey.SCENE_JSON, sceneNum);
        redisUtil.set(key, JSON.toJSONString(sceneJson));

        //删除发布数据中的user目录
        String publicUserPath = String.format(UploadFilePath.USER_VIEW_PATH, sceneNum);
        uploadToOssUtil.deleteFile(publicUserPath);

//        String editUserPath = String.format(UploadFilePath.USER_EDIT_PATH, sceneNum);
        //复制编辑目录到发布目录
        String editPath = String.format(UploadFilePath.EDIT_PATH, param.getNum());
        String viewPath = String.format(UploadFilePath.VIEW_PATH, param.getNum());
        uploadToOssUtil.copyFiles(editPath,viewPath);

        //入库
        if(sceneEditInfo.getId() == null){
            this.save(sceneEditInfo);
        }else{
            this.updateById(sceneEditInfo);
        }
        sceneEditInfoExt.setSceneProId(scenePro.getId());
        sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
        sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);

//        sceneEditControls.setEditInfoId(sceneEditInfo.getId());
//        if(sceneEditControls.getId() == null){
//            sceneEditControlsService.save(sceneEditControls);
//        }else{
//            sceneEditControlsService.updateById(sceneEditControls);
//        }

        return ResultData.ok();
    }

    private void buildVideo(SceneEditInfo sceneEditInfo, String path, String num) throws Exception{
        if(CommonStatus.NO.equals(sceneEditInfo.getBuildVideoStatus())){
            return;
        }
        if(path != null && !"".equals(path) && path.startsWith("http")){
            path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
        }

        String target = path + "_images";

        log.info("球幕视频路径：{}", target + File.separator + "extras/video");
        File video = new File(target + File.separator + "extras/video");
        //如果文件夹或者文件不存在，跳出
        if(!video.exists() || video.listFiles() == null || video.listFiles().length == 0){
            return;
        }

        String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
        for(String videoName : video.list()){
            log.info("球幕视频名称：{}", videoName);
            uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName,userEditPath + videoName);

            CreateObjUtil.mp4ToFlv(target + File.separator + "extras/video/" + videoName,
                target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"));

            uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"),userEditPath + videoName.replace("mp4", "flv"));

            //覆盖原始视频资源
            FileUtils.copyFile(target + File.separator + "extras/video/" + videoName,
                path + File.separator + "caches/videos/" + videoName, true);

        }
        FileUtils.deleteDirectory(target + File.separator + "extras/video/");

        //重置状态
        sceneEditInfo.setBuildVideoStatus(CommonStatus.NO.code());
    }

    private void publicHotData(String sceneNum, SceneJsonBean sceneJson, SceneEditInfo sceneEditInfo) throws IOException {
        String hotDataKey = String.format(RedisKey.SCENE_HOT_DATA, sceneNum);
        Map<String, String> hotMap = redisUtil.hmget(hotDataKey);
        List<String> hotList = Lists.newArrayList();
        hotMap.entrySet().stream().forEach(entry->{
            if(StrUtil.isNotEmpty(entry.getValue())){
                hotList.add(entry.getValue());
            }
        });
        if(CollUtil.isNotEmpty(hotList)){
            JSONArray jsonhots = new JSONArray();
            hotList.stream().forEach(hot->{
                jsonhots.add(JSONObject.parseObject(hot));
            });
            String hotJsonPath = String.format(UploadFilePath.USER_EDIT_PATH, sceneNum) + "hot.json";
            uploadToOssUtil.upload(jsonhots.toString().getBytes(), hotJsonPath);

            //修改tags状态为是，标识有热点数据
            ScenePro scenePro = sceneProService.findBySceneNum(sceneNum);

            this.saveTagsToSceneEditInfo(sceneNum, scenePro.getId(), sceneEditInfo);
        }
    }

    @Override
    public SceneInfoVO getSceneInfo(@Var SceneInfoParamVO param) throws Exception{
        SceneInfoReqType sceneInfoReqType = SceneInfoReqType.get(param.getReqType());
        switch (sceneInfoReqType){
            //如果是编辑页面请求，查数据库
            case EDIT:
                return this.getSceneInfo4Edit(param.getNum());
            //如果是查看页面请求，查redis
            case VIEW:
                return this.getSceneInfo4View(param.getNum());
        }
        return null;
    }

    /**
     * <p>
            编辑页面获取场景详情信息
     * </p>
     * @author dengsixing
     * @date 2022/1/21
     * @param num
     * @return com.fdkankan.scene.vo.SceneInfoVO
     **/
    private SceneInfoVO getSceneInfo4Edit(String num) throws Exception{
        ScenePro scenePro = sceneProService.findBySceneNum(num);
        if(Objects.isNull(scenePro)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }
        SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());

        SceneEditInfo sceneEditInfo = this.getBySceneProId(scenePro.getId());
        if(Objects.isNull(sceneEditInfo)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }
        SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
        SceneEditControls sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
        SceneInfoVO sceneInfoVO = new SceneInfoVO();
        BeanUtil.copyProperties(sceneEditInfoExt, sceneInfoVO);
        BeanUtil.copyProperties(sceneEditInfo, sceneInfoVO);
        if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
            sceneInfoVO.setFloorPlanAngle(0);
        }
        if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
            sceneInfoVO.setFloorPlanCompass(0);
        }
        sceneInfoVO.setControls(BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class));
        sceneInfoVO.setNum(num);
        //生成sceneJson
//        if(StrUtil.isNotEmpty(sceneEditInfo.getFloorPlanPath())){
//            sceneInfoVO.setFloorPlanPaths(sceneEditInfo.getFloorPlanPath().split(","));
//        }
        sceneInfoVO.setCreateTime(scenePro.getCreateTime());

        sceneInfoVO.setSceneResolution(sceneProExt.getSceneResolution());
        sceneInfoVO.setSceneFrom(sceneProExt.getSceneFrom());
        sceneInfoVO.setVideos(scenePro.getVideos());

        this.setExtData(sceneInfoVO, scenePro.getCameraId());

        return sceneInfoVO;
    }

    /**
     * <p>
            getInfo接口返回字段扩展
     * </p>
     * @author dengsixing
     * @date 2022/3/9
     **/
    private void setExtData(SceneInfoVO sceneInfoVO, Long cameraId) throws Exception{

        ResultData<CameraDetail> cameraResultData = platformGoodsClient.getCameraDetailByCameraId(cameraId);
        if(!cameraResultData.getSuccess()){
            throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
        }
        CameraDetail camera = cameraResultData.getData();
        if(Objects.isNull(camera)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7010);
        }
        if(Objects.isNull(camera.getCompanyId())){
            return;
        }
        ResultData<Company> companyResultData = platformUserClient.getCompanyById(camera.getCompanyId());
        if(!companyResultData.getSuccess()){
            throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
        }
        Company company = companyResultData.getData();
        if(Objects.isNull(company)){
            return;
        }
        sceneInfoVO.setDataSync(company.getDataSync());
    }

    /**
     * <p>
     查看页面获取场景详情信息
     * </p>
     * @author dengsixing
     * @date 2022/1/21
     * @param num
     * @return com.fdkankan.scene.vo.SceneInfoVO
     **/
    private SceneInfoVO getSceneInfo4View(String num) throws Exception{

        ScenePro scenePro = sceneProService.findBySceneNum(num);

        String key = String.format(RedisKey.SCENE_JSON, num);
        String sceneJson = redisUtil.get(key);
        SceneInfoVO sceneInfoVO = null;
        //先查询redis
        if(StrUtil.isNotEmpty(sceneJson)) {
            sceneInfoVO = JSON.parseObject(sceneJson, SceneInfoVO.class);
            this.setExtData(sceneInfoVO, scenePro.getCameraId());
            sceneInfoVO.setScenePassword(null);
            if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
                sceneInfoVO.setFloorPlanAngle(0);
            }
            if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
                sceneInfoVO.setFloorPlanCompass(0);
            }
            return sceneInfoVO;
        }

        //如果redis没找到，从scene.json中获取
        String objectName = String.format(ConstantFilePath.SCENE_VIEW_DATA_DATA_SCENEJSON, num);
        String objectContent = uploadToOssUtil.getObjectContent(bucket, objectName);
        if(StrUtil.isEmpty(objectContent))
            return null;
        sceneInfoVO = JSON.parseObject(objectContent, SceneInfoVO.class);
        this.setExtData(sceneInfoVO, scenePro.getCameraId());
        sceneInfoVO.setScenePassword(null);
        if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
            sceneInfoVO.setFloorPlanAngle(0);
        }
        if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
            sceneInfoVO.setFloorPlanCompass(0);
        }
        redisUtil.set(key, JSON.toJSONString(sceneInfoVO));

        return sceneInfoVO;
    }

    @Override
//    @Transactional
    public ResultData saveUpload(SaveUploadParamVO param) throws Exception {
        FileBizType fileBizType = FileBizType.get(param.getBizType());
        String num = param.getNum();
        String fileInfo = param.getFileInfo();
        if(Objects.isNull(fileBizType)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7003);
        }
        switch (fileBizType){
            case MUSIC:
                return new MusicHandlerFactory().getHandler().save(num, fileInfo);
            case FLOOR_LOGO:
                return new FloorLogoHandlerFactory().getHandler().save(num, fileInfo);
//            case FLOOR_PLAN:
//                return new FloorPlanHandlerFactory().getHandler().save(num, fileInfo);
            case SCREENSHOT:
                return new ScreenshotHandlerFactory().getHandler().save(num, fileInfo);

        }


        return ResultData.ok();
    }

    @Override
    public ResultData saveCad(BaseDataParamVO param) throws Exception {

        String num = param.getNum();
        ScenePro scenePro = sceneProService.findBySceneNum(num);
        if(Objects.isNull(scenePro))
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);

        String editDataPath = String.format(UploadFilePath.DATA_EDIT_PATH, num);
        String editUserPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
        String localDataPath = String.format(ConstantFilePath.DATABUFFER_FORMAT, num);
        String floorCadUrl = ossUrlPrefix + editDataPath + "floorplan_cad.json?t=" + System.currentTimeMillis();
        String floorUserUrl = ossUrlPrefix + editUserPath + "floorplan_user.json?t=" + System.currentTimeMillis();

        JSONObject fileInfoJson = JSON.parseObject(param.getData());
        String filePathStr = null;

        String floors = fileInfoJson.getString("floors");
        String cadInfo = null;
        Integer angel = null;
        Integer compass = null;
        String floorJsonData = fileInfoJson.getString("floorJsonData");
        if(StrUtil.isNotBlank(floorJsonData)){
            JSONObject jsonObject = JSON.parseObject(floorJsonData);
            cadInfo = jsonObject.getString("cadInfo");
            angel = jsonObject.getInteger("angle");
            compass = jsonObject.getInteger("compass");
        }
//        String filePaths = fileInfoJson.getString("filePaths");
//        Byte reSet = fileInfoJson.getByte("reset");

        //户型图文件路径
//        if(StrUtil.isNotEmpty(filePaths)){
//            cn.hutool.json.JSONArray filePathsArrs = JSONUtil.parseArray(filePaths);
//            List<String> filePathsList = filePathsArrs.toList(String.class);
//            StringBuilder filePathBuilder = new StringBuilder();
//            filePathsList.parallelStream().forEach(path -> {
//                filePathBuilder.append(",").append(path);
//            });
//            filePathStr = filePathBuilder.substring(1);
//        }

        //处理户型图数据
        JSONObject floorUserJson = null;
        if(StrUtil.isNotEmpty(floors)) {
            //如果入参是空，则加载服务器的
            if(StrUtil.isEmpty(floorJsonData)){

                if (!StorageType.LOCAL.code().equals(this.type)) {// TODO: 2022/2/15 这里有可能有问题，可能还需要考虑本地部署的情况
                    FileUtils.downLoadFromUrl(floorUserUrl, "floorplan_user.json", localDataPath);
                }
                floorJsonData = FileUtils.readFile(localDataPath + "floorplan_user.json");
            }

            //如果floors不为空，需要根据对应楼层id修改楼层名称
            if (StrUtil.isNotEmpty(floorJsonData)) {
                floorUserJson = this.updateFloorName(floors, floorJsonData);
                floorJsonData = floorUserJson.toJSONString();
            }
        }
        //上传floorplan_user.json文件
        FileUtils.writeFile(localDataPath + "floorplan_user.json", floorJsonData);
        uploadToOssUtil.upload(localDataPath + "floorplan_user.json", editUserPath + "floorplan_user.json");

        //如果floors不为空，需要根据对应楼层id修改楼层名称
        if(StrUtil.isNotEmpty(floors)) {

            if (!StorageType.LOCAL.code().equals(this.type)) {// TODO: 2022/2/15 这里有可能有问题，可能还需要考虑本地部署的情况
                FileUtils.downLoadFromUrl(floorCadUrl, "floorplan_cad.json", localDataPath);
            }

            String floorCadStr = FileUtils.readFile(localDataPath + "floorplan_cad.json");
            if (StrUtil.isNotEmpty(floorCadStr)) {
                JSONObject floorCadJson = this.updateFloorName(floors, floorCadStr);
                FileUtils.writeFile(localDataPath + "floorplan_cad.json", floorCadJson.toString());
                uploadToOssUtil.upload(localDataPath + "floorplan_cad.json",editDataPath + "floorplan_cad.json");
            }

        }


        //写入数据库
        Byte floorPlanUser = null;
        if(StrUtil.isNotEmpty(floorJsonData)){
            floorPlanUser = CommonStatus.YES.code();
        }
//        if(CommonStatus.YES.code().equals(reSet)){
//            floorPlanUser = CommonStatus.NO.code();
//        }
        SceneEditInfo sceneEditInfoDb = this.getBySceneProId(scenePro.getId());
        if(Objects.nonNull(sceneEditInfoDb)){
            LambdaUpdateWrapper<SceneEditInfo> updateWrapper = new LambdaUpdateWrapper<SceneEditInfo>()
                .setSql("version=version+" + 1)
                .eq(SceneEditInfo::getId, sceneEditInfoDb.getId());
//            if(StrUtil.isNotEmpty(filePathStr)){
//                updateWrapper.set(SceneEditInfo::getFloorPlanPath, filePathStr);
//            }
            if(StrUtil.isNotEmpty(cadInfo)){
                updateWrapper.set(SceneEditInfo::getCadInfo, cadInfo);
            }
            if(floorPlanUser != null){
                updateWrapper.set(SceneEditInfo::getFloorPlanUser, floorPlanUser);
            }
            this.update(updateWrapper);
        }else{
            sceneEditInfoDb = new SceneEditInfo();
            sceneEditInfoDb.setSceneProId(scenePro.getId());
//            sceneEditInfoDb.setFloorPlanPath(filePathStr);
            sceneEditInfoDb.setCadInfo(cadInfo);
            sceneEditInfoDb.setFloorPlanUser(floorPlanUser);
            this.save(sceneEditInfoDb);
        }

        SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoDb.getId());
        if(Objects.isNull(sceneEditInfoExt)){
            sceneEditInfoExt = new SceneEditInfoExt();
            sceneEditInfoExt.setEditInfoId(sceneEditInfoDb.getId());
        }
        if(Objects.nonNull(angel)){
            sceneEditInfoExt.setFloorPlanAngle(angel);
        }
        if(Objects.nonNull(compass)){
            sceneEditInfoExt.setFloorPlanCompass(compass);
        }
        sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);


        return ResultData.ok();

    }

    @Override
    public ResultData resetCad(String num){

        ScenePro scenePro = sceneProService.findBySceneNum(num);
        if(Objects.isNull(scenePro))
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);

        SceneEditInfo sceneEditInfoDb = this.getBySceneProId(scenePro.getId());
        if(Objects.nonNull(sceneEditInfoDb)){
            LambdaUpdateWrapper<SceneEditInfo> updateWrapper = new LambdaUpdateWrapper<SceneEditInfo>()
                .setSql("version=version+" + 1)
                .set(SceneEditInfo::getFloorPlanUser, CommonStatus.NO.code())
                .eq(SceneEditInfo::getId, sceneEditInfoDb.getId());
            this.update(updateWrapper);
        }else{
            sceneEditInfoDb = new SceneEditInfo();
            sceneEditInfoDb.setSceneProId(scenePro.getId());
            sceneEditInfoDb.setFloorPlanUser(CommonStatus.NO.code());
            this.save(sceneEditInfoDb);
        }

        return ResultData.ok();

    }

    private JSONObject updateFloorName(String sourceFloors, String targeFloors){
        JSONArray sourceFloorsJson = JSON.parseArray(sourceFloors);
        JSONObject targeFloorsJson = JSONObject.parseObject(targeFloors);
        JSONArray array = targeFloorsJson.getJSONArray("floors");
        for (int i = 0; i < array.size(); ++i) {
            JSONObject targetFloor = array.getJSONObject(i);
            int targetId = targetFloor.getIntValue("id");
            for (int j = 0; j < sourceFloorsJson.size(); ++j) {
                JSONObject floor = sourceFloorsJson.getJSONObject(j);
                int id = floor.getIntValue("id");
                String name = floor.getString("name");
                if (targetId != id)
                    continue;

                targetFloor.put("name", name);
            }
        }
        return targeFloorsJson;
    }

    @Override
    public void upgradeVersionById(Long id) {
        this.update(new LambdaUpdateWrapper<SceneEditInfo>()
            .setSql("version=version+" + 1)
            .eq(SceneEditInfo::getId, id));
    }

    @Override
    public ResultData uploadPanorama(String num, MultipartFile file) throws Exception {

        //校验压缩包格式
        log.info("全景图压缩包文件名：" + file.getOriginalFilename());
        if(!file.getOriginalFilename().endsWith(".zip")){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7015);
        }

        ScenePro scenePro = sceneProService.findBySceneNum(num);
        if(scenePro == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }
        SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());

        //原始计算根目录
        String path = sceneProExt.getDataSource();
        //全景图计算根目录
        String target = path + "_images";
        //解压缩文件存放目录
        String targetImagesPath = target + "/extras/images";
        //压缩文件保存目录
        String zipTargetFilePath = targetImagesPath + File.separator + file.getOriginalFilename();

        //压缩包保存到本地
//        String cachePath =  String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
//        String path = "F:\\mnt\\4Dkankan\\scene\\t-ieXdyGl6Md\\caches\\images\\";

        //先删除本地文件
        FileUtils.deleteDirectory(targetImagesPath);
        File targetFile = new File(zipTargetFilePath);
        if(!targetFile.getParentFile().exists()){
            targetFile.getParentFile().mkdirs();
        }
        file.transferTo(targetFile);

        //解压zip包
        FileUtil.unZip(zipTargetFilePath, targetImagesPath);
        //删除压缩包
        FileUtil.delFile(zipTargetFilePath);

        //获取解压后的文件列表
        List<String> uploadFileList = FileUtil.getFileList(targetImagesPath);
        if(CollUtil.isEmpty(uploadFileList)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7014);
        }

        //列出caches/images中的文件列表
        String imgCachePath = String.format(UploadFilePath.IMG_CACHES_PATH, num);
        List<String> keyList = uploadToOssUtil.listKeys(imgCachePath);

        //比对图片列表，不存在的要返回名称集合
        List<String> notExistFileList = uploadFileList.stream().filter(filePath -> {
            filePath = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
            for (String key : keyList) {
                key = key.substring(key.lastIndexOf("/") + 1);
                if (filePath.equals(key)) {
                    return false;
                }
            }
            return true;
        }).map(filePath -> {
            return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
        }).collect(Collectors.toList());

        //有文件对不上号，就退出
        if(CollUtil.isNotEmpty(notExistFileList)){
            return ResultData.error(ErrorCode.FAILURE_CODE_7012, notExistFileList);
        }

        //上传
        Map<String, String> map = new HashMap<>();
        uploadFileList.stream().forEach(filePath->{
            map.put(filePath, filePath.replace(targetImagesPath, imgCachePath));
        });

        String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
        if(StorageType.AWS.code().equals(this.type)){
            FileUtils.downLoadFromUrl(ConstantUrl.PREFIX_AWS + imgViewPath + "vision.modeldata" + "?m="+new Date().getTime(),
                "vision.modeldata", target + File.separator + "extras" + File.separator);
        }
        if(StorageType.OSS.code().equals(this.type)){
            FileUtils.downLoadFromUrl(prefixAli + imgViewPath + "vision.modeldata" + "?m="+new Date().getTime(),
                "vision.modeldata", target + File.separator + "extras" + File.separator);
        }
        CreateObjUtil.convertVisionmodeldataToTxt(target + File.separator + "extras" + File.separator + "vision.modeldata",
            target + File.separator + "extras" + File.separator + "vision.txt");

        FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
        FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);

        //data.json增加extras为执行重建算法
        String data = FileUtils.readFile(target + File.separator+"data.json");
        if(data != null){
            JSONObject floorplanJson = new JSONObject();
            floorplanJson.put("has_source_images", true);
            floorplanJson.put("has_vision_txt", true);

            JSONObject dataJson = JSONObject.parseObject(data);
            dataJson.put("extras", floorplanJson);
            //V5表示不需要生成high，low文件
            dataJson.put("skybox_type", "SKYBOX_V6");
            if(scenePro.getSceneScheme() == 11){
                dataJson.put("skybox_type", "SKYBOX_V7");
            }
            dataJson.put("split_type", "SPLIT_V8");
            //sceneScheme为3切成瓦片图
            if(scenePro.getSceneScheme() == 3){
                dataJson.put("skybox_type", "SKYBOX_V4");
            }
            FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
        }
        if(new File(target + File.separator + "capture").exists()){
            new File(target + File.separator + "capture").delete();
        }
        if(new File(target + File.separator + "results").exists()){
            FileUtils.delAllFile(target + File.separator + "results");
        }
        if(StorageType.AWS.code().equals(this.type)){
            //亚马逊保持旧方式，超链接capture
            CreateObjUtil.createSoftConnection(path + File.separator + "capture", target + File.separator + "capture");
        }
        CreateObjUtil.ossUtilCp(ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "") + "/data.fdage", target + File.separator + "capture/");
        CreateObjUtil.build3dModel(target , "1");

        String uploadJsonPath= target + File.separator + "results" +File.separator+"upload.json";
        Thread.sleep(2000);
        boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, 3, 5000);
        if(!exist){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5042);
        }
        String uploadData = FileUtils.readFile(uploadJsonPath);
        JSONObject uploadJson = null;
        JSONArray array = null;
        if(uploadData!=null) {
            uploadJson = JSONObject.parseObject(uploadData);
            array = uploadJson.getJSONArray("upload");
        }
        if(array == null){
            throw new Exception("upload.json数据出错");
        }
        JSONObject fileJson = null;
        String fileName = "";
        for(int i = 0, len = array.size(); i < len; i++) {
            fileJson = array.getJSONObject(i);
            fileName = fileJson.getString("file");
            //文件不存在抛出异常
            if (!new File(target + File.separator + "results" + File.separator + fileName)
                .exists()) {
                throw new Exception(
                    target + File.separator + "results" + File.separator + fileName + "文件不存在");
            }

            //high文件夹
            if (fileJson.getIntValue("clazz") == 3) {
                map.put(target + File.separator + "results" + File.separator + fileName,
                    imgViewPath + "pan/high/" + fileName.replace("high/", ""));
                continue;
            }
            //low文件夹
            if (fileJson.getIntValue("clazz") == 4) {
                map.put(target + File.separator + "results" + File.separator + fileName,
                    imgViewPath + "pan/low/" + fileName.replace("low/", ""));
                continue;
            }

            //tiles文件夹,亚马逊没有裁剪图片api，不需要上传4k图
            if (fileJson.getIntValue("clazz") == 5) {
                map.put(target + File.separator + "results" + File.separator + fileName,
                    imgViewPath + fileName);
                continue;
            }

            //tiles文件夹,亚马逊瓦片图
            if (fileJson.getIntValue("clazz") == 7) {
                map.put(target + File.separator + "results" + File.separator + fileName,
                    imgViewPath + fileName);
                continue;
            }
        }
        if(map.size()>0) {
            uploadToOssUtil.uploadMulFiles(map);
        }

        return null;
    }

    @Override
    public ResultData downloadPanorama(FileParamVO param) throws Exception {

        String num = param.getNum();
        String fileName = param.getFileName();

        String cachePath = String.format(ConstantFilePath.SCENE_CACHE, num);
        String imgCachePath = String.format(UploadFilePath.IMG_CACHES_PATH, num);
        String localImagesPath =  String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);

        String cacheFormat = "downloads/scene/%s/caches/";
        String cacheImageFormat = "downloads/scene/%s/caches/images/";

        //删除本地文件
        FileUtils.deleteDirectory(localImagesPath);
        //如果入参文件名不为空，则是单个文件下载，不需要打包
        if(StrUtil.isNotEmpty(fileName)){
            //先下载到本地
            if (!StorageType.LOCAL.code().equals(this.type)) {// TODO: 2022/2/15 这里有可能有问题，可能还需要考虑本地部署的情况
                String filePath = imgCachePath + fileName;
                String imageUrl = ossUrlPrefix + filePath + "?t=" + System.currentTimeMillis();
                FileUtils.downLoadFromUrl(imageUrl, fileName, localImagesPath);
            }
        }else{
            //先下载到本地
            List<String> keyList = uploadToOssUtil.listKeys(imgCachePath);
            if (!StorageType.LOCAL.code().equals(this.type)) {// TODO: 2022/2/15 这里有可能有问题，可能还需要考虑本地部署的情况
                keyList.stream().forEach(key->{
                    String file = key.substring(key.lastIndexOf("/") + 1);
                    String imageUrl = ossUrlPrefix + imgCachePath + file + "?t=" + System.currentTimeMillis();
                    FileUtils.downLoadFromUrl(imageUrl, file, localImagesPath);
                });
            }
        }

        //打包
        String zipName = num + "_images.zip";
        String zipPath = cachePath + zipName;
        FileUtils.zipFile(zipPath, localImagesPath);
        //上传压缩包
        uploadToOssUtil.upload(zipPath, String.format(cacheFormat, num) + zipName);
        String url = ossUrlPrefix + String.format(cacheFormat, num) + zipName + "?t=" + Calendar.getInstance().getTimeInMillis();

        //删除本地压缩包
        FileUtils.deleteFile(zipPath);
        //删除本地目录
        FileUtils.deleteDirectory(localImagesPath);

        Map<String, Object> map = new HashMap<>();
        map.put("fileUrl", url + "?t=" + System.currentTimeMillis());
        map.put("fileName", zipName);
        return ResultData.ok(map);
    }

    @Override
    public void saveTagsToSceneEditInfo(String num, Long sceneProId, SceneEditInfo sceneEditInfo){
        //查询缓存是否包含热点数据
        String key = String.format(RedisKey.SCENE_HOT_DATA, num);
        Map<String, String> allTagsMap = redisUtil.hmget(key);
        boolean hashTags = false;
        for (Entry<String, String> tagMap : allTagsMap.entrySet()) {
            if(StrUtil.isEmpty(tagMap.getValue())){
                continue;
            }
            hashTags = true;
            break;
        }

        //更改热点状态
        sceneEditInfo.setTags(hashTags ? CommonStatus.YES.code() : CommonStatus.NO.code());
        //version 是空的代表 sceneEditInfo记录集不存在
        if(sceneEditInfo.getVersion() != null){
            sceneEditInfo.setVersion(sceneEditInfo.getVersion() + 1);
        }
    }

    @Override
    public ResultData saveVideoBox(FileNameAndDataParamVO param) throws Exception {

        JSONObject boxVideo = JSONObject.parseObject(param.getData());
        String sid = boxVideo.getString("sid");
        if(StrUtil.isEmpty(sid)){
            throw new BusinessException(ErrorCode.PARAM_REQUIRED);
        }
        ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
        if(Objects.isNull(scenePro))
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);

        SceneEditInfo sceneEditInfo = this.getBySceneProId(scenePro.getId());

        //转换视频格式
        this.transferToFlv(param.getNum(), param.getFileName());

        //生成boxVideos数据
        String boxVideos = this.createBoxVideos(sid, boxVideo, sceneEditInfo, OperationType.ADDORUPDATE.code());

        //更新数据库
        this.updateBoxVideos(sceneEditInfo, scenePro.getId(), boxVideos);

        return ResultData.ok();
    }

    @Override
    public ResultData deleteVideoBox(DeleteVidoeBoxParamVO param) throws Exception {

        ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
        if(Objects.isNull(scenePro))
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);

        SceneEditInfo sceneEditInfo = this.getBySceneProId(scenePro.getId());

        //根据sid移除json
        String boxVideos = this.createBoxVideos(param.getSid(), null, sceneEditInfo, OperationType.DELETE.code());

        //写数据库
        this.updateBoxVideos(sceneEditInfo,scenePro.getId(),boxVideos);

        return ResultData.ok();
    }

    @Override
    public DownloadVO downloadBallScreenVideo(BallScreenVideoParamVO param) {
        String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, param.getNum()) + param.getFileName();
        String url = ossUrlPrefix + videoPath + "?t=" + System.currentTimeMillis();
        return DownloadVO.builder()
            .fileName(param.getFileName())
            .url(url)
            .build();
    }

    @Override
    public ResultData uploadBallScreenVideo(String num, String fileName, MultipartFile file) throws Exception {

        //校验格式
        if(!fileName.endsWith(".mp4")){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7016);
        }
        String videosViewPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, num);

        ScenePro scenePro = sceneProService.findBySceneNum(num);
        if(scenePro == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }
        SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());

        //校验文件名
        String videos = scenePro.getVideos();
        if(StrUtil.isEmpty(videos)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
        }
        JSONObject videosJson = JSON.parseObject(videos);
        JSONArray dataArr = videosJson.getJSONArray("data");
        boolean exists = false;
        for (Object o : dataArr) {
            JSONObject jsonObject =  (JSONObject)o;
            if(jsonObject.getString("id").equals(fileName.replace(".mp4", ""))){
                exists = true;
                break;
            }
        }
        if(!exists){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
        }

        String path = sceneProExt.getDataSource();

        if(path != null && !"".equals(path) && path.startsWith("http")){
            path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
        }

        String target = path + "_images";

        String filePath = target + File.separator + "extras/video" + File.separator + fileName;

        File targetFile = new File(filePath);
        if(!targetFile.getParentFile().exists()){
            targetFile.getParentFile().mkdirs();
        }

        //保存视频到本地
        file.transferTo(targetFile);

        File video = new File(target + File.separator + "extras/video");
        //如果文件夹或者文件不存在，跳出
        if(!video.exists() || video.listFiles() == null || video.listFiles().length == 0){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7009);
        }

        for(String videoName : video.list()){
            log.info("球幕视频名称：{}", videoName);
            uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName,videosViewPath + videoName);

            CreateObjUtil.mp4ToFlv(target + File.separator + "extras/video/" + videoName,
                target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"));

            uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"),videosViewPath + videoName.replace("mp4", "flv"));

            //覆盖原始视频资源
            FileUtils.copyFile(target + File.separator + "extras/video/" + videoName,
                path + File.separator + "caches/videos/" + videoName, true);

        }
        FileUtils.deleteDirectory(target + File.separator + "extras/video/");


//        SceneEditInfo sceneEditInfo = this.getBySceneProId(scenePro.getId());
//        if(sceneEditInfo == null){
//            sceneEditInfo = new SceneEditInfo();
//            sceneEditInfo.setSceneProId(scenePro.getId());
//            sceneEditInfo.setBuildVideoStatus(CommonStatus.YES.code());
//            this.save(sceneEditInfo);
//        }else{
//            sceneEditInfo.setBuildVideoStatus(CommonStatus.YES.code());
//            this.updateById(sceneEditInfo);
//        }

        return ResultData.ok();
    }

    @Override
    public ResultData sceneSync(String num, String type, String floorPlanJson, String ajkJson, String cameraJson,
        MultipartFile[] files) throws Exception {

        String lockKey = String.format(RedisLockKey.LOCK_SCENE_SYNC, num);
        Boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_2_HOUR);
        if(!lock){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5036);
        }
        try {

            ScenePro scenePro = sceneProService.findBySceneNum(num);
            if(scenePro == null){
                throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
            }
            SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());

            //更新scene.json文件
            String strsceneInfos = FileUtils.readFile(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + num + File.separator + "scene.json");
            if(strsceneInfos == null)
                new File(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + num + File.separator + "scene.json").createNewFile();

            String path = sceneProExt.getDataSource();//   /mnt/data/0662c5389/831989883441512448/4898cab04f8c_202104141602356060/
            if(path != null && !"".equals(path) && path.startsWith("http")){
                path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
            }
            String target = path + "_ajk";   // /mnt/data/0662c5389/831989883441512448/4898cab04f8c_202104141602356060_ajk
            File editPath = new File(target);
            if(!editPath.exists()){
                editPath.mkdirs();
            }

            //创建文件夹软连接并且复制data.json和project.json
            if(new File(target + File.separator + "capture").exists()){
                new File(target + File.separator + "capture").delete();
            }
            if(new File(target + File.separator + "caches").exists()){
                //删除link
                new File(target + File.separator + "caches" + File.separator + "images").delete();
                //删除所有文件
                FileUtils.delAllFile(target + File.separator + "caches");
            }
            if(new File(target + File.separator + "results").exists()){
                FileUtils.delAllFile(target + File.separator + "results");
            }
            //创建文件夹,并link文件夹
            new File(target + File.separator + "caches").mkdirs();
            CreateObjUtil.createSoftConnection(path + File.separator + "capture", target + File.separator + "capture");
            if(new File(path + File.separator + "caches" + File.separator + "images").exists()){
                CreateObjUtil.createSoftConnection(path + File.separator + "caches" + File.separator + "images", target + File.separator + "caches" + File.separator + "images");
            }

            FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
            FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);

            //data.json增加extras为执行重建算法
            String project = FileUtils.readFile(target + File.separator+"project.json");
            if(project != null){
                JSONObject projectJson = JSONObject.parseObject(project);
                projectJson.put("parent", projectJson.get("uuid"));
                projectJson.put("uuid", UUID.randomUUID().toString());
                projectJson.put("time", System.currentTimeMillis());
                FileUtils.writeFile(path + File.separator + "project.json", projectJson.toString());
            }

            String data = FileUtils.readFile(target + File.separator+"data.json");
            if(data != null){
                JSONObject floorplanJson = new JSONObject();
                floorplanJson.put("has_floor_ajk_json", true);
                floorplanJson.put("has_vision_txt", true);
                floorplanJson.put("has_floorplan_json", true);

                JSONObject dataJson = JSONObject.parseObject(data);
                dataJson.put("extras", floorplanJson);
                //V5表示不需要生成high，low文件
                dataJson.put("skybox_type", "SKYBOX_V8");
                dataJson.put("split_type", "SPLIT_V10");
                FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
            }

            //文件上传的位置可以自定义
            log.info("画墙重建模型开始");
            File targetFile = new File(target + File.separator + "extras" + File.separator + "floor_ajk.json");
            if(!targetFile.getParentFile().exists()){
                targetFile.getParentFile().mkdirs();
            }
            if(targetFile.exists()){
                FileUtils.deleteFile(target + File.separator + "extras" + File.separator + "floor_ajk.json");
            }
            // 保存
            FileUtils.writeFile(target + File.separator + "extras" + File.separator + "floorplan.json", new String(floorPlanJson.getBytes(), "UTF-8"));

            FileUtils.writeFile(target + File.separator + "extras" + File.separator + "floor_ajk.json", new String(ajkJson.getBytes(), "UTF-8"));
            FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+num + File.separator + "floor_ajk.json", new String(ajkJson.getBytes(), "UTF-8"));

            FileUtils.writeFile(target + File.separator + "extras" + File.separator + "vision.txt", new String(cameraJson.getBytes(), "UTF-8"));
            FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+num + File.separator + "camera.json", new String(cameraJson.getBytes(), "UTF-8"));

            for(int i = 0; i < files.length; i ++){

                File cadImg = new File(target + File.separator + "extras" + File.separator + "Floorplans/" + files[i].getOriginalFilename());
                if(!cadImg.getParentFile().exists()){
                    cadImg.getParentFile().mkdirs();
                }
                if(cadImg.exists())
                {
                    cadImg.delete();
                }

                files[i].transferTo(cadImg);
            }
            //下载封面图
            FileUtils.downLoadFromUrl(scenePro.getThumb() + "?t=" + System.currentTimeMillis(),
                "Cover.png", target + File.separator + "extras" + File.separator + "CoverImage");
            //转换成jpg
            FileUtils.pngToJpg(target + File.separator + "extras" + File.separator + "CoverImage/Cover.png",
                target + File.separator + "extras" + File.separator + "CoverImage/Cover.jpg");

            //安居客算法运行
            log.info("安居客算法：开始建模——"+num);
            CreateObjUtil.build3dModel(target , "1");
            String uploadJsonPath = target + File.separator + "results" + File.separator + "upload.json";
            log.info("uploadJsonPath=" + uploadJsonPath);
            //因为共享目录有延迟，这里循环检测算法是否计算完毕3次，每次隔五秒
            boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, 3, 5000);
            if(!exist){
                throw new BusinessException(ErrorCode.FAILURE_CODE_5042);
            }

            String zipPath = target + File.separator + "results/" + num + ".zip";
            new File(zipPath).delete();

            FileUtils.zipFile(zipPath, target + File.separator + "results/ajk/");
//            String command = "bash /opt/ossutil/gzip.sh " + zipPath.replace(".zip", "") + " " + target + File.separator + "results/ajk/";
//            log.info("压缩文件:" + command);
//            CreateObjUtil.callshell(command);

            if(!new File(zipPath).exists()){
                return  ResultData.error(ErrorCode.FAILURE_CODE_5043);
            }
            String fileMD5 = FileMd5Util.getFileMD5(new File(zipPath));

            uploadToOssUtil.upload(zipPath, "data_download/" + num + ".zip");

            SceneDataDownload sceneDataDownload = sceneDataDownloadService.findBySceneNum(num);
            if(sceneDataDownload == null){
                sceneDataDownload = new SceneDataDownload();
                sceneDataDownload.setNum(num);
                sceneDataDownload.setDownloadPath(prefixAli + "data_download/" + num + ".zip");
                sceneDataDownload.setFileMd5(fileMD5);
                sceneDataDownloadService.save(sceneDataDownload);
                return ResultData.ok();
            }

            sceneDataDownload.setFileMd5(fileMD5);
            sceneDataDownload.setUpdateTime(Calendar.getInstance().getTime());
            sceneDataDownloadService.updateById(sceneDataDownload);
        }catch (Exception e){
            log.error("画墙重建模型失败...", e);
            throw new BusinessException(ErrorCode.FAILURE_CODE_5039);
        }finally {
            redisLockUtil.unlockLua(lockKey);
        }

        return ResultData.ok();
    }

    @Override
    public ResultData checkKey(SceneCheckKeyParamVO param) throws Exception {

        String sceneJson = redisUtil.get(String.format(RedisKey.SCENE_JSON, param.getNum()));
        SceneJsonBean sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);

        if(!param.getPassword().equals(sceneJsonBean.getScenePassword())){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5021);
        }

        return ResultData.ok();
    }

    private void updateBoxVideos(SceneEditInfo sceneEditInfo, Long sceneProId, String boxVideos){
        if(Objects.isNull(sceneEditInfo)){
            sceneEditInfo = new SceneEditInfo();
            sceneEditInfo.setSceneProId(sceneProId);
            sceneEditInfo.setBoxVideos(boxVideos);
            this.save(sceneEditInfo);
        }else{
            this.update(new UpdateWrapper<SceneEditInfo>()
                .setSql("version = version + 1")
                .set("box_videos", boxVideos)
                .eq("id", sceneEditInfo.getId()));
        }
    }

    private String createBoxVideos(String sid, JSONObject boxVideo, SceneEditInfo sceneEditInfo, int type){

        String boxVideos = null;
        if(sceneEditInfo != null){
            boxVideos = sceneEditInfo.getBoxVideos();
        }
        JSONArray boxVideosJson = null;
        if (StrUtil.isNotEmpty(boxVideos)) {
            boxVideosJson = JSONArray.parseArray(boxVideos);
        }else {
            boxVideosJson = new JSONArray();
        }

        String result = null;
        //删除
        if(type == OperationType.DELETE.code()){
            if(boxVideosJson.size() == 0)
                return null;
            for(int i=0;i<boxVideosJson.size();++i){
                JSONObject ele = boxVideosJson.getJSONObject(i);
                if(ele.getString("sid").equals(sid)){
                    boxVideosJson.remove(i);
                }
            }

        }else{

            // TODO: 2022/3/1 目前需求只保留一个，所以这里先注释掉，sid不同时，不追加
            //更新
//            boolean exist = false;
//            for(int i=0;i<boxVideosJson.size();++i){
//                JSONObject ele = boxVideosJson.getJSONObject(i);
//                if(ele.getString("sid").equals(sid)){
//                    boxVideosJson.set(i, boxVideo);
//                    exist = true;
//                }
//            }
//        //新增
//        if(!exist){
//            boxVideosJson.add(boxVideo);
//        }

            boxVideosJson.clear();
            boxVideosJson.add(boxVideo);

        }
        if(boxVideosJson.size() != 0){
            result = boxVideosJson.toJSONString();
        }

        return result;
    }

    private void transferToFlv(String num, String fileName) throws Exception {

        String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
        String localImagesPath = String.format(ConstantFilePath.IMAGESBUFFER_FORMAT, num);
        String localFilePath = localImagesPath + fileName;

        File targetFile = new File(localImagesPath);
        if (!targetFile.exists()){
            targetFile.mkdirs();
        }

        targetFile = new File(localFilePath);
        if (targetFile.exists()){
            FileUtils.deleteFile(localFilePath);
        }

        //从用户编辑目录中下载视频到本地
        if (!StorageType.LOCAL.code().equals(this.type)) {
            String filePath = userEditPath + fileName;
            String imageUrl = ossUrlPrefix + filePath + "?t=" + System.currentTimeMillis();
            FileUtils.downLoadFromUrl(imageUrl, fileName, localImagesPath);
        }

        //视频格式转换
        CreateObjUtil.mp4ToFlv(localFilePath, localFilePath.replace("mp4", "flv"));

        //上传
        String flvFileName = fileName.replace("mp4", "flv");
        uploadToOssUtil.upload(localFilePath, userEditPath + fileName);
        uploadToOssUtil.upload(localFilePath.replace("mp4", "flv"), userEditPath+flvFileName);
    }

}
