package org.springblade.yeePay.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.yeepay.yop.sdk.exception.YopClientException;
import com.yeepay.yop.sdk.service.common.YopClient;
import com.yeepay.yop.sdk.service.common.YopClientBuilder;
import com.yeepay.yop.sdk.service.common.request.YopRequest;
import com.yeepay.yop.sdk.service.common.response.YopResponse;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.yeePay.api.YeepayCommon;
import org.springblade.yeePay.common.YeePayConst;
import org.springblade.yeePay.entity.ConfigAppDto;
import org.springblade.yeePay.utils.YeepayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author: lianghanqiang
 * @description: 易宝--通用接口服务
 * @since: 9/6/21 -- 10:57 AM
 */
@Service
@Slf4j
public class YeepayCommonService {
	@Autowired
	private YeePayConst yeePayConst;

	YopClient yopClient = YopClientBuilder.builder().build();

	/**
	 * 上传文件到易宝服务器
	 * */
	public String addFileToYeepay(MultipartFile file ){

		YopRequest request = YeepayCommon.ADD_FILE_TO_YEEPAY.buildRequest();

		InputStream inputStream = null;
		try {
			inputStream = file.getInputStream();
		} catch (IOException e) {
			log.error("上传文件异常："+e.getMessage());
			throw new ServiceException("上传文件异常");
		}
		request.addMultiPartFile("merQual",inputStream);

		//上传文件
		YopResponse response = yopClient.request(request);
		String resString = response.getStringResult();

		return JSON.parseObject(resString).getString("merQualUrl");
	}

	/**
	 * 上传文件到易宝服务器
	 * */
	public String addFileToYeepay(String url){

		YopRequest request = YeepayCommon.ADD_FILE_TO_YEEPAY.buildRequest();

		HttpResponse execute = HttpRequest.get(url).execute();
		InputStream inputStream = execute.bodyStream();
		//上传文件
		request.addMultiPartFile("merQual",inputStream);
		YopResponse response = yopClient.request(request);
		String resString = response.getStringResult();

		return JSON.parseObject(resString).getString("merQualUrl");
	}

	/**
	 *	易宝 公众号配置
	 * @param configAppDto
	 * @return*/
	public R configApp(ConfigAppDto configAppDto) {
		YopRequest request = YeepayCommon.ADD_WECHAT_CONFIG.buildRequest();
		YeepayUtils.addParamsFromObj(configAppDto,request,ConfigAppDto.class);
		try {
			YopResponse response = yopClient.request(request);
			return R.data(JSON.parseObject(response.getStringResult()));
		} catch (YopClientException e) {
			e.printStackTrace();
		}
		return R.fail("配置失败");
	}

	/**
	 *	易宝 查询公众号配置
	 *
	 *
	 * @param parentMerchantNo
	 * @param merchantNo
	 * @param appIdType
	 * @return*/
	public R queryApp(String parentMerchantNo, String merchantNo, String appIdType) {
		YopRequest request = YeepayCommon.QUERY_WECHAT_CONFIG.buildRequest();
		request.addParameter("parentMerchantNo",parentMerchantNo);
		request.addParameter("merchantNo",merchantNo);
		if(appIdType!=null){
			request.addParameter("appIdType",appIdType);
		}
		try {
			YopResponse response = yopClient.request(request);
			return R.data(JSON.parseObject(response.getStringResult()));
		} catch (YopClientException e) {
			e.printStackTrace();
		}
		return R.fail("查询失败");
	}

}
