|
|
@@ -2,24 +2,32 @@ package com.yunfeiyun.agmp.iotm.web.service.impl;
|
|
|
|
|
|
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotDeviceGeoLocation;
|
|
|
import com.yunfeiyun.agmp.iot.common.service.MongoService;
|
|
|
import com.yunfeiyun.agmp.iot.common.util.BigDecimalUtil;
|
|
|
-import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotDeviceListStatReqVo;
|
|
|
-import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotDeviceListStatResVo;
|
|
|
+import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotDeviceListReqVo;
|
|
|
+import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotHomeDeviceListReqVo;
|
|
|
+import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotDeviceListResVo;
|
|
|
+import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotHomeDeviceListResVo;
|
|
|
import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotHomeDeviceStatResVo;
|
|
|
import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotHomeTypeStatResVo;
|
|
|
import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;
|
|
|
import com.yunfeiyun.agmp.iotm.web.service.IIotHomeService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
|
|
-import org.springframework.data.mongodb.core.aggregation.MatchOperation;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.GeoNearOperation;
|
|
|
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
|
|
|
+import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.query.NearQuery;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class IotHomeServiceImpl implements IIotHomeService {
|
|
|
|
|
|
@@ -42,40 +50,68 @@ public class IotHomeServiceImpl implements IIotHomeService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<IotDeviceListStatResVo> getDeviceListStat(IotDeviceListStatReqVo reqVo) {
|
|
|
+ public List<IotHomeDeviceListResVo> getDeviceList(IotHomeDeviceListReqVo reqVo) {
|
|
|
String tid = SecurityUtils.getTid();
|
|
|
- BigDecimal devLng = reqVo.getDevLng();
|
|
|
- BigDecimal devLat = reqVo.getDevLat();
|
|
|
+ String devQery = reqVo.getDevBid();
|
|
|
+ BigDecimal devLng = reqVo.getLng();
|
|
|
+ BigDecimal devLat = reqVo.getLat();
|
|
|
+ String[] devtypeBids = reqVo.getDevtypeBids();
|
|
|
+ List<String> devtypeBidList = new ArrayList<>();
|
|
|
+ if(devtypeBids != null) {
|
|
|
+ devtypeBidList = Arrays.asList(devtypeBids);
|
|
|
+ }
|
|
|
+ Integer distance = reqVo.getDistance();
|
|
|
+
|
|
|
+ IotDeviceListReqVo req = new IotDeviceListReqVo();
|
|
|
+ req.setTid(tid);
|
|
|
+ req.setDevQuery(devQery);
|
|
|
+ req.setDevtypeBidList(devtypeBidList);
|
|
|
+
|
|
|
+ List<IotDeviceListResVo> iotDeviceList = iIotDeviceService.selectIotDeviceListByType(req);
|
|
|
+ Map<String, IotDeviceListResVo> deviceMap = new HashMap<>();
|
|
|
+ for(IotDeviceListResVo item : iotDeviceList) {
|
|
|
+ deviceMap.put(item.getDevBid(), item);
|
|
|
+ }
|
|
|
+
|
|
|
double lng = 0;
|
|
|
double lat = 0;
|
|
|
if(devLng != null && devLat != null) {
|
|
|
- lng = BigDecimalUtil.format(lng).doubleValue();
|
|
|
- lat = BigDecimalUtil.format(lat).doubleValue();
|
|
|
+ lng = BigDecimalUtil.format(devLng).doubleValue();
|
|
|
+ lat = BigDecimalUtil.format(devLat).doubleValue();
|
|
|
}
|
|
|
-
|
|
|
Criteria criteria = new Criteria()
|
|
|
.and("tid").is(tid);
|
|
|
- MatchOperation matchOperation = Aggregation.match(criteria);
|
|
|
+ if(!devtypeBidList.isEmpty()) {
|
|
|
+ criteria = criteria.and("devtypeBid").in(devtypeBidList);
|
|
|
+ }
|
|
|
+ GeoJsonPoint centerPoint = new GeoJsonPoint(lng, lat);
|
|
|
+ NearQuery nearQuery = NearQuery.near(centerPoint).query(new Query(criteria)).distanceMultiplier(6378137);
|
|
|
+ if(distance != null) {
|
|
|
+ nearQuery = nearQuery.maxDistance(distance);
|
|
|
+ }
|
|
|
+ GeoNearOperation geoNearOperation = Aggregation.geoNear(nearQuery, "distance");
|
|
|
|
|
|
ProjectionOperation projectionOperationResult = Aggregation.project("_id")
|
|
|
- .and("_id").as("devtypeBid")
|
|
|
- .and("count").as("count");
|
|
|
+ .and("devBid").as("devBid")
|
|
|
+ .and("devtypeBid").as("devtypeBid")
|
|
|
+ .and("distance").as("distance")
|
|
|
+ .andExpression("{$arrayElemAt: {'$devGeoLocation.coordinates', 0}}").as("lng")
|
|
|
+ .andExpression("{$arrayElemAt: {'$devGeoLocation.coordinates', 1}}").as("lat");
|
|
|
|
|
|
-// Aggregation aggregation = Aggregation.newAggregation(
|
|
|
-// matchOperation,
|
|
|
-// groupOperation,
|
|
|
-// projectionOperationResult
|
|
|
-// );
|
|
|
-// List<IotIndexDevicetypeCountResVo> dataList = mongoService.aggregate(IotDeviceGeoLocation.class, aggregation, IotIndexDevicetypeCountResVo.class);
|
|
|
-// for(IotIndexDevicetypeCountResVo item : dataList) {
|
|
|
-// String devtypeBid = item.getDevtypeBid();
|
|
|
-// TosDevicetype devicetype = devicetypeMap.get(devtypeBid);
|
|
|
-// if (devicetype != null) {
|
|
|
-// item.setDevtypeName(devicetype.getDevtypeName());
|
|
|
-// item.setDevtypePreview(devicetype.getDevtypePreview());
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- return null;
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(
|
|
|
+ geoNearOperation,
|
|
|
+ projectionOperationResult
|
|
|
+ );
|
|
|
+ List<IotHomeDeviceListResVo> dataList = mongoService.aggregate(IotDeviceGeoLocation.class, aggregation, IotHomeDeviceListResVo.class);
|
|
|
+ List<IotHomeDeviceListResVo> resultList = new ArrayList<>();
|
|
|
+ for(IotHomeDeviceListResVo item : dataList) {
|
|
|
+ IotDevice deviceInfo = deviceMap.get(item.getDevBid());
|
|
|
+ if(deviceInfo == null){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ item.setDevName(deviceInfo.getDevName());
|
|
|
+ resultList.add(item);
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
}
|
|
|
}
|