package org.springblade.gateway.active_gateway.util;

import org.springblade.common.utils.SpringContextHolder;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.gateway.active_gateway.constant.ActiveProductConstant;
import org.springblade.sing.active.entity.ActiveProductRecord;

import java.util.Map;

/**
 * @Author: Silent
 * @Description
 * @Date: Created in 16:34 2021/11/10
 * @Modified By:
 */
public class ActiveProductUtil {
	private static final BladeRedis bladeRedis = SpringContextHolder.getBean(BladeRedis.class);

	/**
	 * 添加作品排名
	 * @param activeProductRecord
	 * @return
	 */
	public static boolean addProductRank(ActiveProductRecord activeProductRecord){
		delProductRank(activeProductRecord);
		//后添加
		return bladeRedis.zAdd(new StringBuilder(ActiveProductConstant.PRODUCT_SORT_CACHE_KEY).append(":").append(activeProductRecord.getActiveId().toString()).toString(),
			activeProductRecord.getId(),
			activeProductRecord.getHeatValue().doubleValue());
	}

	/**
	 * 删除作品排行
	 * @param activeProductRecord
	 * @return
	 */
	public static Long delProductRank(ActiveProductRecord activeProductRecord){
		//先移除
		return bladeRedis.zRem(new StringBuilder(ActiveProductConstant.PRODUCT_SORT_CACHE_KEY).append(":").append(activeProductRecord.getActiveId().toString()).toString(),
			activeProductRecord.getId());
	}

	/**
	 * 删除活动缓存
	 * @param activeId
	 * @return
	 */
	public static boolean delActiveCache(String activeId){
		return bladeRedis.del(new StringBuilder(ActiveProductConstant.PRODUCT_SORT_CACHE_KEY).append(":").append(activeId).toString());
	}

	/**
	 * 添加活动缓存
	 * @param activeId
	 * @param activeMap
	 * @return
	 */
	public static Long addActiveCache(String activeId, Map<Object,Double> activeMap){
		delActiveCache(activeId);
		return bladeRedis.zAdd(new StringBuilder(ActiveProductConstant.PRODUCT_SORT_CACHE_KEY).append(":").append(activeId).toString(),activeMap);
	}

	/**
	 * 添加累计加热力值
	 * @param activeId 活动id
	 * @param activeProductId 活动作品id
	 * @param heatValue 热力值
	 * @return
	 */
	public static Double incrementScoreByHeat(Long activeId,Long activeProductId,double heatValue){
		return bladeRedis.zIncrBy(new StringBuilder(ActiveProductConstant.PRODUCT_SORT_CACHE_KEY).append(":").append(activeId).toString(),
			activeProductId,
			heatValue);
	}
}
