SceneProAppServiceImpl.java 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.fdkankan.model.constants.ConstantFileName;
  7. import com.fdkankan.model.constants.ConstantFilePath;
  8. import com.fdkankan.common.constant.ConstantUrl;
  9. import com.fdkankan.common.constant.ErrorCode;
  10. import com.fdkankan.common.exception.BusinessException;
  11. import com.fdkankan.common.response.ResultData;
  12. import com.fdkankan.model.utils.CreateObjUtil;
  13. import com.fdkankan.common.util.FileUtils;
  14. import com.fdkankan.common.util.OkHttpUtils;
  15. import com.fdkankan.common.util.WeiXinUtils;
  16. import com.fdkankan.fyun.constant.FYunTypeEnum;
  17. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  18. import com.fdkankan.scene.bean.RequestSceneEdit;
  19. import com.fdkankan.scene.entity.ScenePro;
  20. import com.fdkankan.scene.entity.SceneProEdit;
  21. import com.fdkankan.scene.mapper.ISceneProMapper;
  22. import com.fdkankan.scene.service.ISceneProAppService;
  23. import com.fdkankan.scene.service.ISceneProEditService;
  24. import com.fdkankan.scene.service.ISceneProService;
  25. import java.io.File;
  26. import java.io.InputStream;
  27. import java.io.PrintWriter;
  28. import java.io.StringWriter;
  29. import java.net.HttpURLConnection;
  30. import java.net.URL;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import java.util.Set;
  34. import lombok.extern.slf4j.Slf4j;
  35. import net.coobird.thumbnailator.Thumbnails;
  36. import org.apache.commons.lang3.StringUtils;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.beans.factory.annotation.Value;
  39. import org.springframework.stereotype.Service;
  40. import org.springframework.web.multipart.MultipartFile;
  41. /**
  42. * Created by Hb_zzZ on 2019/6/28.
  43. */
  44. @Slf4j
  45. @Service
  46. public class SceneProAppServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro> implements ISceneProAppService {
  47. @Autowired
  48. private ISceneProService sceneProService;
  49. @Autowired
  50. private ISceneProEditService sceneProEditService;
  51. @Autowired
  52. private FYunFileServiceInterface fYunFileService;
  53. @Value("${hot.domain.list}")
  54. private String hotDomainList;
  55. @Value("${hot.delete}")
  56. private String hotDelete;
  57. @Value("${fyun.host}")
  58. private String ossPrefixUrl;
  59. @Value("fyun.type")
  60. private String ossType;
  61. @Override
  62. public ResultData uploadFloorplanPng(String sceneNum, MultipartFile file, String cadInfo) throws Exception {
  63. ScenePro sceneProEntity = sceneProService.findBySceneNum(sceneNum);
  64. if(sceneProEntity == null){
  65. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  66. }
  67. if (file.isEmpty() && file.getSize() <= 0) {
  68. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  69. }
  70. StringBuffer dataBuf = new StringBuffer()
  71. .append("data").append(File.separator)
  72. .append("data").append(sceneProEntity.getNum())
  73. .append(File.separator);
  74. StringBuffer imagesBuf = new StringBuffer()
  75. .append("images").append(File.separator)
  76. .append("images").append(sceneProEntity.getNum())
  77. .append(File.separator);
  78. StringBuffer imagesBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(imagesBuf.toString());
  79. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  80. //文件上传的位置可以自定义
  81. String path = ConstantFilePath.SCENE_PATH+"images"+File.separator+"images"+sceneNum;
  82. File targetFile = new File(path);
  83. if (!targetFile.exists()) {
  84. targetFile.mkdirs();
  85. }
  86. String fileName = "floorplan.png";
  87. targetFile = new File(path + File.separator + fileName);
  88. // 保存
  89. synchronized(this)
  90. {
  91. if(targetFile.exists())
  92. {
  93. FileUtils.deleteFile(path + File.separator + fileName);
  94. }
  95. file.transferTo(targetFile);
  96. }
  97. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  98. String floorPlanPng = "images"+File.separator+"images"+sceneNum + File.separator + fileName;
  99. Map<String, Object> map = new HashMap<>();
  100. map.put("floorPlanPng", floorPlanPng);
  101. map.put("uploadFloorPlanPng", 1);
  102. map.put("version", sceneProEditEntity.getVersion()+1);
  103. if(StrUtil.isNotEmpty(cadInfo)){
  104. map.put("cadInfo", cadInfo);
  105. sceneProEditEntity.setCadInfo(cadInfo);
  106. }
  107. FileUtils.writeJsonFile(dataBuffer.toString() + "scene.json", map);
  108. sceneProEditEntity.setFloorPlanPng(floorPlanPng + "?t=" + System.currentTimeMillis());
  109. sceneProEditEntity.setVersion(sceneProEditEntity.getVersion() + 1);
  110. Map<String, Object> oss = new HashMap<>();
  111. fYunFileService.uploadFile(ConstantFilePath.SCENE_PATH+floorPlanPng, floorPlanPng);
  112. sceneProEditService.updateById(sceneProEditEntity);
  113. return ResultData.ok("images"+File.separator+"images"+sceneNum + File.separator + fileName);
  114. }
  115. @Override
  116. public ResultData uploadFloorJsonAndRebuild(RequestSceneEdit sceneEdit) throws Exception {
  117. String lock = sceneEdit.getSceneNum().toString().intern();
  118. synchronized(lock){
  119. long start = System.currentTimeMillis();
  120. log.info("画墙重建模型开始时间:" + start);
  121. if(StringUtils.isEmpty(sceneEdit.getSceneNum())){
  122. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  123. }
  124. ScenePro sceneProEntity = sceneProService.findBySceneNum(sceneEdit.getSceneNum());
  125. if(sceneProEntity == null){
  126. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  127. }
  128. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  129. if(sceneProEditEntity == null){
  130. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  131. }
  132. //更新scene.json文件
  133. String strsceneInfos = FileUtils.readFile(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + sceneEdit.getSceneNum() + File.separator + "scene.json");
  134. JSONObject scenejson = new JSONObject();
  135. if(strsceneInfos!=null)
  136. {
  137. scenejson = JSONObject.parseObject(strsceneInfos);
  138. }else {
  139. new File(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + sceneEdit.getSceneNum() + File.separator + "scene.json").createNewFile();
  140. }
  141. if(!StringUtils.isEmpty(sceneEdit.getSceneData())){
  142. JSONObject sceneObject = JSONObject.parseObject(sceneEdit.getSceneData());
  143. Set<String> set =sceneObject.keySet();
  144. for(String key : set){
  145. scenejson.put(key, sceneObject.get(key));
  146. }
  147. }
  148. //保存floor.json前端下次进入需要使用
  149. if(StringUtils.isEmpty(sceneEdit.getWebFloor())){
  150. sceneEdit.setWebFloor(sceneEdit.getFloor());
  151. }
  152. // //上传过模型只更新floor.json
  153. // if(scenejson.containsKey("isUploadObj") && scenejson.getBooleanValue("isUploadObj")){
  154. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+sceneEdit.getSceneNum() + File.separator + "floor.json", new String(sceneEdit.getWebFloor().getBytes(), "UTF-8"));
  155. //更新scene.json文件
  156. if(strsceneInfos!=null)
  157. {
  158. scenejson.put("floorEditVer", sceneProEditEntity.getFloorPublishVer() + 1);
  159. scenejson.put("floorPublishVer", sceneProEditEntity.getFloorPublishVer() + 1);
  160. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+sceneEdit.getSceneNum()+File.separator+"scene.json", scenejson.toString());
  161. log.info("写入scene.json文件完成——"+sceneEdit.getSceneNum());
  162. }
  163. //floorEditVer字段增加1
  164. sceneProEditEntity.setFloorEditVer(sceneProEditEntity.getFloorPublishVer() + 1);
  165. sceneProEditEntity.setFloorPublishVer(sceneProEditEntity.getFloorPublishVer() + 1);
  166. sceneProEditService.updateById(sceneProEditEntity);
  167. fYunFileService.uploadFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+sceneEdit.getSceneNum() + File.separator + "floor.json",
  168. "data"+File.separator+"data"+sceneEdit.getSceneNum() + File.separator + "floor.json");
  169. }
  170. return ResultData.ok();
  171. }
  172. @Override
  173. public ResultData deleteRecordAudioPart(String sceneNum, String fileName, String times, String index, String type) throws Exception {
  174. if(StrUtil.isEmpty(sceneNum)){
  175. throw new BusinessException(ErrorCode.FAILURE_CODE_3001 );
  176. }
  177. ScenePro sceneProEntity = sceneProService.findBySceneNum(sceneNum);
  178. if(sceneProEntity == null){
  179. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  180. }
  181. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  182. //文件上传的位置可以自定义
  183. String path = ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+sceneNum;
  184. File targetFile = new File(path);
  185. if (!targetFile.exists()) {
  186. targetFile.mkdirs();
  187. }
  188. //默认为app文件名
  189. String originalFileName = ConstantFileName.APP_VOICE_NAME + ".mp3";
  190. if("wechat".equals(type)){
  191. originalFileName = ConstantFileName.WECHAT_VOICE_NAME + ".mp3";
  192. }
  193. log.info("上传的音频文件名:" + originalFileName);
  194. String strsceneInfos = FileUtils.readFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json");
  195. JSONObject scenejson = new JSONObject();
  196. if(strsceneInfos!=null) {
  197. scenejson = JSONObject.parseObject(strsceneInfos);
  198. }
  199. //判断分房间模块文件夹是否存在
  200. String partPath = path + File.separator + "part";
  201. File partFile = new File(partPath);
  202. if(!partFile.exists()){
  203. partFile.mkdirs();
  204. }
  205. //删除指定部分音频文件
  206. FileUtils.deleteFile(partPath + File.separator + index + ".mp3");
  207. //获取总音频多少段,每段时长
  208. String[] time = times.split(",");
  209. //删除所有音频
  210. if(StringUtils.isEmpty(times) || time.length == 0){
  211. Map map = new HashMap();
  212. map.put("screencapVoiceSoundsyncFileName", "");
  213. map.put("screencapVoiceSoundsync", "");
  214. map.put("uploadVoiceSoundsync", 0);
  215. map.put("screencapVoiceType", "");
  216. map.put("version", scenejson.getIntValue("version")+1);
  217. sceneProEditEntity.setScreencapVoiceType("");
  218. sceneProEditEntity.setScreencapVoiceSoundsync("");
  219. sceneProEditEntity.setVersion(scenejson.getIntValue("version")+1);
  220. FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
  221. sceneProEditService.updateById(sceneProEditEntity);
  222. return ResultData.ok();
  223. }
  224. //修改删除的部分视频后面部分音频名字,如删除1.MP3,则将2.mp3修改成1.mp3,往后以此类推
  225. if(Integer.parseInt(index) < time.length) {
  226. for(int i = 0, len = time.length - Integer.parseInt(index); i < len; i++){
  227. if(new File(partPath + File.separator + (Integer.parseInt(index) + 1 + i) + ".mp3").exists()){
  228. FileUtils.copyFile(partPath + File.separator + (Integer.parseInt(index) + 1 + i) + ".mp3",
  229. partPath + File.separator + (Integer.parseInt(index) + i) + ".mp3", true);
  230. }
  231. }
  232. }
  233. //遍历判断音频是否存在,不存在生成空白音效
  234. for(int i = 0, len = time.length; i < len; i++){
  235. if(!new File(partPath + File.separator + i + ".mp3").exists()){
  236. //某部分文件不存在,直接生成一段静音后拼接文件
  237. CreateObjUtil.createMuteViode(Double.valueOf(time[i]), partPath + File.separator + i + ".mp3");
  238. }
  239. }
  240. //拼接所有音频文件
  241. if(time.length > 2){
  242. //若是多部分,两个两个合并,最后合成最终文件
  243. for(int i = 1, len = time.length; i < len; i++){
  244. if(i == 1){
  245. // FileUtils.deleteFile(partPath + File.separator + i + "muteSound.mp3");
  246. CreateObjUtil.mergeVideo(partPath + File.separator + (i - 1) + ".mp3", partPath + File.separator + i + ".mp3",
  247. partPath + File.separator + i + "muteSound.mp3");
  248. }else if(i == len - 1){
  249. CreateObjUtil.mergeVideo(partPath + File.separator + (i - 1) + "muteSound.mp3", partPath + File.separator + i + ".mp3",
  250. path + File.separator + originalFileName);
  251. }else {
  252. CreateObjUtil.mergeVideo(partPath + File.separator + (i - 1) + "muteSound.mp3", partPath + File.separator + i + ".mp3",
  253. partPath + File.separator + i + "muteSound.mp3");
  254. }
  255. }
  256. }else if(time.length == 2){
  257. //若只有两部分,直接合并成最终文件
  258. CreateObjUtil.mergeVideo(partPath + File.separator + "0.mp3", partPath + File.separator + "1.mp3", path + File.separator + originalFileName);
  259. }else {
  260. FileUtils.copyFile(partPath + File.separator + "0.mp3", path + File.separator + originalFileName, true);
  261. }
  262. fYunFileService.uploadFile(path+File.separator +originalFileName, "voice/voice"+sceneNum+"/"+originalFileName);
  263. String voiceSrc = ossPrefixUrl+"voice/voice"+sceneNum+"/"+originalFileName;
  264. Map map = new HashMap();
  265. map.put("screencapVoiceSoundsyncFileName", originalFileName);
  266. map.put("screencapVoiceSoundsync", voiceSrc);
  267. map.put("uploadVoiceSoundsync", 0);
  268. map.put("screencapVoiceType", "soundsync");
  269. map.put("version", scenejson.getIntValue("version")+1);
  270. sceneProEditEntity.setScreencapVoiceType("soundsync");
  271. sceneProEditEntity.setScreencapVoiceSoundsync(voiceSrc);
  272. sceneProEditEntity.setVersion(scenejson.getIntValue("version")+1);
  273. FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
  274. sceneProEditService.updateById(sceneProEditEntity);
  275. return ResultData.ok(voiceSrc);
  276. }
  277. @Override
  278. public ResultData getRecordAudioFromWeixin(RequestSceneEdit base) throws Exception{
  279. if(StrUtil.isEmpty(base.getSceneNum()) || StrUtil.isEmpty(base.getId())){
  280. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  281. }
  282. ScenePro sceneProEntity = sceneProService.findBySceneNum(base.getSceneNum());
  283. if(sceneProEntity == null){
  284. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  285. }
  286. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  287. String accessToken = WeiXinUtils.getAccessToken();
  288. InputStream is = null;
  289. String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="+ accessToken + "&media_id=" + base.getId();
  290. URL urlGet = new URL(url);
  291. HttpURLConnection http = (HttpURLConnection) urlGet
  292. .openConnection();
  293. http.setRequestMethod("GET"); // 必须是get方式请求
  294. http.setRequestProperty("Content-Type",
  295. "application/x-www-form-urlencoded");
  296. http.setDoOutput(true);
  297. http.setDoInput(true);
  298. System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
  299. System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
  300. http.connect();
  301. // 获取文件转化为byte流
  302. is = http.getInputStream();
  303. File dic = new File(ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum());
  304. if(!dic.exists())
  305. {
  306. dic.mkdirs();
  307. }
  308. File voice = new File(ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum()+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr");
  309. if(voice.exists())
  310. {
  311. FileUtils.deleteFile(ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum()+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr");
  312. }
  313. FileUtils.saveImageToDisk( accessToken, base.getId(), ConstantFileName.WECHAT_VOICE_NAME, ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum()+File.separator, is);
  314. try
  315. {
  316. FileUtils.changeVoiceToMp3(ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum()+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr",
  317. ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum()+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".mp3");
  318. }
  319. catch(Exception e)
  320. {
  321. StringWriter trace=new StringWriter();
  322. e.printStackTrace(new PrintWriter(trace));
  323. log.error("voice:"+ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum()+File.separator+ConstantFileName.VOICE_NAME+".amr");
  324. log.error(trace.toString());
  325. return ResultData.error(ErrorCode.FAILURE_CODE_5005);
  326. }
  327. String originalFileName = ConstantFileName.WECHAT_VOICE_NAME+".mp3";
  328. fYunFileService.uploadFile(ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum()+File.separator +originalFileName, "voice/voice"+base.getSceneNum()+"/"+originalFileName);
  329. Map map = new HashMap();
  330. map.put("screencapVoiceSoundsyncFileName", originalFileName);
  331. String voiceSrc = ossPrefixUrl+"voice/voice"+base.getSceneNum()+"/"+originalFileName;
  332. map.put("screencapVoiceSoundsync", voiceSrc);
  333. log.info("微信上传的音频路径:" + voiceSrc);
  334. map.put("screencapVoiceType", "soundsync");
  335. map.put("version", sceneProEditEntity.getVersion()+1);
  336. FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + base.getSceneNum() + "/scene.json", map);
  337. sceneProEditEntity.setScreencapVoiceType("soundsync");
  338. sceneProEditEntity.setScreencapVoiceSoundsync(voiceSrc);
  339. sceneProEditEntity.setVersion(sceneProEditEntity.getVersion());
  340. sceneProEditService.updateById(sceneProEditEntity);
  341. FileUtils.deleteFile(ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+base.getSceneNum()+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr");
  342. if(FYunTypeEnum.AWS.code().equals(ossType)){
  343. return ResultData.ok(ConstantUrl.PREFIX_AWS+"voice/voice"+base.getSceneNum()+"/"+originalFileName);
  344. }
  345. return ResultData.ok(ossPrefixUrl+"voice/voice"+base.getSceneNum()+"/"+originalFileName);
  346. }
  347. @Override
  348. public ResultData uploadPic(String sceneNum, String folderName, MultipartFile file) throws Exception{
  349. if(StrUtil.isEmpty(sceneNum)){
  350. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  351. }
  352. ScenePro sceneProEntity = sceneProService.findBySceneNum(sceneNum);
  353. if(sceneProEntity == null){
  354. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  355. }
  356. if (file.isEmpty() && file.getSize() <= 0) {
  357. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  358. }
  359. //文件上传的位置可以自定义
  360. String path = ConstantFilePath.SCENE_PATH+"images"+File.separator+"images"+sceneNum;
  361. if(StrUtil.isNotEmpty(folderName)) {
  362. path = path+File.separator + folderName;
  363. }
  364. File targetFile = new File(path);
  365. if (!targetFile.exists()) {
  366. targetFile.mkdirs();
  367. }
  368. String fileName = file.getOriginalFilename();
  369. targetFile = new File(path + File.separator + fileName);
  370. // 保存
  371. synchronized(this)
  372. {
  373. if(targetFile.exists())
  374. {
  375. FileUtils.deleteFile(path + File.separator + fileName);
  376. }
  377. file.transferTo(targetFile);
  378. }
  379. return ResultData.ok(targetFile.getName());
  380. }
  381. @Override
  382. public ResultData saveSceneInfo(RequestSceneEdit base) throws Exception{
  383. if(StrUtil.isEmpty(base.getSceneNum()) || StrUtil.isEmpty(base.getSceneName()) ||
  384. StrUtil.isEmpty(base.getSceneType()) || StrUtil.isEmpty(base.getSceneDec())){
  385. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  386. }
  387. ScenePro sceneProEntity = sceneProService.findBySceneNum(base.getSceneNum());
  388. if(sceneProEntity == null){
  389. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  390. }
  391. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  392. StringBuffer dataBuf = new StringBuffer()
  393. .append("data").append(File.separator)
  394. .append("data").append(sceneProEntity.getNum())
  395. .append(File.separator);
  396. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  397. Map map = new HashMap();
  398. map.put("sceneName", base.getSceneName());
  399. map.put("sceneDec", base.getSceneDec());
  400. if(StrUtil.isNotEmpty(base.getSceneType())){
  401. if(sceneProEntity.getSceneType() == 99){
  402. map.put("sceneType", sceneProEntity.getSceneType());
  403. }else {
  404. sceneProEntity.setSceneType(Integer.parseInt(base.getSceneType()));
  405. map.put("sceneType", base.getSceneType());
  406. }
  407. }
  408. if(!StringUtils.isEmpty(base.getSceneKey())) {
  409. map.put("sceneKey", base.getSceneKey());
  410. map.put("needKey", 1);
  411. sceneProEditEntity.setNeedKey(1);
  412. }else {
  413. sceneProEditEntity.setNeedKey(0);
  414. map.put("sceneKey", "");
  415. map.put("needKey", 0);
  416. }
  417. map.put("version", sceneProEditEntity.getVersion()+1);
  418. FileUtils.writeJsonFile(dataBuffer.toString() + "scene.json", map);
  419. sceneProEntity.setSceneName(base.getSceneName());
  420. sceneProEntity.setSceneDec(base.getSceneDec());
  421. sceneProService.updateById(sceneProEntity);
  422. sceneProEditEntity.setSceneKey(base.getSceneKey());
  423. sceneProEditEntity.setVersion(sceneProEditEntity.getVersion() + 1);
  424. sceneProEditService.updateById(sceneProEditEntity);
  425. return ResultData.ok();
  426. }
  427. public ResultData saveAllVisi(RequestSceneEdit base) throws Exception {
  428. if(StrUtil.isEmpty(base.getSceneNum())){
  429. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  430. }
  431. ScenePro sceneProEntity = sceneProService.findBySceneNum(base.getSceneNum());
  432. if(sceneProEntity == null){
  433. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  434. }
  435. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  436. StringBuffer dataBuf = new StringBuffer()
  437. .append("data").append(File.separator)
  438. .append("data").append(sceneProEntity.getNum())
  439. .append(File.separator);
  440. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  441. String strsceneInfos = FileUtils.readFile(dataBuffer.toString() + "scene.json");
  442. JSONObject scenejson = new JSONObject();
  443. if(strsceneInfos!=null) {
  444. scenejson = JSONObject.parseObject(strsceneInfos);
  445. }
  446. if(!StringUtils.isEmpty(base.getMapVisi())){
  447. scenejson.put("mapVisi", Integer.parseInt(base.getMapVisi()));
  448. sceneProEditEntity.setMapVisi(Integer.parseInt(base.getMapVisi()));
  449. }
  450. if(!StringUtils.isEmpty(base.getTourVisi())){
  451. scenejson.put("tourVisi", Integer.parseInt(base.getTourVisi()));
  452. sceneProEditEntity.setTourVisi(Integer.parseInt(base.getTourVisi()));
  453. }
  454. if(!StringUtils.isEmpty(base.getVrVisi())){
  455. scenejson.put("vrVisi", Integer.parseInt(base.getVrVisi()));
  456. sceneProEditEntity.setVrVisi(Integer.parseInt(base.getVrVisi()));
  457. }
  458. if(!StringUtils.isEmpty(base.getRulerVisi())){
  459. scenejson.put("rulerVisi", Integer.parseInt(base.getRulerVisi()));
  460. sceneProEditEntity.setRulerVisi(Integer.parseInt(base.getRulerVisi()));
  461. }
  462. if(!StringUtils.isEmpty(base.getCadImgVisi())){
  463. scenejson.put("cadImgVisi", Integer.parseInt(base.getCadImgVisi()));
  464. sceneProEditEntity.setCadImgVisi(Integer.parseInt(base.getCadImgVisi()));
  465. }
  466. if(!StringUtils.isEmpty(base.getPanoVisi())){
  467. scenejson.put("panoVisi", Integer.parseInt(base.getPanoVisi()));
  468. sceneProEditEntity.setPanoVisi(Integer.parseInt(base.getPanoVisi()));
  469. }
  470. if(!StringUtils.isEmpty(base.getM2dVisi())){
  471. scenejson.put("m2dVisi", Integer.parseInt(base.getM2dVisi()));
  472. sceneProEditEntity.setM2dVisi(Integer.parseInt(base.getM2dVisi()));
  473. }
  474. if(!StringUtils.isEmpty(base.getM3dVisi())){
  475. scenejson.put("m3dVisi", Integer.parseInt(base.getM3dVisi()));
  476. sceneProEditEntity.setM3dVisi(Integer.parseInt(base.getM3dVisi()));
  477. }
  478. if(!StringUtils.isEmpty(base.getMeasureVisi())){
  479. scenejson.put("measureVisi", Integer.parseInt(base.getMeasureVisi()));
  480. sceneProEditEntity.setMeasureVisi(Integer.parseInt(base.getMeasureVisi()));
  481. }
  482. sceneProEditEntity.setVersion(scenejson.getIntValue("version") + 1);
  483. scenejson.put("version", scenejson.getIntValue("version") + 1);
  484. FileUtils.writeFile(dataBuffer.toString() + "scene.json", scenejson.toString());
  485. sceneProEditService.updateById(sceneProEditEntity);
  486. return ResultData.ok();
  487. }
  488. @Override
  489. public ResultData saveInitialPage(RequestSceneEdit base) throws Exception {
  490. if(StringUtils.isEmpty(base.getSceneNum()) || StringUtils.isEmpty(base.getInitialPoint())){
  491. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  492. }
  493. ScenePro sceneProEntity = sceneProService.findBySceneNum(base.getSceneNum());
  494. if(sceneProEntity == null){
  495. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  496. }
  497. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  498. StringBuffer dataBuf = new StringBuffer()
  499. .append("data").append(File.separator)
  500. .append("data").append(sceneProEntity.getNum())
  501. .append(File.separator);
  502. StringBuffer imagesBuf = new StringBuffer()
  503. .append("images").append(File.separator)
  504. .append("images").append(sceneProEntity.getNum())
  505. .append(File.separator);
  506. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  507. StringBuffer imagesBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(imagesBuf.toString());
  508. String path1 = imagesBuffer.toString() + "thumbBigImg.jpg";
  509. String path2 = imagesBuffer.toString() + "thumbSmallImg.jpg";
  510. //生成缩略图
  511. //按指定大小把图片进行缩和放(会遵循原图高宽比例)
  512. //此处把图片压成1024×512的缩略图
  513. Thumbnails.of(path1).size(1024,512).toFile(path2);//变为1024×512
  514. Map map = new HashMap();
  515. map.put("entry", JSONObject.parseObject(base.getInitialPoint()));
  516. map.put("thumbImg", 1);
  517. map.put("version", sceneProEditEntity.getVersion()+1);
  518. FileUtils.writeJsonFile(dataBuffer.toString() + "scene.json", map);
  519. Map<String,String> uploadMap = new HashMap<String,String>();
  520. uploadMap.put(imagesBuffer.toString() + "thumbBigImg.jpg", imagesBuf.toString() + "thumbBigImg.jpg");
  521. uploadMap.put(imagesBuffer.toString() + "thumbFishBigImg.jpg", imagesBuf.toString() + "thumbFishBigImg.jpg");
  522. uploadMap.put(imagesBuffer.toString() + "thumbSmallImg.jpg", imagesBuf.toString() + "thumbSmallImg.jpg");
  523. uploadMap.put(imagesBuffer.toString() + "smallPic.jpg", imagesBuf.toString() + "smallPic.jpg");
  524. uploadMap.put(dataBuffer.toString() + "scene.json", dataBuf.toString() + "scene.json");
  525. fYunFileService.uploadMulFiles(uploadMap);
  526. sceneProEditEntity.setEntry(base.getInitialPoint());
  527. sceneProEditEntity.setThumbStatus(1);
  528. sceneProEditEntity.setVersion(sceneProEditEntity.getVersion() + 1);
  529. sceneProEditService.updateById(sceneProEditEntity);
  530. sceneProEntity.setThumb(ossPrefixUrl + imagesBuf.toString() + "thumbSmallImg.jpg");
  531. sceneProService.updateById(sceneProEntity);
  532. return ResultData.ok();
  533. }
  534. @Override
  535. public ResultData saveBackgroundMusic(RequestSceneEdit base) throws Exception {
  536. if(StringUtils.isEmpty(base.getSceneNum()) || StringUtils.isEmpty(base.getMusicName())){
  537. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  538. }
  539. ScenePro sceneProEntity = sceneProService.findBySceneNum(base.getSceneNum());
  540. if(sceneProEntity == null){
  541. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  542. }
  543. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  544. StringBuffer dataBuf = new StringBuffer()
  545. .append("data").append(File.separator)
  546. .append("data").append(sceneProEntity.getNum())
  547. .append(File.separator);
  548. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  549. Map map = new HashMap();
  550. map.put("bgMusic", base.getMusicName());
  551. map.put("version", sceneProEditEntity.getVersion()+1);
  552. FileUtils.writeJsonFile(dataBuffer.toString() + "scene.json", map);
  553. sceneProEditEntity.setBgMusic(base.getMusicName());
  554. sceneProEditEntity.setVersion(sceneProEditEntity.getVersion() + 1);
  555. sceneProEditService.updateById(sceneProEditEntity);
  556. return ResultData.ok();
  557. }
  558. @Override
  559. public ResultData saveFloorLogo(RequestSceneEdit base) throws Exception {
  560. if(StrUtil.isEmpty(base.getSceneNum()) || StrUtil.isEmpty(base.getSize()) ||
  561. StrUtil.isEmpty(base.getLogoType())){
  562. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  563. }
  564. ScenePro sceneProEntity = sceneProService.findBySceneNum(base.getSceneNum());
  565. if(sceneProEntity == null){
  566. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  567. }
  568. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  569. StringBuffer dataBuf = new StringBuffer()
  570. .append("data").append(File.separator)
  571. .append("data").append(sceneProEntity.getNum())
  572. .append(File.separator);
  573. StringBuffer imagesBuf = new StringBuffer()
  574. .append("images").append(File.separator)
  575. .append("images").append(sceneProEntity.getNum())
  576. .append(File.separator);
  577. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  578. StringBuffer imagesBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(imagesBuf.toString());
  579. Map map = new HashMap();
  580. map.put("floorLogo", base.getLogoType());
  581. map.put("floorLogoSize", base.getSize());
  582. map.put("version", sceneProEditEntity.getVersion()+1);
  583. FileUtils.writeJsonFile(dataBuffer.toString() + "scene.json", map);
  584. sceneProEditEntity.setFloorLogo(base.getLogoType());
  585. sceneProEditEntity.setFloorLogoSize(Integer.parseInt(base.getSize()));
  586. sceneProEditEntity.setVersion(sceneProEditEntity.getVersion() + 1);
  587. sceneProEditService.updateById(sceneProEditEntity);
  588. fYunFileService.uploadFile(imagesBuffer.toString() + "floorLogoImg.png", imagesBuf.toString() + "floorLogoImg.png");
  589. fYunFileService.uploadFile(dataBuffer.toString() + "scene.json", dataBuf.toString() + "scene.json");
  590. return ResultData.ok();
  591. }
  592. @Override
  593. public ResultData saveScreencapData(RequestSceneEdit base) throws Exception {
  594. if(StrUtil.isEmpty(base.getSceneNum())){
  595. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  596. }
  597. ScenePro sceneProEntity = sceneProService.findBySceneNum(base.getSceneNum());
  598. if(sceneProEntity == null){
  599. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  600. }
  601. StringBuffer dataBuf = new StringBuffer()
  602. .append("data").append(File.separator)
  603. .append("data").append(sceneProEntity.getNum())
  604. .append(File.separator);
  605. StringBuffer imagesBuf = new StringBuffer()
  606. .append("images").append(File.separator)
  607. .append("images").append(sceneProEntity.getNum())
  608. .append(File.separator);
  609. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  610. StringBuffer imagesBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(imagesBuf.toString());
  611. String strsceneInfos = FileUtils.readFile(dataBuffer.toString() + "scene.json");
  612. JSONObject scenejson = new JSONObject();
  613. if(strsceneInfos!=null) {
  614. scenejson = JSONObject.parseObject(strsceneInfos);
  615. }
  616. //上传七牛
  617. Map<String,String> map = new HashMap<String,String>();
  618. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  619. scenejson.put("screencapVoiceSound", "");
  620. sceneProEditEntity.setScreencapVoiceSound("");
  621. if(base.getScreencapVoiceType() != null){
  622. scenejson.put("screencapVoiceType", base.getScreencapVoiceType());
  623. sceneProEditEntity.setScreencapVoiceType(base.getScreencapVoiceType());
  624. }
  625. if(!StringUtils.isEmpty(base.getCapData())){
  626. FileUtils.writeFile(dataBuffer.toString() + "capData.json", new String(base.getCapData().getBytes(), "UTF-8"));
  627. scenejson.put("capData", dataBuf.toString() + "capData.json");
  628. }
  629. if(!StringUtils.isEmpty(base.getFrameData())){
  630. FileUtils.writeFile(dataBuffer.toString() + "frameData.json", new String(base.getFrameData().getBytes(), "UTF-8"));
  631. scenejson.put("frameData", dataBuf.toString() + "frameData.json");
  632. }
  633. if(!StringUtils.isEmpty(base.getPlayData())){
  634. FileUtils.writeFile(dataBuffer.toString() + "playData.json", new String(base.getPlayData().getBytes(), "UTF-8"));
  635. scenejson.put("playData", dataBuf.toString() + "playData.json");
  636. sceneProEditEntity.setPlayData(dataBuf.toString() + "playData.json");
  637. map.put(dataBuffer.toString() + "playData.json", dataBuf.toString() + "playData.json");
  638. }
  639. if(!StringUtils.isEmpty(base.getScreencapThumb())){
  640. FileUtils.writeFile(dataBuffer.toString() + "screencapThumb.json", new String(base.getScreencapThumb().getBytes(), "UTF-8"));
  641. scenejson.put("screencapThumb", dataBuf.toString() + "screencapThumb.json");
  642. sceneProEditEntity.setScreencapThumb(dataBuf.toString() + "screencapThumb.json");
  643. map.put(dataBuffer.toString() + "screencapThumb.json", dataBuf.toString() + "screencapThumb.json");
  644. }
  645. if(!StringUtils.isEmpty(base.getRecordType())){
  646. scenejson.put("recordType", base.getRecordType());
  647. }
  648. if(map.size()>0) {
  649. fYunFileService.uploadMulFiles(map);
  650. }
  651. sceneProEditEntity.setVersion(scenejson.getIntValue("version") + 1);
  652. sceneProEditService.updateById(sceneProEditEntity);
  653. log.info("scene.json路径:" + dataBuffer.toString() + "scene.json");
  654. scenejson.put("version", scenejson.getIntValue("version") + 1);
  655. FileUtils.writeFile(dataBuffer.toString() + "scene.json", scenejson.toString());
  656. return ResultData.ok();
  657. }
  658. @Override
  659. public ResultData saveHot(RequestSceneEdit base) throws Exception {
  660. if(StringUtils.isEmpty(base.getType())){
  661. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  662. }
  663. String sid = base.getSid();
  664. ScenePro sceneProEntity = sceneProService.findBySceneNum(base.getSceneNum());
  665. if (sceneProEntity == null ) {
  666. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  667. }
  668. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  669. log.info("hotData:" + base.getHotData());
  670. JSONObject jsonhot = JSONObject.parseObject(base.getHotData());
  671. StringBuffer dataBuf = new StringBuffer()
  672. .append("data").append(File.separator)
  673. .append("data").append(sceneProEntity.getNum())
  674. .append(File.separator);
  675. StringBuffer imagesBuf = new StringBuffer()
  676. .append("images").append(File.separator)
  677. .append("images").append(sceneProEntity.getNum())
  678. .append(File.separator);
  679. StringBuffer imagesBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(imagesBuf.toString());
  680. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  681. String str = FileUtils.readFile(dataBuffer.toString() + "hot.json");
  682. JSONArray jsonhots = null;
  683. if (StringUtils.isNotEmpty(str)) {
  684. jsonhots = JSONArray.parseArray(str);
  685. }else {
  686. File file = new File(dataBuffer.toString() + "hot.json");
  687. if(!file.getParentFile().exists()){
  688. file.getParentFile().mkdirs();
  689. }
  690. if(!file.exists()){
  691. file.createNewFile();
  692. }
  693. jsonhots = new JSONArray();
  694. }
  695. String hotsids = "";
  696. //添加或者修改
  697. if("1".equals(base.getType())){
  698. sid = jsonhot.getString("sid");
  699. if(StrUtil.isEmpty(sid)){
  700. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  701. }
  702. jsonhots.add(jsonhot);
  703. }
  704. else if("0".equals(base.getType())){
  705. sid = jsonhot.getString("sid");
  706. if(StrUtil.isEmpty(sid)){
  707. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  708. }
  709. }
  710. else if("-1".equals(base.getType())){
  711. if(StrUtil.isEmpty(sid)){
  712. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  713. }
  714. }
  715. //判断是否需要删除自定义热点样式
  716. String[] styles = null;
  717. if(StrUtil.isNotEmpty(base.getStyleName())){
  718. styles = base.getStyleName().split(",");
  719. }
  720. for(int i=0;i<jsonhots.size();++i) {
  721. JSONObject ele = jsonhots.getJSONObject(i);
  722. //名字对应上就删除
  723. if(styles != null){
  724. for(String style : styles){
  725. if(style.equals(ele.getString("styleId"))){
  726. jsonhots.getJSONObject(i).put("styleId", null);
  727. jsonhots.getJSONObject(i).put("style", "default");
  728. jsonhots.getJSONObject(i).put("styleImageURL", null);
  729. }
  730. }
  731. }
  732. hotsids += ele.getString("sid") +",";
  733. if(ele.getString("sid").equals(sid)) {
  734. if("-1".equals(base.getType())) {
  735. jsonhots.remove(i);
  736. jsonhot = ele;
  737. if(!FYunTypeEnum.AWS.code().equals(this.ossType)){
  738. JSONObject paramData = new JSONObject();
  739. paramData.put("hotId", sid);
  740. JSONObject hotListJson = JSONObject.parseObject(OkHttpUtils.httpGet(hotDomainList));
  741. if(hotListJson != null){
  742. JSONArray hotListArray = hotListJson.getJSONArray("data");
  743. if(hotListArray != null){
  744. for(int l = 0, len = hotListArray.size(); l < len; l++){
  745. OkHttpUtils.httpPostJson(hotListArray.getString(l) + hotDelete + "?hotId=" + sid
  746. , paramData.toJSONString());
  747. }
  748. }
  749. }
  750. }
  751. }
  752. else if("0".equals(base.getType())) {
  753. jsonhots.set(i, jsonhot);
  754. }
  755. }
  756. }
  757. Map<String,String> map = new HashMap<String,String>();
  758. if(jsonhot.containsKey("media")) {
  759. String fileType = jsonhot.getString("media");
  760. if(fileType.contains("photo")) {
  761. if("-1".equals(base.getType())) {
  762. FileUtils.deleteFile(imagesBuffer.toString() + "hot"+sid+".jpg");
  763. }
  764. else {
  765. map.put(imagesBuffer.toString() + "hot"+sid+".jpg", imagesBuf.toString() + "hot"+sid+".jpg");
  766. }
  767. }
  768. if(fileType.contains("audio") || fileType.contains("voice")) {
  769. if("-1".equals(base.getType())) {
  770. FileUtils.deleteFile(imagesBuffer.toString() + "hot"+sid+".mp3");
  771. }
  772. else {
  773. map.put(imagesBuffer.toString() + "hot"+sid+".mp3", imagesBuf.toString() + "hot"+sid+".mp3");
  774. }
  775. }
  776. if(fileType.contains("video")) {
  777. if("-1".equals(base.getType())) {
  778. FileUtils.deleteFile(imagesBuffer.toString() + "hot"+sid+".mp4");
  779. }
  780. else
  781. {
  782. map.put(imagesBuffer.toString() + "hot"+sid+".mp4", imagesBuf.toString() + "hot"+sid+".mp4");
  783. map.put(imagesBuffer.toString() + "hot"+sid+"-cut.jpg", imagesBuf.toString() + "hot"+sid+"-cut.jpg");
  784. }
  785. }
  786. }
  787. FileUtils.deleteFile(dataBuffer.toString() + "hot.json");
  788. File dataPath = new File(dataBuffer.toString());
  789. if(!dataPath.exists()){
  790. dataPath.mkdirs();
  791. }
  792. FileUtils.writeFile(dataBuffer.toString() + "hot.json", jsonhots.toString());
  793. map.put(dataBuffer.toString() + "hot.json", dataBuf.toString() + "hot.json");
  794. String strsceneInfos = FileUtils.readFile(dataBuffer.toString() + "scene.json");
  795. JSONObject scenejson = new JSONObject();
  796. if(strsceneInfos!=null){
  797. scenejson = JSONObject.parseObject(strsceneInfos);
  798. }
  799. if(!StringUtils.isEmpty(base.getCapData())){
  800. FileUtils.writeFile(dataBuffer.toString() + "capData.json", new String(base.getCapData().getBytes(), "UTF-8"));
  801. scenejson.put("capData", dataBuf.toString() + "capData.json");
  802. }
  803. if(!StringUtils.isEmpty(base.getFrameData())){
  804. FileUtils.writeFile(dataBuffer.toString() + "frameData.json", new String(base.getFrameData().getBytes(), "UTF-8"));
  805. scenejson.put("frameData", dataBuf.toString() + "frameData.json");
  806. }
  807. if(!StringUtils.isEmpty(base.getPlayData())){
  808. FileUtils.writeFile(dataBuffer.toString() + "playData.json", new String(base.getPlayData().getBytes(), "UTF-8"));
  809. scenejson.put("playData", dataBuf.toString() + "playData.json");
  810. sceneProEditEntity.setPlayData(dataBuf.toString() + "playData.json");
  811. map.put(dataBuffer.toString() + "playData.json", dataBuf.toString() + "playData.json");
  812. }
  813. if(!StringUtils.isEmpty(base.getScreencapThumb())){
  814. FileUtils.writeFile(dataBuffer.toString() + "screencapThumb.json", new String(base.getScreencapThumb().getBytes(), "UTF-8"));
  815. scenejson.put("screencapThumb", dataBuf.toString() + "screencapThumb.json");
  816. sceneProEditEntity.setScreencapThumb(dataBuf.toString() + "screencapThumb.json");
  817. map.put(dataBuffer.toString() + "screencapThumb.json", dataBuf.toString() + "screencapThumb.json");
  818. }
  819. fYunFileService.uploadMulFiles(map);
  820. sceneProEditEntity.setHotsIds(hotsids);
  821. sceneProEditEntity.setVersion(sceneProEditEntity.getVersion() + 1);
  822. sceneProEditService.updateById(sceneProEditEntity);
  823. if(jsonhots.size()>0){
  824. scenejson.put("hots", 1);
  825. }
  826. else{
  827. scenejson.put("hots", 0);
  828. }
  829. scenejson.put("version", sceneProEditEntity.getVersion() + 1);
  830. FileUtils.writeFile(dataBuffer.toString() + "scene.json", scenejson.toString());
  831. return ResultData.ok();
  832. }
  833. @Override
  834. public ResultData getRecordAudioFromWeixinV3(String sceneNum, String id, String type, String fileName,
  835. String length, String replace, String times, String index) throws Exception {
  836. if(StrUtil.isEmpty(sceneNum) || StrUtil.isEmpty(id)){
  837. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  838. }
  839. ScenePro sceneProEntity = sceneProService.findBySceneNum(sceneNum);
  840. if(sceneProEntity == null){
  841. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  842. }
  843. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  844. String strsceneInfos = FileUtils.readFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json");
  845. JSONObject scenejson = new JSONObject();
  846. if(strsceneInfos!=null) {
  847. scenejson = JSONObject.parseObject(strsceneInfos);
  848. }
  849. //文件上传的位置可以自定义
  850. String path = ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+sceneNum;
  851. log.info("path:" + path);
  852. String accessToken = WeiXinUtils.getAccessToken();
  853. InputStream is = null;
  854. String url = "https://file.api.weixin.qq.com/cgi-bin/media/get?access_token="+ accessToken + "&media_id=" + id;
  855. log.info("url:" + url);
  856. URL urlGet = new URL(url);
  857. HttpURLConnection http = (HttpURLConnection) urlGet
  858. .openConnection();
  859. http.setRequestMethod("GET"); // 必须是get方式请求
  860. http.setRequestProperty("Content-Type",
  861. "application/x-www-form-urlencoded");
  862. http.setDoOutput(true);
  863. http.setDoInput(true);
  864. System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
  865. System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
  866. http.connect();
  867. // 获取文件转化为byte流
  868. is = http.getInputStream();
  869. File dic = new File(path);
  870. if(!dic.exists())
  871. {
  872. dic.mkdirs();
  873. }
  874. File voice = new File(path+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr");
  875. if(voice.exists())
  876. {
  877. FileUtils.deleteFile(path+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr");
  878. }
  879. FileUtils.saveImageToDisk(accessToken, id, ConstantFileName.WECHAT_VOICE_NAME, path+File.separator, is);
  880. try
  881. {
  882. FileUtils.changeVoiceToMp3(path+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr",
  883. path+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".mp3");
  884. }
  885. catch(Exception e)
  886. {
  887. StringWriter trace=new StringWriter();
  888. e.printStackTrace(new PrintWriter(trace));
  889. log.error("voice:"+path+File.separator+ConstantFileName.VOICE_NAME+".amr");
  890. log.error(trace.toString());
  891. return ResultData.error(ErrorCode.FAILURE_CODE_5005);
  892. }
  893. String originalFileName = ConstantFileName.WECHAT_VOICE_NAME+".mp3";
  894. //判断分房间模块文件夹是否存在
  895. String partPath = path + File.separator + "part";
  896. File partFile = new File(partPath);
  897. if(!partFile.exists()){
  898. partFile.mkdirs();
  899. }
  900. if(!"1".equals(replace)){
  901. if(new File(partPath+File.separator+fileName).exists()) {
  902. FileUtils.deleteFile(partPath + File.separator + fileName);
  903. }
  904. FileUtils.changeVoiceToMp3(path+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr",
  905. partPath+File.separator+fileName);
  906. //获取总音频多少段,每段时长
  907. String[] time = times.split(",");
  908. //遍历判断音频是否存在,不存在生成空白音效
  909. for(int i = 0, len = time.length; i < len; i++){
  910. if(!new File(partPath + File.separator + i + ".mp3").exists()){
  911. //某部分文件不存在,直接生成一段静音后拼接文件
  912. CreateObjUtil.createMuteViode(Double.valueOf(time[i]), partPath + File.separator + i + ".mp3");
  913. }
  914. }
  915. //拼接所有音频文件
  916. if(time.length > 2){
  917. //若是多部分,两个两个合并,最后合成最终文件
  918. for(int i = 1, len = time.length; i < len; i++){
  919. if(i == 1){
  920. CreateObjUtil.mergeVideo(partPath + File.separator + (i - 1) + ".mp3", partPath + File.separator + i + ".mp3",
  921. partPath + File.separator + i + "muteSound.mp3");
  922. }else if(i == len - 1){
  923. CreateObjUtil.mergeVideo(partPath + File.separator + (i - 1) + "muteSound.mp3", partPath + File.separator + i + ".mp3",
  924. path + File.separator + originalFileName);
  925. }else {
  926. CreateObjUtil.mergeVideo(partPath + File.separator + (i - 1) + "muteSound.mp3", partPath + File.separator + i + ".mp3",
  927. partPath + File.separator + i + "muteSound.mp3");
  928. }
  929. }
  930. }else {
  931. //若只有两部分,直接合并成最终文件
  932. CreateObjUtil.mergeVideo(partPath + File.separator + "0.mp3", partPath + File.separator + "1.mp3", path + File.separator + originalFileName);
  933. }
  934. fYunFileService.uploadFile(path+File.separator +originalFileName, "voice/voice"+sceneNum+"/"+originalFileName);
  935. String voiceSrc = ossPrefixUrl+"voice/voice"+sceneNum+"/"+originalFileName;
  936. Map map = new HashMap();
  937. map.put("screencapVoiceSoundsyncFileName", originalFileName);
  938. map.put("screencapVoiceSoundsync", voiceSrc);
  939. map.put("uploadVoiceSoundsync", 1);
  940. map.put("screencapVoiceType", type);
  941. map.put("version", scenejson.getIntValue("version")+1);
  942. sceneProEditEntity.setScreencapVoiceType("soundsync");
  943. sceneProEditEntity.setScreencapVoiceSoundsync(voiceSrc);
  944. sceneProEditEntity.setVersion(scenejson.getIntValue("version")+1);
  945. FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
  946. sceneProEditService.updateById(sceneProEditEntity);
  947. FileUtils.deleteFile(path+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr");
  948. return ResultData.ok(voiceSrc);
  949. }
  950. FileUtils.delAllFile(path + File.separator + "part");
  951. //保存0.mp3文件到part
  952. FileUtils.changeVoiceToMp3(path+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr",
  953. partPath+File.separator+fileName);
  954. FileUtils.copyFile(partPath+File.separator+fileName, path + File.separator + originalFileName, true);
  955. fYunFileService.uploadFile(path+File.separator +originalFileName, "voice/voice"+sceneNum+"/"+originalFileName);
  956. String voiceSrc = ossPrefixUrl+"voice/voice"+sceneNum+"/"+originalFileName;
  957. Map map = new HashMap();
  958. map.put("screencapVoiceSoundsyncFileName", originalFileName);
  959. map.put("screencapVoiceSoundsync", voiceSrc);
  960. map.put("uploadVoiceSoundsync", 1);
  961. map.put("screencapVoiceType", type);
  962. map.put("version", scenejson.getIntValue("version")+1);
  963. sceneProEditEntity.setScreencapVoiceType("soundsync");
  964. sceneProEditEntity.setScreencapVoiceSoundsync(voiceSrc);
  965. sceneProEditEntity.setVersion(scenejson.getIntValue("version")+1);
  966. FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
  967. sceneProEditService.updateById(sceneProEditEntity);
  968. FileUtils.deleteFile(path+File.separator+ConstantFileName.WECHAT_VOICE_NAME+".amr");
  969. return ResultData.ok(voiceSrc);
  970. }
  971. @Override
  972. public ResultData uploadBgMusic(String sceneNum, String fileName, MultipartFile file) throws Exception {
  973. if(StringUtils.isEmpty(sceneNum) || StringUtils.isEmpty(fileName)){
  974. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  975. }
  976. ScenePro sceneProEntity = sceneProService.findBySceneNum(sceneNum);
  977. if(sceneProEntity == null){
  978. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  979. }
  980. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  981. StringBuffer dataBuf = new StringBuffer()
  982. .append("data").append(File.separator)
  983. .append("data").append(sceneProEntity.getNum())
  984. .append(File.separator);
  985. StringBuffer imagesBuf = new StringBuffer()
  986. .append("images").append(File.separator)
  987. .append("images").append(sceneProEntity.getNum())
  988. .append(File.separator);
  989. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  990. StringBuffer imagesBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(imagesBuf.toString());
  991. File buMusicFile = new File(imagesBuffer.toString() + fileName);
  992. if(!buMusicFile.getParentFile().exists()){
  993. buMusicFile.getParentFile().mkdirs();
  994. }
  995. //上传文件
  996. file.transferTo(buMusicFile);
  997. fYunFileService.uploadFile(imagesBuffer.toString() + fileName, imagesBuf + fileName);
  998. String strsceneInfos = FileUtils.readFile(dataBuffer.toString() + "scene.json");
  999. JSONObject scenejson = new JSONObject();
  1000. if(strsceneInfos!=null) {
  1001. scenejson = JSONObject.parseObject(strsceneInfos);
  1002. }
  1003. sceneProEditEntity.setBgMusicName(fileName);
  1004. sceneProEditEntity.setVersion(scenejson.getIntValue("version") + 1);
  1005. scenejson.put("bgMusicName", fileName);
  1006. scenejson.put("uploadBgMusic", 0);
  1007. scenejson.put("version", scenejson.getIntValue("version") + 1);
  1008. FileUtils.writeFile(dataBuffer.toString() + "scene.json", scenejson.toString());
  1009. sceneProEditService.updateById(sceneProEditEntity);
  1010. return ResultData.ok();
  1011. }
  1012. @Override
  1013. public ResultData deleteUploadBgMusic(String sceneNum) throws Exception {
  1014. if(StringUtils.isEmpty(sceneNum)){
  1015. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  1016. }
  1017. ScenePro sceneProEntity = sceneProService.findBySceneNum(sceneNum);
  1018. if(sceneProEntity == null){
  1019. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1020. }
  1021. SceneProEdit sceneProEditEntity = sceneProEditService.findBySceneProId(sceneProEntity.getId());
  1022. StringBuffer dataBuf = new StringBuffer()
  1023. .append("data").append(File.separator)
  1024. .append("data").append(sceneProEntity.getNum())
  1025. .append(File.separator);
  1026. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  1027. String strsceneInfos = FileUtils.readFile(dataBuffer.toString() + "scene.json");
  1028. JSONObject scenejson = new JSONObject();
  1029. if(strsceneInfos!=null) {
  1030. scenejson = JSONObject.parseObject(strsceneInfos);
  1031. }
  1032. sceneProEditEntity.setBgMusicName("");
  1033. sceneProEditEntity.setVersion(scenejson.getIntValue("version") + 1);
  1034. scenejson.put("bgMusicName", "");
  1035. scenejson.put("version", scenejson.getIntValue("version") + 1);
  1036. FileUtils.writeFile(dataBuffer.toString() + "scene.json", scenejson.toString());
  1037. sceneProEditService.updateById(sceneProEditEntity);
  1038. return ResultData.ok();
  1039. }
  1040. }