lizijie
2021-12-09 348adf3f47bc9765b317e83b3c61981172e9200f
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
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.AlarmInfo;
import com.moral.api.entity.Device;
import com.moral.api.entity.Organization;
import com.moral.api.mapper.AlarmInfoMapper;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.service.AlarmInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.OrganizationService;
import com.moral.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-11-10
 */
@Service
public class AlarmInfoServiceImpl extends ServiceImpl<AlarmInfoMapper, AlarmInfo> implements AlarmInfoService {
 
    @Autowired
    private OrganizationService organizationService;
 
    @Autowired(required = false)
    private DeviceMapper deviceMapper;
 
    @Autowired(required = false)
    private AlarmInfoMapper alarmInfoMapper;
 
    @Override
    public Map<String, Object> getDataByCondition(Map<String, Object> parameters) {
        Map<String, Object> resultMap = new HashMap<>();
        int orgId = Integer.parseInt(parameters.get("organization_id").toString());
        //定义一个集合,存放所有id
        List<Integer> allOrgId = new ArrayList<>();
        allOrgId.add(orgId);
        //循环集合
        //所有子组织
        List<Organization> allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId);
        if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1){
            for (Organization organization:allChildrenOrganization) {
                allOrgId.add(organization.getId());
            }
        }
        //集合去重
        List<Integer> allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList());
        //声明一个list,存放设备id
        List<Integer> deviceIdList = new ArrayList<>();
        for (Integer orgIdWithoutDuplicates:allOrgIdWithoutDuplicates) {
            //根据id查询所属设备
            QueryWrapper<Device> wrapper_device = new QueryWrapper<>();
            wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgIdWithoutDuplicates);
            List<Device> devices = new ArrayList<>();
            devices = deviceMapper.selectList(wrapper_device);
            if (devices.size()>0){
                for (Device device:devices) {
                    int deviceId = device.getId();
                    deviceIdList.add(deviceId);
                }
            }else {
                continue;
            }
        }
        if (deviceIdList.size()>0){
            Map<String, Object> map = new HashMap<>();
            QueryWrapper<AlarmInfo> alarmInfoQueryWrapper = new QueryWrapper<>();
            map.put("deviceIds",deviceIdList);
            alarmInfoQueryWrapper.in("device_id",deviceIdList);
            int size = Integer.parseInt(parameters.get("size").toString());
            int current = Integer.parseInt(parameters.get("current").toString());
            int start = (current-1)*size;
            map.put("size",size);
            map.put("start",start);
            String startTime = parameters.get("startTime")+" 00:00:00";
            String endTime = parameters.get("endTime")+" 23:59:59";
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            alarmInfoQueryWrapper.between("alarm_time", startTime, endTime);
            String index = parameters.get("index").toString();
            if (!index.equals("all")){
                map.put("index",index);
                alarmInfoQueryWrapper.eq("`index`", index);
            }
            String alarmType = parameters.get("alarmType").toString();
            switch (alarmType){
                case "overrun" : map.put("alarmType","超限");
                    alarmInfoQueryWrapper.eq("alarm_type","超限");
                    break;
                case "sudden" : map.put("alarmType","突然高");
                    alarmInfoQueryWrapper.eq("alarm_type","突然高");
                    break;
                case "state100" : map.put("alarmType","超过国控站点100%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过国控站点100%");
                    break;
                case "state150" : map.put("alarmType","超过国控站点150%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过国控站点150%");
                    break;
                case "state250" : map.put("alarmType","超过国控站点250%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过国控站点250%");
                    break;
                case "city150" : map.put("alarmType","超过市区均值150%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过市区均值150%");
                    break;
                case "city250" : map.put("alarmType","超过市区均值250%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过市区均值250%");
                    break;
                default:break;
            }
            List<Map<String, Object>> resultList = alarmInfoMapper.selectDataByCondition(map);
            SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map<String, Object> alarmInfo:resultList) {
                String alarm_time = SDF.format(alarmInfo.get("alarm_time"));
                alarmInfo.put("alarm_time",alarm_time);
            }
            Integer totalNumber = alarmInfoMapper.selectCount(alarmInfoQueryWrapper);
            resultMap.put("totalNumber", totalNumber);
            resultMap.put("alarmInfos", resultList);
            resultMap.put("current",current);
            resultMap.put("size",size);
            int totalPageNumber = totalNumber/size;
            if(totalNumber%size != 0){
                totalPageNumber += 1;
            }
            resultMap.put("totalPageNumber",totalPageNumber);
            return resultMap;
        }
        return null;
    }
 
    @Override
    public Map<String, Object> alarmReminder(Map<String, Object> parameters) {
        Map<String,Object> resultMap = new HashMap<>();
        int orgId = Integer.parseInt(parameters.get("organization_id").toString());
        //定义一个集合,存放所有id
        List<Integer> allOrgId = new ArrayList<>();
        allOrgId.add(orgId);
        //循环集合
        //所有子组织
        List<Organization> allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId);
        if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1){
            for (Organization organization:allChildrenOrganization) {
                allOrgId.add(organization.getId());
            }
        }
        //集合去重
        List<Integer> allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList());
        //声明一个list,存放设备id
        List<Integer> deviceIdList = new ArrayList<>();
        for (Integer orgIdWithoutDuplicates:allOrgIdWithoutDuplicates) {
            //根据id查询所属设备
            QueryWrapper<Device> wrapper_device = new QueryWrapper<>();
            wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgIdWithoutDuplicates);
            List<Device> devices = new ArrayList<>();
            devices = deviceMapper.selectList(wrapper_device);
            if (devices.size()>0){
                for (Device device:devices) {
                    int deviceId = device.getId();
                    deviceIdList.add(deviceId);
                }
            }else {
                continue;
            }
        }
        if (deviceIdList.size()>0){
            Map<String, Object> map = new HashMap<>();
            map.put("deviceIds",deviceIdList);
            int size = Integer.parseInt(parameters.get("size").toString());
            map.put("size",size);
            List<Map<String, Object>> resultList = alarmInfoMapper.selectNewestData(map);
            SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map<String, Object> alarmInfo:resultList) {
                String alarm_time = SDF.format(alarmInfo.get("alarm_time"));
                alarmInfo.put("alarm_time",alarm_time);
            }
            resultMap.put("alarmInfors",resultList);
            QueryWrapper<AlarmInfo> alarmInfoQueryWrapper = new QueryWrapper<>();
            alarmInfoQueryWrapper.in("device_id",deviceIdList);
            Integer totalNumber = alarmInfoMapper.selectCount(alarmInfoQueryWrapper);
            resultMap.put("totalNumber",totalNumber);
            return resultMap;
        }
        return null;
    }
}