xuhy
2025-07-22 ed8a37dcf91881fde24155bfbe76a8d59d201e4b
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
package com.ruoyi.chargingPile.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.chargingPile.api.dto.PageChargingPileListDTO;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.chargingPile.api.model.TChargingPile;
import com.ruoyi.chargingPile.api.query.BatchSetAccountingStrategy;
import com.ruoyi.chargingPile.api.query.PageChargingPileList;
import com.ruoyi.chargingPile.api.vo.TChargingPileVO;
import com.ruoyi.chargingPile.mapper.TChargingPileMapper;
import com.ruoyi.chargingPile.service.TChargingGunService;
import com.ruoyi.chargingPile.service.TChargingPileService;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 充电桩 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-06
 */
@Slf4j
@Service
public class TChargingPileServiceImpl extends ServiceImpl<TChargingPileMapper, TChargingPile> implements TChargingPileService {
    
    @Resource
    private TChargingGunService chargingGunService;
 
    @Resource
    private RedisTemplate redisTemplate;
    
 
 
    /**
     * 获取充电桩列表数据
     * @param page
     * @return
     */
    @Override
    public PageInfo<PageChargingPileListDTO> pageChargingPileList(PageChargingPileList page) {
        Set<Integer> siteIds = null;
        PageInfo<PageChargingPileListDTO> pageInfo = new PageInfo<>(page.getPageCurr(), page.getPageSize());
        List<PageChargingPileListDTO> list = this.baseMapper.pageChargingPileList(pageInfo, page, siteIds);
        return pageInfo.setRecords(list);
    }
    
    
    /**
     * 添加充电桩
     * @param chargingPile
     * @return
     */
    @Override
    public AjaxResult addChargingPile(TChargingPile chargingPile) {
        AjaxResult ajaxResult = addChargingPileVerify(chargingPile);
        if(ajaxResult.isError()){
            return ajaxResult;
        }
        long count = this.count(new LambdaQueryWrapper<TChargingPile>().eq(TChargingPile::getCode, chargingPile.getCode())
                .eq(TChargingPile::getDelFlag, 0));
        if(count > 0){
            return AjaxResult.error("设备编号已存在");
        }
        this.save(chargingPile);
        return AjaxResult.success();
    }
    
    
    /**
     * 校验必填项
     * @param chargingPile
     * @return
     */
    AjaxResult addChargingPileVerify(TChargingPile chargingPile){
        if(StringUtils.isEmpty(chargingPile.getCode())){
            return AjaxResult.error("设备编号不能为空");
        }
        if(StringUtils.isEmpty(chargingPile.getName())){
            return AjaxResult.error("充电设备名称不能为空");
        }
        if(null == chargingPile.getNumber()){
            return AjaxResult.error("桩号不能为空");
        }
        if(null == chargingPile.getType()){
            return AjaxResult.error("设备类型不能为空");
        }
        if(null == chargingPile.getSiteId()){
            return AjaxResult.error("归属电站不能为空");
        }
        if(null == chargingPile.getPartnerId()){
            return AjaxResult.error("归属合作商不能为空");
        }
        if(null == chargingPile.getRatedPower()){
            return AjaxResult.error("额定功率不能为空");
        }
        return AjaxResult.success();
    }
    
    
    /**
     * 获取充电桩详情
     * @param id 充电桩id
     * @return
     */
    @Override
    public TChargingPile getChargingPile(Integer id) {
        return this.baseMapper.getChargingPile(id);
    }
    
    
    /**
     * 编辑充电桩
     * @param chargingPile
     * @return
     */
    @Override
    public AjaxResult editChargingPile(TChargingPile chargingPile) {
        AjaxResult ajaxResult = addChargingPileVerify(chargingPile);
        if(ajaxResult.isError()){
            return ajaxResult;
        }
        TChargingPile one = this.getOne(new LambdaQueryWrapper<TChargingPile>().eq(TChargingPile::getCode, chargingPile.getCode())
                .eq(TChargingPile::getDelFlag, 0));
        if(null != one && !one.getId().equals(chargingPile.getId())){
            return AjaxResult.error("设备编号已存在");
        }
        this.updateById(chargingPile);
        
        List<TChargingGun> list = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getChargingPileId, chargingPile.getId()).eq(TChargingGun::getDelFlag, 0));
        for (TChargingGun chargingGun : list) {
            TChargingGun chargingGun1 = new TChargingGun();
            chargingGun1.setId(chargingGun.getId());
            chargingGun1.setFullNumber(chargingPile.getCode() + chargingGun.getCode());
            chargingGun1.setSiteId(chargingPile.getSiteId());
            chargingGunService.updateById(chargingGun1);
        }
        return AjaxResult.success();
    }
    
    
    /**
     * 删除充电桩
     * @param ids
     * @return
     */
    @Override
    public AjaxResult delChargingPile(Integer[] ids) {
        //检查是否有关联数据
        long count = chargingGunService.count(new LambdaQueryWrapper<TChargingGun>().in(TChargingGun::getChargingPileId, Arrays.asList(ids))
                .eq(TChargingGun::getDelFlag, 0));
        if(count > 0){
            return AjaxResult.error("该充电桩已添加接口,不可删除。");
        }
        for (Integer id : ids) {
            TChargingPile chargingPile = this.getById(id);
            //调用华为Iot删除设备
//            if(StringUtils.isNotEmpty(chargingPile.getIotdDeviceId())){
//                ShowDeviceResp showDeviceResp = iotInterfaceClient.showDeviceRequest(chargingPile.getCode()).getData();
//                if(null != showDeviceResp){
//                    DeleteDeviceResp deleteDeviceResp = iotInterfaceClient.deleteDevice(chargingPile.getCode()).getData();
//                    if(null == deleteDeviceResp || (deleteDeviceResp.getHttpStatusCode() != 200 && deleteDeviceResp.getHttpStatusCode() != 201 && deleteDeviceResp.getHttpStatusCode() != 204)){
//                        return AjaxResult.error("删除设备异常,请查看华为设备管理");
//                    }
//                    chargingPile.setIotdDeviceId("");
//                }
//
//            }
            this.removeById(chargingPile);
        }
        return AjaxResult.success();
    }
    
    /**
     * 批量设置计费策略
     * @param setAccountingStrategy
     */
    @Override
    public void batchSetAccountingStrategy(BatchSetAccountingStrategy setAccountingStrategy) {
        List<Integer> id = setAccountingStrategy.getId();
        List<TChargingGun> list = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().in(TChargingGun::getChargingPileId, id).eq(TChargingGun::getDelFlag, 0));
        for (TChargingGun tChargingGun : list) {
            TChargingGun tChargingGun1 = new TChargingGun();
            tChargingGun1.setId(tChargingGun.getId());
            tChargingGun1.setAccountingStrategyId(setAccountingStrategy.getAccountingStrategyId());
            chargingGunService.updateById(tChargingGun1);
        }
    }
 
    @Override
    public List<TChargingPileVO> getChargingGunList(Integer siteId,Integer type) {
        LambdaQueryWrapper<TChargingGun> wrapper = new LambdaQueryWrapper<>();
        if(Objects.nonNull(type)){
            wrapper.eq(TChargingGun::getChargeMode, type);
        }
        wrapper.eq(TChargingGun::getSiteId, siteId);
        List<TChargingGun> chargingGuns = chargingGunService.list(wrapper);
        Set<Integer> collect = chargingGuns.stream().map(TChargingGun::getChargingPileId).collect(Collectors.toSet());
        List<TChargingPileVO> chargingPileVOS = new ArrayList<>();
        if(collect.size() > 0){
            List<TChargingPile> tChargingPiles = this.baseMapper.selectBatchIds(collect);
            for (TChargingPile tChargingPile : tChargingPiles) {
                TChargingPileVO vo = new TChargingPileVO();
                List<TChargingGun> chargingGunList = chargingGuns.stream().filter(gun -> gun.getChargingPileId().equals(tChargingPile.getId())).collect(Collectors.toList());
                for (TChargingGun chargingGun : chargingGunList) {
                    if(chargingGun.getStatus().equals(4)){
                        // 查询正在充电的单子的实时记录
                    }
                    if(chargingGun.getStatus().equals(5)){
                        chargingGun.setSoc(100);
                    }
                }
                vo.setChargingGunList(chargingGunList);
                vo.setId(tChargingPile.getId());
                vo.setCode(tChargingPile.getCode());
                vo.setName(tChargingPile.getName());
                vo.setNumber(tChargingPile.getNumber());
                chargingPileVOS.add(vo);
                
            }
        }
        return chargingPileVOS;
    }
    
    
    /**
     * 定时任务修改充电桩状态
     */
    @Override
    public void updateStatus() {
        //查询出redis集合汇总符合条件的数据
        Set keys = redisTemplate.opsForHash().keys("charging_gun_online");
        List<String> fullNumbers = new ArrayList<>();
        keys.forEach(key -> {
                        Long time = (Long) redisTemplate.opsForHash().get("charging_gun_online", key);
                        if(null != time && System.currentTimeMillis() - time > 300000){
                            log.info("设备编号:{} 已离线", key);
                            log.info("间隔时间", System.currentTimeMillis() - time);
                            fullNumbers.add(key.toString());
                        }
        });
        if(fullNumbers.size() == 0){
            return;
        }
        
        //处理符合条件的数据,修改设备状态
        List<TChargingGun> list = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().in(TChargingGun::getFullNumber, fullNumbers).eq(TChargingGun::getDelFlag, 0));
        for (TChargingGun chargingGun : list) {
            TChargingGun chargingGun1 = new TChargingGun();
            chargingGun1.setId(chargingGun.getId());
            chargingGun1.setStatus(1);
            chargingGunService.updateById(chargingGun1);
            //桩处于正常,桩所属的枪都处于非正常,修改桩状态为异常
            TChargingPile chargingPile = this.getById(chargingGun.getChargingPileId());
            List<TChargingGun> list1 = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getChargingPileId, chargingPile.getId()).eq(TChargingGun::getDelFlag, 0));
            int size = list1.stream().filter(s -> s.getStatus() == 1 || s.getStatus() == 7).collect(Collectors.toList()).size();
            if(chargingPile.getStatus() == 1 && list1.size() == size){
                TChargingPile chargingPile1 = new TChargingPile();
                chargingPile1.setId(chargingGun.getChargingPileId());
                chargingPile1.setStatus(2);
                this.updateById(chargingPile1);
            }
        }
    }
}