fengxiang
2018-01-16 ea9f0a00572fbee090305dd66b7e2db26834bbd3
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
package com.moral.service.impl;
 
import com.moral.common.bean.Constants;
import com.moral.common.util.RedisUtils;
import com.moral.entity.alarm.AlarmConfig;
import com.moral.entity.alarm.AlarmConfigValue;
import com.moral.entity.alarm.AlarmMode;
import com.moral.entity.alarm.AlarmSensorLevel;
import com.moral.mapper.AlarmConfigMapper;
import com.moral.service.AlarmConfigService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service
public class AlarmConfigServiceImpl implements AlarmConfigService{
    @Resource
    RedisUtils redisUtils;
    @Resource
    AlarmConfigMapper alarmConfigMapper;
    @Override
    public AlarmConfig SelectByOrganizationId(int organizationId) {
         return alarmConfigMapper.selectByOrganizationId(organizationId);
    }
 
    @Override
    public void addOrModify(AlarmConfig alarmConfig) {
        try{
            if(alarmConfig.getId()==null){
                alarmConfig.setCreateTime(new Date());
                alarmConfig.setUpdateTime(new Date());
                alarmConfigMapper.insertSelective(alarmConfig);
            }else{
                alarmConfig.setUpdateTime(new Date());
                alarmConfigMapper.updateByPrimaryKeySelective(alarmConfig);
            }
            String key = RedisUtils.getAlarmKey(alarmConfig.getOrganizationId());
            redisUtils.set(key,simplifyAlarmConfigValue(alarmConfig.getValue()));
        }
        catch (Exception ex){
            throw  ex;
        }
    }
    public AlarmConfigValue simplifyAlarmConfigValue(AlarmConfigValue value){
          Map<String,AlarmSensorLevel> alarmSensorLevelMap = value.getAlarmLevels();
          List<String> cleanKeys = new ArrayList<>();
          for(String key:alarmSensorLevelMap.keySet()){
                 AlarmSensorLevel alarmSensorLevel = alarmSensorLevelMap.get(key);
                 if(!alarmSensorLevel.isEnable()){
                     cleanKeys.add(key);
                 }else{
                     List<Float> increments = alarmSensorLevel.getIncrement();
                     if(increments!=null){
                         boolean isIncrement =  increments.size()<3||
                                 (increments.get(0)==0&&increments.get(1)==0&&increments.get(2)==0);
                         if(isIncrement){
                             alarmSensorLevel.setIncrement(null);
                         }
                     }
                     List<Float> degressions = alarmSensorLevel.getDegression();
                     if(degressions!=null){
                         boolean isDegression =  degressions.size()<3||
                                 (degressions.get(0)==0&&degressions.get(1)==0&&degressions.get(2)==0);
                         if(isDegression){
                             alarmSensorLevel.setDegression(null);
                         }
                     }
                 }
          }
          for (String key:cleanKeys){
              alarmSensorLevelMap.remove(key);
          }
          AlarmMode alarmMode = value.getAlarmMode();
          if(!alarmMode.isEnable()){
              alarmMode.setLevel1(null);
              alarmMode.setLevel2(null);
              alarmMode.setLevel3(null);
          }else{
              if(alarmMode.getLevel1()!=null&&alarmMode.getLevel1().isEmpty()){
                  alarmMode.setLevel1(null);
              }
              if(alarmMode.getLevel2()!=null&&alarmMode.getLevel2().isEmpty()){
                  alarmMode.setLevel2(null);
              }
              if(alarmMode.getLevel3()!=null&&alarmMode.getLevel3().isEmpty()){
                  alarmMode.setLevel3(null);
              }
          }
          return value;
    }
}