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

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.enums.thirdparty.ThirdPlatformTypeEnum;
import com.jsowell.common.util.PageUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.*;
import com.jsowell.pile.dto.*;
import com.jsowell.pile.service.*;
import com.jsowell.pile.vo.base.MerchantInfoVO;
import com.jsowell.pile.vo.base.ThirdPartyStationInfoVO;
import com.jsowell.pile.vo.base.ThirdPartyStationRelationVO;
import com.jsowell.pile.vo.uniapp.customer.BillingPriceVO;
import com.jsowell.pile.vo.web.PileModelInfoVO;
import com.jsowell.pile.vo.zdl.EquipBusinessPolicyVO;
import com.jsowell.thirdparty.common.CommonService;
import com.jsowell.pile.thirdparty.CommonParamsDTO;
import com.jsowell.thirdparty.lianlian.service.LianLianService;
import com.jsowell.thirdparty.platform.util.Cryptos;
import com.jsowell.thirdparty.platform.util.Encodes;
import com.jsowell.thirdparty.platform.util.GBSignUtils;
import com.jsowell.thirdparty.platform.util.HttpRequestUtil;
import com.jsowell.thirdparty.yongchengboche.dto.YCBCGetTokenDTO;
import com.jsowell.thirdparty.yongchengboche.service.YCBCService;
import com.jsowell.pile.thirdparty.ZDLConnectorInfo;
import com.jsowell.pile.thirdparty.ZDLEquipmentInfo;
import com.jsowell.pile.thirdparty.ZDLStationInfo;
import com.jsowell.thirdparty.zhongdianlian.dto.ZDLGetTokenDTO;
import com.jsowell.thirdparty.zhongdianlian.service.ZDLService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 中电联 Service
 *
 * @author Lemon
 * @Date 2023/9/2 13:54
 */
@Service
public class ZDLServiceImpl implements ZDLService {

    @Autowired
    private LianLianService lianLianService;

    @Autowired
    private YCBCService ycbcService;

    @Autowired
    private PileStationInfoService pileStationInfoService;

    @Autowired
    private ThirdPartyPlatformConfigService thirdPartyPlatformConfigService;

    @Autowired
    private PileBasicInfoService pileBasicInfoService;

    @Autowired
    private PileModelInfoService pileModelInfoService;

    @Autowired
    private PileMerchantInfoService pileMerchantInfoService;

    @Autowired
    private PileConnectorInfoService pileConnectorInfoService;

    @Autowired
    private ThirdPartyStationRelationService thirdPartyStationRelationService;

    @Autowired
    private PileBillingTemplateService pileBillingTemplateService;

    @Autowired
    private ThirdPartySettingInfoService thirdPartySettingInfoService;

    @Autowired
    private CommonService commonService;

    /**
     * 获取令牌
     * @param dto
     * @return
     */
    @Override
    public String ZDLGetToken(ZDLGetTokenDTO dto) {
        if (dto == null) {
            return null;
        }
        return lianLianService.getToken(dto.getUrlAddress(), dto.getOperatorId(),
                dto.getOperatorSecret(), dto.getDataSecretIv(), dto.getSignSecret(), dto.getDataSecret());
    }

    /**
     * 根据充电站id，推送充电站信息
     * @param dto
     */
    @Override
    public String pushStationInfo(PushStationInfoDTO dto) {
        // 通过id查询站点相关信息
        PileStationInfo pileStationInfo = pileStationInfoService.selectPileStationInfoById(dto.getStationId());
        // 通过站点id查询相关配置信息
        ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        relation.setStationId(dto.getStationId());
        ThirdPartyStationRelationVO relationInfo = thirdPartyStationRelationService.selectRelationInfo(relation);
        if (relationInfo == null) {
            // 新增
            relation.setThirdPartyType(dto.getThirdPartyType());
            thirdPartyStationRelationService.insertThirdPartyStationRelation(relation);

            relationInfo = thirdPartyStationRelationService.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();
        String thirdPartyType = relation.getThirdPartyType();

        // 组装中电联平台所需要的数据格式
        ZDLStationInfo info = ZDLStationInfo.builder()
                .stationId(String.valueOf(dto.getStationId()))
                .operatorId(Constants.OPERATORID_JIANG_SU)
                // .equipmentOwnerId(Constants.OPERATORID_JIANG_SU)
                .stationName(pileStationInfo.getStationName())
                .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()))

                .build();
        // 截取运营商组织机构代码(去除最后一位后的最后九位)
        MerchantInfoVO merchantInfo = pileMerchantInfoService.getMerchantInfoVO(String.valueOf(pileStationInfo.getMerchantId()));
        String organizationCode = merchantInfo.getOrganizationCode();
        if (StringUtils.isNotBlank(organizationCode) && organizationCode.length() == 18) {
            String equipmentOwnerId = StringUtils.substring(organizationCode, organizationCode.length() - 10, organizationCode.length() - 1);
            info.setEquipmentOwnerId(equipmentOwnerId);
        }

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

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

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

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

        // 获取令牌
        String token = "";
        String result = "";
        if (StringUtils.equals(ThirdPlatformTypeEnum.YONG_CHENG_BO_CHE.getTypeCode(), thirdPartyType)) {
            YCBCGetTokenDTO ycbcGetTokenDTO = new YCBCGetTokenDTO();
            ycbcGetTokenDTO.setOperatorId(operatorId);
            ycbcGetTokenDTO.setDataSecret(dataSecret);
            ycbcGetTokenDTO.setSignSecret(signSecret);
            ycbcGetTokenDTO.setUrlAddress(urlAddress);
            ycbcGetTokenDTO.setDataSecretIv(dataSecretIv);
            ycbcGetTokenDTO.setOperatorSecret(operatorSecret);

            token = ycbcService.YCBCGetToken(ycbcGetTokenDTO);
            result = HttpRequestUtil.YCBCSendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);
        }else {
            ZDLGetTokenDTO tokenDTO = ZDLGetTokenDTO.builder()
                    .urlAddress(urlAddress)
                    .operatorId(operatorId)
                    .operatorSecret(operatorSecret)
                    .dataSecret(dataSecret)
                    .dataSecretIv(dataSecretIv)
                    .signSecret(signSecret)
                    .build();
            token = ZDLGetToken(tokenDTO);
            result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);
        }
        // System.out.println(result);
        return result;
    }


    public String pushStationInfoV2(PushStationInfoDTO dto) {
        // 通过id查询站点相关信息
        PileStationInfo pileStationInfo = pileStationInfoService.selectPileStationInfoById(dto.getStationId());
        // 通过第三方配置类型查询相关配置信息
        ThirdPartySettingInfo settingInfo = new ThirdPartySettingInfo();
        settingInfo.setType(dto.getThirdPartyType());
        ThirdPartySettingInfo thirdPartySettingInfo = thirdPartySettingInfoService.selectSettingInfo(settingInfo);

        String operatorId = thirdPartySettingInfo.getOperatorId();
        String operatorSecret = thirdPartySettingInfo.getOperatorSecret();
        String signSecret = thirdPartySettingInfo.getSignSecret();
        String dataSecret = thirdPartySettingInfo.getDataSecret();
        String dataSecretIv = thirdPartySettingInfo.getDataSecretIv();
        String urlAddress = thirdPartySettingInfo.getUrlAddress();

        // 组装中电联平台所需要的数据格式
        ZDLStationInfo info = ZDLStationInfo.builder()
                .stationId(String.valueOf(dto.getStationId()))
                .operatorId(Constants.OPERATORID_JIANG_SU)
                // .equipmentOwnerId(Constants.OPERATORID_JIANG_SU)
                .stationName(pileStationInfo.getStationName())
                .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()))
                .build();
        // 截取运营商组织机构代码(去除最后一位后的最后九位)
        MerchantInfoVO merchantInfo = pileMerchantInfoService.getMerchantInfoVO(String.valueOf(pileStationInfo.getMerchantId()));
        String organizationCode = merchantInfo.getOrganizationCode();
        if (StringUtils.isNotBlank(organizationCode) && organizationCode.length() == 18) {
            String equipmentOwnerId = StringUtils.substring(organizationCode, organizationCode.length() - 10, organizationCode.length() - 1);
            info.setEquipmentOwnerId(equipmentOwnerId);
        }
        // 站点图片
        if (StringUtils.isNotBlank(pileStationInfo.getPictures())) {
            info.setPictures(Lists.newArrayList(pileStationInfo.getPictures().split(",")));
        }

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

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

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

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

        // 获取令牌
        String token = "";
        String result = "";
        if (StringUtils.equals(ThirdPlatformTypeEnum.YONG_CHENG_BO_CHE.getTypeCode(), dto.getThirdPartyType())) {
            YCBCGetTokenDTO ycbcGetTokenDTO = new YCBCGetTokenDTO();
            ycbcGetTokenDTO.setOperatorId(operatorId);
            ycbcGetTokenDTO.setDataSecret(dataSecret);
            ycbcGetTokenDTO.setSignSecret(signSecret);
            ycbcGetTokenDTO.setUrlAddress(urlAddress);
            ycbcGetTokenDTO.setDataSecretIv(dataSecretIv);
            ycbcGetTokenDTO.setOperatorSecret(operatorSecret);

            token = ycbcService.YCBCGetToken(ycbcGetTokenDTO);
            result = HttpRequestUtil.YCBCSendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);
        }else {
            ZDLGetTokenDTO tokenDTO = ZDLGetTokenDTO.builder()
                    .urlAddress(urlAddress)
                    .operatorId(operatorId)
                    .operatorSecret(operatorSecret)
                    .dataSecret(dataSecret)
                    .dataSecretIv(dataSecretIv)
                    .signSecret(signSecret)
                    .build();
            token = ZDLGetToken(tokenDTO);
            result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);
        }
        // 新增数据库
        commonService.insertInfo2DataBase(dto);
        return dto.getThirdPartyType() + "：" + result;
    }



    public static void main(String[] args) {
        String startTime = "00:00:00"; // 00:00:00 格式
        // 需要将中间的冒号去掉
        String replace = StringUtils.replace(startTime, ":", "");
        System.out.println(replace);

        // String areaCode = "330000,330200,330213";
        // // 根据逗号分组
        // String[] split = StringUtils.split(areaCode, ",");
        // // 只取最后一部分
        // String subAreaCode = split[split.length - 1];
        //
        // System.out.println(subAreaCode);
        //
        //
        // String organizationCode = "91310120MAC13L2Q9P";
        // String equipmentOwnerId = StringUtils.substring(organizationCode, organizationCode.length() - 10, organizationCode.length() - 1);
        // System.out.println(equipmentOwnerId); // MAC13L2Q9
    }

    /**
     * 查询站点信息
     * @return
     */
    @Override
    public Map<String, String> queryStationsInfo(QueryStationInfoDTO dto) {
        List<ZDLStationInfo> resultList = new ArrayList<>();
        int pageNo = dto.getPageNo() == null ? 1 : dto.getPageNo();
        int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize();

        // 根据第三方平台类型查询出要查询的充电站id并set进 dto 的stationIds
        ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        relation.setThirdPartyType(dto.getThirdPlatformType());
        List<ThirdPartyStationRelation> xdtList = thirdPartyStationRelationService.selectThirdPartyStationRelationList(relation);
        if (CollectionUtils.isNotEmpty(xdtList)) {
            List<String> stationList = xdtList.stream()
                    .map(x -> String.valueOf(x.getStationId()))
                    .collect(Collectors.toList());
            dto.setStationIds(stationList);
        }

        PageUtils.startPage(pageNo, pageSize);
        List<ThirdPartyStationInfoVO> stationInfos = pileStationInfoService.getStationInfosByThirdParty(dto);
        if (CollectionUtils.isEmpty(stationInfos)) {
            // 未查到数据
            return null;
        }
        ThirdPartyPlatformConfig configInfo = thirdPartyPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
        if (configInfo == null) {
            return null;
        }
        PageInfo<ThirdPartyStationInfoVO> pageInfo = new PageInfo<>(stationInfos);
        for (ThirdPartyStationInfoVO pileStationInfo : pageInfo.getList()) {
            ZDLStationInfo stationInfo = new ZDLStationInfo();
            stationInfo.setStationId(String.valueOf(pileStationInfo.getId()));
            stationInfo.setOperatorId(Constants.OPERATORID_JIANG_SU); // 组织机构代码
            String organizationCode = pileStationInfo.getOrganizationCode();
            if (StringUtils.isNotBlank(organizationCode) && organizationCode.length() == 18) {
                String equipmentOwnerId = StringUtils.substring(organizationCode, organizationCode.length() - 10, organizationCode.length() - 1);
                stationInfo.setEquipmentOwnerId(equipmentOwnerId);
            }else {
                stationInfo.setEquipmentOwnerId(Constants.OPERATORID_JIANG_SU);
            }
            stationInfo.setStationName(pileStationInfo.getStationName());
            stationInfo.setCountryCode(pileStationInfo.getCountryCode());
            String areaCode = pileStationInfo.getAreaCode();  // 330000,330200,330213
            // 根据逗号分组
            String[] split = StringUtils.split(areaCode, ",");
            // 只取最后一部分  330213
            String subAreaCode = split[split.length - 1];
            stationInfo.setAreaCode(subAreaCode);
            stationInfo.setAddress(pileStationInfo.getAddress());
            stationInfo.setServiceTel(pileStationInfo.getStationTel());
            stationInfo.setStationType(Integer.parseInt(pileStationInfo.getStationType()));
            stationInfo.setStationStatus(Integer.parseInt(pileStationInfo.getStationStatus()));
            stationInfo.setParkNums(Integer.parseInt(pileStationInfo.getParkNums()));
            stationInfo.setStationLng(new BigDecimal(pileStationInfo.getStationLng()));
            stationInfo.setStationLat(new BigDecimal(pileStationInfo.getStationLat()));
            stationInfo.setConstruction(Integer.parseInt(pileStationInfo.getConstruction()));
            // 停车费率描述
            if (StringUtils.isNotBlank(pileStationInfo.getParkFeeDescribe())) {
                stationInfo.setParkFee(pileStationInfo.getParkFeeDescribe());
            }
            // 站点图片
            if (StringUtils.isNotBlank(pileStationInfo.getPictures())) {
                stationInfo.setPictures(Lists.newArrayList(pileStationInfo.getPictures().split(",")));
            }

            List<ZDLEquipmentInfo> 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);

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

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

        return resultMap;
    }

    /**
     * 查询统计信息
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryStationStats(QueryStationInfoDTO dto) {
        return lianLianService.query_station_stats(dto);
    }

    /**
     * 设备接口状态查询
     * 此接口用于批量查询设备实时状态
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryStationStatus(QueryStationInfoDTO dto) {
        return lianLianService.query_station_status(dto);
    }

    /**
     * 设备状态变化推送
     * @param dto
     * @return
     */
    @Override
    public String notificationStationStatus(PushRealTimeInfoDTO dto) {
        return lianLianService.pushConnectorStatus(dto);
    }

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

    /**
     * 生成令牌
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> generateToken(CommonParamsDTO dto) throws UnsupportedEncodingException {
        return lianLianService.generateToken(dto);
    }

    /**
     * 请求设备认证
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryEquipAuth(QueryEquipmentDTO dto) {
        return lianLianService.query_equip_auth(dto);
    }

    /**
     * 请求开始充电
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryStartCharge(QueryStartChargeDTO dto) {
        return lianLianService.query_start_charge(dto);
    }

    /**
     * 查询业务策略信息结果
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryEquipBusinessPolicy(QueryStartChargeDTO dto) {
        List<EquipBusinessPolicyVO.PolicyInfo> policyInfoList = new ArrayList<>();
        String pileConnectorCode = dto.getConnectorID();
        ThirdPartyPlatformConfig configInfo = thirdPartyPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
        if (configInfo == null) {
            return null;
        }
        // 截取桩号
        String pileSn = StringUtils.substring(pileConnectorCode, 0, 14);
        // 查询该桩的站点id
        PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(pileSn);
        // 根据桩号查询正在使用的计费模板
        List<BillingPriceVO> billingPriceVOList = pileBillingTemplateService.queryBillingPrice(String.valueOf(pileBasicInfo.getStationId()));

        if (CollectionUtils.isEmpty(billingPriceVOList)) {
            return null;
        }
        EquipBusinessPolicyVO.PolicyInfo policyInfo = null;
        for (BillingPriceVO billingPriceVO : billingPriceVOList) {
            // 将时段开始时间、电费、服务费信息进行封装
            policyInfo = new EquipBusinessPolicyVO.PolicyInfo();
            String startTime = billingPriceVO.getStartTime() + ":00"; // 00:00:00 格式
            // 需要将中间的冒号去掉，改为 000000 格式
            String replace = StringUtils.replace(startTime, ":", "");
            policyInfo.setStartTime(replace);
            policyInfo.setElecPrice(new BigDecimal(billingPriceVO.getElectricityPrice()).setScale(4, BigDecimal.ROUND_HALF_UP));
            policyInfo.setServicePrice(new BigDecimal(billingPriceVO.getServicePrice()).setScale(4, BigDecimal.ROUND_HALF_UP));

            policyInfoList.add(policyInfo);
        }

        // 拼装所需要的数据格式
        EquipBusinessPolicyVO vo = EquipBusinessPolicyVO.builder()
                .equipBizSeq(dto.getEquipBizSeq())
                .connectorId(dto.getConnectorID())
                .succStat(0)
                .failReason(0)
                .sumPeriod(policyInfoList.size())
                .policyInfos(policyInfoList)

                .build();

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

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

        return resultMap;
    }

    /**
     * 查询充电状态
     * @param dto
     */
    @Override
    public Map<String, String> queryEquipChargeStatus(QueryEquipChargeStatusDTO dto) {
        return lianLianService.query_equip_charge_status(dto);
    }

    /**
     * 请求停止充电
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryStopCharge(QueryStartChargeDTO dto) {
        return lianLianService.query_stop_charge(dto);
    }

    /**
     * 推送启动充电结果
     * @return
     */
    @Override
    public String notificationStartChargeResult(String orderCode) {
        return lianLianService.pushStartChargeResult(orderCode);
    }

    /**
     * 推送停止充电结果(仅在 0x19的帧类型中调用)
     * @param orderCode
     * @return
     */
    @Override
    public String notificationStopChargeResult(String orderCode) {
        return lianLianService.pushStopChargeResult(orderCode);
    }

    /**
     * 校验签名
     * @param dto
     */
    @Override
    public Map<String, String> checkoutSign(CommonParamsDTO dto) {
        return lianLianService.checkoutSign(dto);
    }

    /**
     * 推送充电状态
     * @param orderCode
     * @return
     */
    @Override
    public String notificationEquipChargeStatus(String orderCode) throws UnsupportedEncodingException {
        return lianLianService.pushChargeStatus(orderCode);
    }

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

            equipmentInfo.setEquipmentId(pileSn);

            PileModelInfoVO modelInfo = pileModelInfoService.getPileModelInfoByPileSn(pileSn);
            equipmentInfo.setEquipmentType(Integer.parseInt(modelInfo.getSpeedType()));
            equipmentInfo.setPower(new BigDecimal(modelInfo.getRatedPower()).setScale(1, BigDecimal.ROUND_HALF_UP));

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

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

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

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

            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);
            // 车位号
            if (StringUtils.isNotBlank(pileConnectorInfo.getParkNo())) {
                connectorInfo.setParkNo(pileConnectorInfo.getParkNo());
            }
            connectorInfo.setVoltageUpperLimits(Integer.valueOf(modelInfo.getRatedVoltage()));
            connectorInfo.setVoltageLowerLimits(Integer.valueOf(modelInfo.getRatedVoltage()));
            connectorInfo.setCurrent(Integer.valueOf(modelInfo.getRatedCurrent()));
            connectorInfo.setNationalStandard(2);
            // if (!StringUtils.equals(modelInfo.getConnectorNum(), "1")) {
            //     // 如果不是单枪，则枪口功率需要除以枪口数量
            //     String ratedPowerStr = modelInfo.getRatedPower();
            //     BigDecimal ratedPower = new BigDecimal(ratedPowerStr);
            //     connectorInfo.setPower(ratedPower.divide(new BigDecimal(modelInfo.getConnectorNum()), 1, BigDecimal.ROUND_HALF_UP));
            // }else {
            // }
            connectorInfo.setPower(new BigDecimal(modelInfo.getRatedPower()).setScale(1, BigDecimal.ROUND_HALF_UP));

            resultList.add(connectorInfo);
        }

        return resultList;
    }
}
