1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.moral.service.impl;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
 
import com.moral.entity.SensorUnit;
import com.moral.mapper.SensorUnitMapper;
import com.moral.service.SensorService;
import com.moral.service.SensorUnitService;
 
@Service
public class SensorUnitServiceImpl implements SensorUnitService {
    private static Logger log = Logger.getLogger(SensorService.class);
    @Resource
    private SensorUnitMapper sensorUnitMapper;
 
    @Override
    public boolean addOrModify(SensorUnit sensorUnit) {
        try {
            if (sensorUnit.getId() != null) {
                sensorUnit.setUpdateTime(new Date());
                sensorUnitMapper.updateByPrimaryKeySelective(sensorUnit);
            } else {
                sensorUnit.setIsDelete(false);
                sensorUnit.setCreateTime(new Date());
                sensorUnitMapper.insertSelective(sensorUnit);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage());
        }
        return false;
    }
 
    @Override
    public List<SensorUnit> queryListBySensorId(int sensorId) {
        return queryListBySensorId(sensorId, false);
    }
 
    /**
     * 根据传感器id 获取 所属传感器数组
     *
     * @param sensorId
     * @return
     */
    @Override
    public List<SensorUnit> queryListBySensorId(int sensorId, Boolean isDelete) {
        SensorUnit sensorUnitQuery = new SensorUnit();
        sensorUnitQuery.setSensorId(sensorId);
        sensorUnitQuery.setIsDelete(isDelete);
        return sensorUnitMapper.select(sensorUnitQuery);
    }
 
    /**
     * 根据传感器单位实体id 删除
     *
     * @param id
     */
    @Override
    public void remove(Integer id) {
        SensorUnit sensorUnit = new SensorUnit();
        sensorUnit.setId(id);
        sensorUnit.setIsDelete(true);
        sensorUnit.setUpdateTime(new Date());
        sensorUnitMapper.updateByPrimaryKeySelective(sensorUnit);
    }
 
    /**
     * 获取传感器单位数组分组
     *
     * @param sensorIds
     * @return
     */
    @Override
    public Map<Integer, List<SensorUnit>> queryGroupSensorBySids(List<Integer> sensorIds) {
        SensorUnit sensorUnitQuery = new SensorUnit();
        Map<Integer, List<SensorUnit>> resultListMap = new HashMap<>();
        for (Integer sensorId : sensorIds) {
            sensorUnitQuery.setSensorId(sensorId);
            sensorUnitQuery.setIsDelete(false);
            List<SensorUnit> sensorUnitList = sensorUnitMapper.select(sensorUnitQuery);
            if (sensorUnitList != null && sensorUnitList.size() > 0) {
                resultListMap.put(sensorId, sensorUnitList);
            }
        }
        return resultListMap;
    }
 
    @Override
    public Map<String, Object> getSensorByMac(String mac) {
        List<Map<String, Object>> list = sensorUnitMapper.getSensorsByDeviceMac(mac);
        if (list != null && !list.isEmpty()) {
            Map<String, Object> sensorUnitMap = new HashMap<>();
            for (Map<String, Object> sensorMap : list) {
                String sensor_key = (String) sensorMap.get("sensor_key");
                String name = (String) sensorMap.get("name");
                sensorUnitMap.put(sensor_key, name);
            }
            return sensorUnitMap;
        } else {
            Map<String, Object> sensorUnitMap = null;
            return sensorUnitMap;
        }
    }
 
    @Override
    public Map<String, Map<String, Object>> getSensorsByMonitPointId(String id) {
        List<Map<String, Object>> list = sensorUnitMapper.getSensorsByMonitPointId(id);
        if (list != null && !list.isEmpty()) {
            Map<String, Map<String, Object>> sensorUnitMap = new HashMap<>();
            for (Map<String, Object> sensorMap : list) {
                Map<String, Object> innerMap = new HashMap<>();
                innerMap.put("unit", sensorMap.get("unit"));
                innerMap.put("name", sensorMap.get("name"));
                innerMap.put("rules", sensorMap.get("rules"));
                String sensor_key = (String) sensorMap.get("sensor_key");
                sensorUnitMap.put(sensor_key, innerMap);
            }
            //System.out.println(sensorUnitMap);
            return sensorUnitMap;
        } else {
            Map<String, Map<String, Object>> sensorUnitMap = null;
            return sensorUnitMap;
        }
    }
 
    @Override
    public List<Map<String, Object>> getSensorsByMonitPointId2(String id) {
        List<Map<String, Object>> list = sensorUnitMapper.getSensorsByMonitPointId(id);
        return list;
    }
}