whyclxw
2025-05-30 10a094864f75928f853e1ebb0311b0a20b858901
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.BattDto;
import com.whyc.dto.InfoDto;
import com.whyc.dto.Real.QuarterDto;
import com.whyc.dto.Response;
import com.whyc.dto.Statistic.StationStic;
import com.whyc.factory.InfoFactory;
import com.whyc.mapper.BattInfMapper;
import com.whyc.mapper.PowerInfMapper;
import com.whyc.mapper.StationInfMapper;
import com.whyc.pojo.db_station.BattInf;
import com.whyc.pojo.db_station.PowerInf;
import com.whyc.pojo.db_station.StationInf;
import com.whyc.pojo.db_user.User;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class BattInfService {
    @Autowired(required = false)
    private BattInfMapper mapper;
 
    @Autowired(required = false)
    private PowerInfMapper pinfMapper;
 
    @Autowired(required = false)
    private StationInfMapper sinfMapper;
 
    @Autowired(required = false)
    private BaojigroupService bjService;
 
    /*新建电池组(新的设备新的电池组)
     * @param binf
     * 1.在电源下新建设备,记录设备编号,devNum,设备名称为设备类型+devNum
     */
    @Transactional
    public void addDev(BattInf adddinf) {
        //检测电源下是否存在设备
        int devNum=mapper.getMaxDevNum(adddinf.getPowerId());
        if(devNum==0){
            devNum=1;
        }else{
            devNum+=1;
        }
        adddinf.setDevNum(devNum);
        adddinf.setDevName(adddinf.getDevType()+devNum);
        //获取对应的设备id,电池组
        int devId = mapper.getMaxdevId();
        int battGroupId=mapper.getMaxBattGroupId();
        if (devId == 0) {//数据库中没有站点
            devId = 10001;
        } else {
            devId += 1;
        }
        if (battGroupId == 0) {//数据库中没有站点
            battGroupId = 10001;
        } else {
            battGroupId += 1;
        }
        adddinf.setDevId(devId);
        adddinf.setBattgroupId(battGroupId);
        adddinf.setBattgroupName("电池组1");
        adddinf.setBattgroupNum(1);
        adddinf.setCreateTime(new Date());
        mapper.insert(adddinf);
    }
    //设备下添加电池组
    @Transactional
    public void addBatt(BattInf  addbinf) {
        if(addbinf.getAddBinfFlag()==0){
            //获取设备的通用信息
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("dev_id",addbinf.getDevId());
            wrapper.select("dev_name","dev_type","dev_num","dev_ip","power_id","station_id");
            wrapper.last("limit 1");
            BattInf binf=mapper.selectOne(wrapper);
            addbinf.setPowerId(binf.getPowerId());
            addbinf.setStationId(binf.getStationId());
            addbinf.setDevName(binf.getDevName());
            addbinf.setDevType(binf.getDevType());
            addbinf.setDevNum(binf.getDevNum());
            addbinf.setDevIp(binf.getDevIp());
        }else{//电源下新建一个设备
            //检测电源下是否存在设备
            int devNum=mapper.getMaxDevNum(addbinf.getPowerId());
            if(devNum==0){
                devNum=1;
            }else{
                devNum+=1;
            }
            addbinf.setDevNum(devNum);
            addbinf.setDevName(addbinf.getDevType()+devNum);
            //获取对应的设备id,电池组
            int devId = mapper.getMaxdevId();
            int battGroupId=mapper.getMaxBattGroupId();
            if (devId == 0) {//数据库中没有站点
                devId = 10001;
            } else {
                devId += 1;
            }
            addbinf.setDevId(devId);
        }
        //检测设备下是否存在电池组
        int battgroupNum=mapper.getMaxBattgroupNum(addbinf.getDevId());
        if(battgroupNum==0){
            battgroupNum=1;
        }else{
            battgroupNum+=1;
        }
        int battGroupId=mapper.getMaxBattGroupId();
        if (battGroupId == 0) {//数据库中没有站点
            battGroupId = 10001;
        } else {
            battGroupId += 1;
        }
        addbinf.setBattgroupNum(battgroupNum);
        addbinf.setBattgroupId(battGroupId);
        addbinf.setBattgroupName("电池组"+battgroupNum);
        addbinf.setBattgroupNum(battgroupNum);
        addbinf.setCreateTime(new Date());
        mapper.insert(addbinf);
    }
    //删除电池组
    @Transactional
    public Response delBatt(Integer stationId,  Integer powerId,Integer battgroupId) {
        List<BattInf> binfList=new ArrayList<>();
        if(battgroupId!=null){
            UpdateWrapper wrapper=new UpdateWrapper();
            wrapper.eq("battgroup_id",battgroupId);
            mapper.delete(wrapper);
            //检测如果powerId下电池组删除完了,要将电源也删除掉
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("power_id",powerId);
            binfList=mapper.selectList(wrapper1);
        }
        if(binfList==null||binfList.size()==0){
            UpdateWrapper wrapper2=new UpdateWrapper();
            wrapper2.eq("power_id",powerId);
            pinfMapper.delete(wrapper2);
            //删除所有包机组下的电源
            bjService.delPowerInBaoji(powerId);
            //再检测如果机房下电源也全删除了,要将机房也删除
            QueryWrapper wrapper3=new QueryWrapper();
            wrapper3.eq("station_id",stationId);
            List<PowerInf> pinfList=pinfMapper.selectList(wrapper3);
            if(pinfList==null||pinfList.size()==0){
                UpdateWrapper wrapper4=new UpdateWrapper();
                wrapper4.eq("station_id",stationId);
                sinfMapper.delete(wrapper4);
                //删除所有包机组下的机房
                bjService.delStationInBaoji(stationId);
            }
        }
        return new Response().set(1,true);
    }
    //编辑机房,电源,电池组信息
    @Transactional
    public Response updateInfo(InfoDto info) {
        //先修改机房信息
        PowerInf pinf=InfoFactory.infoToPower(info);
        StationInf sinf=pinf.getSinf();
        UpdateWrapper wrapper1=new UpdateWrapper();
        if(sinf.getStationName()!=null){
            //先查看机房名和stationid是不是匹配的
            QueryWrapper wrapper2=new QueryWrapper();
            wrapper2.eq("station_id",sinf.getStationId());
            wrapper2.last("limit 1");
            StationInf s=sinfMapper.selectOne(wrapper2);
            if(!s.getStationName().equalsIgnoreCase(sinf.getStationName())){
                wrapper1.set("station_name",sinf.getStationName());
                String fullName=sinf.getProvice()+"_"+sinf.getCity()+"_"+sinf.getCountry()+"_"+sinf.getStationName();
                //检测该机房下要修改的电源名是否存在
                QueryWrapper queryWrapper=new QueryWrapper();
                queryWrapper.eq("full_name",fullName);
                queryWrapper.last("limit 1");
                StationInf jueges=sinfMapper.selectOne(queryWrapper);
                if(jueges!=null){
                    return new Response().set(1,false,"已存在"+fullName+"的机房");
                }
                wrapper1.set("full_name",fullName);
            }
        }
        if(sinf.getStationType()!=null){
            wrapper1.set("station_type",sinf.getStationType());
        }
        if(sinf.getLongitude()!=null){
            wrapper1.set("longitude",sinf.getLongitude());
        }
        if(sinf.getLatitude()!=null){
            wrapper1.set("latitude",sinf.getLatitude());
        }
        wrapper1.eq("station_id",sinf.getStationId());
 
        //再修改电源信息
        UpdateWrapper wrapper2=new UpdateWrapper();
        if(pinf.getCompany()!=null){
            wrapper2.set("company",pinf.getCompany());
        }
        if(pinf.getPowerModel()!=null){
            wrapper2.set("power_model",pinf.getPowerModel());
        }
        if(pinf.getProtocol()!=null){
            wrapper2.set("protocol",pinf.getProtocol());
        }
        if(pinf.getPowerIp()!=null){
            wrapper2.set("power_ip",pinf.getPowerIp());
        }
        if(pinf.getPowerType()!=null){
            wrapper2.set("power_type",pinf.getPowerType());
        }
        /*if(pinf.getPowerName()!=null){
            //检测该机房下要修改的电源名是否存在
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("power_name",pinf.getPowerName());
            queryWrapper1.eq("station_id",sinf.getStationId());
            queryWrapper1.last("limit 1");
            PowerInf juegep=pinfMapper.selectOne(queryWrapper1);
            if(juegep!=null){
                return new Response().set(1,false,"该机房下已存在"+pinf.getPowerName()+"的电源");
            }
            wrapper2.set("power_name",pinf.getPowerName());
        }*/
        wrapper2.eq("power_id",pinf.getPowerId());
 
        //最后修改设备信息
        BattInf binf=InfoFactory.infoToBatt(info);
        if(binf.getDevId()!=null) {
            UpdateWrapper wrapper3 = new UpdateWrapper();
            if (binf.getDevIp() != null) {
                wrapper3.set("dev_ip", binf.getDevIp());
            }
           /* if (binf.getDevName() != null) {
                //检测该电源下修改的设备名是否存在
                QueryWrapper queryWrapper2 = new QueryWrapper();
                queryWrapper2.eq("dev_name", binf.getDevName());
                queryWrapper2.eq("power_id", pinf.getPowerId());
                queryWrapper2.last("limit 1");
                BattInf jueged = mapper.selectOne(queryWrapper2);
                if (jueged != null) {
                    return new Response().set(1, false, "该电源下已存在" + binf.getDevName() + "的设备");
                }
                wrapper3.set("dev_name", binf.getDevName());
            }*/
            /*if(binf.getDevType()!=null){
                wrapper3.set("dev_type",binf.getDevType());
            }*/
            wrapper3.eq("dev_id", binf.getDevId());
 
            //修改电池组信息
            UpdateWrapper wrapper4 = new UpdateWrapper();
            /*if (binf.getBattgroupName() != null) {
                //检测该机房下要修改的电源名是否粗在
                QueryWrapper queryWrapper3 = new QueryWrapper();
                queryWrapper3.eq("battgroup_name", binf.getBattgroupName());
                queryWrapper3.eq("dev_id", binf.getDevId());
                queryWrapper3.last("limit 1");
                BattInf juegeb = mapper.selectOne(queryWrapper3);
                if (juegeb != null) {
                    return new Response().set(1, false, "该设备下已存在" + binf.getBattgroupName() + "的电池组");
                }
                wrapper4.set("battgroup_name", binf.getBattgroupName());
            }*/
            if (binf.getMonvolstd() != null) {
                wrapper4.set("monvolstd", binf.getMonvolstd());
            }
            if (binf.getMoncapstd() != null) {
                wrapper4.set("moncapstd", binf.getMoncapstd());
            }
            if (binf.getMonresstd() != null) {
                wrapper4.set("monresstd", binf.getMonresstd());
            }
            if (binf.getProduct() != null) {
                wrapper4.set("product", binf.getProduct());
            }
            if (binf.getMoncount() != null) {
                wrapper4.set("moncount", binf.getMoncount());
            }
            if (binf.getBattModel() != null) {
                wrapper4.set("batt_model", binf.getBattModel());
            }
            wrapper4.eq("battgroup_id", binf.getBattgroupId());
            mapper.update((BattInf) ActionUtil.objeNull,wrapper3);
            mapper.update((BattInf) ActionUtil.objeNull,wrapper4);
        }
        sinfMapper.update((StationInf) ActionUtil.objeNull,wrapper1);
        pinfMapper.update((PowerInf) ActionUtil.objeNull,wrapper2);
        return new Response().set(1,true,"修改信息成功");
    }
    /*//修改电池组
    public Response updateBatt(BattInf binf) {
        UpdateWrapper wrapper1=new UpdateWrapper();
        wrapper1.eq("dev_id",binf.getDevId());
        if(binf.getDevIp()!=null){
            wrapper1.set("dev_ip",binf.getDevIp());
        }
        if(binf.getDevName()!=null){
            wrapper1.set("dev_name",binf.getDevName());
        }
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("battgroup_id",binf.getBattgroupId());
        if(binf.getBattgroupName()!=null){
            //检测该机房下要修改的电源名是否粗在
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("battgroup_name",binf.getBattgroupName());
            queryWrapper.eq("dev_id",binf.getDevId());
            queryWrapper.last("limit 1");
            BattInf juegeb=mapper.selectOne(queryWrapper);
            if(juegeb!=null){
                return new Response().set(1,false,"该设备下已存在"+binf.getBattgroupName()+"的电池组");
            }
            wrapper.set("battgroup_name",binf.getBattgroupName());
        }
        if(binf.getMonvolstd()!=null){
            wrapper.set("monvolstd",binf.getMonvolstd());
        }
        if(binf.getMoncapstd()!=null){
            wrapper.set("moncapstd",binf.getMoncapstd());
        }
        if(binf.getMonresstd()!=null){
            wrapper.set("monresstd",binf.getMonresstd());
        }
        if(binf.getProduct()!=null){
            wrapper.set("product",binf.getProduct());
        }
        if(binf.getMoncount()!=null){
            wrapper.set("moncount",binf.getMoncount());
        }
        if(binf.getBattModel()!=null){
            wrapper.set("batt_model",binf.getBattModel());
        }
        mapper.update((BattInf) ActionUtil.objeNull,wrapper);
        return new Response().set(1,true);
    }*/
    //查询机房,电源,电池组信息
    public Response getInfo(BattDto dto) {
        User user= ActionUtil.getUser();
        dto.setUid(user.getId());
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<InfoDto> list=mapper.getInfo(dto);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"查询电池");
    }
 
    //获取电池品牌(下拉)
    public Response getProductByUid(Integer uid) {
        List<String> list=mapper.getProductByUid(uid);
        return new Response().setII(1,list.size()>0,list,"获取电池品牌(下拉)");
    }
    //获取标称单体电压(下拉)
    public Response getMonVolByUid(Integer uid) {
        List<Float> list=mapper.getMonVolByUid(uid);
        return new Response().setII(1,list.size()>0,list,"获取标称单体电压(下拉)");
    }
 
    //获取标称容量(下拉)
    public Response getMonCapByUid(Integer uid) {
        List<Float> list=mapper.getMonCapByUid(uid);
        return new Response().setII(1,list.size()>0,list,"获取标称容量(下拉)");
    }
 
    //获取标称内阻(下拉)
    public Response getMonResByUid(Integer uid) {
        List<Float> list=mapper.getMonResByUid(uid);
        return new Response().setII(1,list.size()>0,list,"获取标称内阻(下拉)");
    }
    //获取设备型号(下拉)
    public Response getDevTypeByUid(Integer uid) {
        List<String> list=mapper.getDevTypeByUid(uid);
        return new Response().setII(1,list.size()>0,list,"获取设备型号(下拉)");
    }
 
    //根据电池组id获取电池组信息
    public BattInf getBinfByBattgroupId(Integer battgroupId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("battgroup_id",battgroupId);
        wrapper.last("limit 1");
        BattInf binf=mapper.selectOne(wrapper);
        return binf;
    }
   //当没有内助测试时初始内阻值为标称内阻
    public List<QuarterDto> getBinfResStd(Integer battgroupId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("battgroup_id",battgroupId);
        wrapper.last("limit 1");
        BattInf binf=mapper.selectOne(wrapper);
        Float monresstd=binf.getMonresstd();
        List<QuarterDto> list=new ArrayList<>();
        for(int i=0;i<binf.getMoncount();i++){
            QuarterDto dto=new QuarterDto();
            dto.setRecordTime(binf.getCreateTime());
            dto.setMonNum(i+1);
            dto.setNumValue(monresstd);
        }
        return list;
    }
 
    //设备信息统计
    public Response getDevStatistic(StationStic stic) {
        PageHelper.startPage(stic.getPageNum(),stic.getPageSize());
        List<BattInf> list=mapper.getDevStatistic(stic);
        for (BattInf binf:list) {
            //获取设备下电池组个数
            int battCount=mapper.getBattCountBydevId(binf.getDevId());
            binf.setBattCount(battCount);
        }
        PageInfo<BattInf> pageInfo=new PageInfo<>(list);
        return new Response().setII(1,list.size()>0,pageInfo,"设备信息统计");
    }
}