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

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.Sm2Util;
import com.jsowell.common.util.http.HttpUtils;
import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.service.OrderBasicInfoService;
import com.jsowell.pile.vo.ThirdPartySecretInfoVO;
import com.jsowell.thirdparty.platform.service.ThirdPartyPlatformService;
import com.jsowell.thirdparty.platform.factory.ThirdPartyPlatformFactory;
import com.jsowell.thirdparty.service.ThirdpartySecretInfoService;
import org.bouncycastle.crypto.CryptoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;

/**
 * 宁波点行平台
 *
 * @author Lemon
 * @Date 2024/5/9 10:18:12
 */
@Service
public class DianXingPlatformServiceImpl implements ThirdPartyPlatformService {
    @Autowired
    private OrderBasicInfoService orderBasicInfoService;

    @Autowired
    private ThirdpartySecretInfoService thirdpartySecretInfoService;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    // 平台类型
    private final String thirdPlatformType = ThirdPlatformTypeEnum.DIAN_XING_PLATFORM.getTypeCode();

    @Override
    public void afterPropertiesSet() throws Exception {
        ThirdPartyPlatformFactory.register(thirdPlatformType, this);
    }

    /**
     * 推送订单接口
     * @param orderCode 订单编号
     * @return
     */
    @Override
    public String notificationChargeOrderInfo(String orderCode) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, CryptoException {
        // 通过订单编号查询订单信息
        OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
        if (orderBasicInfo == null) {
            throw new BusinessException("", "未查询到该订单信息");
        }
        String plateNum = orderBasicInfo.getPlateNumber().equals("") ? "无" : orderBasicInfo.getPlateNumber();
        // 拼装订单信息 JSON 参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("outTradeId", orderBasicInfo.getOrderCode()); // 充电桩平台订单号
        jsonObject.put("plateNum", plateNum); // 车牌号
        jsonObject.put("startTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeStartTime())); // 充电开始时间，格式 yyyy-MM-dd HH:mm:ss
        jsonObject.put("endTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, orderBasicInfo.getChargeEndTime())); // 充电结束时间，
        jsonObject.put("payAmount", String.valueOf(orderBasicInfo.getPayAmount())); // 支付金额，格式为小数点后保留 2 位
        jsonObject.put("recordTime", DateUtils.getDateTime()); // 记录上报时间

        // 查询出该平台的配置参数
        ThirdPartySecretInfoVO thirdPartySecretInfoVO = thirdpartySecretInfoService.queryByThirdPlatformType(thirdPlatformType);
        if (thirdPartySecretInfoVO == null) {
            throw new BusinessException("", "无此平台配置信息");
        }
        // 加密参数(方法中已将 sign 放入参数中并返回)
        String recordInfoStr = Sm2Util.generateEncryptedRequestInfo(jsonObject, thirdPartySecretInfoVO.getTheirPublicSecret(), thirdPartySecretInfoVO.getOurPrivateSecret());
        String tpToken = "e17fe5c7b0f7404c9ea31091fbb5806f";

        // 发送请求
        String url = thirdPartySecretInfoVO.getTheirUrlPrefix();
        JSONObject postParam = new JSONObject();
        postParam.put("recordInfo", recordInfoStr);

        String result = HttpUtils.sendPostTpToken(url, postParam.toJSONString(), tpToken);
        logger.info("点行平台绑定优惠券 result:{}", result);
        return result;
    }


}
