whyclxw
2025-05-08 4c0f551e508bce3e853737a57a7cb09c0fbbb56f
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.dto.AlarmDaoFactory;
import com.whyc.dto.GroupWithStationAndAlarmDTO;
import com.whyc.dto.Response;
import com.whyc.mapper.BaoJiGroupMapper;
import com.whyc.mapper.BaoJiGroupUserMapper;
import com.whyc.mapper.BaoJiGroupBattGroupMapper;
import com.whyc.mapper.CommonMapper;
import com.whyc.pojo.*;
import com.whyc.util.ActionUtil;
import com.whyc.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class BaoJiGroupService {
 
    @Resource
    private BaoJiGroupMapper mapper;
 
    @Resource
    private BaoJiGroupUserMapper baoJiGroupUserMapper;
 
    @Resource
    private BaoJiGroupBattGroupMapper BaoJiGroupBattGroupMapper;
 
    @Resource
    private CommonMapper commonMapper;
 
    @Autowired
    private BattInfService battInfService;
 
    @Autowired
    private BattalarmDataService battAlarmDataService;
 
    @Autowired
    private DevalarmDataService deviceAlarmDataService;
 
    @Autowired
    private PwrdevAlarmService powerAlarmService;
 
    @Autowired
    private PermitGroupService permitGroupService;
 
    public void add(String groupName) {
        BaoJiGroup baoJiGroup = new BaoJiGroup();
        baoJiGroup.setBaoJiGroupName(groupName);
        baoJiGroup.setDischargePlanFlag(0);
 
        mapper.insert(baoJiGroup);
    }
 
    public void update(BaoJiGroup baoJiGroup) {
        mapper.updateById(baoJiGroup);
    }
 
    public void delete(long baoJiGroupId) {
        //删除包机组
        mapper.deleteById(baoJiGroupId);
        //删除关联的用户
        UpdateWrapper<BaoJiGroupUser> userWrapper = Wrappers.update();
        userWrapper.eq("baoji_group_id",baoJiGroupId);
        baoJiGroupUserMapper.delete(userWrapper);
        //删除关联的站点/电池组
        UpdateWrapper<BaojiGroupBattGroup> battGroupWrapper = Wrappers.update();
        battGroupWrapper.eq("baoji_group_id",baoJiGroupId);
        BaoJiGroupBattGroupMapper.delete(battGroupWrapper);
    }
 
    public Response getBaoJiGroup() {
        //验证是否存在usr_query_permit权限
        if (!permitGroupService.checkUserPermitWithName("usr_query_permit")) {
            return new Response<Boolean>().set(1,false, "当前用户没有查询权限");
        }
        List<BaoJiGroup> list=mapper.selectList((Wrapper<BaoJiGroup>) ActionUtil.objeNull);
        return new Response<List<BaoJiGroup>>().set(1,list);
    }
 
    /**
     * 每次设置放电标识时,提前校验组内是否与其他标识班组存在相同站点,存在则提示,不修改
     * @param baoJiGroupId
     * @param flag
     * @return
     */
    public Response updateDischargeFlag(Long baoJiGroupId, Integer flag) {
        if(flag==1) {
            //查询要设置的包机组的站点
            QueryWrapper<BaojiGroupBattGroup> query = Wrappers.query();
            query.select("baoji_group_id", "StationId").eq("baoji_group_id", baoJiGroupId);
            List<BaojiGroupBattGroup> stationIdList = BaoJiGroupBattGroupMapper.selectList(query);
            stationIdList = stationIdList.stream().distinct().collect(Collectors.toList());
            //查询其他的组,包含的所有站点
            List<BaojiGroupBattGroup> stationIdListWithDischargePlanFlag = BaoJiGroupBattGroupMapper.getStationIdListWithDischargePlanFlag();
            stationIdListWithDischargePlanFlag = stationIdListWithDischargePlanFlag.stream().distinct().collect(Collectors.toList());
            List<BaojiGroupBattGroup> repeatedStationIdList = new LinkedList<>();
            for (int i = 0; i < stationIdList.size(); i++) {
                BaojiGroupBattGroup baojiGroupBattGroup = stationIdList.get(i);
                for (int j = 0; j < stationIdListWithDischargePlanFlag.size(); j++) {
                    BaojiGroupBattGroup baojiGroupBattGroupWithFlag = stationIdListWithDischargePlanFlag.get(j);
                    if (baojiGroupBattGroupWithFlag.getStationId().equals(baojiGroupBattGroup.getStationId())) {
                        repeatedStationIdList.add(baojiGroupBattGroupWithFlag);
                        break;
                    }
                }
            }
            //查询重复站点的信息
            if (repeatedStationIdList.size() > 0) {
                return new Response().setII(1, false, repeatedStationIdList, "设置失败");
            }
        }
        UpdateWrapper<BaoJiGroup> update = Wrappers.update();
        update.set("discharge_plan_flag",flag).eq("baoji_group_id",baoJiGroupId);
        mapper.update((BaoJiGroup) ActionUtil.objeNull,update);
        return new Response().set(1,true,"设置完成");
    }
 
    public List<BaoJiGroup> getBaoJiGroupNameListByFlag(int dischargePlanFlag) {
        QueryWrapper<BaoJiGroup> query = Wrappers.query();
        query.eq("discharge_plan_flag",dischargePlanFlag);
        return mapper.selectList(query);
    }
 
    public Response getGroupWithStationAndAlarmForScreen(int userId) {
        try {
            List<GroupWithStationAndAlarmDTO> list = mapper.getGroupWithStation(userId);
            list.forEach(item -> {
                List<StationInf> stationInfList = item.getStationInfList();
                List<StationInf> stationInfList2 = stationInfList.stream().collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(StationInf::getTogetherFlag))),
                        ArrayList::new));
                item.setStationInfList(stationInfList2);
                item.setStationCount(stationInfList2.size());
                List<String> stationIdList = stationInfList.stream().map(StationInf::getStationId).collect(Collectors.toList());
                //电池
                int battAlarmCount = battAlarmDataService.getCountByStationIds(stationIdList,1);
                item.setBattAlarmCount(battAlarmCount);
                //设备
                int deviceAlarmCount = deviceAlarmDataService.getCountByStationIds(stationIdList,1);
                item.setDeviceAlarmCount(deviceAlarmCount);
                //电源
                int powerAlarmCount = powerAlarmService.getCountByStationIds(stationIdList,1);
                item.setPowerAlarmCount(powerAlarmCount);
            });
 
            return new Response().set(1, list);
        }catch (Exception e){
            return new Response<>().set(1,false,"发生异常:"+e.getCause());
        }
    }
    //山西晋源特定接口
    public Response getGroupWithStationAndAlarmForScreenJY() {
        try {
            List<GroupWithStationAndAlarmDTO> list = mapper.getGroupWithStationJY();
            list.forEach(item -> {
                List<StationInf> stationInfList = item.getStationInfList();
                List<StationInf> stationInfList2 = stationInfList.stream().collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(StationInf::getTogetherFlag))),
                        ArrayList::new));
                item.setStationInfList(stationInfList2);
                item.setStationCount(stationInfList2.size());
                List<String> stationIdList = stationInfList.stream().map(StationInf::getStationId).collect(Collectors.toList());
                //电池
                int battAlarmCount = battAlarmDataService.getCountByStationIds(stationIdList,1);
                item.setBattAlarmCount(battAlarmCount);
                //设备
                int deviceAlarmCount = deviceAlarmDataService.getCountByStationIds(stationIdList,1);
                item.setDeviceAlarmCount(deviceAlarmCount);
                //电源
                int powerAlarmCount = powerAlarmService.getCountByStationIds(stationIdList,1);
                item.setPowerAlarmCount(powerAlarmCount);
            });
 
            return new Response().set(1, list);
        }catch (Exception e){
            return new Response<>().set(1,false,"发生异常:"+e.getCause());
        }
    }
 
    public Response getGroupWithStationAndAlarm(int userId) {
        try {
            List<GroupWithStationAndAlarmDTO> list = mapper.getGroupWithStation(userId);
            list.forEach(item -> {
                List<StationInf> stationInfList = item.getStationInfList();
                List<StationInf> stationInfList2 = stationInfList.stream().collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(StationInf::getTogetherFlag))),
                        ArrayList::new));
                //站点合并统计机房数
                item.setStationCount(stationInfList2.size());
                List<String> stationIdList = stationInfList.stream().map(StationInf::getStationId).collect(Collectors.toList());
                //电池
                List<BattalarmData> battAlarmList = battAlarmDataService.getListByStationIds(stationIdList);
                for (BattalarmData data : battAlarmList) {
                    data.setAlarmName(AlarmDaoFactory.getAllAlarmName(data.getAlmSignalId()));
                }
                item.setBattAlarmCount(battAlarmList.size());
                //设备
                List<DevalarmData> deviceAlarmList = deviceAlarmDataService.getListByStationIds(stationIdList);
                for (DevalarmData data : deviceAlarmList) {
                    data.setAlarmName(AlarmDaoFactory.getAllAlarmName(data.getAlmType()));
                }
                item.setDeviceAlarmCount(deviceAlarmList.size());
                //电源
                List<PwrdevAlarm> powerAlarmList = powerAlarmService.getListByStationIds(stationIdList);
                for (PwrdevAlarm data : powerAlarmList) {
                    data.setAlarmName(AlarmDaoFactory.getAllAlarmName(data.getAlmType()));
                    data.setPwrAlmFlag((int) (data.getPowerDeviceId()/1000000));
                }
                item.setPowerAlarmCount(powerAlarmList.size());
                //遍历每个告警列表,如果与站点相符,则加入到站点里面
                for (BattalarmData alarm : battAlarmList) {
                    for (StationInf stationInf : stationInfList) {
                        if(alarm.getStationId().equals(stationInf.getStationId())) {
                            List<BattalarmData> containList = stationInf.getBattAlarmList();
                            if(containList == null){
                                containList = new LinkedList<>();
                            }
                            containList.add(alarm);
                            stationInf.setBattAlarmList(containList);
                            stationInf.setAlarmFlag(1);
                            break;
                        }
                    }
                }
                for (DevalarmData alarm : deviceAlarmList) {
                    for (StationInf stationInf : stationInfList) {
                        if(alarm.getStationId().equals(stationInf.getStationId())) {
                            List<DevalarmData> containList = stationInf.getDeviceAlarmList();
                            if(containList == null){
                                containList = new LinkedList<>();
                            }
                            containList.add(alarm);
                            stationInf.setDeviceAlarmList(containList);
                            stationInf.setAlarmFlag(1);
                            break;
                        }
                    }
                }
                for (PwrdevAlarm alarm : powerAlarmList) {
                    for (StationInf stationInf : stationInfList) {
                        if(alarm.getStationId().equals(stationInf.getStationId())) {
                            List<PwrdevAlarm> containList = stationInf.getPowerAlarmList();
                            if(containList == null){
                                containList = new LinkedList<>();
                            }
                            containList.add(alarm);
                            stationInf.setPowerAlarmList(containList);
                            stationInf.setAlarmFlag(1);
                            break;
                        }
                    }
                }
                List<StationInf> stationInfListOrdered = stationInfList.stream().sorted(Comparator.comparing(StationInf::getAlarmFlag).reversed()).collect(Collectors.toList());
                item.setStationInfList(stationInfListOrdered);
            });
            return new Response().set(1, list);
        }catch (Exception e){
            return new Response<>().set(1,false,"发生异常:"+e.getCause());
        }
    }
}