package com.ruoyi.chargingPile.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.chargingPile.api.dto.TChargingGunDTO; import com.ruoyi.chargingPile.api.feignClient.SiteClient; import com.ruoyi.chargingPile.api.model.Site; import com.ruoyi.chargingPile.api.model.TChargingGun; import com.ruoyi.chargingPile.api.model.TChargingPile; import com.ruoyi.chargingPile.api.query.TChargingGunQuery; import com.ruoyi.chargingPile.api.vo.*; import com.ruoyi.chargingPile.api.domain.SiteMenu; import com.ruoyi.chargingPile.mapper.SiteMapper; import com.ruoyi.chargingPile.mapper.TChargingGunMapper; import com.ruoyi.chargingPile.service.IPartnerService; import com.ruoyi.chargingPile.service.TChargingGunService; import com.ruoyi.chargingPile.service.TChargingPileService; import com.ruoyi.common.core.enums.status.ChargingGunModeEnum; import com.ruoyi.common.core.enums.status.ChargingGunStatusEnum; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.core.web.page.PageInfo; import com.ruoyi.common.security.service.TokenService; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.integration.api.feignClient.ChengDuJianGuanClient; import com.ruoyi.integration.api.feignClient.SendMessageClient; import com.ruoyi.integration.api.feignClient.TCECClient; import com.ruoyi.integration.api.model.QrCodeDelivery; import com.ruoyi.other.api.feignClient.RoleSiteClient; import com.ruoyi.other.api.feignClient.UserSiteClient; import com.ruoyi.system.api.domain.SysUser; import com.ruoyi.system.api.feignClient.SysUserClient; import com.ruoyi.system.api.feignClient.SysUserRoleClient; import com.ruoyi.system.api.model.SysUserRoleVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.util.*; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** *

* 充电枪 服务实现类 *

* * @author xiaochen * @since 2024-08-06 */ @Service public class TChargingGunServiceImpl extends ServiceImpl implements TChargingGunService { @Resource private SysUserClient sysUserClient; @Resource private IPartnerService partnerService; @Resource private UserSiteClient userSiteClient; @Resource private RoleSiteClient roleSiteClient; @Autowired private TChargingPileService chargingPileService; @Resource private SysUserRoleClient sysUserRoleClient; @Resource private SendMessageClient sendMessageClient; @Resource private SiteClient siteClient; @Autowired private TCECClient tcecClient; @Resource private TokenService tokenService; @Override public PageInfo pageList(TChargingGunQuery query) { Set siteIds = null; //校验合作商权限 SysUser sysUser = sysUserClient.getSysUser(tokenService.getLoginUser().getUserid()).getData(); Integer roleType = sysUser.getRoleType(); Integer objectId = sysUser.getObjectId(); //合作商 if(roleType == 2){ siteIds = partnerService.authSite(objectId, SiteMenu.CHARGING_GUN); }else{ //非管理员需要根据角色和用户配置查询允许的站点数据 if(!SecurityUtils.isAdmin(tokenService.getLoginUser().getUserid())){ List data = userSiteClient.getSiteIds(sysUser.getUserId()).getData(); List data2 = sysUserRoleClient.getRoleByUserId(sysUser.getUserId()).getData(); List data1 = roleSiteClient.getSiteIds(data2.get(0).getRoleId()).getData(); data.addAll(data1); siteIds = new HashSet<>(data); } } PageInfo pageInfo = new PageInfo<>(query.getPageCurr(),query.getPageSize()); List list = this.baseMapper.pageList(query,pageInfo, siteIds); for (TChargingGunVO tChargingGunVO : list) { Integer siteId = tChargingGunVO.getSiteId(); tChargingGunVO.setAuthDelete(roleType == 1 ? true : partnerService.authMenu(objectId, siteId, SiteMenu.CHARGING_GUN_DELETE)); tChargingGunVO.setAuthDownloadQRCode(roleType == 1 ? true : partnerService.authMenu(objectId, siteId, SiteMenu.CHARGING_GUN_DOWNLOAD_QR_CODE)); tChargingGunVO.setAuthEndCharge(roleType == 1 ? true : partnerService.authMenu(objectId, siteId, SiteMenu.CHARGING_GUN_END_CHARGE)); tChargingGunVO.setAuthQueryInfo(roleType == 1 ? true : partnerService.authMenu(objectId, siteId, SiteMenu.CHARGING_GUN_QUERY_INFO)); tChargingGunVO.setAuthUpdate(roleType == 1 ? true : partnerService.authMenu(objectId, siteId, SiteMenu.CHARGING_GUN_UPDATE)); tChargingGunVO.setAuthViewRates(roleType == 1 ? true : partnerService.authMenu(objectId, siteId, SiteMenu.CHARGING_GUN_VIEW_RATES)); if(null == tChargingGunVO.getAccountingStrategyId()){ List data = siteClient.getSiteByIds(Arrays.asList(tChargingGunVO.getSiteId())).getData(); if(data.size() > 0){ Site site = data.get(0); tChargingGunVO.setAccountingStrategyId(site.getAccountingStrategyId()); } } } pageInfo.setRecords(list); return pageInfo; } @Autowired private SiteMapper siteMapper; @Resource private ChengDuJianGuanClient chengDuJianGuanClient; /** * 添加充电枪 * @param dto * @return */ @Override public AjaxResult add(TChargingGunDTO dto) { AjaxResult ajaxResult = addVerify(dto); if(ajaxResult.isError()){ return ajaxResult; } long count = this.count(new LambdaQueryWrapper().eq(TChargingGun::getCode, dto.getCode()) .eq(TChargingGun::getDelFlag, 0).eq(TChargingGun::getChargingPileId, dto.getChargingPileId())); if(count > 0){ return AjaxResult.error("接口编码已存在"); } TChargingPile chargingPile = chargingPileService.getById(dto.getChargingPileId()); dto.setFullNumber(chargingPile.getCode() + dto.getCode()); this.save(dto); //下发硬件充电二维码 String code_prefix = "https://mxcd.zhinenganguan.com?No="; QrCodeDelivery qrCodeDelivery = new QrCodeDelivery(); qrCodeDelivery.setCharging_pile_code(chargingPile.getCode()); qrCodeDelivery.setCharging_gun_code(dto.getCode()); qrCodeDelivery.setCode_format(1); qrCodeDelivery.setPrefix_length(code_prefix.length()); qrCodeDelivery.setCode_prefix(code_prefix); qrCodeDelivery.setQr_code(code_prefix + chargingPile.getCode() + dto.getCode()); qrCodeDelivery.setCreate_time(new Date()); qrCodeDelivery.setLast_time(new Date()); sendMessageClient.qrCodeDelivery(qrCodeDelivery); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingDeque<>()); threadPoolExecutor.execute(()->{ //推送成都监管 chengDuJianGuanClient.notificationStationInfo(chargingPile.getSiteId()); }); return AjaxResult.success(); } /** * 编辑充电枪 * @param dto * @return */ @Override public AjaxResult update(TChargingGunDTO dto) { TChargingGun chargingGun = this.getById(dto.getId()); if(Arrays.asList(3,4,5,6).contains(chargingGun.getStatus())){ return AjaxResult.error("接口占用中,无法编辑!"); } AjaxResult ajaxResult = addVerify(dto); if(ajaxResult.isError()){ return ajaxResult; } TChargingGun one = this.getOne(new LambdaQueryWrapper().eq(TChargingGun::getCode, dto.getCode()) .eq(TChargingGun::getDelFlag, 0).eq(TChargingGun::getChargingPileId, dto.getChargingPileId())); if(null != one && !dto.getId().equals(one.getId())){ return AjaxResult.error("接口编码已存在"); } TChargingPile chargingPile = chargingPileService.getById(dto.getChargingPileId()); dto.setFullNumber(chargingPile.getCode() + dto.getCode()); this.updateById(dto); //下发硬件充电二维码 String code_prefix = "https://mxcd.zhinenganguan.com?No="; QrCodeDelivery qrCodeDelivery = new QrCodeDelivery(); qrCodeDelivery.setCharging_pile_code(chargingPile.getCode()); qrCodeDelivery.setCharging_gun_code(dto.getCode()); qrCodeDelivery.setCode_format(1); qrCodeDelivery.setPrefix_length(code_prefix.length()); qrCodeDelivery.setCode_prefix(code_prefix); qrCodeDelivery.setQr_code(code_prefix + chargingPile.getCode() + dto.getCode()); qrCodeDelivery.setCreate_time(new Date()); qrCodeDelivery.setLast_time(new Date()); sendMessageClient.qrCodeDelivery(qrCodeDelivery); tcecClient.superviseNotificationStationInfo(one.getSiteId()); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingDeque<>()); threadPoolExecutor.execute(()->{ //推送成都监管 chengDuJianGuanClient.notificationStationInfo(chargingPile.getSiteId()); }); return AjaxResult.success(); } @Override public Map getChargingGunCount(Integer siteId) { Map map = new HashMap<>(4); //查询充电桩 List pileList = chargingPileService.list(Wrappers.lambdaQuery(TChargingPile.class) .eq(TChargingPile::getSiteId, siteId)); if(CollectionUtils.isEmpty(pileList)){ map.put("superSufficient", new ChargingGunCountVO(0,0)); map.put("fastSufficient", new ChargingGunCountVO(0,0)); map.put("slowSufficient", new ChargingGunCountVO(0,0)); map.put("totalSufficient", new ChargingGunCountVO(0,0)); return map; } Set pileIds = pileList.stream().map(TChargingPile::getId).collect(Collectors.toSet()); // 查询全部充电枪 List list = this.list(Wrappers.lambdaQuery(TChargingGun.class).eq(TChargingGun::getSiteId, siteId) .in(TChargingGun::getChargingPileId, pileIds)); int freeCount = 0; // 超级充 ChargingGunCountVO superSufficient = getGunCount(ChargingGunModeEnum.SUPER_SUFFICIENT.getCode(), list); freeCount += superSufficient.getFreeCount(); map.put("superSufficient", superSufficient); // 快充 ChargingGunCountVO fastSufficient = getGunCount(ChargingGunModeEnum.FAST_SUFFICIENT.getCode(), list); freeCount += fastSufficient.getFreeCount(); map.put("fastSufficient", fastSufficient); // 慢充 ChargingGunCountVO slowSufficient = getGunCount(ChargingGunModeEnum.SLOW_SUFFICIENT.getCode(), list); freeCount += slowSufficient.getFreeCount(); map.put("slowSufficient", slowSufficient); ChargingGunCountVO chargingGunCountVO = new ChargingGunCountVO(); chargingGunCountVO.setTotalCount(list.size()); chargingGunCountVO.setFreeCount(freeCount); map.put("totalSufficient", chargingGunCountVO); return map; } @Override public GunStatusStatisticsVO gunStatusStatistics(List siteIds) { GunStatusStatisticsVO gunStatusStatisticsVO = new GunStatusStatisticsVO(); if(CollectionUtils.isEmpty(siteIds)){ // List> modeStatisticsNull = new ArrayList<>(); // HashMap objectObjectHashMap1 = new HashMap<>(); // objectObjectHashMap1.put(1, 0); // modeStatisticsNull.add(objectObjectHashMap1); // HashMap objectObjectHashMap2= new HashMap<>(); // objectObjectHashMap2.put(2, 0); // modeStatisticsNull.add(objectObjectHashMap2); // HashMap objectObjectHashMap3= new HashMap<>(); // objectObjectHashMap3.put(3, 0); // modeStatisticsNull.add(objectObjectHashMap3); // gunStatusStatisticsVO.setModeStatistics(modeStatisticsNull); // Map statusStatisticsNull = new HashMap<>(); // statusStatisticsNull.put(1, 0); // statusStatisticsNull.put(2, 0); // statusStatisticsNull.put(3, 0); // gunStatusStatisticsVO.setStatusStatistics(statusStatisticsNull); List statusModeStatistics = new ArrayList<>(); for (int i = 1; i < 4; i++) { StatusModeStatisticsVO statusModeStatisticsVO = new StatusModeStatisticsVO(); statusModeStatisticsVO.setChargeMode(i); statusModeStatisticsVO.setChargingCount(0); statusModeStatisticsVO.setFilledCount(0); statusModeStatisticsVO.setInsertCount(0); statusModeStatisticsVO.setFreeCount(0); statusModeStatistics.add(statusModeStatisticsVO); } gunStatusStatisticsVO.setStatusModeStatistics(statusModeStatistics); } List> modeStatistics = this.baseMapper.getModeStatistics(siteIds); if(CollectionUtils.isEmpty(modeStatistics)){ Map modeStatistics1 = new HashMap<>(); modeStatistics1.put("1", "0"); modeStatistics1.put("2", "0"); modeStatistics1.put("3", "0"); modeStatistics.add(modeStatistics1); } gunStatusStatisticsVO.setModeStatistics(modeStatistics); List> statusStatistics = new ArrayList<>(); // 离线 Long count = this.baseMapper.selectCount(Wrappers.lambdaQuery(TChargingGun.class) .eq(TChargingGun::getStatus, 1)); HashMap objectObjectHashMap1 = new HashMap<>(); objectObjectHashMap1.put("charge_mode",1); objectObjectHashMap1.put("modeCount", count.intValue()); statusStatistics.add(objectObjectHashMap1); // statusStatistics.put(1, count.intValue()); // 故障 count = this.baseMapper.selectCount(Wrappers.lambdaQuery(TChargingGun.class) .eq(TChargingGun::getStatus, 7)); HashMap objectObjectHashMap2 = new HashMap<>(); // objectObjectHashMap2.put("2", count.intValue()); objectObjectHashMap2.put("charge_mode",2); objectObjectHashMap2.put("modeCount", count.intValue()); statusStatistics.add(objectObjectHashMap2); // statusStatistics.put(2, count.intValue()); // 充电中 count = this.baseMapper.selectCount(Wrappers.lambdaQuery(TChargingGun.class) .in(TChargingGun::getStatus, Arrays.asList(3,4,5,6))); HashMap objectObjectHashMap = new HashMap<>(); objectObjectHashMap.put("charge_mode",3); objectObjectHashMap.put("modeCount", count.intValue()); statusStatistics.add(objectObjectHashMap); // statusStatistics.put(3, count.intValue()); gunStatusStatisticsVO.setStatusStatistics(statusStatistics); List statusModeStatisticsVOS = new ArrayList<>(); StatusModeStatisticsVO statusModeStatistics1 = this.baseMapper.getStatusModeStatistics(siteIds,1); StatusModeStatisticsVO statusModeStatistics2 = this.baseMapper.getStatusModeStatistics(siteIds,2); StatusModeStatisticsVO statusModeStatistics3 = this.baseMapper.getStatusModeStatistics(siteIds,3); if (statusModeStatistics1==null){ StatusModeStatisticsVO vo = new StatusModeStatisticsVO(); vo.setChargeMode(1); vo.setFreeCount(0); vo.setFilledCount(0); vo.setInsertCount(0); vo.setChargingCount(0); statusModeStatistics1 =vo; } statusModeStatisticsVOS.add(statusModeStatistics1); if (statusModeStatistics2==null){ StatusModeStatisticsVO vo = new StatusModeStatisticsVO(); vo.setChargeMode(2); vo.setFreeCount(0); vo.setFilledCount(0); vo.setInsertCount(0); vo.setChargingCount(0); statusModeStatistics2 =vo; } statusModeStatisticsVOS.add(statusModeStatistics2); if (statusModeStatistics3==null){ StatusModeStatisticsVO vo = new StatusModeStatisticsVO(); vo.setChargeMode(3); vo.setFreeCount(0); vo.setFilledCount(0); vo.setInsertCount(0); vo.setChargingCount(0); statusModeStatistics3 =vo; } statusModeStatisticsVOS.add(statusModeStatistics3); gunStatusStatisticsVO.setStatusModeStatistics(statusModeStatisticsVOS); return gunStatusStatisticsVO; } private ChargingGunCountVO getGunCount(Integer chargeMode, List list){ ChargingGunCountVO chargingGunCountVO = new ChargingGunCountVO(); chargingGunCountVO.setTotalCount(list.stream().filter(tChargingGun -> tChargingGun.getChargeMode().equals(chargeMode)).collect(Collectors.toList()).size()); chargingGunCountVO.setFreeCount(list.stream().filter(tChargingGun -> tChargingGun.getChargeMode().equals(chargeMode) && tChargingGun.getStatus()== ChargingGunStatusEnum.IDLE.getCode()).collect(Collectors.toList()).size()); return chargingGunCountVO; } /** * 校验必填项 * @param dto * @return */ AjaxResult addVerify(TChargingGunDTO dto){ if(StringUtils.isEmpty(dto.getCode())){ return AjaxResult.error("接口编码不能为空"); } if(StringUtils.isEmpty(dto.getName())){ return AjaxResult.error("接口名称不能为空"); } if(null == dto.getType()){ return AjaxResult.error("接口类型不能为空"); } if(null == dto.getStatus()){ return AjaxResult.error("接口状态不能为空"); } if(null == dto.getChargeMode()){ return AjaxResult.error("充电方式不能为空"); } if(null == dto.getUpperRatedVoltage()){ return AjaxResult.error("额定电压上限不能为空"); } if(null == dto.getLowerLimitOfRatedVoltage()){ return AjaxResult.error("额定电压下限不能为空"); } if(null == dto.getRatedCurrent()){ return AjaxResult.error("额定电流不能为空"); } if(null == dto.getRatedPower()){ return AjaxResult.error("额定功率不能为空"); } if(StringUtils.isEmpty(dto.getNationalStandard())){ return AjaxResult.error("国家标准不能为空"); } return AjaxResult.success(); } }