luodangjia
2024-09-18 a96c5eddcd63fa6f09e4cdce9e28c7b846068f0c
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingGunServiceImpl.java
@@ -1,26 +1,37 @@
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.model.TChargingGun;
import com.ruoyi.chargingPile.api.model.TChargingPile;
import com.ruoyi.chargingPile.api.query.TChargingGunQuery;
import com.ruoyi.chargingPile.api.vo.TChargingGunVO;
import com.ruoyi.chargingPile.api.vo.TMonitoringEquipmentVO;
import com.ruoyi.chargingPile.api.vo.*;
import com.ruoyi.chargingPile.domain.SiteMenu;
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.utils.SecurityUtils;
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.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * <p>
@@ -38,13 +49,24 @@
    
    @Resource
    private IPartnerService partnerService;
    @Resource
    private UserSiteClient userSiteClient;
    @Resource
    private RoleSiteClient roleSiteClient;
    @Autowired
    private TChargingPileService chargingPileService;
    @Resource
    private SysUserRoleClient sysUserRoleClient;
    
    
    
    @Override
    public PageInfo<TChargingGunVO> pageList(TChargingGunQuery query) {
        List<Integer> siteIds = null;
        Set<Integer> siteIds = null;
        //校验合作商权限
        SysUser sysUser = sysUserClient.getSysUser(SecurityUtils.getUserId()).getData();
        Integer roleType = sysUser.getRoleType();
@@ -52,6 +74,15 @@
        //合作商
        if(roleType == 2){
            siteIds = partnerService.authSite(objectId, SiteMenu.CHARGING_GUN);
        }else{
            //非管理员需要根据角色和用户配置查询允许的站点数据
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                List<Integer> data = userSiteClient.getSiteIds(sysUser.getUserId()).getData();
                List<SysUserRoleVo> data2 = sysUserRoleClient.getRoleByUserId(sysUser.getUserId()).getData();
                List<Integer> data1 = roleSiteClient.getSiteIds(data2.get(0).getRoleId()).getData();
                data.addAll(data1);
                siteIds = new HashSet<>(data);
            }
        }
        PageInfo<TChargingGunVO> pageInfo = new PageInfo<>(query.getPageCurr(),query.getPageSize());
        List<TChargingGunVO> list = this.baseMapper.pageList(query,pageInfo, siteIds);
@@ -79,7 +110,8 @@
        if(ajaxResult.isError()){
            return ajaxResult;
        }
        long count = this.count(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getCode, dto.getCode()).eq(TChargingGun::getDelFlag, 0));
        long count = this.count(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getCode, dto.getCode())
                .eq(TChargingGun::getDelFlag, 0));
        if(count > 0){
            return AjaxResult.error("接口编码已存在");
        }
@@ -95,18 +127,125 @@
     */
    @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<TChargingGun>().eq(TChargingGun::getCode, dto.getCode()).eq(TChargingGun::getDelFlag, 0));
        TChargingGun one = this.getOne(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getCode, dto.getCode())
                .eq(TChargingGun::getDelFlag, 0));
        if(null != one && !dto.getId().equals(one.getId())){
            return AjaxResult.error("接口编码已存在");
        }
        this.updateById(dto);
        return AjaxResult.success();
    }
    @Override
    public Map<String, ChargingGunCountVO> getChargingGunCount(Integer siteId) {
        Map<String, ChargingGunCountVO> map = new HashMap<>(4);
        //查询充电桩
        List<TChargingPile> 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<Integer> pileIds = pileList.stream().map(TChargingPile::getId).collect(Collectors.toSet());
        // 查询全部充电枪
        List<TChargingGun> 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<Integer> siteIds) {
        GunStatusStatisticsVO gunStatusStatisticsVO = new GunStatusStatisticsVO();
        if(CollectionUtils.isEmpty(siteIds)){
            Map<Integer, Integer> modeStatisticsNull = new HashMap<>();
            modeStatisticsNull.put(1, 0);
            modeStatisticsNull.put(2, 0);
            modeStatisticsNull.put(3, 0);
            gunStatusStatisticsVO.setModeStatistics(modeStatisticsNull);
            Map<Integer, Integer> statusStatisticsNull = new HashMap<>();
            statusStatisticsNull.put(1, 0);
            statusStatisticsNull.put(2, 0);
            statusStatisticsNull.put(3, 0);
            gunStatusStatisticsVO.setStatusStatistics(statusStatisticsNull);
            List<StatusModeStatisticsVO> 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);
        }
        Map<Integer, Integer> modeStatistics = this.baseMapper.getModeStatistics(siteIds);
        gunStatusStatisticsVO.setModeStatistics(modeStatistics);
        Map<Integer, Integer> statusStatistics = new HashMap<>();
        // 离线
        Long count = this.baseMapper.selectCount(Wrappers.lambdaQuery(TChargingGun.class)
                .eq(TChargingGun::getStatus, 1));
        statusStatistics.put(1, count.intValue());
        // 故障
        count = this.baseMapper.selectCount(Wrappers.lambdaQuery(TChargingGun.class)
                .eq(TChargingGun::getStatus, 7));
        statusStatistics.put(2, count.intValue());
        // 充电中
        count = this.baseMapper.selectCount(Wrappers.lambdaQuery(TChargingGun.class)
                .in(TChargingGun::getStatus, Arrays.asList(3,4,5,6)));
        statusStatistics.put(3, count.intValue());
        gunStatusStatisticsVO.setModeStatistics(statusStatistics);
        List<StatusModeStatisticsVO> statusModeStatisticsVOS = new ArrayList<>();
        StatusModeStatisticsVO statusModeStatistics1 = this.baseMapper.getStatusModeStatistics(siteIds,1);
        StatusModeStatisticsVO statusModeStatistics2 = this.baseMapper.getStatusModeStatistics(siteIds,2);
        StatusModeStatisticsVO statusModeStatistics3 = this.baseMapper.getStatusModeStatistics(siteIds,3);
        statusModeStatisticsVOS.add(statusModeStatistics1);
        statusModeStatisticsVOS.add(statusModeStatistics2);
        statusModeStatisticsVOS.add(statusModeStatistics3);
        gunStatusStatisticsVO.setStatusModeStatistics(statusModeStatisticsVOS);
        return gunStatusStatisticsVO;
    }
    private ChargingGunCountVO getGunCount(Integer chargeMode, List<TChargingGun> 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