package com.fdkankan.contro.mq.service.impl;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.constant.CommonSuccessStatus;
import com.fdkankan.common.constant.PayStatus;
import com.fdkankan.common.constant.SceneSource;
import com.fdkankan.common.constant.SceneStatus;
import com.fdkankan.common.util.FileUtils;
import com.fdkankan.contro.entity.RelocationBatch;
import com.fdkankan.contro.entity.RelocationBatchDetail;
import com.fdkankan.contro.entity.ScenePlus;
import com.fdkankan.contro.entity.ScenePlusExt;
import com.fdkankan.contro.mq.service.IBuildSceneService;
import com.fdkankan.contro.service.*;
import com.fdkankan.fyun.face.FYunFileServiceInterface;
import com.fdkankan.model.constants.ConstantFileName;
import com.fdkankan.model.constants.ConstantFilePath;
import com.fdkankan.model.constants.UploadFilePath;
import com.fdkankan.model.enums.ModelTypeEnums;
import com.fdkankan.model.utils.CreateObjUtil;
import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
import com.fdkankan.rabbitmq.util.RabbitMqProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* TODO
*
*
* @author dengsixing
* @since 2022/4/20
**/
@Slf4j
@Service
@RefreshScope
public class BuildSxRelocationServiceImpl implements IBuildSceneService {
@Value("${queue.modeling.sx-relocation-post:sx-relocation-post}")
private String queueModelingPost;
@Value("${model.type:#{null}}")
private String modelType;
@Value("${env:gn}")
private String env;
@Autowired
private RabbitMqProducer mqProducer;
@Resource
private FYunFileServiceInterface fYunFileService;
@Autowired
private IScenePlusService scenePlusService;
@Autowired
private IScenePlusExtService scenePlusExtService;
@Autowired
private IBuildSceneDTService buildSceneDTService;
@Autowired
private ICommonService commonService;
@Autowired
private IBuildService buildService;
@Autowired
private IRelocationBatchDetailService relocationBatchDetailService;
@Autowired
private IRelocationBatchService relocationBatchService;
@Override
public void buildScenePre(BuildSceneCallMessage message) throws Exception{
String num = message.getSceneNum();
Long batchId = Long.valueOf(message.getExt().get("batchId").toString());
ScenePlus scenePlusByNum = scenePlusService.getScenePlusByNum(num);
try {
//重新计算时需要删除文件夹,否知使用缓存
if(new File(message.getPath() + File.separator + "results").exists()){
FileUtils.deleteDirectory(message.getPath() + File.separator + "results");
}
//由于刘强说caches会影响计算结果,所以这里删除caches
if(new File(message.getPath() + File.separator + "caches").exists()){
FileUtils.deleteDirectory(message.getPath() + File.separator + "caches");
}
this.downLoadSource(message, message.getPath());
message.setResultReceiverMqName(queueModelingPost);
Map buildContext = new HashMap<>();
buildContext.put("sceneNum",message.getSceneNum());
message.setBuildContext(buildContext);
Map dataMap = new HashMap<>();
dataMap.put("splitType", "SPLIT_V31");
dataMap.put("skyboxType", "SKYBOX_V6");
if(ModelTypeEnums.TILE_CODE.equals(modelType)){
dataMap.put("skyboxType", "SKYBOX_V14");
}
buildService.writeDataJson(message, new JSONObject(), dataMap, null);
}catch (Exception e){
scenePlusByNum.setSceneStatus(SceneStatus.FAILD.code());
scenePlusService.updateById(scenePlusByNum);
//修改重定位记录为退出计算
relocationBatchService.update(new LambdaUpdateWrapper().eq(RelocationBatch::getId, batchId).set(RelocationBatch::getStatus, 2));
buildSceneDTService.handBaseFail("深巡场景重定位资源准备异常!", message.getPath(), message.getSceneNum(), "计算控制服务器");
throw e;
}
}
@Override
public void downLoadSource(BuildSceneCallMessage buildSceneMqMessage,String path){
String num = buildSceneMqMessage.getSceneNum();
Long batchId = Long.valueOf(buildSceneMqMessage.getExt().get("batchId").toString());
//下载深巡缓存数据
String extrasPath = path + File.separator + "extras";
fYunFileService.downloadFileByCommand(extrasPath, String.format(UploadFilePath.scene_result_data_path, num) + "pose/");
String imagesPath = extrasPath + File.separator + "images";
//下载图片
String queryPath = imagesPath + File.separator + "query";
List detailList = relocationBatchDetailService.listByBatchId(batchId);
detailList.stream().forEach(v->{
fYunFileService.downloadFile(v.getBucket(), v.getImgKey(), queryPath + File.separator + v.getFileName());
});
//生成query.json
List imgNameList = FileUtil.listFileNames(queryPath);
List filenames = imgNameList.stream().map(v -> {
JSONObject a = new JSONObject();
a.put("filename", v);
return a;
}).collect(Collectors.toList());
JSONObject query = new JSONObject();
query.put("query", filenames);
FileUtil.writeUtf8String(query.toJSONString(), extrasPath + File.separator + "query.json");
}
@Override
public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
String sceneCode = message.getBuildContext().get("sceneNum").toString();
Long batchId = Long.valueOf(message.getExt().get("batchId").toString());
String path = message.getPath();
Map downParams = new HashMap<>();
downParams.put("sceneCode", sceneCode);
downParams.put("path", path);
try {
// 上传计算日志
//如果是重复计算,没有走到计算逻辑,不需要上传日志文件
log.info("开始上传计算日志");
String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, sceneCode);
fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
log.info("计算日志上传完成");
if (!message.getBuildSuccess()) {
log.error("重定位计算报错,修改状态为失败状态");
scenePlusService.update(new LambdaUpdateWrapper()
.set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
.eq(ScenePlus::getNum, sceneCode));
// 发送钉钉消息,计算失败
buildSceneDTService.handModelFail("深巡场景重定向计算失败", message.getPath(), sceneCode, message.getHostName());
return;
}
ScenePlus scenePlus = scenePlusService.getScenePlusByNum(sceneCode);
Map uploadFiles = this.getUploadFiles(scenePlus,path);
scenePlus.setPayStatus(PayStatus.PAY.code());
scenePlus.setUpdateTime(new Date());
scenePlus.setSceneStatus(SceneStatus.NO_DISPLAY.code());
ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
log.info("开始上传场景计算结果数据,num:{}", sceneCode);
//上传文件
fYunFileService.uploadMulFiles(uploadFiles);
//上传caches/images
String ossResultPath = String.format(UploadFilePath.scene_result_data_path, sceneCode);
String localCachesImagePath = path + "/caches/images/";
String ossCachesImagePath = ossResultPath + "caches/images/";
if(FileUtil.exist(localCachesImagePath)){
fYunFileService.uploadFileByCommand(localCachesImagePath, ossCachesImagePath);
}
//容量统计
Long space = commonService.getSpace(sceneCode);
Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
//如果相机容量不足,需要把场景的paystatus改为容量不足状态
scenePlus.setPayStatus(commonService.getPayStatus(scenePlus.getCameraId(), space, new JSONObject()));
this.uploadStatusJson(scenePlus, scenePlusExt);
scenePlusService.updateById(scenePlus);
scenePlusExtService.updateById(scenePlusExt);
//国际环境需要发邮件通知
if("eur".equals(env) &&
!scenePlus.getSceneSource().equals(SceneSource.JG.code()) &&
!scenePlus.getSceneSource().equals(SceneSource.SG.code())){
commonService.sendEmail(sceneCode, "relocation");
}
//发送消息到点云系统处理
downParams.put("status", CommonSuccessStatus.SUCCESS.code());
log.info("场景重定位计算结果处理结束,场景码:{}", sceneCode);
}catch (Exception e){
log.error("场景重定位计算结果处理出错,num"+sceneCode, e);
downParams.put("status", CommonSuccessStatus.FAIL.code());
buildSceneDTService.handBaseFail("场景重定位计算结果处理出错!", message.getPath(), sceneCode, "计算控制服务器");
throw e;
} finally {
//发送消息到激光系统做处理
mqProducer.sendByWorkQueue("sx-relocation-done", downParams);
//修改重定位记录为退出计算
relocationBatchService.update(new LambdaUpdateWrapper().eq(RelocationBatch::getId, batchId).set(RelocationBatch::getStatus, 2));
}
}
private Map getUploadFiles(ScenePlus scenePlus,String path) throws Exception {
if (ObjectUtils.isEmpty(scenePlus)) {
throw new Exception("未找到场景信息:" + path);
}
String projectNum = scenePlus.getNum();
String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, projectNum);
String imagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum);
String resultsPath = path + File.separator + "results" + File.separator;
String uploadData = FileUtils.readFile(resultsPath + "upload.json");
JSONArray array = JSONObject.parseObject(uploadData).getJSONArray("upload");
JSONObject fileJson = null;
String fileName = "";
Map map = new HashMap();
for (int i = 0; i < array.size(); ++i) {
fileJson = array.getJSONObject(i);
fileName = fileJson.getString("file");
String filePath = resultsPath + fileName;
if (!(new File(filePath)).exists()) {
throw new Exception(filePath + "文件不存在");
}
if ("vision2.txt".equals(fileName)) {
CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision2.txt", resultsPath + "vision2.modeldata");
map.put(resultsPath + "vision2.modeldata", imagesPath + "vision2.modeldata");
map.put(resultsPath + "vision2.txt", imagesPath + "vision2.txt");
}
if (fileJson.getIntValue("clazz") == 2) {
map.put(filePath, imagesPath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
} else if (fileJson.getIntValue("clazz") == 3) {
map.put(filePath, imagesPath + "pan/high/" + fileName.replace("high/", ""));
} else if (fileJson.getIntValue("clazz") == 4) {
map.put(filePath, imagesPath + "pan/low/" + fileName.replace("low/", ""));
} else if (fileJson.getIntValue("clazz") == 5) {
map.put(filePath, imagesPath + fileName);
} else if (fileJson.getIntValue("clazz") == 7) {
map.put(filePath, imagesPath + fileName);
} else if (fileJson.getIntValue("clazz") == 10) {
String updown = FileUtils.readFile(filePath);
JSONObject updownJson = JSONObject.parseObject(updown);
String mappingOssPath = String.format("scene_edit_data/%s/data/", projectNum) + fileName.replace("updown", "mapping");
map.put(filePath, mappingOssPath);
} else {
if (fileJson.getIntValue("clazz") == 11 || fileJson.getIntValue("clazz") == 12) {
map.put(filePath, videoPath + fileName.replace("videos/", ""));
if (fileName.contains(".mp4")) {
map.put(resultsPath + fileName.replace("mp4", "flv"), videoPath + fileName.replace("videos/", "").replace("mp4", "flv"));
}
}
if (fileJson.getIntValue("clazz") == 16) {
map.put(filePath, dataViewPath + fileName);
}
if (fileJson.getIntValue("clazz") == 18) {
map.put(filePath, imagesPath + fileName);
}
}
}
CreateObjUtil.convertTxtToVisionmodeldata(resultsPath + "vision.txt", resultsPath + "vision.modeldata");
map.put(resultsPath + "vision.txt", imagesPath + "vision.txt");
map.put(resultsPath + "vision.modeldata", imagesPath + "vision.modeldata");
return map;
}
private void uploadStatusJson(ScenePlus scenePlus, ScenePlusExt scenePlusExt){
String num = scenePlus.getNum();
String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
Integer status = 1;
// 上传status JSON.
JSONObject statusJson = new JSONObject();
//临时将-2改成1,app还没完全更新
statusJson.put("status", status);
statusJson.put("webSite", scenePlusExt.getWebSite());
statusJson.put("sceneNum", num);
statusJson.put("thumb", scenePlusExt.getThumb());
statusJson.put("payStatus", scenePlus.getPayStatus());
statusJson.put("sceneScheme", scenePlusExt.getSceneScheme());
FileUtils.writeFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json", statusJson.toString());
fYunFileService.uploadFile(statusJson.toJSONString().getBytes(StandardCharsets.UTF_8), dataViewPath + "status.json");
}
}