package org.springblade.common.cache;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.utils.SpringUtil;
import org.springblade.ldt.platform.entity.PlatformSetting;
import org.springblade.ldt.platform.service.IPlatformSettingService;

import java.util.Objects;

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

	private static IPlatformSettingService platformSettingService;

	private static String PLATFORM_PREFIX = "platform";

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

	/**
	 * 	获取平台的各种费用参数设置
	 * */
	public static PlatformConfig platFormConfig(){
		PlatformConfig platformConfig = (PlatformConfig) CacheUtil.get(CacheNames.PLATFORM, PLATFORM_PREFIX, PlatformConst.PLATFORM_ARG);

		//没有缓存从数据库获取，然后放缓存
		if(Objects.isNull(platformConfig)){
			PlatformSetting platformSetting = platformSettingService.getOne(new QueryWrapper<PlatformSetting>()
				.lambda()
				.eq(PlatformSetting::getSettingKey, PlatformConst.PLATFORM_ARG));
			platformConfig = JSON.parseObject(platformSetting.getSettingValue(), PlatformConfig.class);
			CacheUtil.put(CacheNames.PLATFORM,PLATFORM_PREFIX,PlatformConst.PLATFORM_ARG,platformConfig);
		}
		return platformConfig;
	}
}
