package com.dji.sample.territory.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.dynamic.datasource.annotation.DS; import com.dji.sample.media.model.MediaFileEntity; import com.dji.sample.patches.model.entity.LotInfo; import com.dji.sample.territory.dao.ITbFjMapper; import com.dji.sample.territory.model.entity.TbFjEntity; import com.dji.sample.territory.service.ITbFJService; import com.google.gson.JsonObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.UUID; @Service @DS("sqlite") public class TbFjServiceImpl implements ITbFJService { @Autowired private ITbFjMapper mapper; /** * 按照地块编号所对应的信息和音视频文件存入sqlite数据库 * * @param mediaFile * @param lotInfo * @return * @throws IOException */ public TbFjEntity insertData(List mediaFile, LotInfo lotInfo) throws IOException { TbFjEntity tbFj = new TbFjEntity(); for (int i = 0; i < mediaFile.size(); i++) { MediaFileEntity file = mediaFile.get(i); tbFj = dbConvertToEntity(file, lotInfo); try { mapper.insert(tbFj); } catch (Exception e) { e.printStackTrace(); } } return tbFj; } /** * 将Media和Lotinfo实体类转化为TbFjEntity实体类 * * @param mediaFile * @param lotInfo * @return * @throws IOException */ private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) { String jsonString = JSONObject.toJSONString(mediaFile.getMetadata()); JSONObject jsonObject = JSONObject.parseObject(jsonString); Double absoluteAltitude = jsonObject.getDouble("absoluteAltitude"); Double relativeAltitude = jsonObject.getDouble("relativeAltitude"); Double gimbalYawDegree = jsonObject.getDouble("gimbalYawDegree"); JSONObject shootPosition = jsonObject.getJSONObject("shootPosition"); Double lat = shootPosition.getDouble("lat"); Double lng = shootPosition.getDouble("lng"); Long pssj = mediaFile.getCreateTime(); String bsm= UUID.randomUUID().toString(); String fjmc= mediaFile.getFileName(); String key = mediaFile.getObjectKey(); String head = "http://dev.jxpskj.com:9000/cloud-bucket"; String url = head + key; byte[] FJ = downloadFileAsBytes(url); TbFjEntity.TbFjEntityBuilder builder = TbFjEntity.builder(); if (lotInfo != null){ builder.bsm(bsm) .dkbsm(lotInfo.getDkbh()) .xzqdm(lotInfo.getXzqdm()) .dklx(lotInfo.getDklx()) .xdgd(relativeAltitude) .jdgd(absoluteAltitude) .Latitude(lat) .longitude(lng) .fj(FJ) .fjmc(fjmc) .fjlx(1) .psfyj(gimbalYawDegree) .pssj(String.valueOf(pssj)) .build();} else { throw new IllegalArgumentException("未匹配到相关地块"); } return builder.build(); } /** * 将音视频文件转换成字节流 * * @param fileUrl * @return */ public static byte[] downloadFileAsBytes(String fileUrl) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { URL url = new URL(fileUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); try (InputStream inputStream = connection.getInputStream()) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } } catch (IOException e) { e.printStackTrace(); } return outputStream.toByteArray(); } }