MongoTemplates.java
12.8 KB
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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.objecteye.dao;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.objecteye.pojo.FaceInfoParam;
import com.objecteye.pojo.RabbitMQVehicle;
import com.objecteye.pojo.VehicleCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
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.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
@Component
public class MongoTemplates {
@Autowired
private MongoTemplate mongoTemplate;
//mongodb插入车辆数据
public RabbitMQVehicle insertData(RabbitMQVehicle rabbitMQVehicle) {
return mongoTemplate.insert(rabbitMQVehicle);
}
//mongodb插入司机数据
public void insertData(FaceInfoParam faceInfoParam) {
FaceInfoParam save = mongoTemplate.insert(faceInfoParam);
System.out.println(save.getId());
}
//mongodb查询根据id
public <T> T findById(Class<T> entityClass, String id) {
return mongoTemplate.findById(id, entityClass);
}
//mongodb查询所有的数据信息
public <T> List<T> findAll(Class<T> entityClass) {
return mongoTemplate.findAll(entityClass);
}
//mongodb查询根据多条件
public JSONObject findBy(VehicleCondition vehicleCondition, Integer vehicle_special_type_number) {
JSONObject jsonObject = new JSONObject();
List<JSONObject> vehicleTable = null;
try {
Query query = new Query();
//必须条件
Criteria c = new Criteria();
//范围条件
//模糊查询号牌
int currentpage = vehicleCondition.getCurrentpage();
int pagevolume = vehicleCondition.getPagevolume();
String hphm = vehicleCondition.getHphm();
Long starttime = vehicleCondition.getStarttime();
Long endtime = vehicleCondition.getEndtime();
int[] condition = vehicleCondition.getCondition();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//特殊品类检索
if (vehicle_special_type_number != null) {
c.and("vehicle_special_type").is(vehicle_special_type_number);
}
//模糊号牌检索
if (hphm != null && hphm.length() > 0) {
hphm = hphm.replaceAll("\\?", ".?").replaceAll("\\*", ".*");
Pattern pattern = Pattern.compile("^.*" + hphm + ".*$", Pattern.CASE_INSENSITIVE);
c.and("vehicle_plate_hphm").regex(pattern);
}
//时间段范围内
if (starttime != 0l && endtime != 0l) {
c.and("pictime").gte(starttime).lte(endtime);
}
/*无号牌(nullhphm):1 污损号牌(stained):2 主驾驶安全带(driverbelt):3 副驾驶安全带(copilotbelt):4
主驾驶打电话(drivercall):5 主驾驶吸烟(driversmoke):6 是否苫盖(cover):7*/
//条件查询
if (condition != null && condition.length > 0) {
for (int i : condition) {
switch (i) {
case 1:
//无号牌
c.and("vehicle_plate_numScore").is(0);
break;
case 2:
//污损号牌(stained)
c.and("vehicleplatedetectscore").is(-1); //当识别度为负数时认为是污损号牌
//因目前sdk不支持污损号牌检测,所以此处略
break;
case 3:
//主驾驶未系安全带(driverbelt):
c.and("vehicle_illegal_driver_person_status").is(1004);
c.and("vehicle_illegal_driver_belt_status").is(1000);
break;
case 4:
//副驾驶未系安全带(copilotbelt):4
c.and("vehicle_illegal_copilot_person_status").is(1004);
c.and("vehicle_illegal_copilot_belt_status").is(1000);
break;
case 5:
//主驾驶打电话(drivercall):
c.and("vehicle_illegal_driver_person_status").is(1004);
c.and("vehicle_illegal_driver_phone_status").is(1000);
break;
case 6:
//主驾驶吸烟(driversmoke):
c.and("vehicle_illegal_driver_person_status").is(1004);
c.and("vehicle_illegal_driver_smoke_status").is(1000);
break;
case 7:
//是否苫盖(cover):
//因目前sdk不支持苫盖,所以此处略
c.and("vehicleplatedetectscore").is(-1); //因为检测分数永远都不可能为负数
break;
}
}
}
query.addCriteria(c);
long totalNumber = mongoTemplate.count(query, JSONObject.class, "rabbitMQVehicle");
//按时间进行排序
query.with(new Sort(Sort.Direction.DESC, "pictime"));
// 分页
query.skip((currentpage - 1) * pagevolume).limit(pagevolume);
vehicleTable = mongoTemplate.find(query, JSONObject.class, "rabbitMQVehicle");
JSONObject data = new JSONObject();
JSONArray array = new JSONArray();
for (JSONObject json : vehicleTable) {
String id = json.containsKey("_id") ? json.getString("_id") : null;
String hphm_new = json.containsKey("vehicle_plate_hphm") ? json.getString("vehicle_plate_hphm") : null;
String equipmentName = json.containsKey("equipmentName") ? json.getString("equipmentName") : null;
String pictime = json.containsKey("pictime") ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(json.getLong("pictime"))) : null;
String recordid = json.containsKey("recordid") ? json.getString("recordid") : null;
json.put("site", equipmentName);
json.put("id", id);
json.put("hphm", hphm_new);
json.put("phototime", pictime);
json.put("recordid", recordid);
//添加snapshoturl的值
String snapshoturl = json.getString("snapshoturl");
json.put("picurl", snapshoturl); //此处将原来的全景图url换成了快照图绝对路径
array.add(json);
}
//根据总条数获取到总页数
int totalPage = (int) Math.ceil(((double) totalNumber) / pagevolume);
data.put("total", totalPage);
data.put("row", array);
if (totalNumber > 0 && array != null && array.size() > 0) {
jsonObject.put("code", 200);
jsonObject.put("message", "success");
jsonObject.put("data", data);
} else {
jsonObject.put("code", 201);
jsonObject.put("message", "没有查询到符合要求的结果");
jsonObject.put("data", data);
}
} catch (Exception e) {
e.printStackTrace();
jsonObject.put("code", 202);
jsonObject.put("message", "请检查数据是否正确");
jsonObject.put("data", null);
}
return jsonObject;
}
/**
* 根据车辆条件查询符合条件的车辆列表
*
* @param vehicleCondition
* @return
*/
public List<RabbitMQVehicle> getVehcieListBy(VehicleCondition vehicleCondition) {
List<RabbitMQVehicle> rabbitMqVehicles = null;
try {
Query query = new Query();
//必须条件
Criteria c = new Criteria();
//范围条件
//模糊查询号牌
int currentpage = vehicleCondition.getCurrentpage();
int pagevolume = vehicleCondition.getPagevolume();
String hphm = vehicleCondition.getHphm();
Long starttime = vehicleCondition.getStarttime();
Long endtime = vehicleCondition.getEndtime();
int[] condition = vehicleCondition.getCondition();
//模糊号牌检索
if (hphm != null && hphm.length() > 0) {
hphm = hphm.replaceAll("\\?", "\\\\S").replaceAll("\\*", ".*");
Pattern pattern = Pattern.compile(hphm, Pattern.CASE_INSENSITIVE);
c.and("vehicle_plate_hphm").regex(pattern);
}
//时间段范围内
if (starttime != 0L && endtime != 0L) {
c.and("pictime").gte(starttime).lte(endtime);
}
if (vehicleCondition.getVehicleSpecialType() != null) {
c.and("vehicle_special_type").is(vehicleCondition.getVehicleSpecialType());
}
if (vehicleCondition.getVehicleColorIndex() != null) {
c.and("vehicle_color_index").is(vehicleCondition.getVehicleColorIndex());
}
if (vehicleCondition.getVehicleRecgIssueYear() != null) {
c.and("vehicle_recg_issue_year").is(vehicleCondition.getVehicleRecgIssueYear());
}
if (vehicleCondition.getVehicleRecgBrand() != null) {
c.and("vehicle_recg_brand").is(vehicleCondition.getVehicleRecgBrand());
}
if (vehicleCondition.getVehicleRecgType() != null) {
c.and("vehicle_recg_type").is(vehicleCondition.getVehicleRecgType());
}
if (vehicleCondition.getVehicleRecgSubbrand() != null) {
c.and("vehicle_recg_subbrand").is(vehicleCondition.getVehicleRecgSubbrand());
}
/*无号牌(nullhphm):1 污损号牌(stained):2 主驾驶安全带(driverbelt):3 副驾驶安全带(copilotbelt):4
主驾驶打电话(drivercall):5 主驾驶吸烟(driversmoke):6 是否苫盖(cover):7*/
//条件查询
if (condition != null && condition.length > 0) {
for (int i : condition) {
switch (i) {
case 1:
//无号牌
c.and("vehicleplatedetectscore").lte(0.5);
break;
case 2:
//污损号牌(stained)
//因目前sdk不支持污损号牌检测,所以此处略
break;
case 3:
//主驾驶安全带(driverbelt):
c.and("vehicle_illegal_driver_belt_status").is(1000);
break;
case 4:
//副驾驶安全带(copilotbelt):4
c.and("vehicle_illegal_copilot_belt_status").is(1000);
break;
case 5:
//主驾驶打电话(drivercall):
c.and("vehicle_illegal_driver_phone_status").is(1000);
break;
case 6:
//主驾驶吸烟(driversmoke):
c.and("vehicle_illegal_driver_smoke_status").is(1000);
break;
case 7:
//是否苫盖(cover):
//因目前sdk不支持苫盖,所以此处略
break;
default:
break;
}
}
}
query.addCriteria(c);
// 分页
query.skip((currentpage - 1) * pagevolume).limit(pagevolume);
query.fields().include("id");
query.fields().include("vehicle_plate_hphm");
query.fields().include("picurl");
query.fields().include("snapshoturl");
query.fields().include("recordid");
query.fields().include("equipmentName");
query.fields().include("pictime");
return mongoTemplate.find(query, RabbitMQVehicle.class);
} catch (Exception e) {
e.printStackTrace();
}
return rabbitMqVehicles;
}
}