whycxzp
2025-04-14 8557f4a954278b76a3054ee1edbb93f656b533ba
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
package com.whyc.service;
 
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.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.DevInfDto;
import com.whyc.dto.Response;
import com.whyc.mapper.DevA200RtStateMapper;
import com.whyc.mapper.ActmRealStateMapper;
import com.whyc.mapper.BattGroupBaojiGroupMapper;
import com.whyc.mapper.DevLithiumInfMapper;
import com.whyc.pojo.db_lithium_ram_db.DevA200RtState;
import com.whyc.pojo.db_lithium_ram_db.ActmRealState;
import com.whyc.pojo.db_lithium_ram_db.DevLithiumInf;
import com.whyc.pojo.db_user.BattGroupBaojiGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class DevLithiumInfService {
    @Autowired(required = false)
    private DevLithiumInfMapper mapper;
 
    @Autowired(required = false)
    private DevA200RtStateMapper a200Mapper;
 
    @Autowired(required = false)
    private ActmRealStateMapper actmMapper;
 
    @Autowired(required = false)
    private BattGroupBaojiGroupMapper battBaojiMapper;
 
    //添加设备
    @Transactional
    public Response addDev(DevLithiumInf devInf) {
        QueryWrapper wrapper=new QueryWrapper();
        //判断设备类型生成devId
        int devId=getDevId(devInf.getDevType());
        devInf.setDevId(devId);
        devInf.setCreateTime(new Date());
        int bl=mapper.insert(devInf);
        if(bl>0){
            if(devInf.getBaojiIdList()!=null){
                List<BattGroupBaojiGroup> listBj=new ArrayList<>();
                for (int baojiId:devInf.getBaojiIdList()) {
                    BattGroupBaojiGroup baojigroup=new BattGroupBaojiGroup();
                    baojigroup.setDevId(devId);
                    baojigroup.setBaojiGroupId(baojiId);
                    listBj.add(baojigroup);
                }
                //将设备添加进指定的包机组
                battBaojiMapper.insertBatchSomeColumn(listBj);
            }
        }
        return new Response().set(1,bl>0);
    }
   //判断设备类型生成devId
    private int getDevId(Integer devType) {
        //查询该类型最大设备编号
        Integer devId=mapper.getMaxDevId(devType);
        if(devId==null){
            switch (devType){
                case 1:devId=100000000;break;
                case 2:devId=200000000;break;
            }
        }
        return devId+1;
    }
 
    //获取所有的设备信息
    public Response getAllInf(Integer uid, DevInfDto devInfDto) {
        Map<String, Object> allMap = new HashMap<>();
        PageHelper.startPage(devInfDto.getPageNum(), devInfDto.getPageSize());
        devInfDto.setUid(uid);
        List<DevLithiumInf> listype=mapper.getAllInf(devInfDto);
        PageInfo pageInfo=new PageInfo(listype);
 
        List<DevLithiumInf> list=mapper.getLine(uid);
        Map<Integer, List<DevLithiumInf>> typeDevMap = list.stream().collect(Collectors.groupingBy(DevLithiumInf::getDevType));
        Map<Integer, List<DevLithiumInf>> onlineDevMap = list.stream().collect(Collectors.groupingBy(DevLithiumInf::getDevOnline));
        Map<Integer, Object> typeMap = new HashMap<>();
        typeMap.put(1,0);
        typeMap.put(2,0);
        Map<Integer, Object> onlineMap = new HashMap<>();
        onlineMap.put(0,0);
        onlineMap.put(1,0);
        int devSum = list.size();
        for (Integer type : typeDevMap.keySet()) {
            typeMap.put(type, typeDevMap.get(type).size());
        }
        for (Integer online : onlineDevMap.keySet()) {
            onlineMap.put(online, onlineDevMap.get(online).size());
        }
        allMap.put("type",typeMap);
        allMap.put("onlineMap",onlineMap);
        allMap.put("devSum",devSum);
        allMap.put("pageInfo",pageInfo);
 
        return new Response().setII(1,listype!=null,allMap,"获取所有的设备信息");
    }
 
    //获取左侧列表
    public Response getDevByType(Integer devType) {
        QueryWrapper wrapper=new QueryWrapper();
        if (devType!=null){
            wrapper.eq("dev_type",devType);
        }
        wrapper.orderByAsc("dev_id");
        List<DevLithiumInf> list=mapper.selectList(wrapper);
        return new Response().setII(1,list!=null,list,"获取左侧列表");
    }
   //获取左侧泪飙
    public Response getDevType(int uid) {
        Map<String, Object> allMap = new HashMap<>();
        List<DevLithiumInf> a200List=mapper.getDevType(uid,1);
        for (DevLithiumInf a200:a200List) {
            QueryWrapper a200wrapper= Wrappers.query();
            a200wrapper.eq("dev_id",a200.getDevId());
            a200wrapper.last("limit 1");
            DevA200RtState a200state=a200Mapper.selectOne(a200wrapper);
            //a200.setA200sTate(a200state);
            a200.setState(a200state!=null?a200state:null);
        }
        List<DevLithiumInf> actmList=mapper.getDevType(uid,2);
        for (DevLithiumInf actm:actmList) {
            QueryWrapper actmwrapper= Wrappers.query();
            actmwrapper.eq("dev_id",actm.getDevId());
            List<ActmRealState> actmstates=actmMapper.selectList(actmwrapper);
            //actm.setActmsTate(actmstate);
            actm.setActmstates(actmstates!=null?actmstates:null);
        }
        allMap.put("a200List",a200List);
        allMap.put("actmList",actmList);
        return new Response().setII(1,allMap.size()>0,allMap,"获取左侧列表");
    }
    //编辑设备信息
    public Response updateInf(DevLithiumInf dinf) {
        //编辑设备就重置加载
        dinf.setReloadEn(1);
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("dev_id",dinf.getDevId());
        int flag=mapper.update(dinf,wrapper);
        return new Response().set(1,flag>0,"编辑设备信息");
    }
    //获取设备信息(不分页用于包机组)
    public Response getDevInf() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByAsc("dev_id");
        List<DevLithiumInf> list=mapper.selectList(wrapper);
        return new Response().setII(1,list!=null,list,"获取设备信息(不分页用于包机组)");
    }
 
    //根据设备id获取设备信息
    public Response getDevInfById(Integer devId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("dev_id",devId);
        wrapper.last("limit 1");
        DevLithiumInf dinf=mapper.selectOne(wrapper);
        return new Response().setII(1,dinf!=null,dinf,"根据设备id获取设备信息");
    }
    //删除设备(同时删除实时和包机组信息)
    @Transactional
    public Response delDevInf(Integer devId) {
        //先删除包机组信息
        UpdateWrapper baojiWrapper=new UpdateWrapper();
        baojiWrapper.eq("dev_id",devId);
        battBaojiMapper.delete(baojiWrapper);
        //再删除实时信息
        if(devId/100000000==1){
            UpdateWrapper a200Wrapper=new UpdateWrapper();
            a200Wrapper.eq("dev_id",devId);
            a200Mapper.delete(a200Wrapper);
        }else{
            UpdateWrapper actmWrapper=new UpdateWrapper();
            actmWrapper.eq("dev_id",devId);
            actmMapper.delete(actmWrapper);
        }
        //最后删除设备信息
        UpdateWrapper dinfWrapper=new UpdateWrapper();
        dinfWrapper.eq("dev_id",devId);
        mapper.delete(dinfWrapper);
        return new Response().set(1,true,"删除设备(同时删除实时和包机组信息)");
    }
    //强制移除批量设备
    @Transactional
    public Response cancelContPl(List<Integer> devIds) {
        for (Integer devId:devIds) {
            UpdateWrapper wrapper=new UpdateWrapper();
            wrapper.eq("dev_id",devId);
            wrapper.set("batch_state",0);
            mapper.update(null,wrapper);
        }
        return new Response().set(1,true,"强制移除批量设备");
    }
    //设备分类总数统计
    public Map<String,Object> getDevStaticByType(int userId) {
        Map<String,Object> map=new HashMap<>();
        Map<Integer,Integer> staticTypeMap=new HashMap<>();
        staticTypeMap.put(1,0);
        staticTypeMap.put(2,0);
 
        Map<Integer,Integer> a200staticStateMap=new HashMap<>();
        a200staticStateMap.put(0,0);
        a200staticStateMap.put(1,0);
        Map<Integer,Integer> actmstaticStateMap=new HashMap<>();
        actmstaticStateMap.put(0,0);
        actmstaticStateMap.put(1,0);
        Map<String,Object> typestateMap=new HashMap<>();
        typestateMap.put("a200",a200staticStateMap);
        typestateMap.put("actm",actmstaticStateMap);
        List<DevLithiumInf> list=mapper.getDevStaticByType(userId);
        Map<Integer, List<DevLithiumInf>> typeMap = list.stream().collect(Collectors.groupingBy(DevLithiumInf::getDevType));
        for (Integer type : typeMap.keySet()) {
            staticTypeMap.put(type, typeMap.get(type).size());
            List<DevLithiumInf> typeList= typeMap.get(type);
            Map<Integer, List<DevLithiumInf>> stateMap = typeList.stream().collect(Collectors.groupingBy(DevLithiumInf::getDevOnline));
            if(type==1){
                for (Integer state : stateMap.keySet()) {
                    a200staticStateMap.put(state, stateMap.get(state).size());
                }
                typestateMap.put("a200", a200staticStateMap);
            }
            if(type==2){
                for (Integer state : stateMap.keySet()) {
                    actmstaticStateMap.put(state, stateMap.get(state).size());
                }
                typestateMap.put("actm", actmstaticStateMap);
            }
        }
        map.put("dinf",list!=null?list:null);
        map.put("type",staticTypeMap);
        map.put("state",typestateMap);
        return map;
    }
 
    //根据devId获取设备信息
    public DevLithiumInf getDinfByDevId(int devId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("dev_id",devId);
        wrapper.last("limit 1");
        DevLithiumInf dinf=mapper.selectOne(wrapper);
        return dinf;
    }
 
}