lizijie
2020-12-03 a66d53c3cbfb0024804045f4d795be06089d4f9d
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.moral.service.impl;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
import javax.annotation.Resource;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.moral.entity.DictionaryData;
import com.moral.entity.OrganizationLayout;
import com.moral.entity.OrganizationSensorUnit;
import com.moral.entity.Sensor;
import com.moral.entity.SensorUnit;
import com.moral.entity.layout.RealTimeDeviceLayout;
import com.moral.entity.layout.RtdLayoutUpload;
import com.moral.entity.layout.SensorComb;
import com.moral.mapper.DictionaryDataMapper;
import com.moral.mapper.OrganizationLayoutMapper;
import com.moral.mapper.OrganizationSensorUnitMapper;
import com.moral.mapper.SensorMapper;
import com.moral.service.OrganizationLayoutService;
 
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
 
@Service
public class OrganizationLayoutServiceImpl implements OrganizationLayoutService{
    @Resource
    SensorMapper sensorMapper;
    @Resource
    OrganizationLayoutMapper orgLayoutMapper;
    @Resource
    OrganizationSensorUnitMapper orgUnitMapper;
    private static String page_layout_fixed_items = "page_layout_fixed_items";
    private static String page_layout_versions_sort = "page_layout_versions_sort";
    @Resource
    DictionaryDataMapper dataMapper;
    private Optional<String> queryDictionaryDataValue(String dictDataKey) {
        DictionaryData query = new DictionaryData();
        query.setDictDataKey(dictDataKey);
        List<DictionaryData> dataList = dataMapper.select(query);
        List<String> fixedItemKeys = new ArrayList<>();
        String json  = null;
        if(dataList.size()>0) {
            DictionaryData data = dataList.get(0);
            json = data.getDictDataValue() != null ? data.getDictDataValue().toString() : null;
        }
        return Optional.of(json);
    }
    @Override
    public List<String> getFixedItemKeys() {
        Optional jsonString = queryDictionaryDataValue(page_layout_fixed_items);
        List<String> fixedItemKeys = new ArrayList<>();
        if(jsonString.isPresent()) {
            List<String> keyList  = JSON.parseObject(jsonString.get().toString(),
                    new TypeReference<List<String>>(){});
            if(keyList!=null) {
                fixedItemKeys.addAll(keyList);
            }
        }
 
        return fixedItemKeys;
    }
    public Integer queryPageConfigCountByOrgId(Integer orgId,String pageType) {
        OrganizationLayout query = new OrganizationLayout();
        query.setOrganizationId(orgId);
        query.setPageType(pageType);
        return  orgLayoutMapper.selectCount(query);
    }
    @Override
    public RealTimeDeviceLayout queryRealTimeDeviceLayout(Integer orgId,Integer version){
        String pageType = getPageType(version);
        if(queryPageConfigCountByOrgId(orgId,pageType) > 0) {
            RealTimeDeviceLayout rtdLayout = new RealTimeDeviceLayout();
            Example example = new Example(OrganizationLayout.class);
            // 升序排序
            example.orderBy("pagePositionIndex").asc();
            // 查询图表key
            example.or().andEqualTo("pageType",pageType)
                    .andEqualTo("organizationId",orgId)
                    .andEqualTo("pagePosition",RealTimeDeviceLayout.ChartSensorKey);
           List<OrganizationLayout> ChartSensorKeys = orgLayoutMapper.selectByExample(example);
           if(ChartSensorKeys.size()>0) {
               rtdLayout.setChartSensorKey(ChartSensorKeys.get(0).getPagePositionValue());
           }
           // 查询默认区
            example.clear();
            example.or().andEqualTo("pageType",pageType)
                    .andEqualTo("organizationId",orgId)
                    .andEqualTo("pagePosition",RealTimeDeviceLayout.DefaultMonitorItems);
            List<OrganizationLayout> defaultMonitorItems = orgLayoutMapper.selectByExample(example);
            List<SensorComb> defaultSensorCombList = defaultMonitorItems.stream().map(
                    item-> {
                        SensorComb sensorComb = SensorComb.generate(item);
                        return  sensorComb;
                    }
            ).collect(Collectors.toList());
            rtdLayout.setDefaultMonitorItems(defaultSensorCombList);
            // 查询核心区
            example.clear();
            example.or().andEqualTo("pageType",pageType)
                    .andEqualTo("organizationId",orgId)
                    .andEqualTo("pagePosition",RealTimeDeviceLayout.CoreMonitorItems);
            List<OrganizationLayout> coreMonitorItems = orgLayoutMapper.selectByExample(example);
            List<SensorComb> coreSensorCombList = coreMonitorItems.stream().map(
                    item-> {
                        SensorComb sensorComb = SensorComb.generate(item);
                        return  sensorComb;
                    }
            ).collect(Collectors.toList());
            rtdLayout.setCoreMonitorItems(coreSensorCombList);
            // 查询固定区
            example.clear();
            example.or().andEqualTo("pageType",pageType)
                    .andEqualTo("organizationId",orgId)
                    .andEqualTo("pagePosition",RealTimeDeviceLayout.FixedMonitorItems);
            List<OrganizationLayout> fixedMonitorItems = orgLayoutMapper.selectByExample(example);
            List<SensorComb> fixedSensorCombList = fixedMonitorItems.stream().map(
                    item-> {
                        SensorComb sensorComb = SensorComb.generate(item);
                        return  sensorComb;
                    }
            ).collect(Collectors.toList());
            rtdLayout.setFixedMonitorItems(fixedSensorCombList);
            this.loadSensorToComb(rtdLayout);
            return  rtdLayout;
        }
        return  null;
    }
    private void loadSensorToComb(RealTimeDeviceLayout rtdLayout) {
        List<SensorComb> allSensorCombList = new ArrayList<>();
        allSensorCombList.addAll(rtdLayout.getCoreMonitorItems());
        allSensorCombList.addAll(rtdLayout.getDefaultMonitorItems());
        allSensorCombList.addAll(rtdLayout.getFixedMonitorItems());
        List<String> sensorKeyList = allSensorCombList.stream().map(
                item -> {
                    return item.getSensorKey();
                }
        ).collect(Collectors.toList());
        Example example = new Example(Sensor.class);
        example.or().andIn("sensorKey",sensorKeyList);
        List<Sensor> sensorList = sensorMapper.selectByExample(example);
        for (SensorComb sensorComb: allSensorCombList) {
              for (Sensor sensor :sensorList) {
                  if(sensor.getSensorKey()!=null
                          &&sensor.getSensorKey().equals(sensorComb.getSensorKey())){
                        sensorComb.setSensor(sensor);
//                      sensorComb.setName(sensor.getDescription());
//                      sensorComb.setSensorId(sensor.getId());
//                      sensorComb.setSourceUnit(sensor.getUnit());
//                      sensorComb.setTargetUnit(sensor.getUnit());
                  }
              }
        }
    }
    public static  String getPageType(Integer version) {
      return  RealTimeDeviceLayout.PageTypePrefix + version.toString();
    }
    @Transient
    @Override
    public void  saveRtdLayout(RtdLayoutUpload rtdUpload) {
 
        String pageType = getPageType(rtdUpload.getVersion());
        Integer orgId = rtdUpload.getOrganizationId();
        RealTimeDeviceLayout rtdLayout = rtdUpload.getRealTimeDeviceLayout();
        // 清空旧数据
        this.deleteRtdLayout(orgId,rtdUpload.getVersion());
        OrganizationLayout orgLayout = new OrganizationLayout();
        orgLayout.setOrganizationId(orgId);
        orgLayout.setPageType(pageType);
        //  储存chartSensorKey
        orgLayout.setPagePosition(RealTimeDeviceLayout.ChartSensorKey);
        orgLayout.setPagePositionIndex(0);
        orgLayout.setPagePositionValue(rtdLayout.getChartSensorKey());
        orgLayoutMapper.insert(orgLayout);
        // 储存核心区
        List<SensorComb> coreSensorList = rtdLayout.getCoreMonitorItems();
        orgLayout.setPagePosition(RealTimeDeviceLayout.CoreMonitorItems);
        if( coreSensorList !=null ) {
            for(int index=0;index< coreSensorList.size();index++) {
                orgLayout.setPagePositionIndex(index);
                orgLayout.setPagePositionValue(coreSensorList.get(index).getSensorKey());
                orgLayoutMapper.insert(orgLayout);
            }
        }
        // 储存核心区
        List<SensorComb> defaultSensorList = rtdLayout.getDefaultMonitorItems();
        orgLayout.setPagePosition(RealTimeDeviceLayout.DefaultMonitorItems);
        if ( defaultSensorList != null ){
            for(int index=0;index< defaultSensorList.size();index++) {
                orgLayout.setPagePositionIndex(index);
                orgLayout.setPagePositionValue(defaultSensorList.get(index).getSensorKey());
                orgLayoutMapper.insert(orgLayout);
            }
        }
 
        // 储存固定区
        List<SensorComb> fixedSensorList = rtdLayout.getFixedMonitorItems();
        orgLayout.setPagePosition(RealTimeDeviceLayout.FixedMonitorItems);
        if( fixedSensorList != null ) {
            for(int index=0;index< fixedSensorList.size();index++) {
                orgLayout.setPagePositionIndex(index);
                orgLayout.setPagePositionValue(fixedSensorList.get(index).getSensorKey());
                orgLayoutMapper.insert(orgLayout);
            }
        }
    }
    @Override
    public  void  deleteRtdLayout(Integer orgId, Integer version) {
        // 清空旧数据
        OrganizationLayout query = new OrganizationLayout();
        query.setOrganizationId(orgId);
        query.setPageType(getPageType(version));
        orgLayoutMapper.delete(query);
    }
    @Override
    public  RealTimeDeviceLayout queryRtdLayoutWithUnit(Integer orgId, Integer versionNo) {
        String pageType = getPageType(versionNo);
        if(queryPageConfigCountByOrgId(orgId,pageType) == 0 ){
            // orgId = dataMapper.selectSupperOrgId();
            orgId = 0;
        }
        RealTimeDeviceLayout rtdLayout = queryRealTimeDeviceLayout(orgId,versionNo);
        loadUnitToComb(orgId,rtdLayout);
        return  rtdLayout;
    }
   private List<Integer> getVersionNosSort() {
         List<Integer> versionNoList = new ArrayList<>();
         Optional<String> jsonString = queryDictionaryDataValue(page_layout_versions_sort);
         if( jsonString.isPresent() ) {
             versionNoList = JSON.parseArray(jsonString.get(),Integer.class);
         }
         return  versionNoList;
   }
    @Override
    public RealTimeDeviceLayout queryRtdLayoutWithUnit(Integer orgId, List<Integer> versionNos) {
        RealTimeDeviceLayout rtdLayout = null;
        if(versionNos.size() == 1) {
            Integer versionNo = versionNos.get(0);
            rtdLayout = queryRtdLayoutWithUnit(orgId,versionNo);
        } else {
            List<Integer> versionNosSort = getVersionNosSort();
            Integer resultVersion = null;
            for( int num =0 ; num < versionNosSort.size(); num++ ) {
                Integer version =versionNosSort.get(num);
                for (Integer ver:versionNos) {
                    if(ver == version) {
                        resultVersion = ver;
                        versionNos.remove(ver);
                        break;
                    }
                }
                if(resultVersion!=null) {
                    break;
                }
            }
            if(resultVersion != null ) {
                rtdLayout = queryRtdLayoutWithUnit(orgId,resultVersion);
                if (rtdLayout !=null) {
                    List<Sensor> sensorList = sensorMapper.selectByVersionNos(versionNos);
                    List<SensorComb> allMonitorItems =  new ArrayList<>();
                    allMonitorItems.addAll(rtdLayout.getDefaultMonitorItems());
                    allMonitorItems.addAll(rtdLayout.getCoreMonitorItems());
                    if(allMonitorItems.size()>0 && sensorList!=null) {
                        List<SensorComb> sensrCombPatchs =  new ArrayList<>();
                        List<String> fixedItems = getFixedItemKeys();
                        sensorList
                                // 排除固定项
                                .stream().filter(
                                item -> {
                                    return !fixedItems.stream().anyMatch(
                                            key-> key.equals(item.getSensorKey())
                                    );
                                }
                        ).collect(Collectors.toList()).forEach(sensor -> {
                            boolean isPath = !allMonitorItems.stream().anyMatch(
                                    sensorComb -> sensorComb.getSensorId() ==sensor.getId()
                            );
                            if(isPath) {
                                SensorComb sensorComb = SensorComb.generate(sensor);
                                sensrCombPatchs.add(sensorComb);
                            }
                        });
                        if( sensrCombPatchs.size() > 0 ) {
                            RealTimeDeviceLayout rtdLayoutTemp = new RealTimeDeviceLayout();
                            rtdLayoutTemp.setDefaultMonitorItems(sensrCombPatchs);
                            loadUnitToComb(orgId,rtdLayoutTemp);
                            rtdLayout.getDefaultMonitorItems()
                                    .addAll(rtdLayoutTemp.getDefaultMonitorItems());
                        }
                    }
                }
            }
        }
 
        return rtdLayout;
    }
 
    // 装载单位信息到布局配置
    private void loadUnitToComb(Integer orgId,RealTimeDeviceLayout rtdLayout) {
        List<SensorComb> allList = new ArrayList<>();
        allList.addAll(rtdLayout.getCoreMonitorItems());
        allList.addAll(rtdLayout.getDefaultMonitorItems());
        allList.addAll(rtdLayout.getFixedMonitorItems());
        List<OrganizationSensorUnit> orgUnitList = orgUnitMapper.selectByOrgId(orgId);
        allList.forEach(item -> {
              for(OrganizationSensorUnit orgUnit: orgUnitList) {
                  SensorUnit sensorUnit = orgUnit.getSensorUnit();
                  if( sensorUnit!=null && sensorUnit.getSensorId() !=null) {
                        if(sensorUnit.getSensorId() == item.getSensorId()) {
                            item.setUnit(sensorUnit);
//                            item.setTargetUnit(sensorUnit.getName());
//                            item.setEvaluator(sensorUnit.getRules());
                        }
                  }
              }
        });
    }
}