package cn.hanghui.outapi.platform.alipay.api;

import cn.hanghui.outapi.platform.alipay.entity.param.AlipayUserInfo;
import cn.hanghui.outapi.platform.common.exception.CommonException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.request.ZolozAuthenticationCustomerFtokenQueryRequest;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.api.response.ZolozAuthenticationCustomerFtokenQueryResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@AllArgsConstructor(onConstructor_ = @Autowired)
public class AlipayUserApi {

    private final AlipayCommonApi alipayCommonApi;

    /**
     * 获取支付宝用户信息
     *
     * @param alipayClient AlipayClient
     * @param authCode     authCode
     * @return 付宝用户信息
     */
    public AlipayUserInfo getUserInfo(AlipayClient alipayClient, String authCode, String thirdAppAuthToken) {
        try {
            String accessToken = alipayCommonApi.getAccessToken(alipayClient, authCode, thirdAppAuthToken);
            AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
            AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken, thirdAppAuthToken);
            log.info("获取支付宝用户信息返回：" + JSON.toJSONString(response));
            if (!response.isSuccess()) {
                throw new CommonException("获取支付宝用户信息失败");
            }
            AlipayUserInfo alipayUserInfo = new AlipayUserInfo();
            alipayUserInfo.setUserId(response.getUserId());
            alipayUserInfo.setUserName(response.getUserName());
            alipayUserInfo.setCertNo(response.getCertNo());
            alipayUserInfo.setMobile(response.getMobile());
            alipayUserInfo.setPersonPictures(response.getPersonPictures());
            alipayUserInfo.setAvatar(response.getAvatar());
            return alipayUserInfo;
        } catch (Exception e) {
            throw new CommonException("调用支付宝服务异常", e);
        }
    }

    public String getPhotoBase64ByFtoken(AlipayClient alipayClient, String ftoken, String bizType, String thirdAppAuthToken, String uid) {
        String authimgBase64 = null;
        try {
            ZolozAuthenticationCustomerFtokenQueryRequest request = new ZolozAuthenticationCustomerFtokenQueryRequest();
            JSONObject obj = new JSONObject();
            obj.put("ftoken", ftoken);
            obj.put("biz_type", bizType != null ? bizType : "2");
            obj.put("uid", uid);
            request.setBizContent(obj.toJSONString());
            ZolozAuthenticationCustomerFtokenQueryResponse response = alipayClient.execute(request, null, thirdAppAuthToken);
            log.info("--->人脸ftoken查询消费接口相应结果:{}", JSONObject.toJSONString(response));
            if (response.isSuccess()) {
                authimgBase64 = response.getAuthimgBase64();
            }
        } catch (Exception e) {
            log.error("--->人脸ftoken查询消费接口异常", e);
        }
        return authimgBase64;
    }


}
