package com.jsowell.thirdparty.common;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.RealTimeMonitorData;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.enums.parkplatform.ParkingPlatformEnum;
import com.jsowell.common.enums.thirdparty.BusinessInformationExchangeEnum;
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
import com.jsowell.common.enums.ykc.PileConnectorStatusEnum;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.enums.ykc.StartModeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.domain.PileBasicInfo;
import com.jsowell.pile.domain.ThirdPartyStationRelation;
import com.jsowell.pile.domain.ThirdpartyParkingConfig;
import com.jsowell.pile.dto.PushRealTimeInfoDTO;
import com.jsowell.pile.dto.PushStationInfoDTO;
import com.jsowell.pile.dto.ThirdPartyCommonStartChargeDTO;
import com.jsowell.pile.dto.ThirdPartyCommonStopChargeDTO;
import com.jsowell.pile.dto.lutongyunting.BindCouponDTO;
import com.jsowell.pile.dto.ruanjie.UseCouponDTO;
import com.jsowell.pile.service.*;
import com.jsowell.pile.vo.ThirdPartySecretInfoVO;
import com.jsowell.pile.vo.base.ThirdPartyStationRelationVO;
import com.jsowell.pile.vo.huawei.QueryStartChargeVO;
import com.jsowell.pile.vo.web.PileStationVO;
import com.jsowell.thirdparty.huawei.HuaweiServiceV2;
import com.jsowell.thirdparty.platform.service.impl.DianXingPlatformServiceImpl;
import com.jsowell.thirdparty.platform.service.impl.HaiNanPlatformServiceImpl;
import com.jsowell.thirdparty.huawei.HuaWeiService;
import com.jsowell.thirdparty.lianlian.service.LianLianService;
import com.jsowell.thirdparty.platform.service.impl.NinaXiaPlatformServiceImpl;
import com.jsowell.thirdparty.platform.service.impl.QingHaiPlatformServiceImpl;
import com.jsowell.thirdparty.platform.util.HttpRequestUtil;
import com.jsowell.thirdparty.lutongyunting.service.LTYTService;
import com.jsowell.thirdparty.nanrui.service.NRService;
import com.jsowell.thirdparty.ruanjie.service.RJService;
import com.jsowell.thirdparty.service.ThirdpartySecretInfoService;
import com.jsowell.thirdparty.xindiantu.service.XDTService;
import com.jsowell.thirdparty.yongchengboche.dto.YCBCGetTokenDTO;
import com.jsowell.thirdparty.yongchengboche.service.YCBCService;
import com.jsowell.thirdparty.zhongdianlian.service.ZDLService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections4.CollectionUtils;
import org.bouncycastle.crypto.CryptoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author Lemon
 * @Date 2024/1/18 15:11:32
 */
@Service
@Slf4j
public class CommonService {

    @Autowired
    private PileBasicInfoService pileBasicInfoService;

    @Autowired
    private PileStationInfoService pileStationInfoService;

    @Autowired
    private OrderBasicInfoService orderBasicInfoService;

    @Autowired
    private ThirdPartyStationRelationService thirdPartyStationRelationService;

    @Autowired
    private ThirdPartyParkingConfigService thirdPartyParkingConfigService;

    @Autowired
    private LianLianService lianLianService;

    @Autowired
    private ZDLService zdlService;

    @Autowired
    private NRService nrService;

    @Autowired
    private YCBCService ycbcService;

    @Autowired
    private XDTService xdtService;

    @Autowired
    private LTYTService ltytService;

    @Autowired
    private RJService rjService;

    @Autowired
    private HuaWeiService huaWeiService;

    @Autowired
    private HuaweiServiceV2 huaweiServiceV2;

    @Autowired
    private HaiNanPlatformServiceImpl haiNanChargeService;

    @Autowired
    private QingHaiPlatformServiceImpl qingHaiPlatformService;

    @Autowired
    private NinaXiaPlatformServiceImpl ninaXiaPlatformService;

    @Autowired
    private DianXingPlatformServiceImpl dianXingPlatformService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ThirdpartySecretInfoService thirdpartySecretInfoService;

    @Autowired
    private NotificationService notificationService;

    /**
     * 将站点--第三方平台类型对应关系存入关系表
     * @param dto
     */
    public void insertInfo2DataBase(PushStationInfoDTO dto) {
        String thirdPartyType = dto.getThirdPartyType();
        ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        relation.setStationId(dto.getStationId());
        relation.setThirdPartyType(thirdPartyType);
        if (StringUtils.equals(thirdPartyType, ThirdPlatformTypeEnum.HUA_WEI.getTypeCode())) {
            relation.setStartMode(Constants.ONE);
        }
        ThirdPartyStationRelationVO vo = thirdPartyStationRelationService.selectRelationInfo(relation);
        if (vo != null) {
            return;
        }
        // 新增数据库
        thirdPartyStationRelationService.insertThirdPartyStationRelation(relation);
    }


    /**
     * 统一方法推送站点信息
     * @param dto
     * @return
     * @throws ParseException
     */
    public String commonPushStation(PushStationInfoDTO dto) throws ParseException {
        List<String> types = dto.getThirdPartyTypes();

        String result = "";
        StringBuilder finalResult = new StringBuilder();
        for (String type : types) {
            dto.setThirdPartyType(type);
            if (StringUtils.equals(ThirdPlatformTypeEnum.LIAN_LIAN_PLATFORM.getTypeCode(), dto.getThirdPartyType())) {
                // 推送联联
                // result = lianLianService.pushStationInfo(dto);
                result = lianLianService.pushStationInfoV2(dto);
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.NING_BO_PLATFORM.getTypeCode(), dto.getThirdPartyType())) {
                // 中电联
                // result = zdlService.pushStationInfo(dto);
                result = zdlService.pushStationInfoV2(dto);
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.JIANG_SU_PLATFORM.getTypeCode(), dto.getThirdPartyType())) {
                // 江苏省平台
                // result = nrService.pushStationInfo(dto);
                result = nrService.pushStationInfoV2(dto);
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.YONG_CHENG_BO_CHE.getTypeCode(), dto.getThirdPartyType())) {
                // 甬城泊车平台
                // result = ycbcService.pushStationInfo(dto);
                result = ycbcService.pushStationInfoV2(dto);
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.XIN_DIAN_TU.getTypeCode(), dto.getThirdPartyType())) {
                // 新电途平台
                result = xdtService.pushStationInfoV2(dto);
            }
            // if(StringUtils.equals(ThirdPlatformTypeEnum.HUA_WEI.getCode(), dto.getThirdPartyType())) {
            // 	// 华为
            // 	result = huaWeiService.notificationOperationSystemInfo(dto);
            // }
            if(StringUtils.equals(ThirdPlatformTypeEnum.HAI_NAN_1_PLATFORM.getTypeCode(), dto.getThirdPartyType())) {
            	// 海南
            	result = haiNanChargeService.pushStationInfoV2(dto);
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.QING_HAI_PLATFORM.getTypeCode(), dto.getThirdPartyType())) {
                // 青海平台
                insertInfo2DataBase(dto);
                result = dto.getThirdPartyType() + "：" + "OK";
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.DIAN_XING_PLATFORM.getTypeCode(), dto.getThirdPartyType())) {
                // 宁波点行平台
                insertInfo2DataBase(dto);
                result = ThirdPlatformTypeEnum.DIAN_XING_PLATFORM.getTypeLabel() + "：" + "OK";
            }
            finalResult.append(result).append("\n");
        }
        return finalResult.toString();
    }

    /**
     * 统一推送第三方平台实时数据
     * @param pileSn 桩编号
     * @param connectorCode 枪口号
     * @param connectorStatus 枪口状态
     * @param realTimeMonitorData 实时数据
     * @param transactionCode 交易流水号
     */
    public void pushRealTimeInfo(String pileSn, String connectorCode, String connectorStatus,
                                   RealTimeMonitorData realTimeMonitorData, String transactionCode) throws UnsupportedEncodingException {
        // 推送第三方平台，先通过桩编号查出站点信息   pushToThirdPartyPlatforms
        PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(pileSn);

        // Map<String, String> pileStatusMap = pileConnectorInfoService.getPileStatus(Lists.newArrayList(pileSn));
        // String pileStatus = pileStatusMap.get(pileSn);
        // if (StringUtils.equals(connectorStatus, pileStatus)) {
        // 	return null;
        // }
        // 状态不一样，传给第三方平台

        // ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        // if (StringUtils.isBlank(String.valueOf(pileBasicInfo.getStationId()))) {
        // 	return;
        // }
        // relation.setStationId(pileBasicInfo.getStationId());
        // 查询该站点是否推送第三方平台
        List<ThirdPartyStationRelationVO> list = thirdPartyStationRelationService.getRelationInfoList(String.valueOf(pileBasicInfo.getStationId()));
        // ThirdPartyStationRelationVO relationInfo = thirdPartyStationRelationService.selectRelationInfo(relation);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        String pileConnectorCode = pileSn + connectorCode;
        // 将枪口状态转换成对接平台的状态
        String changedStatus = changeConnectorStatus(connectorStatus, realTimeMonitorData.getPutGunType());
        PushRealTimeInfoDTO dto = new PushRealTimeInfoDTO();
        dto.setPileConnectorCode(pileConnectorCode);
        dto.setStatus(changedStatus);
        for (ThirdPartyStationRelationVO vo : list) {
            String thirdPartyType = vo.getThirdPartyType();
            if (StringUtils.equals(Constants.ONE, vo.getStartMode())) {
                // 如果是类似华为格式，不需要传
                continue;
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.LIAN_LIAN_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 联联
                OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
                if (orderInfo == null) {
                    return;
                }
                // log.info("推送联联平台实时数据 params: orderBasicInfo:{}", JSON.toJSONString(orderInfo));
                // 设备状态变化推送  notification_stationStatus
                dto.setThirdPartyType(ThirdPlatformTypeEnum.LIAN_LIAN_PLATFORM.getTypeCode());
                lianLianService.pushConnectorStatus(dto);
                if (StringUtils.equals(connectorStatus, "03")) {
                    // 充电中
                    lianLianService.pushPileChargeStatusChange(orderInfo.getOrderCode());
                }
                // 推送充电状态
                lianLianService.pushChargeStatus(orderInfo.getOrderCode());
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.NING_BO_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 中电联
                dto.setThirdPartyType(ThirdPlatformTypeEnum.NING_BO_PLATFORM.getTypeCode());
                // log.info("推送中电联平台实时数据 pileConnectorCode:{}, connectorStatus:{}", pileConnectorCode, connectorStatus);
                zdlService.notificationStationStatus(dto);
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.JIANG_SU_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 先判断缓存中是否有数据
                String redisKey = CacheConstants.JIANGSU_PUSH_PILE_STATUS + pileConnectorCode;
                Object cacheObject = redisCache.getCacheObject(redisKey);

                dto.setThirdPartyType(ThirdPlatformTypeEnum.JIANG_SU_PLATFORM.getTypeCode());
                realTimeMonitorData.setThirdPartyType(ThirdPlatformTypeEnum.JIANG_SU_PLATFORM.getTypeCode());
                // 江苏省平台(充电状态至少一分钟推送一次)
                if (StringUtils.equals(connectorStatus, "03")) {
                    // 充电状态
                    // 如果缓存有数据，证明上次推送未超过一分钟，不予推送，若缓存中无数据，说明需要推送
                    if (cacheObject == null) {
                        // log.info("推送江苏省平台 充电状态 params:{}", realTimeMonitorData);
                        String result = nrService.pushPileStatus(realTimeMonitorData);
                        redisCache.setCacheObject(redisKey, realTimeMonitorData, 1, TimeUnit.MINUTES);
                        // log.info("推送江苏省平台 充电状态 result:{}", result);
                    }
                } else {
                    // 如果不是充电状态，直接推送就可以
                    // log.info("推送江苏省平台 非充电状态 params:{}", realTimeMonitorData);
                    String result = nrService.pushPileStatus(realTimeMonitorData);
                    // log.info("推送江苏省平台 非充电状态 result:{}", result);
                }

                // log.info("推送江苏省平台实时数据 result:{}", result);
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.XIN_DIAN_TU.getTypeCode(), thirdPartyType)) {
                // 新电途平台
                dto.setThirdPartyType(ThirdPlatformTypeEnum.XIN_DIAN_TU.getTypeCode());
                log.info("新电途平台设备状态变化推送 params:{}", JSON.toJSONString(dto));
                String result1 = xdtService.notificationStationStatus(dto);
                log.info("新电途平台设备状态变化推送 result:{}", result1);
                OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
                if (orderInfo == null) {
                    return;
                }
                log.info("推送新电途平台 充电状态 params:{}", orderInfo.getOrderCode());
                String result = xdtService.notificationEquipChargeStatus(orderInfo.getOrderCode());
                log.info("推送新电途平台 充电状态 result:{}", result);
            }
            // if (StringUtils.equals(ThirdPlatformTypeEnum.HUA_WEI.getCode(), thirdPartyType)) {
            //     // 华为平台
            //     huaWeiService.notificationStationStatus(pileConnectorCode, changedStatus);
            //     OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
            //     if (orderInfo == null) {
            //         return;
            //     }
            //     huaWeiService.notificationEquipChargeStatus(orderInfo.getOrderCode());
            // }
            if (StringUtils.equals(ThirdPlatformTypeEnum.HAI_NAN_1_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 海南平台
                dto.setThirdPartyType(ThirdPlatformTypeEnum.HAI_NAN_1_PLATFORM.getTypeCode());
                haiNanChargeService.notificationStationStatus(dto);
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.YONG_CHENG_BO_CHE.getTypeCode(), thirdPartyType)) {
                // 甬城泊车
                dto.setThirdPartyType(ThirdPlatformTypeEnum.YONG_CHENG_BO_CHE.getTypeCode());
                // log.info("推送甬城泊车平台设备状态变化推送 pileConnectorCode:{}, changedStatus:{}", pileConnectorCode, changedStatus);
                // 设备状态变化推送  notification_stationStatus
                String result1 = ycbcService.notificationStationStatus(dto);
                // log.info("推送甬城泊车平台设备状态变化推送 result:{}", result1);
                // 推送充电状态
                OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
                if (orderInfo == null) {
                    return;
                }
                if (StringUtils.equals(orderInfo.getStartMode(), StartModeEnum.THIRD_PARTY_PLATFORM.getValue())) {
                    // log.info("推送甬城泊车充电订单状态 param:{}", orderInfo);
                    String result2 = ycbcService.pushChargeStatus(orderInfo.getOrderCode());
                    // log.info("推送甬城泊车充电订单状态 result:{}", result2);
                }
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.QING_HAI_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 青海省平台
                dto.setThirdPartyType(ThirdPlatformTypeEnum.QING_HAI_PLATFORM.getTypeCode());
                String result = qingHaiPlatformService.notificationStationStatus(dto);
                log.info("推送青海平台设备状态变化推送 params:{}", JSON.toJSONString(dto));
                // 先判断缓存中是否有数据
                String redisKey = CacheConstants.JIANGSU_PUSH_PILE_STATUS + pileConnectorCode;
                Object cacheObject = redisCache.getCacheObject(redisKey);

                if (StringUtils.equals(connectorStatus, "03")) {
                    // 充电状态, 查出订单信息
                    OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
                    if (orderInfo == null) {
                        return;
                    }
                    // 如果缓存有数据，证明上次推送未超过2分钟，不予推送，若缓存中无数据，说明需要推送
                    if (cacheObject == null) {
                        String pushResult = qingHaiPlatformService.notificationEquipChargeStatus(orderInfo.getOrderCode());
                        redisCache.setCacheObject(redisKey, realTimeMonitorData, 2, TimeUnit.MINUTES);
                    }
                }
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.NING_XIA_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 宁夏平台
                dto.setThirdPartyType(ThirdPlatformTypeEnum.NING_XIA_PLATFORM.getTypeCode());
                String result = ninaXiaPlatformService.notificationStationStatus(dto);
                log.info("宁夏平台推送充电设备接口状态信息 result:{}", result);
                if (StringUtils.equals(connectorStatus, "03")) {
                    // 充电状态, 查出订单信息
                    OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
                    if (orderInfo == null) {
                        continue;
                    }
                    log.info("宁夏平台推送充电状态信息 params:{}", JSONObject.toJSONString(orderInfo));
                    String result2 = ninaXiaPlatformService.notificationEquipChargeStatus(orderInfo.getOrderCode());
                    log.info("宁夏平台推送充电状态信息 result:{}", result2);
                }
            }

        }
    }

    /**
     * 推送实时数据到第三方平台
     * @param pileSn
     * @param connectorCode
     * @param connectorStatus
     * @param realTimeMonitorData
     * @param transactionCode
     * @throws UnsupportedEncodingException
     */
    public void pushRealTimeInfoV2(String pileSn, String connectorCode, String connectorStatus,
                                 RealTimeMonitorData realTimeMonitorData, String transactionCode) throws UnsupportedEncodingException {
        // 推送第三方平台，先通过桩编号查出站点信息   pushToThirdPartyPlatforms
        PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(pileSn);

        String pileConnectorCode = pileSn + connectorCode;
        // 将枪口状态转换成对接平台的状态
        String changedStatus = changeConnectorStatus(connectorStatus, realTimeMonitorData.getPutGunType());

        String stationId = String.valueOf(pileBasicInfo.getStationId());
        // 查询该站点是否推送第三方平台
        List<ThirdPartySecretInfoVO> thirdPartySecretInfoVOS = thirdpartySecretInfoService.queryStationToPlatformList(stationId);
        if (CollectionUtils.isEmpty(thirdPartySecretInfoVOS)) {
            return;
        }

        // 查询订单信息
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
        if (Objects.isNull(orderInfo)) {
            return;
        }

        // 推送
        for (ThirdPartySecretInfoVO thirdPartySecretInfoVO : thirdPartySecretInfoVOS) {
            NotificationDTO dto = new NotificationDTO();
            dto.setStationId(stationId);
            dto.setPileConnectorCode(pileConnectorCode);
            dto.setStatus(changedStatus);
            dto.setPlatformType(thirdPartySecretInfoVO.getPlatformType());
            dto.setOrderCode(orderInfo.getOrderCode());
            notificationService.notificationConnectorChargeStatus(dto);
        }

    }


    /**
     * 统一方法推送订单信息
     * @param orderBasicInfo 订单信息
     */
    public void commonPushOrderInfo(OrderBasicInfo orderBasicInfo) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, CryptoException {
        // 查询该站点是否推送第三方平台
        // ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        // relation.setStationId(Long.parseLong(orderBasicInfo.getStationId()));
        List<ThirdPartyStationRelationVO> relationInfoList = thirdPartyStationRelationService.getRelationInfoList(orderBasicInfo.getStationId());
        // ThirdPartyStationRelationVO relationInfo = thirdPartyStationRelationService.selectRelationInfo(relation);
        if (CollectionUtils.isEmpty(relationInfoList)) {
            return;
        }
        for (ThirdPartyStationRelationVO relationVO : relationInfoList) {
            String thirdPartyType = relationVO.getThirdPartyType();
            if (StringUtils.equals(Constants.ONE, relationVO.getStartMode())) {
                // 如果是类似华为格式，不需要传
                continue;
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.LIAN_LIAN_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 联联平台
                // 推送停止充电结果
                lianLianService.pushStopChargeResult(orderBasicInfo.getOrderCode());
                // 推送订单信息
                lianLianService.pushOrderInfo(orderBasicInfo.getOrderCode());
                // 推送充电订单信息
                lianLianService.pushChargeOrderInfo(orderBasicInfo.getOrderCode());
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.NING_BO_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 中电联
                zdlService.pushChargeOrderInfo(orderBasicInfo.getOrderCode());
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.JIANG_SU_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 江苏平台
                // 推送订单信息
                nrService.pushOrderInfo(orderBasicInfo.getOrderCode());
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.XIN_DIAN_TU.getTypeCode(), thirdPartyType)) {
                // 新电途平台
                // 推送停止充电结果
                xdtService.notificationStopChargeResult(orderBasicInfo.getOrderCode());
                // 推送订单信息
                xdtService.pushChargeOrderInfo(orderBasicInfo.getOrderCode());
            }
            // if (StringUtils.equals(ThirdPlatformTypeEnum.HUA_WEI.getCode(), thirdPartyType)) {
            //     // 华为
            //     // 推送停止充电结果
            //     result = huaWeiService.notificationStopChargeResult(orderBasicInfo.getOrderCode());
            //     // 推送订单信息
            //     result = huaWeiService.pushChargeOrderInfo(orderBasicInfo.getOrderCode());
            // }
            if (StringUtils.equals(ThirdPlatformTypeEnum.HAI_NAN_1_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 海南平台
                haiNanChargeService.notificationChargeOrderInfo(orderBasicInfo.getOrderCode());
            }

            if (StringUtils.equals(ThirdPlatformTypeEnum.YONG_CHENG_BO_CHE.getTypeCode(), thirdPartyType)) {
                if (StringUtils.equals(orderBasicInfo.getStartMode(), StartModeEnum.THIRD_PARTY_PLATFORM.getValue())) {
                    // 甬城泊车
                    // 推送停止充电结果
                    ycbcService.pushStopChargeResult(orderBasicInfo.getOrderCode());
                    // 推送订单信息
                    // log.info("甬城泊车平台 推送充电订单信息 param:{}", orderBasicInfo.getOrderCode());
                    ycbcService.pushChargeOrderInfo(orderBasicInfo.getOrderCode());
                    // log.info("甬城泊车平台 推送充电订单信息 result:{}", result);
                }
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.QING_HAI_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 青海平台
                qingHaiPlatformService.notificationChargeOrderInfo(orderBasicInfo.getOrderCode());
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.NING_XIA_PLATFORM.getTypeCode(), thirdPartyType)) {
                // 宁夏平台
                ninaXiaPlatformService.notificationChargeOrderInfo(orderBasicInfo.getOrderCode());
            }
            if (StringUtils.equals(ThirdPlatformTypeEnum.DIAN_XING_PLATFORM.getTypeCode(), thirdPartyType)) {
                log.info("点行平台推送订单数据 params:{}", orderBasicInfo);
                String result = dianXingPlatformService.notificationChargeOrderInfo(orderBasicInfo.getOrderCode());
                log.info("点行平台推送订单数据 result:{}", result);
            }
        }
    }

    public void commonPushOrderInfoV2(OrderBasicInfo orderBasicInfo) {
        String stationId = orderBasicInfo.getStationId();
        // 查询该站点是否推送第三方平台
        List<ThirdPartySecretInfoVO> relationInfoList = thirdpartySecretInfoService.queryStationToPlatformList(stationId);
        // ThirdPartyStationRelationVO relationInfo = thirdPartyStationRelationService.selectRelationInfo(relation);
        if (CollectionUtils.isEmpty(relationInfoList)) {
            return;
        }
        for (ThirdPartySecretInfoVO relationVO : relationInfoList) {
            NotificationDTO dto = new NotificationDTO();
            dto.setStationId(stationId);
            dto.setPlatformType(relationVO.getPlatformType());
            dto.setOrderCode(orderBasicInfo.getOrderCode());
            notificationService.notificationChargeOrderInfo(dto);
        }
    }


    /**
     * 统一推送启动充电结果
     * @param orderBasicInfo
     * @return
     */
    public String commonPushStartChargeResult(OrderBasicInfo orderBasicInfo) {
        // 通过第三方平台类型查询相关配置信息
        ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        relation.setThirdPartyType(orderBasicInfo.getThirdPartyType());
        ThirdPartyStationRelationVO relationInfo = thirdPartyStationRelationService.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 thirdPartyType = relationInfo.getThirdPartyType();

        // 推送启动充电结果(调用接口 notification_start_charge_result)
        String url = urlAddress + BusinessInformationExchangeEnum.NOTIFICATION_START_CHARGE_RESULT.getValue();

        // 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", orderBasicInfo.getOrderCode());
        json.put("ConnectorID", orderBasicInfo.getPileConnectorCode());
        json.put("StartChargeSeqStat", 2); // 一定要给 2-充电中
        json.put("StartTime", DateUtils.getDateTime());

        String jsonString = JSON.toJSONString(json);

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

            token = ycbcService.YCBCGetToken(dto);
            result = HttpRequestUtil.YCBCSendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);
        }else {
            token = lianLianService.getToken(urlAddress, operatorId, operatorSecret, dataSecretIv, signSecret, dataSecret);
            result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);
        }
        return result;
    }

    /**
     * 绑定优惠券
     *
     * @param orderBasicInfo
     * @return
     * @throws UnsupportedEncodingException
     */
    public String bindCoupon(OrderBasicInfo orderBasicInfo) throws UnsupportedEncodingException {
        if (orderBasicInfo == null) {
            return null;
        }
        // 如果该站点的停车场优惠券信息配置不为空，则需绑定一张优惠券
        String stationId = orderBasicInfo.getStationId();
        PileStationVO stationInfo = pileStationInfoService.getStationInfo(stationId);
        if (StringUtils.isBlank(stationInfo.getParkingId())) {
            return orderBasicInfo.getOrderCode() + " 该订单无需绑定优惠券";
        }
        if (StringUtils.equals(ParkingPlatformEnum.LU_TONG_YUN_TING_PLATFORM.getCode(), stationInfo.getParkingId())) {
            // 路通云停
            // 查询密钥等配置
            ThirdpartyParkingConfig parkingInfo = thirdPartyParkingConfigService.selectByPrimaryKey(Integer.parseInt(stationInfo.getParkingId()));
            if (parkingInfo == null) {
                throw new BusinessException(ReturnCodeEnum.CODE_QUERY_PARKING_INFO_IS_NULL);
            }
            BindCouponDTO dto = BindCouponDTO.builder()
                    .appId(parkingInfo.getAppId())
                    .merchantId(parkingInfo.getParkingMerchantId())
                    .couponId(parkingInfo.getCouponId())
                    .secretKey(parkingInfo.getSecretKey())
                    .plateNumber(orderBasicInfo.getPlateNumber())
                    .plateColor(5) // 5-绿牌车
                    .build();
            // 绑定优惠券
            return ltytService.bindCoupon(dto);
        } else if (StringUtils.equals(ParkingPlatformEnum.RUAN_JIE_PLATFORM.getCode(), stationInfo.getParkingId())) {
            // 软杰
            UseCouponDTO dto = UseCouponDTO.builder()
                    .fCouponCode(orderBasicInfo.getOrderCode()) // 优惠券编号(使用订单编号)
                    .fCouponType("2") // 优惠类型 2-时长
                    .fCouponValue("120") // 优惠券面额 单位：分钟
                    .fPlateCode(orderBasicInfo.getPlateNumber()) // 车牌号
                    .build();
            return rjService.useCoupon(dto);
        }
        return null;
    }

    /**
     * 统一请求启动充电，目前给华为平台用
     * @param dto
     * @return
     */
    public String commonQueryStartCharge(ThirdPartyCommonStartChargeDTO dto) {
        String thirdPartyType = dto.getThirdPartyType();
        // List<String> stationIds = dto.getStationIds();
        // String pileConnectorCode = dto.getPileConnectorCode();
        // BigDecimal chargeAmount = dto.getChargeAmount();
        // String payMode = dto.getPayMode();

        // 判断平台类型
        if (StringUtils.equals(ThirdPlatformTypeEnum.HUA_WEI.getTypeCode(), thirdPartyType)) {
            // 华为平台
            String result = huaweiServiceV2.startChargeFlow(dto);
            log.info("华为统一请求启动充电 result:{}", result);
        }

        return null;
    }


    /**
     * 统一停止充电
     * @param dto
     * @return
     */
    public String commonStopCharge(ThirdPartyCommonStopChargeDTO dto) {
        OrderBasicInfo orderBasicInfo = dto.getOrderBasicInfo();
        String thirdPartyType = dto.getThirdPartyType();

        // 判断平台类型
        if (StringUtils.equals(ThirdPlatformTypeEnum.HUA_WEI.getTypeCode(), thirdPartyType)) {
            QueryStartChargeVO vo = huaweiServiceV2.queryStopCharge(orderBasicInfo.getOrderCode());
            return String.valueOf(vo.getSuccStat());
        }
        return null;
    }



    /**
     * 转换枪口状态
     * @param connectorStatus 有电充平台枪口状态
     * @param putGunType 是否插枪
     * @return 第三方平台枪口状态
     */
    public String changeConnectorStatus(String connectorStatus, String putGunType) {
        if (StringUtils.equals(connectorStatus, PileConnectorStatusEnum.FREE.getValue())) {
            // 空闲
            return Constants.ONE;
        } else if (StringUtils.equals(connectorStatus, PileConnectorStatusEnum.FREE.getValue()) && StringUtils.equals(putGunType, "01")) {
            // 占用（未充电）
            return Constants.TWO;
        } else if (StringUtils.equals(connectorStatus, PileConnectorStatusEnum.FAULT.getValue())) {
            // 故障
            return "255";
        }else {
            // 0-离网和 3-充电中不需要转换，直接返回
            return connectorStatus;
        }
    }

    public void commonQueryStationStatus(String stationId, String thirdPartyType) {
        if (StringUtils.equals(ThirdPlatformTypeEnum.HUA_WEI.getTypeCode(), thirdPartyType)) {
            // 华为平台
            Map<String, String> map = huaweiServiceV2.queryStationStatus(Lists.newArrayList(stationId));
            // key: pileConnectorCode,
            // value: status
        }
    }
}
