package com.ho.filecenter.service.impl;


import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.Station;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.vo.req.CardInfoReqVo;
import com.ho.filecenter.entity.CardInfo;
import com.ho.filecenter.entity.FlowMonitor;
import com.ho.filecenter.feignclient.BusinessFeignClient;
import com.ho.filecenter.mapper.FlowMonitorMapper;
import com.ho.filecenter.service.CardInfoService;
import com.ho.filecenter.service.FlowMonitorService;
import com.ho.filecenter.service.MobileApiService;
import com.ho.filecenter.service.TelecomApiService;
import com.ho.filecenter.vo.flowMonitor.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yy
 * @description 针对表【flow_monitor】的数据库操作Service实现
 * @createDate 2024-01-16 11:24:15
 */
@Service
@Slf4j
public class FlowMonitorServiceImpl implements FlowMonitorService {
    @Autowired
    private FlowMonitorMapper flowMonitorMapper;

    @Autowired
    private MobileApiService mobileApiService;

    @Autowired
    private CardInfoService cardInfoService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private TelecomApiService telecomApiService;

    @Autowired
    private BusinessFeignClient businessFeignClient;

    @Value("${telecom.appId}")
    String telecomAppId;

    @Value("${telecom.appId}")
    String appId;

    public int deleteByPrimaryKey(Integer id) {
        return flowMonitorMapper.deleteByPrimaryKey(id);
    }


    @Override
    public int insertSelective(FlowMonitorAddVo vo) {
        FlowMonitor record = new FlowMonitor();
        record.setPackageName(vo.getPackageName());
        record.setPackageType(vo.getPackageType());
        record.setGroupId(vo.getGroupId());
        record.setSingleOrGroup(vo.getSingleOrGroup());
        record.setTotalFlow(vo.getTotalAmount());
        record.setDeleted(CommonConstant.DELETED);
        int i = flowMonitorMapper.insertSelective(record);
        if (CommonConstant.ONE.equals(record.getPackageType()) && record.getGroupId() == null) {
            throw new BusinessException(BaseResponseCode.PACKAGE_NO_GROUPID);
        }
        String queryPackage = mobileApiService.getQueryPackage(record);
        PackageDetailRespVo packageDetailRespVo = mobileApiService.analysisPackage(queryPackage);
        String key = "flowMonitor:mobile:" + record.getId();
        log.info("flowMonitorKey:" + key);
        log.info("packageDetailRespVo:" + packageDetailRespVo);
        redisService.set(key, packageDetailRespVo);
        return i;
    }

    @Override
    public int updateByPrimaryKeySelective(FlowMonitorPutVo vo) {
        FlowMonitor record = new FlowMonitor();
        record.setPackageName(vo.getPackageName());
        record.setPackageType(vo.getPackageType());
        record.setTotalFlow(vo.getTotalAmount());
        record.setSingleOrGroup(vo.getSingleOrGroup());
        record.setGroupId(vo.getGroupId());
        record.setId(vo.getId());
        record.setDeleted(vo.getDeleted());
        return flowMonitorMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void deletedByIds(List<Integer> ids) {
        for (Integer id : ids) {
            //判断是否存在卡
            FlowMonitor flowMonitor = selectById(id);
            if (flowMonitor != null) {
                CardInfoReqVo vo = new CardInfoReqVo();
                vo.setFlowMonitorId(flowMonitor.getId());
                List<CardInfoReqVo> list = cardInfoService.selectByCondition(vo);
                if (!list.isEmpty()) {
                    List<CardInfoReqVo> voList = list.stream().filter(s -> s.getStationId() != null).collect(Collectors.toList());
                    if (!voList.isEmpty()) {
                        throw new BusinessException(BaseResponseCode.CARD_PACKAGE_ASSOCIATED_POWER_STATION);
                    }
                }
            }
            flowMonitorMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public FlowMonitor selectById(Integer flowMonitorId) {
        FlowMonitor flowMonitor = flowMonitorMapper.selectById(flowMonitorId);
        return flowMonitor;
    }

    public List<FlowMonitor> selectAll(FlowMonitor vo) {
        List<FlowMonitor> list = flowMonitorMapper.selectAll(vo);
        return list;
    }

    @Override
    public FlowMonitor selectByPid(FlowMonitorAddVo vo) {
        FlowMonitor flowMonitor = flowMonitorMapper.selectByPid(vo.getGroupId());
        return flowMonitor;
    }

    @Override
    public void addCardInfo(CardAddVo vo) {
        //判断增加的卡号是否重复
        CardInfo cardValue = cardInfoService.selectByCard(vo.getCard());
        if (cardValue != null) {
            throw new BusinessException(BaseResponseCode.STATION_ICCID_EXISTS);
        }
        CardInfo cardInfo = new CardInfo();
        cardInfo.setCard(vo.getCard());
        cardInfo.setFlowMonitorId(vo.getFlowMonitorId());
        cardInfo.setCardStatus(vo.getStatus());
        //根据套餐id获取运营商
        FlowMonitor flowMonitor = selectById(vo.getFlowMonitorId());
        cardInfo.setPackageType(flowMonitor.getPackageType());
        //根据卡号查缓存(1：中国移动2：中国联通3：中国电信)
        //电信的需要到缓存中拿取数据
        if (CommonConstant.THREE.equals(flowMonitor.getPackageType())) {
            String key = "flowMonitor:telecom:appId:" + telecomAppId + ":iccId:" + vo.getCard();
            CardInfoDate cardInfoDate = (CardInfoDate) redisService.get(key);
            if (cardInfoDate != null) {
                //取开户激活时间
                if (cardInfoDate.getOpenDate() != null) {
                    cardInfo.setOpenDate(cardInfoDate.getOpenDate());
                } else {
                    cardInfo.setOpenDate(null);
                }
                if (cardInfoDate.getActivationDate() != null) {
                    cardInfo.setActivationDate(cardInfoDate.getActivationDate());
                } else {
                    cardInfo.setActivationDate(null);
                }
            } else {
                CardInfoDate single = telecomApiService.getSingle(vo.getCard());
                if (single != null) {
                    single.setFlowMonitorId(vo.getFlowMonitorId());
                    redisService.set(key, single);
                }
                if (single.getOpenDate() != null) {
                    cardInfo.setOpenDate(single.getOpenDate());
                } else {
                    cardInfo.setOpenDate(null);
                }
                if (single.getActivationDate() != null) {
                    cardInfo.setActivationDate(single.getActivationDate());
                } else {
                    cardInfo.setActivationDate(null);
                }
            }
        }
        cardInfo.setStationId(vo.getStationId());
        cardInfoService.insertValue(cardInfo);
    }

    @Override
    public List<FlowMonitorAllData> selectAllPack(FlowMonitor vo) {
        List<FlowMonitorAllData> data = new ArrayList<>();
        //先查询所有套餐
        List<FlowMonitor> flowMonitors = selectAll(vo);
        //将所有的的套餐进行分类
        Map<Integer, List<FlowMonitor>> flowMonitorMap = flowMonitors.stream().collect(Collectors.groupingBy(FlowMonitor::getPackageType));
        // 1：中国移动
        if (flowMonitorMap.containsKey(CommonConstant.ONE)) {
            //移动组缓存key
            List<FlowMonitor> mobileFlowMonitors = flowMonitorMap.get(CommonConstant.ONE);
            for (FlowMonitor mobileFlowMonitor : mobileFlowMonitors) {
                FlowMonitorAllData flowMonitorAllData = new FlowMonitorAllData();
                String key = "flowMonitor:mobile:" + mobileFlowMonitor.getId();
                PackageDetailRespVo packageDetailRespVo = (PackageDetailRespVo) redisService.get(key);
                if (null != packageDetailRespVo) {
                    //公式换算 1mb=1024kb
                    BigDecimal decimal = new BigDecimal("1024");
                    flowMonitorAllData.setId(mobileFlowMonitor.getId());
                    flowMonitorAllData.setGroupId(mobileFlowMonitor.getGroupId());
                    flowMonitorAllData.setSingleOrGroup(mobileFlowMonitor.getSingleOrGroup());
                    flowMonitorAllData.setPackageName(mobileFlowMonitor.getPackageName());
                    flowMonitorAllData.setPackageType(mobileFlowMonitor.getPackageType());
                    if (packageDetailRespVo.getRemainAmount() != null) {
                        BigDecimal remainAmount = new BigDecimal(packageDetailRespVo.getRemainAmount()).divide(decimal, 2, BigDecimal.ROUND_HALF_UP);
                        flowMonitorAllData.setRemainAmount(remainAmount.toString());
                    } else {
                        flowMonitorAllData.setRemainAmount(null);
                    }
                    if (packageDetailRespVo.getTotalAmount() != null) {
                        BigDecimal totalAmount = new BigDecimal(packageDetailRespVo.getTotalAmount()).divide(decimal, 2, BigDecimal.ROUND_HALF_UP);
                        flowMonitorAllData.setTotalAmount(totalAmount.toString());
                    } else {
                        if (mobileFlowMonitor.getTotalFlow() != null) {
                            flowMonitorAllData.setTotalAmount(mobileFlowMonitor.getTotalFlow().toString());
                        } else {
                            flowMonitorAllData.setTotalAmount(null);
                        }
                    }
                    if (packageDetailRespVo.getUseAmount() != null) {
                        BigDecimal useAmount = new BigDecimal(packageDetailRespVo.getUseAmount()).divide(decimal, 2, BigDecimal.ROUND_HALF_UP);
                        flowMonitorAllData.setUseAmount(useAmount.toString());
                    } else {
                        flowMonitorAllData.setUseAmount(null);
                    }
                    data.add(flowMonitorAllData);
                } else {
                    flowMonitorAllData.setId(mobileFlowMonitor.getId());
                    flowMonitorAllData.setGroupId(mobileFlowMonitor.getGroupId());
                    flowMonitorAllData.setSingleOrGroup(mobileFlowMonitor.getSingleOrGroup());
                    flowMonitorAllData.setPackageName(mobileFlowMonitor.getPackageName());
                    flowMonitorAllData.setPackageType(mobileFlowMonitor.getPackageType());
                    flowMonitorAllData.setRemainAmount(CommonConstant.ZERO.toString());
                    if (mobileFlowMonitor.getTotalFlow() != null) {
                        flowMonitorAllData.setTotalAmount(mobileFlowMonitor.getTotalFlow().toString());
                    } else {
                        flowMonitorAllData.setTotalAmount(CommonConstant.ZERO.toString());
                    }
                    flowMonitorAllData.setUseAmount(CommonConstant.ZERO.toString());
                    data.add(flowMonitorAllData);
                }
            }
        }
        // 2：中国联通
        if (flowMonitorMap.containsKey(CommonConstant.TWO)) {
            List<FlowMonitor> list = flowMonitorMap.get(CommonConstant.TWO);
            for (FlowMonitor monitor : list) {
                FlowMonitorAllData flowMonitorAllData = new FlowMonitorAllData();
                flowMonitorAllData.setId(monitor.getId());
                flowMonitorAllData.setGroupId(monitor.getGroupId());
                flowMonitorAllData.setSingleOrGroup(monitor.getSingleOrGroup());
                flowMonitorAllData.setPackageName(monitor.getPackageName());
                flowMonitorAllData.setPackageType(monitor.getPackageType());
                flowMonitorAllData.setRemainAmount(CommonConstant.ZERO.toString());
                flowMonitorAllData.setUseAmount(CommonConstant.ZERO.toString());
                if (monitor.getTotalFlow() != null) {
                    flowMonitorAllData.setTotalAmount(monitor.getTotalFlow().toString());
                } else {
                    flowMonitorAllData.setTotalAmount(CommonConstant.ZERO.toString());
                }
                data.add(flowMonitorAllData);
            }
        }
        // 3：中国电信
        if (flowMonitorMap.containsKey(CommonConstant.THREE)) {
            //电信（计算该套餐下几张卡和每个卡总流量相乘->总 每个卡的使用相加 —>已使用流量 两个相减就是剩余）
            List<FlowMonitor> telecomFlowMonitors = flowMonitorMap.get(CommonConstant.THREE);
            for (FlowMonitor telecomFlowMonitor : telecomFlowMonitors) {
                FlowMonitorAllData flowMonitorAllData = new FlowMonitorAllData();
                //根据套餐查询卡列表
                CardInfoReqVo cardInfo = new CardInfoReqVo();
                cardInfo.setFlowMonitorId(telecomFlowMonitor.getId());
                List<CardInfoReqVo> list = cardInfoService.selectByCondition(cardInfo);
                if (list.isEmpty()) {
                    flowMonitorAllData.setId(telecomFlowMonitor.getId());
                    flowMonitorAllData.setGroupId(telecomFlowMonitor.getGroupId());
                    flowMonitorAllData.setSingleOrGroup(telecomFlowMonitor.getSingleOrGroup());
                    flowMonitorAllData.setPackageName(telecomFlowMonitor.getPackageName());
                    flowMonitorAllData.setPackageType(telecomFlowMonitor.getPackageType());
                    flowMonitorAllData.setRemainAmount(CommonConstant.ZERO.toString());
                    if (telecomFlowMonitor.getTotalFlow() != null) {
                        flowMonitorAllData.setTotalAmount(telecomFlowMonitor.getTotalFlow().toString());
                    } else {
                        flowMonitorAllData.setTotalAmount(CommonConstant.ZERO.toString());
                    }
                    flowMonitorAllData.setUseAmount(CommonConstant.ZERO.toString());
                    data.add(flowMonitorAllData);
                } else {
                    BigDecimal UseAmount = new BigDecimal("0");
                    for (CardInfoReqVo cardInfoReqVo : list) {
                        String key = "flowMonitor:telecom:appId:" + telecomAppId + ":iccId:" + cardInfoReqVo.getCard();
                        CardInfoDate cardInfoDate = (CardInfoDate) redisService.get(key);
                        if (cardInfoDate != null && cardInfoDate.getUserAmount() != null) {
                            String userAmount = cardInfoDate.getUserAmount();
                            UseAmount = UseAmount.add(new BigDecimal(userAmount));
                        } else {
                            UseAmount = UseAmount.add(new BigDecimal("0"));
                        }
                    }
                    //判断是否为 单卡流量
                    String totalAmount = "0";
                    if (CommonConstant.ONE.equals(telecomFlowMonitor.getSingleOrGroup())) {
                        if (!list.isEmpty()) {
                            if (telecomFlowMonitor.getTotalFlow() != null) {
                                Integer totalFlow = list.size() * telecomFlowMonitor.getTotalFlow();
                                totalAmount = totalFlow.toString();
                            }
                        }
                    }
                    flowMonitorAllData.setTotalAmount(totalAmount);
                    //公式换算 1G=1024×1024=1048576KB
                    BigDecimal decimal = new BigDecimal("1024");
                    BigDecimal useAmount = UseAmount.divide(decimal, 2, BigDecimal.ROUND_HALF_UP);
                    flowMonitorAllData.setUseAmount(useAmount.toString());
                    BigDecimal subtract = new BigDecimal(totalAmount).subtract(useAmount);
                    flowMonitorAllData.setRemainAmount(subtract.toString());
                    flowMonitorAllData.setId(telecomFlowMonitor.getId());
                    flowMonitorAllData.setGroupId(telecomFlowMonitor.getGroupId());
                    flowMonitorAllData.setSingleOrGroup(telecomFlowMonitor.getSingleOrGroup());
                    flowMonitorAllData.setPackageName(telecomFlowMonitor.getPackageName());
                    flowMonitorAllData.setPackageType(telecomFlowMonitor.getPackageType());
                    data.add(flowMonitorAllData);
                }
            }
        }
        return data;
    }

    @Override
    public List<CardInfoDate> selectByCardInfo(CardSelectVO vo) {
        List<CardInfoDate> date = new ArrayList<>();
        //根据套餐id查询该套餐
        FlowMonitor flowMonitor = flowMonitorMapper.selectById(vo.getFlowMonitorId());
        BigDecimal decimal = new BigDecimal("1024");
        // 1：中国移动
        if (flowMonitor.getPackageType().equals(CommonConstant.ONE)) {
            CardInfoReqVo cardInfo = new CardInfoReqVo();
            cardInfo.setFlowMonitorId(flowMonitor.getId());
            List<CardInfoReqVo> list = cardInfoService.selectByCondition(cardInfo);
            List<Integer> stationIdList = list.stream().map(CardInfoReqVo::getStationId)
                    .collect(Collectors.toList());
            Map<Integer, List<Station>> dataMap = getByStations(stationIdList);
            //移动组缓存key
            String key = "flowMonitor:mobile:" + flowMonitor.getId();
            PackageDetailRespVo packageDetailRespVo = (PackageDetailRespVo) redisService.get(key);
            Map<String, List<CardInfoDate>> map = new HashMap<>();
            if (packageDetailRespVo != null) {
                List<CardInfoDate> cardInfoList = packageDetailRespVo.getCardInfoList();
                if (cardInfoList != null) {
                    map = cardInfoList.stream().collect(Collectors.groupingBy(CardInfoDate::getCardNo));
                }
            }
            for (CardInfoReqVo cardInfoReqVo : list) {
                if (map.containsKey(cardInfoReqVo.getCard())) {
                    List<CardInfoDate> dates = map.get(cardInfoReqVo.getCard());
                    for (CardInfoDate cardInfoDate : dates) {
                        CardInfoDate resultDate = new CardInfoDate();
                        resultDate.setId(cardInfoReqVo.getId());
                        resultDate.setCardNo(cardInfoDate.getCardNo());
                        resultDate.setStatus(cardInfoDate.getStatus());
                        BigDecimal useAmount = new BigDecimal(CommonConstant.ZERO);
                        if (cardInfoDate.getUserAmount() != null) {
                            useAmount = new BigDecimal(cardInfoDate.getUserAmount()).divide(decimal, 2, BigDecimal.ROUND_HALF_UP);
                        }
                        resultDate.setUserAmount(useAmount.toString());
                        resultDate.setUpdateTime(cardInfoDate.getUpdateTime());
                        resultDate.setStationId(cardInfoReqVo.getStationId());
                        resultDate.setFlowMonitorId(flowMonitor.getId());
                        resultDate.setOpenDate(cardInfoReqVo.getOpenDate());
                        resultDate.setActivationDate(cardInfoReqVo.getActivationDate());
                        if (dataMap.containsKey(cardInfoReqVo.getStationId())) {
                            List<Station> stationList = dataMap.get(cardInfoReqVo.getStationId());
                            Station station = stationList.get(0);
                            resultDate.setStationName(station.getName());
                        } else {
                            resultDate.setStationName(null);
                        }
                        date.add(resultDate);
                    }
                } else {
                    CardInfoDate resultDate = new CardInfoDate();
                    resultDate.setId(cardInfoReqVo.getId());
                    resultDate.setCardNo(cardInfoReqVo.getCard());
                    resultDate.setStatus(cardInfoReqVo.getCardStatus());
                    resultDate.setUserAmount(CommonConstant.ZERO.toString());
                    resultDate.setStationId(cardInfoReqVo.getStationId());
                    resultDate.setFlowMonitorId(cardInfoReqVo.getFlowMonitorId());
                    resultDate.setOpenDate(cardInfoReqVo.getOpenDate());
                    resultDate.setActivationDate(cardInfoReqVo.getActivationDate());
                    if (dataMap.containsKey(cardInfoReqVo.getStationId())) {
                        List<Station> stationList = dataMap.get(cardInfoReqVo.getStationId());
                        Station station = stationList.get(0);
                        resultDate.setStationName(station.getName());
                    } else {
                        resultDate.setStationName(null);
                    }
                    date.add(resultDate);
                }
            }
        }
        //todo 2：中国联通
        if (flowMonitor.getPackageType().equals(CommonConstant.TWO)) {
            CardInfoReqVo cardInfo = new CardInfoReqVo();
            cardInfo.setFlowMonitorId(flowMonitor.getId());
            List<CardInfoReqVo> list = cardInfoService.selectByCondition(cardInfo);
            List<Integer> stationIdList = list.stream().map(CardInfoReqVo::getStationId)
                    .collect(Collectors.toList());
            Map<Integer, List<Station>> dataMap = getByStations(stationIdList);
            for (CardInfoReqVo cardInfoReqVo : list) {
                CardInfoDate card = new CardInfoDate();
                card.setId(cardInfoReqVo.getId());
                card.setCardNo(cardInfoReqVo.getCard());
                card.setStatus(cardInfoReqVo.getCardStatus());
                card.setUserAmount(CommonConstant.ZERO.toString());
                card.setStationId(cardInfoReqVo.getStationId());
                if (dataMap.containsKey(cardInfoReqVo.getStationId())) {
                    List<Station> stationList = dataMap.get(cardInfoReqVo.getStationId());
                    Station station = stationList.get(0);
                    card.setStationName(station.getName());
                } else {
                    card.setStationName(null);
                }
                card.setFlowMonitorId(cardInfoReqVo.getFlowMonitorId());
                date.add(card);
            }
        }
        // 3：中国电信
        if (flowMonitor.getPackageType().equals(CommonConstant.THREE)) {
            CardInfoReqVo cardInfo = new CardInfoReqVo();
            cardInfo.setFlowMonitorId(flowMonitor.getId());
            List<CardInfoReqVo> list = cardInfoService.selectByCondition(cardInfo);
            List<Integer> stationIdList = list.stream().map(CardInfoReqVo::getStationId)
                    .collect(Collectors.toList());
            Map<Integer, List<Station>> dataMap = getByStations(stationIdList);
            if (!list.isEmpty()) {
                for (CardInfoReqVo cardInfoReqVo : list) {
                    String key = "flowMonitor:telecom:appId:" + telecomAppId + ":iccId:" + cardInfoReqVo.getCard();
                    CardInfoDate cardInfoDate = (CardInfoDate) redisService.get(key);
                    if (cardInfoDate != null) {
                        CardInfoDate card = new CardInfoDate();
                        card.setId(cardInfoReqVo.getId());
                        if (cardInfoDate.getCardNo() != null) {
                            card.setCardNo(cardInfoDate.getCardNo());
                        } else {
                            card.setCardNo(cardInfoReqVo.getCard());
                        }
                        if (cardInfoDate.getStatus() != null) {
                            card.setStatus(cardInfoDate.getStatus());
                        } else {
                            card.setStatus(cardInfoReqVo.getCardStatus());
                        }
                        if (cardInfoDate.getUserAmount() != null) {
                            BigDecimal useAmount = new BigDecimal(cardInfoDate.getUserAmount()).divide(decimal, 2, BigDecimal.ROUND_HALF_UP);
                            card.setUserAmount(useAmount.toString());
                        } else {
                            card.setUserAmount(CommonConstant.ZERO.toString());
                        }
                        card.setUpdateTime(cardInfoDate.getUpdateTime());
                        card.setOpenDate(cardInfoDate.getOpenDate());
                        card.setActivationDate(cardInfoDate.getActivationDate());
                        card.setGroupId(cardInfoDate.getGroupId());
                        card.setStationId(cardInfoReqVo.getStationId());
                        card.setFlowMonitorId(cardInfoReqVo.getFlowMonitorId());
                        if (dataMap.containsKey(cardInfoReqVo.getStationId())) {
                            List<Station> stationList = dataMap.get(cardInfoReqVo.getStationId());
                            Station station = stationList.get(0);
                            card.setStationName(station.getName());
                        } else {
                            card.setStationName(null);
                        }
                        //1 是单卡  2 套餐
                        if (CommonConstant.ONE.equals(flowMonitor.getSingleOrGroup())) {
                            card.setSingleTotalFlow(flowMonitor.getTotalFlow().toString());
                        }
                        date.add(card);
                    } else {
                        CardInfoDate card = new CardInfoDate();
                        card.setId(cardInfoReqVo.getId());
                        card.setCardNo(cardInfoReqVo.getCard());
                        card.setStatus(cardInfoReqVo.getCardStatus());
                        card.setOpenDate(cardInfoReqVo.getOpenDate());
                        card.setActivationDate(cardInfoReqVo.getActivationDate());
                        card.setUserAmount(CommonConstant.ZERO.toString());
                        card.setStationId(cardInfoReqVo.getStationId());
                        card.setFlowMonitorId(cardInfoReqVo.getFlowMonitorId());
                        if (dataMap.containsKey(cardInfoReqVo.getStationId())) {
                            List<Station> stationList = dataMap.get(cardInfoReqVo.getStationId());
                            Station station = stationList.get(0);
                            card.setStationName(station.getName());
                        } else {
                            card.setStationName(null);
                        }
                        //1 是单卡  2 套餐
                        if (CommonConstant.ONE.equals(flowMonitor.getSingleOrGroup())) {
                            card.setSingleTotalFlow(flowMonitor.getTotalFlow().toString());
                        }
                        date.add(card);
                    }
                }
            }
        }
        return date;
    }

    //根据电站list查询
    public Map<Integer, List<Station>> getByStations(List<Integer> stationIdList) {
        DataResult<List<Station>> listDataResult = businessFeignClient.selectByStationIds(stationIdList);
        Map<Integer, List<Station>> dataMap = new HashMap<>();
        if (listDataResult.isSuccess()) {
            List<Station> data = listDataResult.getData();
            dataMap = data.stream().collect(Collectors.groupingBy(Station::getId));
        }
        return dataMap;
    }

    @Override
    public void getPackageInfo() {
        //先查询所有套餐
        FlowMonitor vo = new FlowMonitor();
        List<FlowMonitor> flowMonitors = selectAll(vo);
        //将所有的的套餐进行分类
        Map<Integer, List<FlowMonitor>> flowMonitorMap = flowMonitors.stream().collect(Collectors.groupingBy(FlowMonitor::getPackageType));
        // 1：中国移动
        if (flowMonitorMap.containsKey(CommonConstant.ONE)) {
            List<FlowMonitor> mobileFlowMonitors = flowMonitorMap.get(CommonConstant.ONE);
            List<FlowMonitor> flowMonitorList = new ArrayList<>();
            mobileFlowMonitors.stream().filter(i -> null != i.getGroupId()).forEach(s -> flowMonitorList.add(s));
            mobileApiService.getMobileAllData(flowMonitorList);
        }
        // 2：中国联通
        if (flowMonitorMap.containsKey(CommonConstant.TWO)) {
            List<FlowMonitor> list = flowMonitorMap.get(CommonConstant.TWO);
        }
        // 3：中国电信
        if (flowMonitorMap.containsKey(CommonConstant.THREE)) {
            List<FlowMonitor> telecomFlowMonitors = flowMonitorMap.get(CommonConstant.THREE);
            telecomApiService.getTelecomAllData(telecomFlowMonitors);
        }
    }
}
