package org.springblade.common.cache;


import com.alibaba.fastjson.JSON;
import org.springblade.common.enums.ResCode;
import org.springblade.common.exception.BizException;
import org.springblade.common.utils.RSASecretUtil;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.ldt.billrecord.entity.BillRecord;
import org.springblade.modules.payment.entity.SuccessParams;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;

/**
 * @author: lianghanqiang
 * @description: 支付模块缓存
 * @since: 7/30/21 -- 9:37 AM
 */
public class PaymentCache {

	public static final String QR_CODE = "qrcode:";
	public static final String BAR_CODE = "barcode:";
	public static final String PAY_INFO = "payinfo:";
	public static final String ORDER_INFO = "orderInfo:";

	private static final String PRIVATEKEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCVj9vx7e6z+5kD43EnxZvfIZ299SwYxYzjAgIfweu/Ot73mRzAzVbbCsbUuWGYgqpN9otzjHze3hGOys61SA8hCUhg4yuzkZu/C7F5pgN9AkUOhcS2qgOyuwlWnxn8QyyyZGnIBH1Juua0UUi6oRKK7geOmGViXuoYUGoX1rLv2TQLVSOe1vf377BmPmtjForS8OgGWJ1bO5zo8V6yGGV2g8dj2A1Q+xbUKPVsU+0xCVSrJbLWgJx7apKkX+E9xoh8J01T3hE6/m1SSQePcDZsjOoy93CeCLNY0tmw5F93gpwlO4L+oxGVQHWtku6dHW8TVP7t9DAW+UcDwsaVaNk5AgMBAAECggEAH52/ow7nLQj8ucO0wfGWF78ld2iCn+Hy/HIKPOhji5NIWxL07xT5TWijy4g8x16n3b9Gj3CdXQeYspTQyaWn/RXXRRQAwv7+643vQIYFqsF2E7HXeb3K9ldvi+4qMheJNVTgv7WjPpjR+EBOnZU7ND9JPa2dkQP7fJRTOSLbxLPTa3pIBQqsuNZyPI3h3XxIyXya24ncenVLRJNsYUgA63KOS79sft3kqPuEVDOWrenvAEeby4bCbx31Z3pPlXiA/5O4vDQrb3G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faLc4x83t4RjsrOtUgPIQlIYOMrs5GbvwuxeaYDfQJFDoXEtqoDsrsJVp8Z/EMssmRpyAR9SbrmtFFIuqESiu4HjphlYl7qGFBqF9ay79k0C1Ujntb39++wZj5rYxaK0vDoBlidWzuc6PFeshhldoPHY9gNUPsW1Cj1bFPtMQlUqyWy1oCce2qSpF/hPcaIfCdNU94ROv5tUkkHj3A2bIzqMvdwngizWNLZsORfd4KcJTuC/qMRlUB1rZLunR1vE1T+7fQwFvlHA8LGlWjZOQIDAQAB";

	private static RedisTemplate redisTemplate;

	static   {
		 redisTemplate = SpringUtil.getBean("redisTemplate");
	}

	/**
	 * MD5串获取二维码信息，key：MD5 ，value： JSON
	 *
	 * @param md5Str MD5
	 * @return T 信息实体
	 */
	public static <T> T getCodeInfo(String md5Str,Class<T> clazz) {
		String content = (String) CacheUtil.get(CacheNames.PAYMENT,":"+PAY_INFO,md5Str);
		return JSON.parseObject(content,clazz);
	}

	/**
	 * MD5 缓存 二维码信息 key：MD5 ，value： JSON
	 *
	 * @param md5Str MD5
	 * @return
	 */
	public static void putCodeInfo(String md5Str,Object object) {
		CacheUtil.put(CacheNames.PAYMENT,":"+PAY_INFO,md5Str,JSON.toJSONString(object));
	}

	/**
	 * MD5 缓存 二维码信息 key：MD5 ，value： JSON
	 *
	 * @param md5Str MD5
	 * @param expireTime 过期时间
	 * @return
	 */
	public static void putCodeInfoWithEx(String md5Str,Object object,Long expireTime) {
		redisTemplate.opsForValue().set(CacheNames.PAYMENT+":"+PAY_INFO+md5Str,JSON.toJSONString(object),expireTime, TimeUnit.SECONDS);
	}


	/**
	 * 	缓存付款码订单信息
	 * @param billRecord 订单信息
	 * */
	public static void putOrderInfo(BillRecord billRecord,String userId){
		String text = JSON.toJSONString(billRecord);
		String secret = null;
		try {
			secret = RSASecretUtil.privateEncrypt(text,RSASecretUtil.getPrivateKey(PRIVATEKEY));
		} catch (Exception e) {
			throw new BizException(ResCode.ENCODE_FAIL);
		}
		CacheUtil.put(CacheNames.PAYMENT, ":" + PAY_INFO, userId,secret);
	}

	/**
	 * 	获取付款码订单信息
	 * @param
	 * */
	public static BillRecord getOrderInfo(String userId ){
		 String secret = (String) CacheUtil.get(CacheNames.PAYMENT, ":" + PAY_INFO, userId);
		String text = null;
		try {
			text = RSASecretUtil.publicDecrypt(secret,RSASecretUtil.getPublicKey(PUBLICKEY));
		} catch (Exception e) {
			throw new BizException(ResCode.DECODE_FAIL);
		}
		return JSON.parseObject(text,BillRecord.class);
	}

	/**
	 *	缓存订单回调信息
	 * @param billId 订单id
	 * @param successParams 回调参数
	 * @return
	 */
	public static void putSuccessParams(String billId, SuccessParams successParams) {
		CacheUtil.put(CacheNames.PAYMENT, ORDER_INFO, billId,JSON.toJSONString(successParams),false);
	}

	/**
	 *	获取订单回调信息
	 * @param billId 订单id
	 * @return
	 */
	public static SuccessParams getSuccessParams(String billId) {
		String params =  CacheUtil.get(CacheNames.PAYMENT, ORDER_INFO, billId,String.class,false);
		return JSON.parseObject(params,SuccessParams.class);
	}

}
