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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.AlipayOpenSpOperationApplyModel;
import com.alipay.api.domain.AlipayOpenSpOperationQrcodeQueryModel;
import com.alipay.api.domain.AlipayOpenSpOperationResultQueryModel;
import com.alipay.api.request.AlipayOpenSpOperationApplyRequest;
import com.alipay.api.request.AlipayOpenSpOperationQrcodeQueryRequest;
import com.alipay.api.request.AlipayOpenSpOperationResultQueryRequest;
import com.alipay.api.response.AlipayOpenSpOperationApplyResponse;
import com.alipay.api.response.AlipayOpenSpOperationQrcodeQueryResponse;
import com.alipay.api.response.AlipayOpenSpOperationResultQueryResponse;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.paychannel.alipaybiz.IAlipayOpenSpOperationService;
import com.jeequan.jeepay.core.model.alipay.AlipaySpOperationInfo;
import com.jeequan.jeepay.core.model.params.alipay.AlipayIsvParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/*
* 支付宝服务商代运营授权
*
* @author zx
*
* @date 2023/3/8 15:13
*/
@Slf4j
@Service
public class AlipayOpenSpOperationService implements IAlipayOpenSpOperationService {

    @Override
    public JSONObject querySpOperationQrcode(AlipayIsvParams alipayIsvParams, String alipayAccount) {
        AlipayOpenSpOperationQrcodeQueryRequest request = new AlipayOpenSpOperationQrcodeQueryRequest();
        AlipayOpenSpOperationQrcodeQueryModel model = new AlipayOpenSpOperationQrcodeQueryModel();
        model.setOutBizNo(IdUtil.fastSimpleUUID()); // 外部操作流水，由服务商自定义，需确保每次操作唯一
        model.setOperateType("OPERATION_AUTH"); // OPERATION_AUTH：代运营授权，支持间连、直连商户。ACCOUNT_BIND：账号绑定，仅支持间连商户。
        model.setAlipayAccount(alipayAccount);
        model.setAccessProductCode("OPENAPI_AUTH_DEFAULT");
        request.setBizModel(model);

        AlipayOpenSpOperationQrcodeQueryResponse response = AlipayKit.getAlipayClientWrapper(alipayIsvParams).getAlipayClient().execute(request);
        if (!response.isSuccess()) {
            throw new BizException(AlipayKit.appendErrMsg(response.getMsg(), response.getSubMsg()));
        }

        return (JSONObject) JSON.toJSON(response);
    }

    @Override
    public JSONObject applySpOperation(AlipayIsvParams alipayIsvParams, String alipayAccount){
        AlipayOpenSpOperationApplyRequest request = new AlipayOpenSpOperationApplyRequest();
        AlipayOpenSpOperationApplyModel model = new AlipayOpenSpOperationApplyModel();
        model.setOutBizNo(IdUtil.fastSimpleUUID());
        model.setOperateType("OPERATION_AUTH");
        model.setAlipayAccount(alipayAccount);
        model.setAccessProductCode("OPENAPI_AUTH_DEFAULT");
        request.setBizModel(model);

        AlipayOpenSpOperationApplyResponse response = AlipayKit.getAlipayClientWrapper(alipayIsvParams).getAlipayClient().execute(request);
        if (!response.isSuccess()) {
            throw new BizException(AlipayKit.appendErrMsg(response.getMsg(), response.getSubMsg()));
        }

        return (JSONObject) JSON.toJSON(response);
    }

    @Override
    public AlipaySpOperationInfo querySpOperationResult(AlipayIsvParams alipayIsvParams, String alipayAccount){
        AlipayOpenSpOperationResultQueryRequest request = new AlipayOpenSpOperationResultQueryRequest();
        AlipayOpenSpOperationResultQueryModel model = new AlipayOpenSpOperationResultQueryModel();
        model.setOperateType("OPERATION_AUTH");
        model.setAlipayAccount(alipayAccount);
        model.setAccessProductCode("OPENAPI_AUTH_DEFAULT");
        request.setBizModel(model);

        AlipayOpenSpOperationResultQueryResponse response = AlipayKit.getAlipayClientWrapper(alipayIsvParams).getAlipayClient().execute(request);

        AlipaySpOperationInfo operationInfo = new AlipaySpOperationInfo();
        BeanUtil.copyProperties(response, operationInfo);

        return operationInfo;
    }

}
