package com.jsowell.thirdparty.lianlian.service.impl;

import cn.hutool.core.util.PageUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.RealTimeMonitorData;
import com.jsowell.common.enums.lianlian.LianLianPileStatusEnum;
import com.jsowell.common.enums.lianlian.PayChannelEnum;
import com.jsowell.common.enums.lianlian.StationPaymentEnum;
import com.jsowell.common.enums.ykc.OrderPayModeEnum;
import com.jsowell.common.enums.ykc.OrderStatusEnum;
import com.jsowell.common.enums.ykc.PileConnectorDataBaseStatusEnum;
import com.jsowell.common.enums.ykc.PileStatusEnum;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.JWTUtils;
import com.jsowell.common.util.PageUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.*;
import com.jsowell.pile.domain.ykcCommond.StartChargingCommand;
import com.jsowell.pile.domain.ykcCommond.StopChargingCommand;
import com.jsowell.pile.dto.*;
import com.jsowell.pile.service.*;
import com.jsowell.pile.vo.base.ConnectorInfoVO;
import com.jsowell.pile.vo.base.MerchantInfoVO;
import com.jsowell.pile.vo.base.StationSettingRelationVO;
import com.jsowell.pile.vo.lianlian.AccumulativeInfoVO;
import com.jsowell.pile.vo.uniapp.BillingPriceVO;
import com.jsowell.pile.vo.web.PileConnectorInfoVO;
import com.jsowell.pile.vo.web.PileModelInfoVO;
import com.jsowell.pile.vo.web.PileStationVO;
import com.jsowell.thirdparty.lianlian.domain.*;
import com.jsowell.thirdparty.lianlian.dto.CommonParamsDTO;
import com.jsowell.thirdparty.lianlian.service.LianLianService;
import com.jsowell.thirdparty.lianlian.util.Cryptos;
import com.jsowell.thirdparty.lianlian.util.Encodes;
import com.jsowell.thirdparty.lianlian.util.GBSignUtils;
import com.jsowell.thirdparty.lianlian.util.HttpRequestUtil;
import com.jsowell.thirdparty.lianlian.vo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class LianLianServiceImpl implements LianLianService {
    // private static final String TEST_URL = "http://testdataexchange.evchargeonline.com:82/shevcs/v1/";  // http://testdataexchange.evchargeonline.com:82/shevcs/v1/
    // private static final String OPERATOR_ID = "MA1JLFUU8"; // MA1JLFUU8  987654321
    // private static final String OPERATOR_SECRET = "fGwLsxW1HdzLw7jp"; // fGwLsxW1HdzLw7jp  1234567890abcdef
    // private static final String SIG_SECRET = "TmsdVaFVTtjzZbLi";              // 签名秘钥  TmsdVaFVTtjzZbLi  1234567890abcdef
    // private static final String DATA_SECRET = "R1Hdq80mSWswwCzt";           // 消息密钥  R1Hdq80mSWswwCzt  1234567890abcdef
    // private static final String DATA_SECRETIV = "LVpz3qHD8sM2PYjl";        // 消息密钥初始化向量  LVpz3qHD8sM2PYjl  1234567890abcdef

    @Autowired
    private IPileMerchantInfoService pileMerchantInfoService;

    @Autowired
    private IPileStationInfoService pileStationInfoService;

    @Autowired
    private IPileBasicInfoService pileBasicInfoService;

    @Autowired
    private IPileModelInfoService pileModelInfoService;

    @Autowired
    private IPileConnectorInfoService pileConnectorInfoService;

    @Autowired
    private IOrderBasicInfoService orderBasicInfoService;

    @Autowired
    private YKCPushCommandService ykcPushCommandService;

    @Autowired
    private IPileBillingTemplateService pileBillingTemplateService;

    @Autowired
    private IThirdPartySettingInfoService thirdPartySettingInfoService;

    @Autowired
    private IDockingPlatformConfigService dockingPlatformConfigService;

    @Autowired
    private IStationSettingRelationService stationSettingRelationService;

    @Override
    public void pushMerchantInfo(Long merchantId) {
        // 通过id查询运营商信息
        MerchantInfoVO merchantInfo = pileMerchantInfoService.getMerchantInfo(String.valueOf(merchantId));
        // 组装联联平台所需要的数据格式
        OperatorInfo operatorInfo = OperatorInfo.builder()
                .OperatorID(Constants.OPERATORID_LIANLIAN) // 组织机构代码
                .OperatorName(merchantInfo.getMerchantName()) // 机构全称
                .OperatorTel1(merchantInfo.getMerchantTel()) // 对接平台客服电话1
                .build();
        // 调用联联平台接口


    }

    /**
     * 根据站点id推送站点信息 notification_stationInfo
     *
     * @param dto
     */
    @Override
    public String pushStationInfo(LianLianPushStationInfoDTO dto) {
        // String OperatorID = "987654321";
        // String SigSecret = "1234567890abcdef";              // 签名秘钥
        // String DataSecret = "1234567890abcdef";           // 消息密钥
        // String DataSecretIV = "1234567890abcdef";        // 消息密钥初始化向量
        // String token = "c548f9276c1c4ea8b88b1f7b1eb7cf07";   //QueryToken类查询接口获取到的token

        // 通过id查询站点相关信息
        PileStationInfo pileStationInfo = pileStationInfoService.selectPileStationInfoById(dto.getStationId());
        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(dto.getStationId());
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(dto.getStationId());
        if (relationInfo == null) {
            // 新增
            relation.setThirdPartyType(dto.getThirdPartyType());
            stationSettingRelationService.insertStationSettingRelation(relation);

            relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        // 组装联联平台所需要的数据格式
        StationInfo info = StationInfo.builder()
                .stationID("LC" +dto.getStationId())
                .operatorID(operatorId)
                .equipmentOwnerID(Constants.OPERATORID_LIANLIAN)
                .stationName(pileStationInfo.getStationName())
                .isAloneApply(Integer.valueOf(pileStationInfo.getAloneApply()))
                .isPublicParkingLot(Integer.valueOf(pileStationInfo.getPublicParking()))
                .countryCode(pileStationInfo.getCountryCode())
                .areaCode(pileStationInfo.getAreaCode())
                .address(pileStationInfo.getAddress())
                .serviceTel(pileStationInfo.getStationTel())
                .stationType(Integer.valueOf(pileStationInfo.getStationType()))
                .stationStatus(Integer.valueOf(pileStationInfo.getStationStatus()))
                .parkNums(Integer.valueOf(pileStationInfo.getParkNums()))
                .stationLng(new BigDecimal(pileStationInfo.getStationLng()))
                .stationLat(new BigDecimal(pileStationInfo.getStationLat()))
                .construction(Integer.valueOf(pileStationInfo.getConstruction()))
                .openAllDay(Integer.valueOf(pileStationInfo.getOpenAllDay()))
                .minElectricityPrice(new BigDecimal(Constants.ZERO))
                .electricityFee(Constants.ZERO)
                .serviceFee(Constants.ZERO)
                .parkFree(Integer.valueOf(pileStationInfo.getParkFree()))
                // .ParkFee("2")
                // .payment("线上")
                .supportOrder(Integer.valueOf(pileStationInfo.getSupportOrder()))
                // .equipmentInfos()
                .parkFeeType(0)
                .toiletFlag(Integer.valueOf(pileStationInfo.getToiletFlag()))
                .storeFlag(Integer.valueOf(pileStationInfo.getStoreFlag()))
                .restaurantFlag(Integer.valueOf(pileStationInfo.getRestaurantFlag()))
                .loungeFlag(Integer.valueOf(pileStationInfo.getLoungeFlag()))
                .canopyFlag(Integer.valueOf(pileStationInfo.getCanopyFlag()))
                .printerFlag(Integer.valueOf(pileStationInfo.getPrinterFlag()))
                .barrierFlag(Integer.valueOf(pileStationInfo.getBarrierFlag()))
                .parkingLockFlag(Integer.valueOf(pileStationInfo.getParkingLockFlag()))

                .build();
        String payment = StationPaymentEnum.getPaymentByCode(pileStationInfo.getPayment());
        info.setPayment(payment);
        List<EquipmentInfo> pileList = getPileList(pileStationInfo);
        if (CollectionUtils.isNotEmpty(pileList)) {
            info.setEquipmentInfos(pileList); // 充电设备信息列表
        }

        // 调用联联平台接口
        String url = urlAddress + "notification_stationInfo";

        String jsonStr = JSONObject.toJSONString(info);
        JSONObject data = new JSONObject();
        data.put("StationInfo", jsonStr);

        String jsonString = JSONObject.toJSONString(data);
        System.out.println("jsonString : " + jsonString);

        // 获取令牌
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret
                , dataSecretIv, operatorId, signSecret);

        // System.out.println(result);
        return result;
    }

    public static void main(String[] args) {
        String s = RandomStringUtils.randomAlphanumeric(16).toUpperCase(Locale.ROOT);
        System.out.println(s);

    }

    /**
     * 查询充电站信息
     * 对外接口 query_stations_info
     *
     * @return
     */
    @Override
    public Map<String, String> query_stations_info(QueryStationInfoDTO dto) {
        List<StationInfo> resultList = new ArrayList<>();
        int pageNo = dto.getPageNo() == null ? 1 : dto.getPageNo();
        int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize();

        PageUtils.startPage(pageNo, pageSize);
        List<PileStationInfo> stationInfos = pileStationInfoService.getStationInfosByThirdParty();
        if (CollectionUtils.isEmpty(stationInfos)) {
            // 未查到数据
            return null;
        }
        DockingPlatformConfig configInfo = dockingPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
        if (configInfo == null) {
            return null;
        }
        PageInfo<PileStationInfo> pageInfo = new PageInfo<>(stationInfos);
        for (PileStationInfo pileStationInfo : pageInfo.getList()) {
            StationInfo stationInfo = new StationInfo();
            stationInfo.setStationID(String.valueOf(pileStationInfo.getId()));
            // MerchantInfoVO merchantInfo = pileMerchantInfoService.getMerchantInfo(String.valueOf(pileStationInfo.getMerchantId()));
            stationInfo.setOperatorID(Constants.OPERATORID_LIANLIAN); // 组织机构代码
            stationInfo.setEquipmentOwnerID(String.valueOf(pileStationInfo.getMerchantId()));
            stationInfo.setStationName(pileStationInfo.getStationName());
            stationInfo.setIsAloneApply(Integer.valueOf(pileStationInfo.getAloneApply()));
            stationInfo.setIsPublicParkingLot(Integer.valueOf(pileStationInfo.getPublicParking()));
            stationInfo.setCountryCode(pileStationInfo.getCountryCode());
            stationInfo.setAreaCode(pileStationInfo.getAreaCode());
            stationInfo.setAddress(pileStationInfo.getAddress());
            stationInfo.setServiceTel(pileStationInfo.getStationTel());
            stationInfo.setStationType(Integer.valueOf(pileStationInfo.getStationType()));
            stationInfo.setParkNums(Integer.valueOf(pileStationInfo.getParkNums()));
            stationInfo.setStationLng(new BigDecimal(pileStationInfo.getStationLng()));
            stationInfo.setStationLat(new BigDecimal(pileStationInfo.getStationLat()));
            stationInfo.setConstruction(Integer.valueOf(pileStationInfo.getConstruction()));
            stationInfo.setOpenAllDay(Integer.valueOf(pileStationInfo.getOpenAllDay()));
            // stationInfo.setMinElectricityPrice(pileStationInfo); // 最低充电电费率
            // stationInfo.setElectricityFee(); // 电费 xx元/度
            // stationInfo.setServiceFee(); // 服务费 xx元/度
            stationInfo.setParkFree(Integer.valueOf(pileStationInfo.getParkFree()));
            stationInfo.setPayment(pileStationInfo.getPayment());
            stationInfo.setSupportOrder(Integer.valueOf(pileStationInfo.getSupportOrder()));
            // stationInfo.setParkFeeType(pileStationInfo); // 停车收费类型
            stationInfo.setToiletFlag(Integer.valueOf(pileStationInfo.getToiletFlag()));
            stationInfo.setStoreFlag(Integer.valueOf(pileStationInfo.getStoreFlag()));
            stationInfo.setRestaurantFlag(Integer.valueOf(pileStationInfo.getRestaurantFlag()));
            stationInfo.setLoungeFlag(Integer.valueOf(pileStationInfo.getLoungeFlag()));
            stationInfo.setCanopyFlag(Integer.valueOf(pileStationInfo.getCanopyFlag()));
            stationInfo.setPrinterFlag(Integer.valueOf(pileStationInfo.getPrinterFlag()));
            stationInfo.setBarrierFlag(Integer.valueOf(pileStationInfo.getBarrierFlag()));
            stationInfo.setParkingLockFlag(Integer.valueOf(pileStationInfo.getParkingLockFlag()));

            List<EquipmentInfo> pileList = getPileList(pileStationInfo);
            if (CollectionUtils.isNotEmpty(pileList)) {
                stationInfo.setEquipmentInfos(pileList); // 充电设备信息列表
            }

            resultList.add(stationInfo);
        }
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("PageNo", pageInfo.getPageNum());
        map.put("PageCount", pageInfo.getPages());
        map.put("ItemSize", resultList.size());
        map.put("StationInfos", resultList);
        // LianLianPageResponse pageResponse = LianLianPageResponse.builder()
        //         .PageNo(pageInfo.getPageNum())
        //         .PageCount(pageInfo.getPages())
        //         .ItemSize(resultList.size())
        //         .list(resultList)
        //         .build();
        // 加密
        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(map).getBytes(),
                configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
        resultMap.put("Sig", resultSign);

        return resultMap;
    }

    /**
     * 设备接口状态查询
     * 此接口用于批量查询设备实时状态
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> query_station_status(QueryStationInfoDTO dto) {
        List<String> stationIds = dto.getStationIds();
        List<StationStatusInfo> StationStatusInfos = new ArrayList<>();
        List<Object> ConnectorStatusInfos = new ArrayList<>();
        DockingPlatformConfig configInfo = dockingPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
        if (configInfo == null) {
            return null;
        }
        for (String stationId : stationIds) {
            // 根据站点id查询
            List<ConnectorInfoVO> list = pileConnectorInfoService.getConnectorListForLianLian(Long.parseLong(stationId));
            for (ConnectorInfoVO connectorInfoVO : list) {

                String connectorStatus = connectorInfoVO.getConnectorStatus();
                if (StringUtils.equals(connectorStatus, PileConnectorDataBaseStatusEnum.OCCUPIED_CHARGING.getValue())) {
                    // 充电中
                    ConnectorChargeStatusInfo info = new ConnectorChargeStatusInfo();
                    OrderBasicInfo orderBasicInfo = orderBasicInfoService.queryChargingByPileConnectorCode(connectorInfoVO.getPileConnectorCode());
                    List<RealTimeMonitorData> chargingRealTimeData = orderBasicInfoService.getChargingRealTimeData(orderBasicInfo.getTransactionCode());
                    RealTimeMonitorData realTimeMonitorData = chargingRealTimeData.get(0);

                    info.setStartChargeSeq(orderBasicInfo.getOrderCode());
                    info.setConnectorID(orderBasicInfo.getPileConnectorCode());
                    info.setConnectorStatus(Integer.valueOf(connectorInfoVO.getConnectorStatus()));
                    info.setCurrentA(new BigDecimal(realTimeMonitorData.getOutputCurrent()));
                    info.setVoltageA(new BigDecimal(realTimeMonitorData.getOutputVoltage()));
                    info.setSoc(new BigDecimal(realTimeMonitorData.getSOC()));
                    info.setStartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeStartTime()));
                    info.setEndTime(DateUtils.getTime()); // 本次采样时间
                    info.setTotalPower(new BigDecimal(realTimeMonitorData.getChargingDegree())); // 累计充电量
                    // info.setElecMoney(); // 累计电费
                    // info.setSeviceMoney(); // 累计服务费
                    info.setTotalMoney(new BigDecimal(realTimeMonitorData.getChargingAmount()));

                    ConnectorStatusInfos.add(info);

                } else {
                    // 其他
                    ConnectorStatusInfo info = new ConnectorStatusInfo();
                    info.setConnectorID(connectorInfoVO.getPileConnectorCode());
                    info.setStatus(connectorInfoVO.getConnectorStatus());

                    ConnectorStatusInfos.add(info);
                }
                StationStatusInfo stationStatusInfo = new StationStatusInfo();
                stationStatusInfo.setStationID(connectorInfoVO.getStationId());
                stationStatusInfo.setConnectorStatusInfos(ConnectorStatusInfos);

                StationStatusInfos.add(stationStatusInfo);
            }
        }
        // 将 StationStatusInfos 分页
        int pageNum = 1;
        int pageSize = 10;
        List<StationStatusInfo> collect = StationStatusInfos.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());

        int total = StationStatusInfos.size();
        int pages = PageUtil.totalPage(total, pageSize);

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("Total", total);
        map.put("StationStatusInfos", collect);
        // LianLianPageResponse response = LianLianPageResponse.builder()
        //         .PageCount(pages)
        //         .PageNo(pageNum)
        //         .list(collect)
        //         .ItemSize(total)
        //         .build();
        // 加密
        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(map).getBytes(),
                configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
        resultMap.put("Sig", resultSign);

        return resultMap;
    }

    /**
     * 查询统计信息
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> query_station_stats(QueryStationInfoDTO dto) {
        DockingPlatformConfig configInfo = dockingPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
        if (configInfo == null) {
            return null;
        }
        // 根据站点id 查出这段时间的充电量
        List<AccumulativeInfoVO> list = orderBasicInfoService.getAccumulativeInfoForLianLian(dto);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        // 根据充电桩编号分组 key=充电桩编号
        Map<String, List<AccumulativeInfoVO>> pileMap = list.stream()
                .collect(Collectors.groupingBy(AccumulativeInfoVO::getPileSn));

        // 存放所有充电桩设备
        List<EquipmentStatsInfo> equipmentStatsInfoList = Lists.newArrayList();
        // 站点用电量
        BigDecimal stationElectricity = BigDecimal.ZERO;
        // 用于记录枪口用电量 在循环每个枪口的时候初始化
        BigDecimal pileElec;
        for (String pileSn : pileMap.keySet()) {
            // 该充电桩下 所有枪口的用电数据
            List<AccumulativeInfoVO> accumulativeInfoVOS = pileMap.get(pileSn);
            if (CollectionUtils.isEmpty(accumulativeInfoVOS)) {
                continue;
            }

            // 存放充电桩用电量
            pileElec = BigDecimal.ZERO;

            // key=枪口编号  value 该枪口的用电数据
            Map<String, List<AccumulativeInfoVO>> collect = accumulativeInfoVOS.stream()
                    .collect(Collectors.groupingBy(AccumulativeInfoVO::getPileConnectorCode));

            List<ConnectorStatsInfo> connectorStatsInfos = Lists.newArrayList();
            for (Map.Entry<String, List<AccumulativeInfoVO>> entry : collect.entrySet()) {
                String pileConnectorCode = entry.getKey();
                List<AccumulativeInfoVO> value = entry.getValue();
                // 枪口用电量求和
                BigDecimal connectorElec = value.stream()
                        .map(AccumulativeInfoVO::getConnectorElectricity)
                        .map(BigDecimal::new)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                connectorStatsInfos.add(
                        ConnectorStatsInfo.builder()
                                .ConnectorID(pileConnectorCode)
                                .ConnectorElectricity(connectorElec)
                                .build()
                );
                // 充电桩电量为枪口用电量累计
                pileElec = pileElec.add(connectorElec);
            }

            EquipmentStatsInfo build = EquipmentStatsInfo.builder()
                    .EquipmentID(pileSn)
                    .EquipmentElectricity(pileElec)
                    .ConnectorStatsInfos(connectorStatsInfos)
                    .build();
            equipmentStatsInfoList.add(build);

            // 所有充电桩用电量之和
            stationElectricity = stationElectricity.add(pileElec);
        }

        StationStatsInfo stationStatsInfo = StationStatsInfo.builder()
                .StationID(dto.getStationID())
                .StartTime(dto.getStartTime())
                .EndTime(dto.getEndTime())
                .StationElectricity(stationElectricity)
                .equipmentStatsInfos(equipmentStatsInfoList) // 设备列表
                .build();

        // 加密
        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(stationStatsInfo).getBytes(),
                configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
        resultMap.put("Sig", resultSign);

        return resultMap;
    }

    /**
     * 请求设备认证
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> query_equip_auth(QueryEquipmentDTO dto) {
        EquipmentAuthVO vo = new EquipmentAuthVO();

        String equipAuthSeq = dto.getEquipAuthSeq();
        String pileConnectorCode = dto.getConnectorID();
        // 通过运营商id + 枪口编码查询数据
        String merchantId = StringUtils.substring(equipAuthSeq, 0, 9);
        String pileSn = StringUtils.substring(pileConnectorCode, 0, 14);
        // 能查到为成功
        vo.setSuccStat(1); // 1-失败 0-成功
        PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(pileSn);
        if (pileBasicInfo == null) {
            vo.setFailReason(2); // 设备检测失败
            vo.setFailReasonMsg("未查到该桩的数据");
            return null;
        }
        DockingPlatformConfig configInfo = dockingPlatformConfigService.getInfoByOperatorId(dto.getOperatorID());
        if (configInfo == null) {
            return null;
        }
        if (pileBasicInfo.getMerchantId() == Long.parseLong(merchantId)) {
            vo.setSuccStat(0);
            // 查询当前数据
            PileConnectorInfoVO connectorInfo = pileConnectorInfoService.getPileConnectorInfoByConnectorCode(pileConnectorCode);
            if (StringUtils.equals(PileConnectorDataBaseStatusEnum.OCCUPIED_NOT_CHARGED.getValue(), String.valueOf(connectorInfo.getStatus()))
                    || StringUtils.equals(PileConnectorDataBaseStatusEnum.OCCUPIED_CHARGING.getValue(), String.valueOf(connectorInfo.getStatus()))
                    || StringUtils.equals(PileConnectorDataBaseStatusEnum.OCCUPIED_APPOINTMENT_LOCK.getValue(), String.valueOf(connectorInfo.getStatus()))
            ) {
                vo.setFailReason(0);
            } else {
                vo.setFailReason(1); // 1- 此设备尚未插枪；
            }
            vo.setFailReasonMsg("");
        }

        vo.setEquipAuthSeq(equipAuthSeq);
        vo.setConnectorID(pileConnectorCode);

        // 加密
        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(vo).getBytes(),
                configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
        resultMap.put("Sig", resultSign);

        return resultMap;
    }


    /**
     * 请求启动充电
     *
     * @param dto
     */
    public Map<String, String> query_start_charge(QueryStartChargeDTO dto) {
        // 通过传过来的订单号和枪口号生成订单
        // String orderCode = dto.getStartChargeSeq();
        String pileConnectorCode = dto.getConnectorID();
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(dto.getStartChargeSeq());
        if (orderInfo != null) {
            // 平台已存在订单
            return null;
        }
        DockingPlatformConfig configInfo = dockingPlatformConfigService.getInfoByOperatorId(dto.getOperatorID());
        if (configInfo == null) {
            return null;
        }
        Map<String, Object> map = orderBasicInfoService.generateOrderForLianLian(dto);
        String orderCode = (String) map.get("orderCode");
        String transactionCode = (String) map.get("transactionCode");
        OrderBasicInfo orderBasicInfo = (OrderBasicInfo) map.get("orderBasicInfo");

        // 发送启机指令
        StartChargingCommand command = StartChargingCommand.builder()
                .pileSn(orderBasicInfo.getPileSn())
                .connectorCode(orderBasicInfo.getConnectorCode())
                .transactionCode(transactionCode)
                .chargeAmount(orderBasicInfo.getPayAmount())
                .logicCardNum(null)
                .physicsCardNum(null)
                .build();
        ykcPushCommandService.pushStartChargingCommand(command);

        // 拼装对应的数据并返回
        QueryStartChargeVO vo = QueryStartChargeVO.builder()
                .StartChargeSeq(orderCode)
                .StartChargeSeqStat(1) // 1-启动中
                .ConnectorID(pileConnectorCode)
                .SuccStat(0)
                .FailReason(0)

                .build();
        // 推送启动充电结果
        pushStartChargeResult(orderCode);

        // 加密
        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(vo).getBytes(),
                configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
        resultMap.put("Sig", resultSign);

        return resultMap;

    }


    /**
     * 查询充电状态
     *
     * @param dto
     * @return
     */
    public Map<String, String> query_equip_charge_status(QueryEquipChargeStatusDTO dto) {
        // 通过订单号查询订单信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(dto.getStartChargeSeq());
        // 通过订单号查询实时数据
        if (orderInfo == null) {
            return null;
        }
        DockingPlatformConfig configInfo = dockingPlatformConfigService.getInfoByOperatorId(dto.getOperatorID());
        if (configInfo == null) {
            return null;
        }
        OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderInfo.getOrderCode());
        List<RealTimeMonitorData> realTimeData = orderBasicInfoService.getChargingRealTimeData(orderInfo.getTransactionCode());
        RealTimeMonitorData data = realTimeData.get(0);
        String orderStatus = orderInfo.getOrderStatus();
        if (StringUtils.equals(orderStatus, OrderStatusEnum.IN_THE_CHARGING.getValue())) {
            // 充电中
            orderStatus = "2";
        }
        if (StringUtils.equals(orderStatus, OrderStatusEnum.ORDER_COMPLETE.getValue())) {
            // 充电完成
            orderStatus = "4";
        } else {
            // 直接给 5-未知
            orderStatus = "5";
        }
        // 拼装联联平台数据
        QueryChargingStatusVO vo = QueryChargingStatusVO.builder()
                .StartChargeSeq(dto.getStartChargeSeq()) // 订单号
                .StartChargeSeqStat(Integer.parseInt(orderStatus)) // 订单状态
                .ConnectorID(orderInfo.getPileConnectorCode()) // 枪口编码
                .ConnectorStatus(Integer.parseInt(data.getConnectorStatus())) // 枪口状态
                .CurrentA(new BigDecimal(data.getOutputCurrent())) // 电流
                .VoltageA(new BigDecimal(data.getOutputVoltage())) // 电压
                .Soc(new BigDecimal(data.getSOC()))
                .StartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderInfo.getChargeStartTime())) // 开始时间
                .EndTime(DateUtils.getTime()) // 本次采样时间
                .TotalPower(new BigDecimal(data.getChargingDegree())) // 累计充电量
                .ElecMoney(orderDetail.getTotalElectricityAmount()) // 累计电费
                .SeviceMoney(orderDetail.getTotalServiceAmount()) // 累计服务费
                .TotalMoney(new BigDecimal(data.getChargingAmount())) // 已充金额

                .build();


        // 加密
        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(vo).getBytes(),
                configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
        resultMap.put("Sig", resultSign);

        return resultMap;
    }


    /**
     * 请求停止充电
     *
     * @param dto
     * @return
     */
    public Map<String, String> query_stop_charge(QueryStartChargeDTO dto) {
        QueryStopChargeVO vo = new QueryStopChargeVO();

        String orderCode = dto.getStartChargeSeq();

        // 根据订单号查询订单信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        if (orderInfo == null) {
            return null;
        }
        DockingPlatformConfig configInfo = dockingPlatformConfigService.getInfoByOperatorId(dto.getOperatorID());
        if (configInfo == null) {
            return null;
        }
        // 若状态为充电中，则发送停机指令
        if (StringUtils.equals(OrderStatusEnum.IN_THE_CHARGING.getValue(), orderInfo.getOrderStatus())) {
            // 充电中
            StopChargingCommand command = StopChargingCommand.builder()
                    .pileSn(orderInfo.getPileSn())
                    .connectorCode(orderInfo.getConnectorCode())
                    .build();
            ykcPushCommandService.pushStopChargingCommand(command);
            vo.setStartChargeSeq(orderCode);
            vo.setStartChargeSeqStat(3); // 3-停止中
        }
        // 加密
        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(vo).getBytes(),
                configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
        resultMap.put("Sig", resultSign);

        return resultMap;
    }

    /**
     * 从联联平台获取令牌
     *
     * @param operatorId
     * @param operatorSecret
     * @return
     */
    @Override
    public String getToken(String urlAddress, String operatorId, String operatorSecret, String dataSecretIv, String signSecret, String dataSecret) {
        // String operatorId = dto.getOperatorId();
        // String operatorSecret = dto.getOperatorSecret();
        String token = "";
        try {
            // 测试用请求地址
            String requestUrl = urlAddress + "query_token";

            // 请求data
            Map<String, String> data = new HashMap<>();
            data.put("OperatorID", operatorId);
            data.put("OperatorSecret", operatorSecret);
            data.put("DataSecretIV", dataSecretIv);
            String dataJson = JSONUtil.toJsonStr(data);

            // 加密
            byte[] encryptText = Cryptos.aesEncrypt(dataJson.getBytes("UTF-8"),
                    dataSecret.getBytes(), dataSecretIv.getBytes());
            String strData = Encodes.encodeBase64(encryptText);

            Map<String, String> request = new LinkedHashMap<>();
            request.put("OperatorID", operatorId);
            request.put("Data", strData);
            request.put("TimeStamp", DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date()));
            request.put("Seq", "0001");

            // 生成签名
            String sig = GBSignUtils.sign(request, signSecret);
            request.put("Sig", sig);

            String tokenRequest = JSONUtil.toJsonStr(request);

            String response = HttpUtil.post(requestUrl, tokenRequest);
            LianLianResultVO result = JSON.parseObject(response, LianLianResultVO.class);

            if (result.getRet() == 0) {
                // 解密data
                byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64((String) result.getData()),
                        dataSecret.getBytes(), dataSecretIv.getBytes());
                String dataStr = new String(plainText, "UTF-8");
                Map<String, String> resultMap = (Map<String, String>) JSON.parse(dataStr);
                token = resultMap.get("AccessToken");
            }
        } catch (Exception e) {
            return token;
        }
        return token;
    }

    /**
     * 联联平台推送 设备状态变化推送  notification_stationStatus
     *
     * @param pileConnectorCode
     * @param status
     * @return
     */
    @Override
    public String pushConnectorStatus(String pileConnectorCode, String status) {
        // 查出该桩所属哪个站点
        String pileSn = StringUtils.substring(pileConnectorCode, 0, 14);
        PileStationVO stationVO = pileStationInfoService.getStationInfoByPileSn(pileSn);
        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(stationVO.getId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(stationVO.getId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        String url = urlAddress + "notification_stationStatus";

        // 获取令牌
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (StringUtils.isBlank(token)) {
            return null;
        }
        ConnectorStatusInfo info = ConnectorStatusInfo.builder()
                .connectorID(pileConnectorCode)
                .status(status)
                .build();
        // 调用联联平台接口
        JSONObject json = new JSONObject();
        json.put("ConnectorStatusInfo", info);
        String jsonString = JSONObject.toJSONString(json);

        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 推送订单信息  notification_orderInfo
     *
     * @param orderCode
     * @return
     */
    @Override
    public String pushOrderInfo(String orderCode) {
        List<ChargeDetail> chargeDetails = new ArrayList<>();
        // 根据订单号查询出信息
        OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode);

        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(orderBasicInfo.getStationId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(orderBasicInfo.getStationId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        String url = urlAddress + "notification_orderInfo";

        // 拼装成联联平台所需格式对象
        OrderInfo orderInfo = OrderInfo.builder()
                .operatorID(operatorId)
                .equipmentOwnerID(Constants.OPERATORID_LIANLIAN)
                .stationID(orderBasicInfo.getStationId())
                .equipmentID(orderBasicInfo.getPileSn())
                .connectorID(orderBasicInfo.getPileConnectorCode())
                .startChargeSeq(orderCode)
                .userChargeType(Constants.one)
                .money(new BigDecimal(String.valueOf(orderBasicInfo.getOrderAmount())).setScale(2, BigDecimal.ROUND_HALF_UP))
                .electMoney(new BigDecimal(String.valueOf(orderDetail.getTotalElectricityAmount())).setScale(2, BigDecimal.ROUND_HALF_UP))
                .serviceMoney(new BigDecimal(String.valueOf(orderDetail.getTotalServiceAmount())).setScale(2, BigDecimal.ROUND_HALF_UP))
                .elect(new BigDecimal(String.valueOf(orderDetail.getTotalUsedElectricity())).setScale(2, BigDecimal.ROUND_HALF_UP))
                .startTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeStartTime()))
                .endTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeEndTime()))
                .paymentAmount(orderBasicInfo.getPayAmount())
                // .payChannel()
                .stopReason(0)
                // .chargeDetails()
                .build();
        // 支付方式
        if (StringUtils.equals(orderBasicInfo.getPayMode(), OrderPayModeEnum.PAYMENT_OF_WECHATPAY.getValue())) {
            // 微信支付
            orderInfo.setPayChannel(Integer.valueOf(PayChannelEnum.WEXIN_PAY.getCode()));
        } else if (StringUtils.equals(orderBasicInfo.getPayMode(), OrderPayModeEnum.PAYMENT_OF_ALIPAY.getValue())) {
            // 支付宝支付
            orderInfo.setPayChannel(Integer.valueOf(PayChannelEnum.ALI_PAY.getCode()));
        } else {
            // 其他
            orderInfo.setPayChannel(Integer.valueOf(PayChannelEnum.OTHER.getCode()));
        }
        // 订单详情
        ChargeDetail detail;

        List<BillingPriceVO> billingList = pileBillingTemplateService.queryBillingPrice(orderBasicInfo.getStationId());
        // 先将list按照 尖、峰、平、谷 时段排序
        // List<BillingPriceVO> collect = billingList.stream().sorted(Comparator.comparing(BillingPriceVO::getTimeType)).collect(Collectors.toList());
        // 再循环该list，拼装对应的充电价格、费率
        for (BillingPriceVO billingPriceVO : billingList) {
            detail = new ChargeDetail();
            if (StringUtils.equals(billingPriceVO.getTimeType(), "1")) {
                // 尖时段
                detail.setDetailStartTime(billingPriceVO.getStartTime());
                detail.setDetailEndTime(billingPriceVO.getEndTime());
                detail.setElecPrice(new BigDecimal(billingPriceVO.getElectricityPrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
                detail.setSevicePrice(new BigDecimal(billingPriceVO.getServicePrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
                detail.setDetailPower(orderDetail.getSharpUsedElectricity());
                detail.setDetailElecMoney(orderDetail.getSharpElectricityPrice());
                detail.setDetailSeviceMoney(orderDetail.getSharpServicePrice());
            } else if (StringUtils.equals(billingPriceVO.getTimeType(), "2")) {
                // 峰时段
                detail.setDetailStartTime(billingPriceVO.getStartTime());
                detail.setDetailEndTime(billingPriceVO.getEndTime());
                detail.setElecPrice(new BigDecimal(billingPriceVO.getElectricityPrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
                detail.setSevicePrice(new BigDecimal(billingPriceVO.getServicePrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
                detail.setDetailPower(orderDetail.getPeakUsedElectricity());
                detail.setDetailElecMoney(orderDetail.getPeakElectricityPrice());
                detail.setDetailSeviceMoney(orderDetail.getPeakServicePrice());
            } else if (StringUtils.equals(billingPriceVO.getTimeType(), "3")) {
                // 平时段
                detail.setDetailStartTime(billingPriceVO.getStartTime());
                detail.setDetailEndTime(billingPriceVO.getEndTime());
                detail.setElecPrice(new BigDecimal(billingPriceVO.getElectricityPrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
                detail.setSevicePrice(new BigDecimal(billingPriceVO.getServicePrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
                detail.setDetailPower(orderDetail.getFlatUsedElectricity());
                detail.setDetailElecMoney(orderDetail.getFlatElectricityPrice());
                detail.setDetailSeviceMoney(orderDetail.getFlatServicePrice());
            } else if (StringUtils.equals(billingPriceVO.getTimeType(), "4")) {
                // 谷时段
                detail.setDetailStartTime(billingPriceVO.getStartTime());
                detail.setDetailEndTime(billingPriceVO.getEndTime());
                detail.setElecPrice(new BigDecimal(billingPriceVO.getElectricityPrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
                detail.setSevicePrice(new BigDecimal(billingPriceVO.getServicePrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
                detail.setDetailPower(orderDetail.getValleyUsedElectricity());
                detail.setDetailElecMoney(orderDetail.getValleyElectricityPrice());
                detail.setDetailSeviceMoney(orderDetail.getValleyServicePrice());
            }
            chargeDetails.add(detail);
        }

        orderInfo.setChargeDetails(chargeDetails);

        // 获取令牌
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (StringUtils.isBlank(token)) {
            return null;
        }
        // 调用联联平台接口
        JSONObject json = new JSONObject();
        json.put("OrderInfo", orderInfo);
        String jsonString = JSONObject.toJSONString(json);

        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 推送启动充电结果
     *
     * @param orderCode
     * @return
     */
    @Override
    public String pushStartChargeResult(String orderCode) {
        // 根据订单号查询订单信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        if (orderInfo == null) {
            return null;
        }
        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(orderInfo.getStationId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(orderBasicInfo.getStationId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        // 推送启动充电结果(调用接口 notification_start_charge_result)
        String url = urlAddress + "notification_start_charge_result";
        // 获取令牌
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (StringUtils.isBlank(token)) {
            return null;
        }
        String orderStatus = orderInfo.getOrderStatus();
        // 订单状态统一
        if (StringUtils.equals(OrderStatusEnum.IN_THE_CHARGING.getValue(), orderStatus)) {
            // 充电中
            orderStatus = "2";
        } else if (StringUtils.equals(OrderStatusEnum.NOT_START.getValue(), orderStatus)) {
            // 未启动
            orderStatus = "1";
        } else {
            // 其他状态都为 未知
            orderStatus = "5";
        }
        // 拼装参数
        JSONObject json = new JSONObject();
        json.put("StartChargeSeq", orderCode);
        json.put("ConnectorID", orderInfo.getPileConnectorCode());
        json.put("StartChargeSeqStat", Integer.valueOf(orderStatus));
        json.put("StartTime", orderInfo.getChargeStartTime());

        String jsonString = JSONObject.toJSONString(json);

        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 推送充电状态(仅桩充电时调)
     *
     * @param orderCode
     * @return
     */
    @Override
    public String pushChargeStatus(String orderCode) throws UnsupportedEncodingException {
        // 根据订单号查询订单信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(orderInfo.getStationId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(orderInfo.getStationId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        // 调用 查询充电状态方法
        QueryEquipChargeStatusDTO dto = new QueryEquipChargeStatusDTO();
        dto.setStartChargeSeq(orderCode);
        Map<String, String> map = query_equip_charge_status(dto);
        if (map == null) {
            return null;
        }
        String data = map.get("Data");
        // 解密data
        byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(data),
                operatorSecret.getBytes(), operatorSecret.getBytes());
        String dataStr = new String(plainText, StandardCharsets.UTF_8);
        // 转成对应的对象
        QueryEquipChargeStatusDTO queryEquipChargeStatusDTO = JSONObject.parseObject(dataStr, QueryEquipChargeStatusDTO.class);
        // 获取令牌
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (StringUtils.isBlank(token)) {
            return null;
        }
        String url = urlAddress + "notification_equip_charge_status";
        // 调用联联平台接口
        String jsonString = JSONObject.toJSONString(queryEquipChargeStatusDTO);

        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 推送停止充电结果(仅在 交易记录的帧类型中调用)
     *
     * @param orderCode
     * @return
     */
    @Override
    public String pushStopChargeResult(String orderCode) {

        // 根据订单号查询订单信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        if (orderInfo == null) {
            return null;
        }
        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(orderInfo.getStationId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(orderInfo.getStationId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        String url = urlAddress + "notification_stop_charge_result";

        String orderStatus = orderInfo.getOrderStatus();
        String successFlag = "1";
        if (StringUtils.equals(orderStatus, OrderStatusEnum.IN_THE_CHARGING.getValue())) {
            // 充电中
            orderStatus = "2";
        } else if (StringUtils.equals(orderStatus, OrderStatusEnum.ORDER_COMPLETE.getValue())) {
            // 订单完成
            orderStatus = "4";
            successFlag = "0";
        } else {
            orderStatus = "5";
        }
        // 获取token
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (StringUtils.isBlank(token)) {
            return null;
        }

        // 拼装联联平台参数
        JSONObject json = new JSONObject();
        json.put("StartChargeSeq", orderCode);
        json.put("StartChargeSeqStat", Integer.valueOf(orderStatus));
        json.put("ConnectorID", orderInfo.getPileConnectorCode());
        json.put("SuccStat", successFlag);
        json.put("FailReason", 0);

        String jsonString = JSONObject.toJSONString(json);

        // 发送请求
        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 推送充电订单信息
     *
     * @param orderCode
     * @return
     */
    @Override
    public String pushChargeOrderInfo(String orderCode) {

        OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode);

        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(orderBasicInfo.getStationId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(orderBasicInfo.getStationId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        String url = urlAddress + "notification_charge_order_info";

        JSONObject json = new JSONObject();
        json.put("StartChargeSeq", orderCode);
        json.put("ConnectorID", orderBasicInfo.getPileConnectorCode());
        json.put("StartTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeStartTime()));
        json.put("EndTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeEndTime()));
        json.put("TotalPower", orderDetail.getTotalUsedElectricity().setScale(2, BigDecimal.ROUND_HALF_UP));
        json.put("TotalElecMoney", orderDetail.getTotalElectricityAmount().setScale(2, BigDecimal.ROUND_HALF_UP));
        json.put("TotalSeviceMoney", orderDetail.getTotalServiceAmount().setScale(2, BigDecimal.ROUND_HALF_UP));
        json.put("TotalMoney", orderDetail.getTotalOrderAmount().setScale(2, BigDecimal.ROUND_HALF_UP));
        json.put("StopReason", 2); // 2：BMS 停止充电

        String jsonString = JSONObject.toJSONString(json);
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (token == null) {
            return null;
        }
        // 发送请求
        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 推送订单结算信息 (联联推给我们)
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> pushOrderSettlementInfo(PushOrderSettlementDTO dto) {
        String orderCode = dto.getStartChargeSeq();
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("StartChargeSeq", orderCode);
        map.put("ConnectorID", dto.getConnectorID());
        int confirmResult = Constants.one;
        // 根据订单号查询订单信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode);
        if (orderInfo == null || orderDetail == null) {
            return null;
        }
        // 通过operatorID 查出 operatorSecret
        DockingPlatformConfig platformConfig = dockingPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
        if (platformConfig == null) {
            return null;
        }
        // orderInfo 和 orderDetail 都不为空，再根据传过来的数据进行修改数据库信息
        confirmResult = Constants.zero;
        orderDetail.setTotalUsedElectricity(dto.getTotalPower()); // 累计充电量
        orderDetail.setTotalOrderAmount(dto.getTotalMoney()); // 累计总金额

        orderBasicInfoService.updateOrderDetail(orderDetail);

        map.put("ConfirmResult", confirmResult);

        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(map).getBytes(),
                platformConfig.getOperatorSecret().getBytes(), platformConfig.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, platformConfig.getOperatorSecret());
        resultMap.put("Sig", resultSign);

        return resultMap;
    }

    /**
     * 查询订单结算信息
     *
     * @param orderCode
     * @return
     */
    @Override
    public String queryOrderSettlementInfo(String orderCode) {

        JSONObject json = new JSONObject();
        json.put("StartChargeSeq", orderCode);
        String jsonString = JSONObject.toJSONString(json);

        OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);

        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(orderBasicInfo.getStationId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(orderBasicInfo.getStationId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        String url = urlAddress + "query_order_settlement_info";

        // 获取令牌
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (StringUtils.isBlank(token)) {
            return null;
        }
        // 发送请求
        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 推送订单对账结果信息
     *
     * @param orderCode
     * @return
     */
    @Override
    public String pushOrderReconciliationInfo(String orderCode) {

        List<ChargeOrder> list = new ArrayList<>();
        // 根据订单号查询订单信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode);
        if (orderInfo == null || orderDetail == null) {
            return null;
        }

        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(orderInfo.getStationId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(orderInfo.getStationId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        String url = urlAddress + "check_charge_orders";

        JSONObject json = new JSONObject();
        json.put("CheckOrderSeq", orderCode);
        json.put("StartTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderInfo.getChargeStartTime()));
        json.put("EndTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderInfo.getChargeEndTime()));
        json.put("OrderCount", 1);
        json.put("TotalOrderPower", orderDetail.getTotalUsedElectricity());
        json.put("TotalOrderMoney", orderDetail.getTotalOrderAmount());

        ChargeOrder chargeOrder = ChargeOrder.builder()
                .startChargeSeq(orderCode)
                .totalPower(orderDetail.getTotalUsedElectricity())
                .totalMoney(orderDetail.getTotalOrderAmount())
                .build();
        list.add(chargeOrder);
        json.put("ChargeOrders", list);

        // 获取令牌
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (StringUtils.isBlank(token)) {
            return null;
        }
        String jsonString = JSONObject.toJSONString(json);
        // 发送请求
        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 设备充电中状态变化推送  notification_connector_charge_status
     *
     * @param orderCode
     * @return
     */
    @Override
    public String pushPileChargeStatusChange(String orderCode) {
        // 通过订单号查询信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        // 根据枪口号查询充电实时状态
        OrderBasicInfo orderBasicInfo = orderBasicInfoService.queryChargingByPileConnectorCode(orderInfo.getPileConnectorCode());
        List<RealTimeMonitorData> list = orderBasicInfoService.getChargingRealTimeData(orderBasicInfo.getTransactionCode());
        RealTimeMonitorData realTimeMonitorData = list.get(0);
        // 拼装联联参数
        ConnectorChargeStatusInfo info = ConnectorChargeStatusInfo.builder()
                .startChargeSeq(orderBasicInfo.getOrderCode())
                .connectorID(orderInfo.getPileConnectorCode())
                .connectorStatus(Integer.valueOf(realTimeMonitorData.getConnectorStatus()))
                .currentA(new BigDecimal(realTimeMonitorData.getOutputCurrent()))
                .voltageA(new BigDecimal(realTimeMonitorData.getOutputVoltage()))
                .soc(new BigDecimal(realTimeMonitorData.getSOC()))
                .startTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeStartTime()))
                .endTime(DateUtils.getTime())
                .totalPower(new BigDecimal(realTimeMonitorData.getChargingDegree()))
                .elecMoney(new BigDecimal("0")) // TODO
                .seviceMoney(new BigDecimal("0")) // TODO
                .totalMoney(new BigDecimal(realTimeMonitorData.getChargingAmount()).setScale(2, BigDecimal.ROUND_HALF_UP))

                .build();

        // 通过站点id查询相关配置信息
        StationSettingRelation relation = new StationSettingRelation();
        relation.setStationId(Long.parseLong(orderInfo.getStationId()));
        StationSettingRelationVO relationInfo = stationSettingRelationService.selectRelationInfo(relation);
        // ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.getInfoByStationId(Long.parseLong(orderBasicInfo.getStationId()));
        if (relationInfo == null) {
            return null;
        }
        String operatorId = relationInfo.getOperatorId();
        String operatorSecret = relationInfo.getOperatorSecret();
        String signSecret = relationInfo.getSignSecret();
        String dataSecret = relationInfo.getDataSecret();
        String dataSecretIv = relationInfo.getDataSecretIv();
        String urlAddress = relationInfo.getUrlAddress();

        String url = urlAddress + "notification_connector_charge_status";

        // 获取令牌
        String token = getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
        if (StringUtils.isBlank(token)) {
            return null;
        }
        JSONObject json = new JSONObject();
        json.put("ConnectorChargeStatusInfo", info);

        String jsonString = JSONObject.toJSONString(json);
        // 发送请求
        String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);

        return result;
    }

    /**
     * 生成令牌
     * @param dto
     * @return
     * @throws UnsupportedEncodingException
     */
    @Override
    public Map<String, String> generateToken(CommonParamsDTO dto) throws UnsupportedEncodingException {
        String operatorID = dto.getOperatorID();
        // 通过operatorID 查出 operatorSecret
        DockingPlatformConfig platformConfig = dockingPlatformConfigService.getInfoByOperatorId(operatorID);
        if (platformConfig == null) {
            return null;
        }
        String operatorSecret = platformConfig.getOperatorSecret();
        // 校验签名
        Map<String, String> checkResultMap = checkoutSign(dto);
        if (checkResultMap == null) {
            // 校验失败
            return null;
        }
        String dataString = checkResultMap.get("Data");
        // 解密data
        byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), operatorSecret.getBytes(), operatorSecret.getBytes());
        String dataStr = new String(plainText, "UTF-8");
        Map<String, String> resMap = (Map<String, String>) JSON.parse(dataStr);
        String secret = resMap.get("OperatorSecret");

        // 对比解出来的密钥是否和数据库中保存的一致
        if (!StringUtils.equals(operatorSecret, secret)) {
            System.out.println("密钥不一致");
            return null;
        }
        System.out.println("密钥校验通过!!  密钥为: " + operatorSecret);

        // 生成token返回 eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0MjUwMTA3NjUiLCJpYXQiOjE2ODUwOTcxMTYsInN1YiI6IjEyMzEyMzEyMzEyM2FhYWEiLCJleHAiOjY4NjkwOTcxMTZ9.NyxOUIZmgsqtfex7oiMRR2LaWePTA56WHVMXIkWWt2w
        long ttlMillis = 60 * 60 * 24 * 1000;
        String token = JWTUtils.createToken(operatorID, operatorSecret, ttlMillis);
        System.out.println("生成的token：" + token);

        // 组装返回参数
        AccessTokenVO vo = new AccessTokenVO();
        vo.setAccessToken(token);
        vo.setOperatorID(operatorID);
        vo.setTokenAvailableTime((int) (ttlMillis / 1000));
        vo.setFailReason(0);
        vo.setSuccStat(0);

        Map<String, String> resultMap = Maps.newLinkedHashMap();
        // 加密数据
        byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(vo).getBytes(),
                platformConfig.getDataSecret().getBytes(), platformConfig.getDataSecretIv().getBytes());
        String encryptData = Encodes.encodeBase64(encryptText);

        resultMap.put("Data", encryptData);
        // 生成sig
        String resultSign = GBSignUtils.sign(resultMap, operatorSecret);
        resultMap.put("Sig", resultSign);

        return resultMap;
    }

    /**
     * 校验签名并返回data(未解密)
     * @param dto
     */
    @Override
    public Map<String, String> checkoutSign(CommonParamsDTO dto){
        String operatorID = dto.getOperatorID();
        // 通过operatorID 查出 operatorSecret
        DockingPlatformConfig platformConfig = dockingPlatformConfigService.getInfoByOperatorId(operatorID);
        if (platformConfig == null) {
            return null;
        }
        String operatorSecret = platformConfig.getOperatorSecret();

        Map<String, String> map = Maps.newLinkedHashMap();
        map.put("OperatorID", dto.getOperatorID());
        map.put("Data", dto.getData());
        map.put("TimeStamp", dto.getTimeStamp());
        map.put("Seq", dto.getSeq());
        String sign = GBSignUtils.sign(map, operatorSecret);
        System.out.println(sign);

        // 验证签名 得到请求方传过来的签名sig->自己拿到请求体后，再按双方约定的协议生成一个sig->对比两个sig是否一致
        if (!StringUtils.equals(dto.getSig(), sign)) {
            System.out.println("签名校验==失败");
            return null;
        }
        System.out.println("签名校验通过！！！");

        // 解密data
        // byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dto.getData()), operatorSecret.getBytes(), operatorSecret.getBytes());
        // String dataStr = new String(plainText, "UTF-8");
        // Map<String, String> resMap = (Map<String, String>) JSON.parse(dataStr);
        // return resMap;
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("Data", dto.getData());
        resultMap.put("OperatorSecret", operatorSecret);
        return resultMap;
    }

    /**
     * TODO 请求打印充电小票
     */
    public void query_print_parking_ticket() {

    }

    /**
     * 获取桩列表信息
     *
     * @param pileStationInfo
     * @return
     */
    private List<EquipmentInfo> getPileList(PileStationInfo pileStationInfo) {
        List<EquipmentInfo> resultList = new ArrayList<>();
        // 通过站点id查询桩基本信息
        List<PileBasicInfo> list = pileBasicInfoService.getPileListByStationId(String.valueOf(pileStationInfo.getId()));
        MerchantInfoVO merchantInfo = pileMerchantInfoService.getMerchantInfo(String.valueOf(pileStationInfo.getMerchantId()));
        // 封装成联联平台对象
        for (PileBasicInfo pileBasicInfo : list) {
            EquipmentInfo equipmentInfo = new EquipmentInfo();
            String pileSn = pileBasicInfo.getSn();

            equipmentInfo.setEquipmentID(pileSn);
            equipmentInfo.setManufacturerID(Constants.OPERATORID_LIANLIAN);
            equipmentInfo.setConstructionTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, pileBasicInfo.getCreateTime()));

            PileModelInfoVO modelInfo = pileModelInfoService.getPileModelInfoByPileSn(pileSn);
            equipmentInfo.setEquipmentType(Integer.valueOf(modelInfo.getSpeedType()));

            // Map<String, String> pileStatus = pileConnectorInfoService.getPileStatus(Lists.newArrayList(pileBasicInfo.getSn()));
            Map<String, String> pileStatusMap = pileConnectorInfoService.getPileStatus(Lists.newArrayList(pileSn));
            String pileStatus = pileStatusMap.get(pileSn);
            if (StringUtils.equals(PileStatusEnum.ON_LINE.getValue(), pileStatus)) {
                // 1-在线
                pileStatus = LianLianPileStatusEnum.NORMAL.getCode();
            } else if (StringUtils.equals(PileStatusEnum.OFF_LINE.getValue(), pileStatus)) {
                // 2-离线
                pileStatus = LianLianPileStatusEnum.CLOSE_OFFLINE.getCode();
            } else if (StringUtils.equals(PileStatusEnum.FAULT.getValue(), pileStatus)) {
                // 3-故障
                pileStatus = LianLianPileStatusEnum.UNDER_MAINTENANCE.getCode();
            }
            equipmentInfo.setEquipmentStatus(Integer.valueOf(pileStatus));
            equipmentInfo.setEquipmentPower(new BigDecimal(modelInfo.getRatedPower()).setScale(1, BigDecimal.ROUND_HALF_UP));
            equipmentInfo.setNewNationalStandard(1);
            equipmentInfo.setVinFlag(1);

            List<ConnectorInfo> connectorList = getConnectorList(pileBasicInfo);
            equipmentInfo.setConnectorInfos(connectorList);

            resultList.add(equipmentInfo);
        }
        return resultList;
    }


    /**
     * 获取枪口列表
     *
     * @param pileBasicInfo
     * @return
     */
    private List<ConnectorInfo> getConnectorList(PileBasicInfo pileBasicInfo) {
        List<ConnectorInfo> resultList = new ArrayList<>();

        List<PileConnectorInfo> list = pileConnectorInfoService.selectPileConnectorInfoList(pileBasicInfo.getSn());
        for (PileConnectorInfo pileConnectorInfo : list) {
            ConnectorInfo connectorInfo = new ConnectorInfo();

            connectorInfo.setConnectorID(pileConnectorInfo.getPileConnectorCode());
            String pileSn = pileConnectorInfo.getPileSn();
            PileModelInfoVO modelInfo = pileModelInfoService.getPileModelInfoByPileSn(pileSn);
            int connectorType = StringUtils.equals("1", modelInfo.getSpeedType()) ? 4 : 3;

            connectorInfo.setConnectorType(connectorType);
            connectorInfo.setVoltageUpperLimits(Integer.valueOf(modelInfo.getRatedVoltage()));
            connectorInfo.setVoltageLowerLimits(Integer.valueOf(modelInfo.getRatedVoltage()));
            connectorInfo.setCurrent(Integer.valueOf(modelInfo.getRatedCurrent()));
            connectorInfo.setPower(new BigDecimal(modelInfo.getRatedPower()).setScale(1, BigDecimal.ROUND_HALF_UP));

            resultList.add(connectorInfo);
        }

        return resultList;
    }
}
