ZhuDongming
2020-06-30 67a7a24b47d71dd843dd0b17c5cdf9f519e29d5d
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -17,15 +17,6 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moral.mapper.DictionaryDataMapper;
import com.moral.mapper.OrganizationMapper;
import com.moral.service.HistoryHourlyService;
import com.moral.util.MyLatLng;
import com.moral.util.mapUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.moral.common.bean.Constants;
import com.moral.common.bean.PageBean;
@@ -35,13 +26,22 @@
import com.moral.common.util.StringUtils;
import com.moral.common.util.ValidateUtil;
import com.moral.entity.Device;
import com.moral.entity.DeviceProperty;
import com.moral.entity.MonitorPoint;
import com.moral.mapper.DeviceMapper;
import com.moral.mapper.DevicePropertyMapper;
import com.moral.mapper.DictionaryDataMapper;
import com.moral.mapper.MonitorPointMapper;
import com.moral.mapper.OrganizationMapper;
import com.moral.service.DeviceService;
import com.moral.service.HistoryHourlyService;
import com.moral.service.MonitorPointService;
import com.moral.service.OrganizationService;
import com.moral.util.MyLatLng;
import com.moral.util.mapUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
@@ -67,12 +67,17 @@
    @Resource
    private HistoryHourlyService historyHourlyService;
    
    @Resource
    DevicePropertyMapper devicePropertyMapper;
    private static Class ENTITY_CLASS = MonitorPoint.class;
    @Override
    public List<MonitorPoint> getMonitorPointsByAreaName(Map<String, Object> parameters) {
        ValidateUtil.notNull(parameters.get("areaName"), "param.is.null");
        return monitorPointMapper.getMonitorPointsByAreaName(parameters);
    }
    @Override
    public List<MonitorPoint> queryWithStateByMap(Map<String, Object> params){
        params.put("isDelete",Constants.IS_DELETE_FALSE);
@@ -90,6 +95,7 @@
        }
        return monitorPointList == null ? new ArrayList<>() : monitorPointList;
    }
    private Integer getStateFromRedis(Integer monitorPointId){
        StringBuilder key = new StringBuilder();
        key.append("state_").append(monitorPointId).append("_*");
@@ -104,6 +110,7 @@
        state = state==-1?4:state;
        return state;
    }
    @Override
    public PageBean queryByPageBean(PageBean pageBean) {
        Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
@@ -112,6 +119,7 @@
        List<MonitorPoint> monitorPointList = monitorPointMapper.selectWithAreaNameByExample(example);
        return new PageBean(monitorPointList);
    }
    @Override
   public MonitorPoint queryWithRelationById(Integer id){
         Example example = new Example(ENTITY_CLASS);
@@ -119,6 +127,7 @@
         List<MonitorPoint> monitorPointList = monitorPointMapper.selectWithAreaNameByExample(example);
         return monitorPointList!=null&&monitorPointList.size()>0?monitorPointList.get(0):null;
    }
    @Override
    public void addOrModify(MonitorPoint monitorPoint) {
        try{
@@ -138,11 +147,11 @@
                    refreshDevicesInRedis(monitorPoint.getId());
                }
            }
        }
        catch (Exception ex){
        } catch (Exception ex) {
            throw  ex;
        }
    }
    /*
      刷新当前监控点下设备 在redis里设备信息
     */
@@ -171,6 +180,7 @@
        }
    }
    @Override
    public void deleteByIds(Integer... ids) {
        MonitorPoint monitorPoint = new MonitorPoint();
@@ -216,7 +226,6 @@
   }
    /**
     *
     * @param idList
     * @return  {id:,state:}
     */
@@ -234,6 +243,7 @@
    /**
     * 获取所属组织的监控点总数
     *
     * @param orgId
     * @return
     */
@@ -247,6 +257,7 @@
        }
        return monitorPointMapper.selectCountByExample(example);
    }
    @Override
   public List<MonitorPoint> getMonitorPointsByOrganizationId(Integer orgId) {
      Example example = new Example(MonitorPoint.class);
@@ -262,6 +273,7 @@
      example.orderBy("name").asc();
      return monitorPointMapper.selectByExample(example);
   }
   @Override
   public List<MonitorPoint> getMonitorPointsByRegion(Map<String, Object> parameters) {
      Example example = new Example(MonitorPoint.class);
@@ -271,10 +283,12 @@
      criteria.andEqualTo(parameters.get("name").toString(), parameters.get("value"));
      return monitorPointMapper.selectByExample(example);
   }
   @Override
    public List<Integer> queryVersionsById(Integer id){
        return  monitorPointMapper.selectVersionsById(id);
    }
    @Override
    public MonitorPoint queryMonitorPointById(Integer mpointId) {
        return  this.monitorPointMapper.selectByPrimaryKey(mpointId);
@@ -304,7 +318,7 @@
      List<MonitorPoint> monitorPoints = monitorPointMapper.selectByExample(example);
      //查询监控点下所有的设备
      Iterator<MonitorPoint> iterator = monitorPoints.iterator();
      while (iterator.hasNext()) {
      /*while (iterator.hasNext()) {
         MonitorPoint monitorPoint = iterator.next();
         List<Device> devices = deviceService.getDevicesByMonitorPointId(monitorPoint.getId());
         if (ObjectUtils.isEmpty(devices)) {
@@ -313,6 +327,44 @@
            monitorPoint.setDevices(devices);
         }
         
      }*/
        Example deviceExample = new Example(Device.class);//实例化
        Criteria deviceCriteria = deviceExample.createCriteria();
        deviceCriteria.orEqualTo("isDelete", Constants.IS_DELETE_FALSE);
        List<Device> devicesInfo = deviceMapper.selectByExample(deviceExample);
        Example monitorExample = new Example(MonitorPoint.class);//实例化
        Criteria monitorCriteria = monitorExample.createCriteria();
        monitorCriteria.orEqualTo("isDelete", Constants.IS_DELETE_FALSE);
        List<MonitorPoint> monitorPointInfo = monitorPointMapper.selectByExample(monitorExample);
        List<DeviceProperty> devicePropertyList = devicePropertyMapper.selectAll();
        for (Device d : devicesInfo) {
            for (DeviceProperty dp : devicePropertyList) {
                if (dp.getId().equals(d.getId())) {
                    d.setDeviceProperty(dp);
                }
            }
        }
        Map<Integer, List<Device>> monitorDeviceMap = new HashMap();
        for (MonitorPoint m : monitorPointInfo) {
            List<Device> monitorDevices = new ArrayList<>();
            for (Device d : devicesInfo) {
                if (m.getId().equals(d.getMonitorPointId())) {
                    monitorDevices.add(d);
                }
            }
            monitorDeviceMap.put(m.getId(), monitorDevices);
        }
        while (iterator.hasNext()) {
            MonitorPoint monitorPoint = iterator.next();
            for (Map.Entry<Integer, List<Device>> entry : monitorDeviceMap.entrySet()) {
                if (monitorPoint.getId().equals(entry.getKey())) {
                    if (!CollectionUtils.isEmpty(entry.getValue())) {
                        monitorPoint.setDevices(entry.getValue());
                    } else {
                        iterator.remove();
                    }
                }
            }
      }
      return monitorPoints;
   }
@@ -368,6 +420,7 @@
      }
       
   }
   @Override
   public Map<String, Object> selectAllById(String id) {
      int id2 = Integer.parseInt(id);