|
|
@@ -5,6 +5,7 @@ import com.zhongshu.iot.client.model.mqtt.DeviceInfoSearchParam;
|
|
|
import com.zhongshu.iot.server.core.dao.base.BaseImpl;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.extend.DeviceInfoDaoExtend;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.device.DeviceInfo;
|
|
|
+import com.zhongshu.iot.server.core.util.CommonUtil;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -12,12 +13,16 @@ import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
@@ -49,9 +54,45 @@ public class DeviceInfoDaoImpl extends BaseImpl implements DeviceInfoDaoExtend {
|
|
|
return mongoTemplate.count(query, DeviceInfo.class);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String, Long> deviceGroupStatistics(DeviceInfoSearchParam param, String fieldName) {
|
|
|
+ Criteria criteria = buildFilterCriteria(param);
|
|
|
+
|
|
|
+ Map<String, Long> map = new HashMap<>();
|
|
|
+
|
|
|
+ List<AggregationOperation> operations = new ArrayList<>(3);
|
|
|
+ // 条件
|
|
|
+ AggregationOperation match = Aggregation.match(criteria);
|
|
|
+ operations.add(match);
|
|
|
+
|
|
|
+ // 分组
|
|
|
+ AggregationOperation group = Aggregation.group("$" + fieldName).count().as("count");
|
|
|
+ operations.add(group);
|
|
|
+
|
|
|
+ // 排序
|
|
|
+ AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, fieldName);
|
|
|
+ operations.add(sort);
|
|
|
+
|
|
|
+ Aggregation groupAggregation = Aggregation.newAggregation(operations);
|
|
|
+ List<Map> countObjs = mongoTemplate.aggregate(groupAggregation, DeviceInfo.class, Map.class).getMappedResults();
|
|
|
+ if (countObjs.size() > 0) {
|
|
|
+ countObjs.stream().forEach(it -> {
|
|
|
+ String name = (String) it.get(fieldName);
|
|
|
+ if (StringUtils.isEmpty(name)) {
|
|
|
+ Object _id = it.get("_id");
|
|
|
+ name = String.valueOf(_id);
|
|
|
+ }
|
|
|
+ Long number = CommonUtil.getLongByObj(it.get("count"));
|
|
|
+ map.put(name, number);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
public Criteria buildFilterCriteria(DeviceInfoSearchParam param) {
|
|
|
Criteria criteria = buildCriteriaAboutTime(param);
|
|
|
|
|
|
+ // 设备品类
|
|
|
if (ObjectUtils.isNotEmpty(param.getDeviceCategorys())) {
|
|
|
criteria.and("deviceCategory").in(param.getDeviceCategorys());
|
|
|
}
|
|
|
@@ -60,22 +101,41 @@ public class DeviceInfoDaoImpl extends BaseImpl implements DeviceInfoDaoExtend {
|
|
|
criteria.and("deviceId").is(param.getDeviceId());
|
|
|
}
|
|
|
|
|
|
+ // 分组
|
|
|
if (StringUtils.isNotEmpty(param.getProjectInfoCode())) {
|
|
|
criteria.and("projectInfoCode").is(param.getProjectInfoCode());
|
|
|
}
|
|
|
|
|
|
+ // 所属产品
|
|
|
+ if (StringUtils.isNotEmpty(param.getProductCode())) {
|
|
|
+ criteria.and("productCode").is(param.getProductCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设备型号
|
|
|
if (param.getSpecType() != null) {
|
|
|
criteria.and("specType").is(param.getSpecType());
|
|
|
}
|
|
|
|
|
|
- if (param.getDeviceType() != null) {
|
|
|
- criteria.and("deviceType").is(param.getDeviceType());
|
|
|
+ if (ObjectUtils.isNotEmpty(param.getDeviceTypes())) {
|
|
|
+ if (ObjectUtils.isNotEmpty(param.getNinDeviceTypes())) {
|
|
|
+ criteria.and("deviceType").in(param.getDeviceTypes()).nin(param.getNinDeviceTypes());
|
|
|
+ } else {
|
|
|
+ criteria.and("deviceType").in(param.getDeviceTypes());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (ObjectUtils.isNotEmpty(param.getNinDeviceTypes())) {
|
|
|
+ criteria.and("deviceType").nin(param.getNinDeviceTypes());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (param.getOnLineState() != null) {
|
|
|
criteria.and("onLineState").is(param.getOnLineState());
|
|
|
}
|
|
|
|
|
|
+ if (param.getState() != null) {
|
|
|
+ criteria.and("state").is(param.getState());
|
|
|
+ }
|
|
|
+
|
|
|
// 模糊搜索
|
|
|
List<Criteria> criterias = new ArrayList<>();
|
|
|
if (StringUtils.isNotEmpty(param.getDeviceName())) {
|