rain
2024-04-19 999c71172bc2135665bf84f5b345d59be01f28bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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.ArrayList;
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 int insertData(List<MediaFileEntity> mediaFile, LotInfo lotInfo) throws IOException {
        int count = 0;
        List<TbFjEntity> list = new ArrayList<>();
        TbFjEntity tbFj;
        for (int i = 0; i < mediaFile.size(); i++) {
            MediaFileEntity file = mediaFile.get(i);
            tbFj = dbConvertToEntity(file, lotInfo);
            list.add(tbFj);
            mapper.insert(tbFj);
            count++;
        }
        return count;
    }
 
    /**
     * 将Media和Lotinfo实体类转化为TbFjEntity实体类
     *
     * @param mediaFile
     * @param lotInfo
     * @return
     * @throws IOException
     */
    private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) throws IOException {
        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");
        int psjd = 0;
        String fjhxz = "0";
        int pshgj = 0;
        String jym = "0";
        String psry = "0";
        String zsdm = "0";
        String dklx = "0";
        String xzqdm = "0";
        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;
        File file =downloadFile(url);
        File file1=WaterMark.addWatermark(file,patchesConfigPojo.getUnzip(),pssj,lat,lng);
        byte[] FJ = fileToByteArray(file1);
        TbFjEntity.TbFjEntityBuilder builder = TbFjEntity.builder();
        if (lotInfo != null) {
            builder.bsm(bsm)
                    .dkbsm(lotInfo.getDkbh())
                    .xzqdm(xzqdm)
                    .dklx(dklx)
                    .xdgd(relativeAltitude)
                    .jdgd(absoluteAltitude)
                    .Latitude(lat)
                    .longitude(lng)
                    .fj(FJ)
                    .fjmc(fjmc)
                    .fjlx(1)
                    .psfyj(gimbalYawDegree)
                    .pssj(String.valueOf(pssj))
                    .psjd(psjd)
                    .fjhxz(fjhxz)
                    .pshgj(pshgj)
                    .zsdm(zsdm)
                    .psry(psry)
                    .jym(jym)
                    .build();
        } else {
            throw new IllegalArgumentException("未匹配到相关地块");
        }
        return builder.build();
    }
 
    /**
     * 将音视频文件转换成字节流
     *
     * @param fileUrl
     * @return
     */
    public  File downloadFile(String fileUrl) {
 
        File downloadedFile = null;
        String localFilePath =  patchesConfigPojo.getUnzip()+ "tmp.jpg";
        try {
            URL url = new URL(fileUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
 
            downloadedFile = new File(localFilePath);
 
            try (InputStream inputStream = connection.getInputStream();
                 OutputStream outputStream = new FileOutputStream(downloadedFile)) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            }
 
            System.out.println("File downloaded and saved at: " + downloadedFile.getAbsolutePath());
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return downloadedFile;
    }
 
    public static byte[] fileToByteArray(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        fis.read(data);
        fis.close();
        return data;
    }
 
}