jinpengyong
2021-12-24 5c427a35595e2deb2c1a025ff9a4c164d498e01d
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
package com.moral.api.service.impl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.Device;
import com.moral.api.entity.Sensor;
import com.moral.api.service.DeviceService;
import com.moral.api.service.ProfessionService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
 
/**
 * <p>
 * 行业贡献率相关接口
 * </p>
 *
 * @author moral
 * @since 2021-12-23
 */
@Service
public class ProfessionServiceImpl implements ProfessionService {
 
    @Autowired
    private DeviceService deviceService;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Override
    public Set<Map<String, Object>> getProfessionsByOrganizationId(Integer organizationId) {
        //获取该组织下所有设备
        List<Map<String, Object>> devices = deviceService.getDevicesByOrganizationId(organizationId);
        Set<Map<String, Object>> result = new HashSet<>();
        for (Map<String, Object> device : devices) {
            List<Map<String, Object>> professions = (List<Map<String, Object>>) device.get("professions");
            result.addAll(professions);
        }
        return result;
    }
 
    @Override
    public Set<Map<String, Object>> getSensorByProfessionsAndOrganizationId(Map<String, Object> params) {
        Set<Map<String, Object>> result = new HashSet<>();
        //获取属于该行业所有设备
        Integer orgId = Integer.parseInt(params.get("organizationId").toString());
        List<String> professions = Arrays.asList(params.get("professions").toString().split(","));
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac", "profession")
                .eq("organization_id", orgId)
                .eq("is_delete", Constants.NOT_DELETE);
        List<Device> devices = deviceService.list(queryWrapper);
        devices = devices.stream().filter(device -> {
            String[] list = device.getProfession().split(",");
            for (String s : list) {
                if (professions.contains(s)) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
 
        //从redis获取设备详细信息
        for (Device device : devices) {
            device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, device.getMac());
            List<Sensor> sensors = device.getVersion().getSensors();
            for (Sensor sensor : sensors) {
                Map<String, Object> sensorMap = new HashMap<>();
                sensorMap.put("sensorCode", sensor.getCode());
                sensorMap.put("sensorName", sensor.getName());
                result.add(sensorMap);
            }
        }
        return result;
    }
}