|
|
@@ -26,6 +26,7 @@ import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.*;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -33,9 +34,7 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @Description: 公共类,方便服务间调用
|
|
|
@@ -358,6 +357,89 @@ public class CommonServiceImpl implements CommonService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public Map<Long, LocationVO> getLocation(List<Long> truckIds) {
|
|
|
+ List<String> clientIds = new ArrayList();
|
|
|
+ for (Long truckId : truckIds) {
|
|
|
+ QueryWrapper<TruckSimCard> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("truck_id", truckId);
|
|
|
+ TruckSimCard truckSimCard = truckSimCardService.getOne(queryWrapper);
|
|
|
+ if (truckSimCard == null) {
|
|
|
+ log.error("绑定信息不存在 truckId:{}", truckId);
|
|
|
+ clientIds.add(null);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ clientIds.add(truckSimCard.getClientId());
|
|
|
+ }
|
|
|
+ log.info("getLocation clientIds:{}", JSON.toJSONString(clientIds));
|
|
|
+ Query query = new Query(Criteria.where("clientId").in(clientIds).and("messageId").is(512));
|
|
|
+ query.with(new Sort(Sort.Direction.DESC, "dateTime"));
|
|
|
+
|
|
|
+// // 聚合操作
|
|
|
+// List<AggregationOperation> operations = new ArrayList<>();
|
|
|
+// // 筛选条件
|
|
|
+// operations.add(Aggregation.match(Criteria.where("clientId").in(clientIds).and("messageId").is(512)));
|
|
|
+// operations.sort(new Sort(Sort.Direction.DESC, "dateTime"));
|
|
|
+//
|
|
|
+// // 分组字段
|
|
|
+// GroupOperation groupOperation = Aggregation.group("clientId").first();
|
|
|
+// SortOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC, "dateTime"));
|
|
|
+// // 聚合查询字段
|
|
|
+//// for (int i = 0; i < queryKey.length; i++) {
|
|
|
+//// groupOperation = groupOperation.sum(queryKey[i]).as(queryKey[i]);
|
|
|
+//// }
|
|
|
+// // 添加选项 (聚合查询字段和添加筛选是有区别的注意)
|
|
|
+// operations.add(groupOperation);
|
|
|
+// operations.add(sortOperation);
|
|
|
+//
|
|
|
+// // 最终聚合查询所有信息
|
|
|
+// Aggregation aggregation = Aggregation.newAggregation(operations);
|
|
|
+//
|
|
|
+// // 查询结果
|
|
|
+// AggregationResults<HashMap> results = mongoTemplate.aggregate(aggregation, "tms_dev", HashMap.class);
|
|
|
+// //获取结果
|
|
|
+// List<HashMap> result = results.getMappedResults();
|
|
|
+
|
|
|
+ TypedAggregation typedAggregation = TypedAggregation.newAggregation(TmsMongoDoc.class,
|
|
|
+ Arrays.asList(
|
|
|
+ //筛选过滤条件
|
|
|
+ TypedAggregation.match(Criteria.where("clientId").in(clientIds).and("messageId").is(512)),
|
|
|
+ //分组过滤条件,sort的顺序有要求,在分组方法前就是先排序后分组
|
|
|
+ TypedAggregation.sort(Sort.by(Sort.Direction.DESC, "dateTime")),
|
|
|
+ TypedAggregation.group("clientId")
|
|
|
+ .first("latitude").as("latitude")
|
|
|
+ .first("longitude").as("longitude")
|
|
|
+ .first("createTime").as("createTime")
|
|
|
+ .first("clientId").as("clientId")
|
|
|
+ ));
|
|
|
+
|
|
|
+ List<TmsMongoDoc> tmsMongoDocs =
|
|
|
+ mongoTemplate.aggregate(typedAggregation, TmsMongoDoc.class).getMappedResults();
|
|
|
+// if (tmsMongoDoc != null) {
|
|
|
+// location = new LocationVO();
|
|
|
+// location.setLatitude(tmsMongoDoc.getLatitude() + "");
|
|
|
+// location.setLongitude(tmsMongoDoc.getLongitude() + "");
|
|
|
+// location.setCarNumber(truckSimCard.getCarNumber());
|
|
|
+// location.setTruckId(truckSimCard.getTruckId());
|
|
|
+// location.setDate(tmsMongoDoc.getCreateTime());
|
|
|
+// }
|
|
|
+// return location;
|
|
|
+ log.info("getLocation results:{}", JSON.toJSONString(tmsMongoDocs));
|
|
|
+ Map<Long, LocationVO> result = new HashMap();
|
|
|
+ for (TmsMongoDoc doc : tmsMongoDocs) {
|
|
|
+ for (int i=0;i<clientIds.size();i++) {
|
|
|
+ if (clientIds.get(i).equals(doc.getClientId())) {
|
|
|
+ LocationVO location = new LocationVO();
|
|
|
+ location.setLatitude(doc.getLatitude() + "");
|
|
|
+ location.setLongitude(doc.getLongitude() + "");
|
|
|
+ location.setDate(doc.getCreateTime());
|
|
|
+ result.put(truckIds.get(i), location);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public LocationVO getLocationByDate(Long truckId, LocalDateTime date) {
|
|
|
LocationVO location = null;
|
|
|
QueryWrapper<TruckSimCard> queryWrapper = new QueryWrapper<>();
|