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,Integer siteId) {
|
// 获取当前登录账号站点
|
Set<Integer> siteIds = new HashSet<>();
|
siteIds.add(siteId);
|
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.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);
|
}
|
}
|
}
|
}
|