package org.springblade.payment.plugin;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.cache.PaymentCache;
import org.springblade.common.enums.OrderType;
import org.springblade.common.enums.PaymentType;
import org.springblade.core.tool.api.R;
import org.springblade.ldt.bills.entity.Bills;
import org.springblade.ldt.bills.service.IBillsService;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.payment.entity.PayParam;
import org.springblade.payment.entity.SuccessParams;
import org.springblade.yeePay.entity.InitOrderDto;
import org.springblade.yeePay.service.YeePayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: lianghanqiang
 * @description: 易宝支付插件
 * @since: 7/29/21 -- 2:56 PM
 */
@Component
@Slf4j
public class YeePayPlugin implements Payment {

	@Autowired
	YeePayService yeePayService;
	@Autowired
	IBillsService billsService;
	@Autowired
	ILoginUserService loginUserService;

	@Override
	public R nativePay(PayParam payParam) {
		return yeePayService.createCode(null);
	}
	@Override
	public R payCode(PayParam payParam) {
		return R.data("我还没有实现，你等等");
	}

	@Override
	public R transfer(PayParam payParam) {
		return yeePayService.payByGateWay(null);
	}

	@Override
	public void callback(HttpServletRequest request) {
//
//		log.info("易宝网关支付回调： map"+ JSON.toJSONString(request.getParameterMap()));
//
//		JSONObject jsonObject = yeePayService.deCodeNotifyData(request.getParameterMap().get("cipherText")[0]);
//		log.info("易宝网关支付解密回调密文："+jsonObject.toJSONString());
//
//		SuccessParams successParams = PaymentCache.getSuccessParams(jsonObject.getString("orderId"));
//
//		Assert.notNull(successParams,"订单回调参数数据获取失败！");
//
//		successParams.setPlatformId(jsonObject.getString("uniqueOrderNo"));
//		successParams.setPayChannel(jsonObject.getString("channel"));
//		successParams.setStatus(
//			"SUCCESS".equals(jsonObject.getString("status")) ?
//			SuccessParams.PAY_STATUS_SUCCESS:
//			SuccessParams.PAY_STATUS_FAIL);
//		//处理付款后业务流程
//		paymentService.success(successParams);
	}

	@Override
	public R miniProgram(PayParam payParam) {

		//查询订单状态
		Bills bill = getOrderDetail(payParam,billsService);
		Assert.notNull(bill,"订单查询失败，无此订单信息！");


		//封装回调参数
		SuccessParams successParams = SuccessParams
			.builder()
			.bills(bill)
			.orderType(OrderType.valueOf(bill.getType()).name())
			.build();

		PaymentCache.putSuccessParams(Convert.toStr(bill.getId()),successParams);

		return yeePayService.initOrder(
			InitOrderDto.builder()
				.payWay("MINI_PROGRAM")
				.channel("WECHAT")
				.scene("OFFLINE")
				.goodsName(bill.getTitle())
				.orderId(Convert.toStr(bill.getId()))
				.orderAmount(bill.getPrice().doubleValue())
				.appId(bill.getAppid())
				.userId(bill.getOpenid())
				.userIp("127.0.0.1")
				.notifyUrl(callbackUrl(PaymentType.YEE_PAY.name()))
				.build()
		);
	}

	public static void main(String[] args) {
		String s = "{\n" +
			"    \"channelOrderId\":\"4200001123202108069046794860\",\n" +
			"    \"orderId\":\"1423594166159466497\",\n" +
			"    \"bankOrderId\":\"5955072001210806\",\n" +
			"    \"paySuccessDate\":\"2021-08-06 18:38:19\",\n" +
			"    \"channel\":\"WECHAT\",\n" +
			"    \"payWay\":\"USER_SCAN\",\n" +
			"    \"uniqueOrderNo\":\"1013202108060000002490940644\",\n" +
			"    \"orderAmount\":\"0.10\",\n" +
			"    \"payAmount\":\"0.10\",\n" +
			"    \"payerInfo\":\"{\\\"bankCardNo\\\":\\\"\\\",\\\"bankId\\\":\\\"CFT\\\",\\\"cardType\\\":\\\"CFT\\\",\\\"mobilePhoneNo\\\":\\\"\\\",\\\"userID\\\":\\\"o19581JoLz7p2SjLLoQ6jBBLN7Ws\\\"}\",\n" +
			"    \"realPayAmount\":\"0.10\",\n" +
			"    \"parentMerchantNo\":\"10086062555\",\n" +
			"    \"merchantNo\":\"10086062555\",\n" +
			"    \"status\":\"SUCCESS\"\n" +
			"}";
		JSONObject jsonObject = JSON.parseObject(s);
		System.out.println(jsonObject.getString("uniqueOrderNo"));

	}
}
