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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
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.params.dgpay.DgpayIsvsubMchParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelUserInfoMsg;
import com.jeequan.jeepay.core.utils.DateKit;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

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

    @Autowired protected ConfigContextQueryService configContextQueryService;

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

    @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("【斗拱】ysfRedirectUrl={}", 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);
            DgpayIsvsubMchParams mchParams = (DgpayIsvsubMchParams)configContextQueryService.queryIsvsubMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), ifCode);

            JSONObject reqJson = new JSONObject();
            // 交易流水号
            reqJson.put("req_seq_id", DateKit.currentTimeMillis()+"");
            // 请求日期
            reqJson.put("req_date", DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN));
            // 汇付商户号
            reqJson.put("huifu_id", mchParams.getHuifuId());
            // 用户授权码
            reqJson.put("auth_code", userAuthCode);
            // 银联支付标识
            reqJson.put("app_up_identifier", appUpIdentifier);

            JSONObject response = DgPayKit.request(mchAppConfigContext, reqJson, "/trade/payment/usermark2/query", ifCode);
            JSONObject resData = response.getJSONObject("data");
            String code = resData.getString("resp_code");
            String userId = "";
            if (DgPayKit.STATE_SUCCESS.equals(code)) {
                userId = resData.getString("user_id");
            }else {
                log.error("请求失败：code={}, sub_msg={}", code, response.getString("resp_desc"));
                return userId;
            }
            return userId;
        } catch (Exception e) {
            log.error("{}异常", logPrefix, e);
            return null;
        }
    }

}
