package org.springblade.gateway.common_gateway.util;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.utils.SpringContextHolder;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.gateway.common_gateway.constant.PlatformSettingConstant;
import org.springblade.gateway.common_gateway.dto.ActiveSettingDto;
import org.springblade.gateway.common_gateway.dto.CommonSettingDto;
import org.springblade.gateway.common_gateway.dto.PointGoodsSettingDto;
import org.springblade.gateway.common_gateway.enums.PlatformSettingEnum;
import org.springblade.sing.desk.entity.PlatformSetting;
import org.springblade.sing.desk.service.IPlatformSettingService;

import javax.validation.constraints.NotNull;

/**
 * @Author: Silent
 * @Description 获取平台设置工具
 * @Date: Created in 15:18 2021/11/9
 * @Modified By:
 */
@Slf4j
public class PlatformSettingUtil {
	private final static IPlatformSettingService platformSettingService = SpringContextHolder.getBean(IPlatformSettingService.class);

	private final static BladeRedis bladeRedis = SpringContextHolder.getBean(BladeRedis.class);


	/**
	 * 公共设置
	 *
	 * @return
	 */
	public static CommonSettingDto getCommonSetting() {
		return getPlatformSettingByCache(PlatformSettingEnum.COMMON_SETTING, null);
	}

	/**
	 * 获取活动配置
	 *
	 * @return
	 */
	public static ActiveSettingDto getActiveSetting(Long activeId) {
		return getPlatformSettingByCache(PlatformSettingEnum.ACTIVE_SETTING, activeId);
	}

	/**
	 * 获取积分商场设置
	 *
	 * @return
	 */
	public static PointGoodsSettingDto getPointGoodsSetting() {
		return getPlatformSettingByCache(PlatformSettingEnum.POINT_GOODS_SETTING, null);
	}

	/**
	 * 获取平台设置
	 *
	 * @return
	 */
	public static <T> T getPlatformSettingByCache(PlatformSettingEnum platformSettingEnum, Long activeId) {
		String platformSettingByCache = getPlatformSettingByCache(platformSettingEnum.name(), activeId);
		if (StringUtils.isNotEmpty(platformSettingByCache)) {
			try {
				return JSON.parseObject(platformSettingByCache, platformSettingEnum.getType());
			} catch (Exception e) {
				log.error(e.getMessage());
			}
		}
		return null;
	}

	/**
	 * 获取平台设置
	 *
	 * @return
	 */
	public static <T> T getPlatformSettingByCache(String key, @NotNull Class<T> tClass, Long activeId) {
		String platformSettingByCache = getPlatformSettingByCache(key, activeId);
		if (StringUtils.isNotEmpty(platformSettingByCache)) {
			try {
				return JSON.parseObject(platformSettingByCache, tClass);
			} catch (Exception e) {
				log.error(e.getMessage());
			}
		}
		return null;
	}

	/**
	 * 获取平台设置
	 *
	 * @return
	 */
	public static String getPlatformSettingByCache(String key, Long activeId) {
		String value = bladeRedis.hGet(PlatformSettingConstant.getCacheKey(activeId), key);
		if (ObjectUtils.isEmpty(value)) {
			LambdaQueryWrapper<PlatformSetting> lambdaQueryWrapper = Wrappers.<PlatformSetting>lambdaQuery()
				.eq(PlatformSetting::getKey, key);
			if (ObjectUtils.isNotEmpty(activeId)) {
				lambdaQueryWrapper.eq(PlatformSetting::getActiveId, activeId);
			}
			PlatformSetting platformSetting = platformSettingService.getOne(lambdaQueryWrapper);
			//返回值
			if (ObjectUtils.isNotEmpty(platformSetting)) {
				//添加到缓存
				setPlatformSettingToCache(platformSetting);
				return platformSetting.getValue();
			}
		}
		return value;
	}

	/**
	 * 设置平台设置缓存
	 *
	 * @param platformSetting
	 */
	public static void setPlatformSettingToCache(PlatformSetting platformSetting) {
		if (ObjectUtils.isEmpty(platformSetting)) {
			return;
		}
		bladeRedis.hSet(PlatformSettingConstant.getCacheKey(platformSetting.getActiveId()),
			platformSetting.getKey(), platformSetting.getValue());
	}

	/**
	 * 删除平台设置缓存
	 *
	 * @param activeId
	 * @param platformSettingEnum
	 */
	public static void delPlatformSettingToCache(Long activeId, PlatformSettingEnum platformSettingEnum) {
		bladeRedis.hDel(PlatformSettingConstant.getCacheKey(activeId),
			platformSettingEnum.name());
	}
}
