whycxzp
2025-04-14 d1c85e8705ecd936461cdde3ef2ce1a77f1e4c9f
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.Response;
import com.whyc.mapper.*;
import com.whyc.pojo.db_batt.PowerInf;
import com.whyc.pojo.db_batt.StationInf;
import com.whyc.pojo.db_batt.StationPowerRelation;
import com.whyc.pojo.db_power_alarm.PowerAlarm;
import com.whyc.pojo.db_real_batt.RtData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class PowerInfService {
    @Autowired(required = false)
    private PowerInfMapper mapper;
 
    @Autowired(required = false)
    private RtDataMapper rtDataMapper;
 
    @Autowired(required = false)
    private PowerRealRt1Mapper realRt1Mapper;
 
    @Autowired(required = false)
    private PowerRealRt2Mapper realRt2Mapper;
 
    @Autowired(required = false)
    private PowerRealRt3Mapper realRt3Mapper;
 
    @Autowired(required = false)
    private PowerAlarmMapper pAlarmMapper;
 
    @Autowired
    private StationPowerRelationService stationPowerRelationService;
 
    @Autowired
    private StationInfService stationInfService;
 
    //根据id获取电源信息
    public  Map<String,Object> getInfById(int powerId) {
        Map<String,Object> map=new HashMap<>();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("power_id",powerId);
        wrapper.last("limit 1");
        PowerInf pinf=mapper.selectOne(wrapper);
        Integer devType=pinf.getDevType();
        map.put("devType",devType);
        if(devType!=3){//第三种不包含单体实时
            List<RtData> list=getBattRt(pinf.getBattGroupId());
            map.put("battRt",list);
        }
        Object obj=getRealRt(powerId,devType);
        map.put("realRt"+devType,obj);
 
        List<PowerAlarm> powerAlarmList=pAlarmMapper.getResPowerAlm(powerId);
        map.put("powerAlarm",powerAlarmList);
        return map;
    }
    //获取电电源信息
    private Object getRealRt(int powerId,int devType) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("power_id",powerId);
        wrapper.last("limit 1");
        Object obj=null;
        if(devType==1){
            obj=realRt1Mapper.selectOne(wrapper);
        }
        if(devType==2){
            obj=realRt2Mapper.selectOne(wrapper);
        }
        if(devType==3){
            obj=realRt3Mapper.selectOne(wrapper);
        }
        return obj;
    }
 
    //获取电源电池组信息
    private List<RtData> getBattRt(int binfId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("binf_id",binfId);
        List<RtData> list=rtDataMapper.selectList(wrapper);
        return list;
    }
 
    @Transactional
    public Response add(PowerInf powerInf) {
        powerInf.setCreateTime(new Date());
        //对powerInf的 devType进行判断
        //如果devType=3,battGroupId为0
        //如果devType!=3,battGroupId为数据库中最大值+1
        Integer devType = powerInf.getDevType();
        if(devType == 3){
            powerInf.setBattGroupId(0);
        }else{
            powerInf.setBattGroupId(mapper.selectMaxBattGroupId()+1);
        }
        //如果没指定channelCount,默认为1
        if(powerInf.getChannelCount() == null){
            powerInf.setChannelCount(1);
        }
        mapper.insert(powerInf);
 
        //电源id获取
        int powerId = powerInf.getPowerId();
        //对站点名称进行查询,如果存在则直接绑定关系
        //如果不存在,则新增后,再绑定关系
        StationInf stationInf = stationInfService.getByStationName(powerInf.getStationName());
        Integer stationId = stationInf.getSinfId();
        if(stationInf == null) {
            StationInf stationNew = new StationInf();
            stationNew.setSinfName(powerInf.getStationName());
            int stationIdInDB = stationInfService.getMaxStationId();
            stationId = stationIdInDB + 1;
            stationNew.setSinfId(stationId);
            stationInfService.add(stationNew);
        }
        //站点和电源id关联
        stationPowerRelationService.add(stationId,powerId);
        return new Response().setII(1,"新增完成");
    }
 
    @Transactional
    public Response delete(int powerId) {
        mapper.deleteById(powerId);
        //删除跟电源相关的所有关联
        stationPowerRelationService.deleteByPowerId(powerId);
        return new Response().setII(1,"删除完成");
    }
 
    public Response update(PowerInf powerInf) {
        powerInf.setUpdateTime(new Date());
        mapper.updateById(powerInf);
        return new Response().setII(1,"修改完成");
    }
 
    public Response getPage(int pageNum, int pageSize) {
        PageHelper helper = new PageHelper();
        helper.startPage(pageNum,pageSize);
        /*QueryWrapper<PowerInf> queryWrapper = Wrappers.query();
        queryWrapper.orderByAsc("power_id");
 
        List<PowerInf> powerInfs = mapper.selectList(queryWrapper);*/
        List<PowerInf> powerInfs = mapper.getList();
        PageInfo<PowerInf> pageInfo = new PageInfo<>(powerInfs);
        return new Response().set(1,pageInfo);
 
    }
 
    public Response getById(int powerId) {
        //PowerInf powerInf = mapper.selectById(powerId);
        PowerInf powerInf = mapper.getById(powerId);
        return new Response().set(1,powerInf);
    }
 
    public Response getProducerList() {
        List<String> producerList = mapper.getProducerList();
        return new Response().set(1,producerList);
    }
 
    public PowerInf getByBattGroupId(Integer battGroupId) {
        QueryWrapper<PowerInf> query = Wrappers.query();
        query.eq("binf_id",battGroupId);
        query.last(" limit 1");
        return mapper.selectOne(query);
    }
}