package com.hzs.third.bankcard.service.impl;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.hzs.common.core.constant.HttpStatus;
import com.hzs.common.core.domain.R;
import com.hzs.third.bankcard.common.HttpConstant;
import com.hzs.third.bankcard.dto.BankCardParam;
import com.hzs.third.bankcard.service.BankCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 银行卡验证接口
 */
@Slf4j
@Service
public class BankCardServiceImpl implements BankCardService {

    @Value("${bankCard.url}")
    private String url;
    @Value("${bankCard.appKey}")
    private String appKey;
    @Value("${bankCard.appCode}")
    private String appCode;

    @Override
    public R<?> verifyBankCard(BankCardParam bankCardParam) {
        String authorization = "APPCODE " + appCode;
        HttpResponse response = HttpRequest.post(url)
                .header(Header.AUTHORIZATION, authorization)
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8")
                .form(getInParam(bankCardParam))
                .timeout(20000)
                .execute();
        return parseResult(response, bankCardParam);
    }


    /**
     * 解析返回的结果集
     * <p>
     * 1200 银行卡验证一致 收费
     * 1201 银行卡验证不一致 收费
     * 1203 银行卡状态错误 收费
     * 1206 验证失败 免费
     *
     * @param response 响应信息
     * @return bankCardVO
     */
    private R<?> parseResult(HttpResponse response, BankCardParam bankCardParam) {
        String result = response.body();
        log.info("调用接口返回信息： " + result);
        if (response.getStatus() == HttpStatus.SUCCESS) {
            JsonObject resultJson = JsonParser.parseString(result).getAsJsonObject();
            //状态码
            String code = resultJson.get("code").getAsString();
            //返回信息
            String message = resultJson.get("message").getAsString();
            if (code.equals(HttpConstant.BANK_CARD_SAME)) {
                return R.ok();
            } else {
                log.info("银行卡四要素校验接口正常返回状态值: {},信息为: {}", code, message);
                return R.fail(message);
            }
        } else {
            String message = "认证失败";
            log.info("银行卡四要素校验接口异常返回状态值: {},信息为: {}", response.getStatus(), message);
            return R.fail(message);
        }
    }

    /**
     * 获取入参
     *
     * @param bankCardParam 银行卡入参
     * @return String
     */
    private Map getInParam(BankCardParam bankCardParam) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("name", bankCardParam.getName());
        jsonObject.addProperty("mobile", bankCardParam.getMobile());
        jsonObject.addProperty("bankcard", bankCardParam.getBankCard());
        jsonObject.addProperty("idcard", bankCardParam.getIdCard());
        Map<String, String> param = new HashMap<>();
        param.put("biz_content", jsonObject.toString());
        return param;
    }
}
