Blame view

src/main/java/com/objecteye/service/impl/VehicleServiceImpl.java 10 KB
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
1
2
3
4
  package com.objecteye.service.impl;
  
  import com.alibaba.fastjson.JSON;
  import com.objecteye.entity.*;
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
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
  import com.objecteye.service.DeployService;
  import com.objecteye.service.VehicleDbService;
  import com.objecteye.service.VehicleService;
  import com.objecteye.utils.GlobalUtil;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.data.domain.PageImpl;
  import org.springframework.data.domain.PageRequest;
  import org.springframework.data.domain.Pageable;
  import org.springframework.data.mongodb.core.MongoTemplate;
  import org.springframework.data.mongodb.core.query.Criteria;
  import org.springframework.data.mongodb.core.query.Query;
  import org.springframework.data.mongodb.core.query.Update;
  import org.springframework.data.redis.core.RedisTemplate;
  import org.springframework.stereotype.Service;
  import org.springframework.web.multipart.MultipartFile;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.List;
  
  /**
   * @Author: lr
   * @Date: 2019/9/16 11:20
   * @Version 1.0
   * @Message:
   */
  @Service
  public class VehicleServiceImpl implements VehicleService {
  
  
      @Autowired
      private MongoTemplate mongoTemplate;
      @Autowired
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
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
      private RedisTemplate redisTemplate;
      @Autowired
      private DeployService deployService;
  
      @Value("${requestVehicleFile}")
      private String url;
      @Value("${picture.url}")
      private String picIpAndPort;
  
      @Autowired
      private VehicleDbService vehicleDbService;
  
      @Override
      public PageResult findPage(int vehicleId, String picName, int pageNum, int pageSize) {
          // 创建查询对象
          Query query = new Query();
          //分页条件
          Pageable pageable = new PageRequest(pageNum - 1, pageSize);
          query.with(pageable);
          //条件查询
          Criteria criteria = new Criteria();
          criteria.and("vehicleId").is(vehicleId);
          if (picName != null) {
              criteria.and("picName").regex(".*" + picName + ".*");
          }
          query.addCriteria(criteria);
          //总条数
          long count = mongoTemplate.count(query, UploadVehicleResult.class, "uploadVehicleDbResult");
  
          //全部数据
          List<UploadVehicleDbResult> datas = mongoTemplate.find(query, UploadVehicleDbResult.class, "uploadVehicleDbResult");
          //分页形式
          PageImpl<UploadVehicleDbResult> alarmPageImpl = new PageImpl<UploadVehicleDbResult>(datas, pageable, count);
  
          int totalPages = alarmPageImpl.getTotalPages();
          List<UploadVehicleDbResult> content = alarmPageImpl.getContent();
          if (datas != null) {
              return new PageResult(totalPages, content);
          }
          return null;
      }
  
      @Override
      public void delete(String[] ids) {
          int length = ids.length;
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
84
          String vehicleId = null;
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
85
86
87
88
89
90
91
92
93
          for (int i = 0; i < length; i++) {
              String id = ids[i];
              UploadVehicleDbResult uploadVehicleDbResult = mongoTemplate.findOne(Query.query(new Criteria("id").is(id)), UploadVehicleDbResult.class);
              vehicleId = uploadVehicleDbResult.getVehicleId();
              String plateNum = uploadVehicleDbResult.getPlateNum();
              float plateScore = uploadVehicleDbResult.getPlateScore();
              if (plateScore > 0.9 && redisTemplate.opsForHash().hasKey("vehicleId", plateNum)) {
                  redisTemplate.opsForHash().delete("vehicleId", plateNum);
              }
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
94
              List<String> deployListByLibAndDeployType = deployService.getDeployListByLibAndDeployType(vehicleId, 1);
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
95
96
97
98
99
100
101
102
103
104
105
106
              if (deployListByLibAndDeployType != null && deployListByLibAndDeployType.size() > 0) {
                  for (int j = 0; j < deployListByLibAndDeployType.size(); j++) {
                      String key = deployListByLibAndDeployType.get(j) + "|" + vehicleId;
                      if (redisTemplate.opsForHash().hasKey(key, id)) {
                          redisTemplate.opsForHash().delete(key, id);
                      }
                  }
              }
              redisTemplate.opsForHash().delete("vehicleId", id);
              redisTemplate.opsForHash().delete("vehicleUrl", id);
              mongoTemplate.remove(uploadVehicleDbResult);
          }
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
107
          SyVehicleDb syVehicleDb = mongoTemplate.findOne(Query.query(Criteria.where("id").is(vehicleId)), SyVehicleDb.class);
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
108
          syVehicleDb.setCount(syVehicleDb.getCount() - length);
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
109
          mongoTemplate.save(syVehicleDb);
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
      }
  
      @Override
      public void update(UploadVehicleDbResult uploadVehicleDbResult) {
          Query query = new Query();
          Criteria criteria = new Criteria();
          criteria.and("id").is(uploadVehicleDbResult.getId());
          query.addCriteria(criteria);
          Update update = Update.update("picName", uploadVehicleDbResult.getPicName());
          mongoTemplate.findAndModify(query, update, UploadVehicleDbResult.class);
      }
  
      @Override
      public List<UploadVehicleDbResult> findByPicName(UploadVehicleDbResult uploadVehicleResult) {
          Query query = new Query();
          Criteria criteria = new Criteria();
          criteria.and("vehicleId").is(uploadVehicleResult.getVehicleId());
          criteria.and("picName").regex(".*" + uploadVehicleResult.getPicName() + ".*");
          query.addCriteria(criteria);
          List<UploadVehicleDbResult> uploadVehicleResultList = mongoTemplate.find(query, UploadVehicleDbResult.class, "uploadVehicleDbResult");
          return uploadVehicleResultList;
      }
  
      @Override
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
134
      public String uploadFiles(MultipartFile[] uploadFiles, String vehicleId) {
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
135
          int count = 0;
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
136
137
          SyVehicleDb one = mongoTemplate.findOne(Query.query(Criteria.where("id").is(vehicleId)), SyVehicleDb.class);
          List<String> deployListByLibAndDeployType = deployService.getDeployListByLibAndDeployType(vehicleId, 1);
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
          if (uploadFiles.length > 0 && one != null) {
  
              int len = uploadFiles.length;
              for (int i = 0; i < len; i++) {
                  MultipartFile multipartFile = uploadFiles[i];
                  String fileName = multipartFile.getOriginalFilename();
  
                  String picPath = GlobalUtil.dbPath1() + File.separator + "picture" + File.separator + fileName;
  
                  String imageUrl = picIpAndPort + fileName;
                  File newFile = new File(picPath);
                  try {
                      multipartFile.transferTo(newFile);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  String s = GlobalUtil.httpExecute(url, newFile);
                  ResponseParam responseParam = JSON.parseObject(s, ResponseParam.class);
                  String code = responseParam.getCode();
                  if ("0".equals(code)) {
                      String s1 = JSON.toJSONString(responseParam.getResult());
                      VehicleAnalysisResultParam vehicleAnalysisResultParam = JSON.parseObject(s1, VehicleAnalysisResultParam.class);
                      UploadVehicleDbResult uploadVehicleDbResult = new UploadVehicleDbResult();
                      VehicleInfoParam[] info = vehicleAnalysisResultParam.getInfo();
                      if (null != info && info.length > 0) {
                          VehicleInfoParam vehicleInfoParam = info[0];
                          VehicleFeatureResultParam vehicleFeaRes = vehicleInfoParam.getVehicle_fea_res();
                          float[] feature = vehicleFeaRes.getFeature();
                          uploadVehicleDbResult.setCount(vehicleAnalysisResultParam.getCount());
                          uploadVehicleDbResult.setVehicleInfoParam(vehicleInfoParam);
  
                          String pic = fileName.substring(0, fileName.indexOf("."));
                          uploadVehicleDbResult.setPicName(pic);
                          uploadVehicleDbResult.setVehicleId(vehicleId);
                          uploadVehicleDbResult.setImageUrl(imageUrl);
  
  
                          VehiclePlateResultParam vehiclePlateDetRecgRes = vehicleInfoParam.getVehicle_plate_det_recg_res();
                          float numScore = vehiclePlateDetRecgRes.getNumScore();
                          uploadVehicleDbResult.setPlateScore(numScore);
                          VehiclePlateNumParam[] plateNumParams = vehiclePlateDetRecgRes.getPlateNumParams();
                          String splateNum = "";
  
                          if (plateNumParams != null && plateNumParams.length > 0) {
                              StringBuffer stringBuffer = new StringBuffer();
                              int length = plateNumParams.length;
                              for (int j = 0; j < length; j++) {
                                  String character = plateNumParams[j].getCharacter();
                                  stringBuffer.append(character);
                              }
                              splateNum = stringBuffer.toString();
                              uploadVehicleDbResult.setPlateNum(splateNum);
                          }
                          UploadVehicleDbResult save = mongoTemplate.save(uploadVehicleDbResult);
                          String id = save.getId();
                          if (numScore > 0.9) {
                              redisTemplate.opsForHash().put("vehiclePlate", splateNum, id);
                          }
                          if (deployListByLibAndDeployType.size() > 0) {
                              for (int j = 0; j < deployListByLibAndDeployType.size(); j++) {
40c853a1   Liu Haoyu   去掉MySQL相关内容, 去掉my...
198
                                  String deployId = deployListByLibAndDeployType.get(j);
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
                                  String key1 = deployId + "|" + vehicleId;
                                  redisTemplate.opsForHash().put(key1, id, 0);
                              }
                          }
                          redisTemplate.opsForHash().put("vehicleId", id, feature);
                          redisTemplate.opsForHash().put("vehicleUrl", id, imageUrl);
                          count++;
                      }
                  }
              }
              Integer count1 = one.getCount();
              one.setCount(count + count1);
              vehicleDbService.updateCountById(one);
              return "上传的文件共有" + len + ",成功上传的个数为: " + count;
          }
          return null;
      }
  }