数据过滤

**问题:**网格,区域,单位,场所,设备。(层级有包含关系,设备在某个场所中,场所在单位中,单位在某个区域中,或者某个网格中),现项目中有一个需求,配置通知人员,该人员可以监控上面五种类型,如:张三监控某一设备,李四监控设备所在单位,若改设备报警,那么,张三李四都应该收到报警通知(电话或者短信),如若张三监控某一个设备,张三也监控设备所在单位,那么他应该只收到一次通知。(监控设备配置了短信,监控单位配置了单位,那么他应该能收到短信&电话,仅一次),未处理时返回的是两条张三的数据,会通知张三2遍,为了解决这个问题,代码进行过滤。(纯属个人记录,小白)

解决:

sql

SELECT *
FROM `notify_user`
WHERE JSON_CONTAINS(`alarm_notify`->'$[*].alarmType', '\"BATTERY_ALARM\"', '$')

查询结果

在这里插入图片描述代码过滤处理

public Set<NotifyUserResponse> findNotifyUser(String departmentId,String buildingId,String zoneId, String deviceId, String alarmType,String imei, String category) {
    Set<NotifyUserResponse> notifyUserResponses = new HashSet<>();
    List<NotifyUser> notifyUsers = new ArrayList<>();
    String parentId = null;
    if (StringUtils.isNotEmpty(imei)) {
      Optional<DeviceDetail> device = deviceDetailRepository.findByImei(imei);
      String categoryStr = device.get().getCategoryId();
      Map<String, Category> categoryMap = PrimaryTask.CATEGORY_MAP;
      Category ca = categoryMap.get(categoryStr);
      parentId = ca.getParentId();
      log.info("parentId-" + parentId);
    } else {
      parentId = category;
    }
    List<NotifyUser> notifyUserList = null;
    if (StringUtils.isNotEmpty(parentId)) {
      notifyUserList = notifyUserRepository
          .findByAlarmNotifyAndCategoryId(parentId, "\"" + alarmType + "\"");
    } else {
      notifyUserList = notifyUserRepository
          .findByAlarmNotify("\"" + alarmType + "\"");
    }
    for (NotifyUser notifyUser : notifyUserList) {
      if (notifyUser.getMonitoringScope() == NotifyUser.DEPARTMENT_SCOPE) {
        if (StringUtils.isNotEmpty(departmentId)) {
          String[] array = StringUtils.split(notifyUser.getDepartmentId(), ",");
          for (int i = 0; i < array.length; i++) {
            if (departmentId.equals(array[i])) {
              notifyUsers.add(notifyUser);
            }
          }
        }
      } else if (notifyUser.getMonitoringScope() == NotifyUser.ZONE_SCOPE) {
        if (StringUtils.isNotEmpty(zoneId)) {
          String[] array = StringUtils.split(notifyUser.getAreaId(), ",");
          for (int i = 0; i < array.length; i++) {
            List<String> zoneList = zoneService.findZoneChildList(array[i]);
            if (zoneList.contains(zoneId)) {
              notifyUsers.add(notifyUser);
            }
          }
        }
      } else if (notifyUser.getMonitoringScope() == NotifyUser.BUILDING_SCOPE) {
        if (StringUtils.isNotEmpty(buildingId)) {
          String[] array = StringUtils.split(notifyUser.getBuildingId(), ",");
          for (int i = 0; i < array.length; i++) {
            if (buildingId.equals(array[i])) {
              notifyUsers.add(notifyUser);
            }
          }
        }
      } else if (notifyUser.getMonitoringScope() == NotifyUser.DEVICE_SCOPE) {
        if (StringUtils.isNotEmpty(deviceId)) {
          JSONArray jsonArray = notifyUser.getDeviceId();
          for (int i = 0; i < jsonArray.size(); i++) {
            if (deviceId.equals(jsonArray.get(i))) {
              notifyUsers.add(notifyUser);
            }
          }
        }
      }
    }
    //上面的是根据条件查出所有符合条件的数据,数据如上面sql查询的结果,但是会包含相同手机号的,下面代码就是为了根据手机号分组,并且通知方式取并集。
    //下面为处理代码
    Map<String, JSONObject> map = new HashMap<>();
    Set<String> set = new HashSet<>();
    for (NotifyUser notifyUser : notifyUsers) {
      JSONArray array = notifyUser.getAlarmNotify();
      JSONObject obj = new JSONObject();
      if (!map.containsKey(notifyUser.getMobile())) {
        set.clear();
      }
      Set<String> set2 = new HashSet<>();
      for (int i = 0; i < array.size(); i++) {
        if (array.getJSONObject(i).getString("alarmType").equals(alarmType)) {
          JSONArray array2 = JSONArray.parseArray(array.getJSONObject(i).getString("notifyType"));
          for (int j = 0; j < array2.size(); j++) {
            set.add(array2.getString(j));
          }
        }
      }
      set2.addAll(set);
      obj.put("mobile", notifyUser.getMobile());
      obj.put("name", notifyUser.getName());
      obj.put("isNotify", notifyUser.getIsNotify());
      obj.put("notifyType", set2);
      map.put(notifyUser.getMobile(), obj);
    }
    //处理完成,结果如:{15150570717={"notifyType":["sms","telephone"],"isNotify":0,"mobile":"15150570717","name":"陈辉"},13000000000={"notifyType":["telephone"],"isNotify":0,"mobile":"13000000000","name":"bucunxa"}}
    log.info("===map:{}", map);
    for (Map.Entry<String, JSONObject> entry : map.entrySet()) {
      String key = entry.getKey();
      JSONObject value = entry.getValue();
      NotifyUserResponse notifyUserResponse = NotifyUserResponse.builder()
              .name(value.getString("name")).mobile(key)
              .notifyType(value.getJSONArray("notifyType"))
              .isNotify(value.getInteger("isNotify")).build();
          notifyUserResponses.add(notifyUserResponse);
    }
    log.info("===notifyUserResponses:{}", notifyUserResponses);
    return notifyUserResponses;
  }

你可能感兴趣的:(项目,java)