package com.jeequan.jeepay.thirdparty.channel.yspay;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.interfaces.paychannel.IChannelUserService;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelUserInfoMsg;
import com.jeequan.jeepay.thirdparty.channel.yspay.utils.YspayKit;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 银盛 获取银联userId实现类
*
* @author jmdhapy
*
* @date 2022/3/17 16:51
*/
@Service
@Slf4j
public class YspayChannelUserService implements IChannelUserService {

    @Autowired protected ConfigContextQueryService configContextQueryService;

    @Override
    public String getIfCode() {
        return CS.IF_CODE.YSPAY;
    }

    @Override
    public ChannelUserInfoMsg buildUserRedirectUrl(String callbackUrlEncode, MchAppConfigContext mchAppConfigContext) {

        //云闪付返回地址
        String ysfRedirectUrl = String.format("https://qr.95516.com/qrcGtwWeb-web/api/userAuth?version=1.0.0&redirectUrl=%s", callbackUrlEncode);
        log.info("银盛ysRedirectUrl={}", ysfRedirectUrl);
        return ChannelUserInfoMsg.gen(ysfRedirectUrl, null);
    }

    @Override
    public String getChannelUserId(String pageType, JSONObject reqParams, MchAppConfigContext mchAppConfigContext, String ifCode) {
        String logPrefix = "【银盛获取银联行业码用户ID】";
        try {
            String userAuthCode = reqParams.getString("userAuthCode"); //云闪付 userAuthCode
            String appUpIdentifier = reqParams.getString("appUpIdentifier");
            log.info("{} userAuthCode={}, appUpIdentifier={}", logPrefix, userAuthCode, appUpIdentifier);
            JSONObject bizContent = new JSONObject();

            // 用户授权码
            bizContent.put("userAuthCode", userAuthCode);
            // 银联支付标识
            bizContent.put("appUpIdentifier", appUpIdentifier);

            String result = YspayKit.payRequest("ysepay.online.cupgetmulapp.userid", mchAppConfigContext, bizContent, null, ifCode);
            JSONObject resJson = JSONObject.parseObject(result);
            JSONObject response = resJson.getJSONObject("ysepay_online_cupgetmulapp_userid_response");
            String code = response.getString("code");
            String userId = "";
            if (YspayKit.RES_CODE.equals(code)) {
                String tradeStatus = response.getString("trade_status");
                if ("SUCCESS".equals(tradeStatus)) {
                    userId = response.getString("userId");
                }
            }else {
                log.error("请求失败：code={}, sub_msg={}", code, response.getString("sub_msg"));
                return userId;
            }
            return userId;
        } catch (Exception e) {
            log.error("{}异常", logPrefix, e);
            return null;
        }
    }

}
