package org.springblade.gateway.active_gateway.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springblade.common.utils.SpringContextHolder;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.gateway.active_gateway.constant.ActiveNoticeConstant;
import org.springblade.sing.active.entity.ActiveNotice;
import org.springblade.sing.active.service.IActiveNoticeService;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @Author: Silent
 * @Description 活动通知工具
 * @Date: Created in 10:02 2021/11/25
 * @Modified By:
 */
public class ActiveNoticeUtil {
	private static final BladeRedis bladeRedis = SpringContextHolder.getBean(BladeRedis.class);
	private static final IActiveNoticeService activeNoticeService = SpringContextHolder.getBean(IActiveNoticeService.class);

	/**
	 * 设置活动通知缓存
	 *
	 * @param activeId
	 */
	public static ActiveNotice getActiveNoticeMapCache(@NotNull Long activeId,@NotNull String title) {
		ActiveNotice activeNotice = JSONObject.parseObject(bladeRedis.hGet(ActiveNoticeConstant.getActiveNoticeCache(activeId), title),ActiveNotice.class);
		if (ObjectUtils.isEmpty(activeNotice)) {
			LambdaQueryWrapper<ActiveNotice> activeNoticeLambdaQueryWrapper = Wrappers.<ActiveNotice>lambdaQuery()
				.eq(ActiveNotice::getTitle, title);
			if(ObjectUtils.isNotEmpty(activeId)){
				activeNoticeLambdaQueryWrapper.eq(ActiveNotice::getActiveId,activeId);
			}
			activeNoticeLambdaQueryWrapper.last("limit 0,1");
			activeNotice = activeNoticeService.getOne(activeNoticeLambdaQueryWrapper);
			if (ObjectUtils.isNotEmpty(activeNotice)) {
				setActiveNoticeCache(activeNotice);
			}
		}
		return activeNotice;
	}

	/**
	 * 设置活动通知缓存
	 *
	 * @param activeId
	 * @param activeNoticeMap
	 */
	public static void setActiveNoticeMapCache(@NotNull Long activeId, @NotNull Map<Object, Object> activeNoticeMap) {
		if (ObjectUtils.isNotEmpty(activeId) && ObjectUtils.isNotEmpty(activeNoticeMap) && activeNoticeMap.size() != 0) {
			bladeRedis.hMset(ActiveNoticeConstant.getActiveNoticeCache(activeId), activeNoticeMap);
		}
	}

	/**
	 * 设置活动通知缓存
	 *
	 * @param activeNotice
	 */
	public static boolean setActiveNoticeCache(ActiveNotice activeNotice) {
		bladeRedis.hSet(ActiveNoticeConstant.getActiveNoticeCache(activeNotice.getActiveId()), activeNotice.getTitle(), JSONObject.toJSONString(activeNotice));
		return true;
	}

	/**
	 * 删除活动通知缓存
	 *
	 * @param activeNotice
	 * @return
	 */
	public static void removeActiveNoticeCache(ActiveNotice activeNotice) {
		removeActiveNoticeCacheByTitles(activeNotice.getActiveId(),activeNotice.getTitle());
	}

	/**
	 * 删除活动通知缓存
	 *
	 * @param activeNoticeList
	 * @return
	 */
	public static void removeActiveNoticeListCache(List<ActiveNotice> activeNoticeList) {
		for (ActiveNotice activeNotice : activeNoticeList) {
			bladeRedis.hDel(ActiveNoticeConstant.getActiveNoticeCache(activeNotice.getActiveId()),activeNotice.getTitle());
		}
	}

	/**
	 * 删除集合
	 *
	 * @param activeId
	 * @return
	 */
	public static void removeActiveNoticeCacheByTitles(@NotNull Long activeId,@NotNull String ...title) {
		bladeRedis.hDel(ActiveNoticeConstant.getActiveNoticeCache(activeId),title);
	}
}
