package org.springblade.common.cache;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jdk.nashorn.internal.runtime.options.Option;
import org.springblade.common.config.entity.PlatformConfig;
import org.springblade.common.constant.PlatformConst;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.ldt.platform.entity.PlatformSetting;
import org.springblade.ldt.platform.service.IPlatformSettingService;
import org.springblade.yeePay.common.YeepayProduct;
import org.springblade.yeePay.entity.saas.InvitationDto;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: lianghanqiang
 * @description:
 * @since: 8/27/21 -- 2:18 PM
 */
public class PlatformCache {

	private static IPlatformSettingService platformSettingService;

	public static String PLATFORM_PREFIX = "saas";

	static {
		platformSettingService = SpringUtil.getBean(IPlatformSettingService.class);
	}

	/**
	 * 获取平台的各种费用参数设置
	 */
	public static PlatformConfig platFormConfig() {
		R res = CacheUtil.get(CacheNames.PLATFORM, PLATFORM_PREFIX, PlatformConst.PLATFORM_ARG, R.class, false);
		PlatformSetting platformSetting;
		if(Objects.isNull(res)){
			platformSetting = getSetting(PlatformConst.PLATFORM_ARG);
		}else{
			platformSetting = (PlatformSetting) res.getData();
		}
		return JSON.parseObject(platformSetting.getSettingValue(), PlatformConfig.class);
	}


	/**
	 * 获取入驻商户需要在易宝开通的产品信息
	 */
	public static InvitationDto.ProductInfo productInfo(YeepayProduct yeepayProduct) {
		R res = CacheUtil.get(CacheNames.PLATFORM, PLATFORM_PREFIX, PlatformConst.YEEPAY_PRODUCT_INFO, R.class, false);
		PlatformSetting platformSetting ;
		if(Objects.isNull(res)){
			platformSetting = getSetting(PlatformConst.YEEPAY_PRODUCT_INFO);
		}else{
			platformSetting = (PlatformSetting) res.getData();
		}
		List<InvitationDto.ProductInfo> productInfos = JSON.parseArray(platformSetting.getSettingValue(), InvitationDto.ProductInfo.class);
		for (InvitationDto.ProductInfo productInfo: productInfos) {
			if (Objects.equals(yeepayProduct,productInfo.getProductCode())){
				return productInfo;
			}
		}
		return null;
	}

	/**
	 * 根据类型获取平台默认易宝的产品信息
	 */
	public static List<InvitationDto.ProductInfo> productInfo(YeepayProduct[] yeepayProduct) {
		R res = CacheUtil.get(CacheNames.PLATFORM, PLATFORM_PREFIX, PlatformConst.YEEPAY_PRODUCT_INFO, R.class, false);
		PlatformSetting platformSetting ;
		if(Objects.isNull(res)){
			platformSetting = getSetting(PlatformConst.YEEPAY_PRODUCT_INFO);
		}else{
			platformSetting = (PlatformSetting) res.getData();
		}
		List<InvitationDto.ProductInfo> productInfos = JSON.parseArray(platformSetting.getSettingValue(), InvitationDto.ProductInfo.class);
		List<InvitationDto.ProductInfo> result = new LinkedList<>();
		//所有产品码
		Set<String> yeepayProductSet = Arrays.stream(yeepayProduct).map(YeepayProduct::name).collect(Collectors.toSet());
		//遍历产品信息
		for (InvitationDto.ProductInfo productInfo : productInfos) {
			if(yeepayProductSet.contains(productInfo.getProductCode().name())){
				result.add(productInfo);
			}
		}
		return result;
	}

	private static PlatformSetting getSetting(String key) {
		return platformSettingService.getOne(new QueryWrapper<PlatformSetting>().lambda()
			.eq(PlatformSetting::getSettingKey, key));
	}
}
