于紫祥_1901
2020-12-01 f22a631422f3045a06aad627e6e83c802be2760d
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
package com.moral.controller;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
import javax.annotation.Resource;
 
import com.moral.common.bean.ResultBean;
import com.moral.entity.DeviceVersion;
import com.moral.entity.layout.RealTimeDeviceLayout;
import com.moral.entity.layout.RtdLayoutUpload;
import com.moral.entity.layout.SensorComb;
import com.moral.service.DeviceVersionService;
import com.moral.service.OrganizationLayoutService;
import com.moral.service.SensorService;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("org-layout")
public class OrganizationLayoutController {
    @Resource
    private OrganizationLayoutService organizationLayoutService;
    @Resource
    private SensorService sensorService;
    @Resource
    private DeviceVersionService deviceVersionService;
    @GetMapping("rtd-config")
    public ResultBean<Map<String,Object>> getRealTimeDevicetConfig(Integer orgId){
        Map<String,Object> resultMap = new HashMap();
        resultMap.put("deviceVersions",new ArrayList<>());
        resultMap.put("sensorCombs", new ArrayList<>());
        resultMap.put("sensors", new ArrayList<>());
        resultMap.put("rtdLayout", new HashMap<>());
        // 获取当前组织所含设备版本数组
        List<DeviceVersion> deviceVersionList = deviceVersionService.queryByOrgId(orgId);
        // 获取第一个version 下所有传感器
        if (deviceVersionList!=null && deviceVersionList.size()>0) {
            Integer versionId = deviceVersionList.get(0).getId();
            Integer versionNum = deviceVersionList.get(0).getVersion();
            // 转为 SensorComb 保持对象一致
            List<String> fixedItemKeys =  organizationLayoutService.getFixedItemKeys();
            List<SensorComb> optionalFixedItems = new ArrayList<>();
            List<SensorComb> sensorCombList =  sensorService.queryListByVersionId(versionId)
                    .stream()
                    .filter(item -> {
                        for (String key: fixedItemKeys) {
                            if( key.equals(item.getSensorKey()) ) {
                                SensorComb sensorComb = new SensorComb();
                                sensorComb.setName(item.getDescription());
                                sensorComb.setSensorKey(item.getSensorKey());
                                optionalFixedItems.add(sensorComb);
                                return false;
                            }
                        }
                        return true;
                    })
                    .map(item -> {
                        SensorComb sensorComb = new SensorComb();
                        sensorComb.setName(item.getDescription());
                        sensorComb.setSensorKey(item.getSensorKey());
                        return  sensorComb;
                    }).collect(Collectors.toList());
            RealTimeDeviceLayout realTimeDeviceLayout = organizationLayoutService.queryRealTimeDeviceLayout(orgId,versionNum);
            resultMap.put("deviceVersions",deviceVersionList);
            resultMap.put("sensorCombs",sensorCombList);
            resultMap.put("rtdLayout",realTimeDeviceLayout);
            resultMap.put("optionalFixedItems",optionalFixedItems);
        }
        return new ResultBean<>(resultMap);
    }
    @GetMapping("rtd-act-config")
    public ResultBean<Map<String,Object>> getRealTimeDeviceConfigWithVer(Integer orgId,Integer versionNo){
        Map<String,Object> resultMap = new HashMap();
        resultMap.put("sensors","");
        resultMap.put("rtdLayout","");
            // 转为 SensorComb 保持对象一致
        List<String> fixedItemKeys =  organizationLayoutService.getFixedItemKeys();
        List<SensorComb> optionalFixedItems = new ArrayList<>();
        List<SensorComb> sensorCombList =  sensorService.queryListByVersionNo(versionNo)
                .stream()
                .filter(item -> {
                    for (String key: fixedItemKeys) {
                        if( key.equals(item.getSensorKey()) ) {
                            SensorComb sensorComb = new SensorComb();
                            sensorComb.setName(item.getDescription());
                            sensorComb.setSensorKey(item.getSensorKey());
                            optionalFixedItems.add(sensorComb);
                            return false;
                        }
                    }
                    return true;
                })
                .map(item -> {
                        SensorComb sensorComb = new SensorComb();
                        sensorComb.setName(item.getDescription());
                        sensorComb.setSensorKey(item.getSensorKey());
                        return  sensorComb;
                    }).collect(Collectors.toList());
            RealTimeDeviceLayout realTimeDeviceLayout = organizationLayoutService.queryRealTimeDeviceLayout(orgId,versionNo);
            resultMap.put("sensorCombs",sensorCombList);
            resultMap.put("rtdLayout",realTimeDeviceLayout);
            resultMap.put("optionalFixedItems",optionalFixedItems); // 可显示固定项
        return new ResultBean<>(resultMap);
    }
    @PostMapping("rtd-save")
    public  ResultBean RealTimeDeviceSave(@RequestBody RtdLayoutUpload rtdLayoutUpload) {
        organizationLayoutService.saveRtdLayout(rtdLayoutUpload);
        return ResultBean.success();
    }
    @GetMapping("rtd-remove")
    public  ResultBean RealTimeDeviceRemove(Integer orgId,Integer version) {
        organizationLayoutService.deleteRtdLayout(orgId,version);
        return ResultBean.success();
    }
}