package com.cyksj.service.mange.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.mapper.mirrorshop.UserMirrorShopMapper;
import com.cyksj.model.entity.UserMirrorShop;
import com.cyksj.redis.RedisService;
import com.cyksj.service.mange.RefreshCacheService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.Set;

/**
 * 项目名: yhlxj2
 * 文件名: RefreshCacheServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/2/22 16:28
 */
@Service
@RequiredArgsConstructor
public class RefreshCacheServiceImpl implements RefreshCacheService {
	private final RedisService redisService;

	private final UserMirrorShopMapper userMirrorShopMapper;

	@Override
	public void refreshYhHomePageCache() {
		Set<String> keys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "*");
		redisService.del(keys.toArray(String[]::new));
	}

	@Override
	public void refreshYhHomeCategoryPageCache() {
		Set<String> keys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "category" + "*");
		Set<String> shoKeys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + "category" + "*");
		redisService.del(keys.toArray(String[]::new));
		redisService.del(shoKeys.toArray(String[]::new));
	}

	@Override
	public void refreshGoodsSkuSubsidyPageCache() {
		Set<String> keys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "subsidy" + "*");
		redisService.del(keys.toArray(String[]::new));
	}

	@Override
	public void refreshShopUserIdCacheByYshId(Long yhsId) {
		String yhShopCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + yhsId + "*";
		Set<String> keys = redisService.keys(yhShopCacheKey);
		redisService.del(keys.toArray(String[]::new));
	}

	@Override
	public void refreshUserShopMangeCache(String customId) {
		Set<String> keys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + customId + "*");
		redisService.del(keys.toArray(String[]::new));
	}

	@Override
	public void goodsQuestionCache() {
		//售前
		String goodsQuestionKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "goods_detail:*";
		Set<String> keys = redisService.keys(goodsQuestionKey);
		redisService.del(keys.toArray(String[]::new));
		//售后缓存
		redisService.del(redisService.keys(RedisService.key.YH_GOODS_QUESTIONS_DETAIL.name().toLowerCase() + "*").toArray(String[]::new));
	}

	@Override
	public void refreshWholesaleGoods() {
		Set<String> keys = redisService.keys(RedisService.key.GOODS_WHOLESALE_KEY.getName() + "*");
		redisService.del(keys.toArray(String[]::new));
	}

	@Override
	public void refreshUserMirrorShopAdvertiseCache(Long userId) {
		if (userId == null) {
			Set<String> keys = redisService.keys(RedisService.key.USER_MIRROR_SHOP_ADVERTISE_CACHE_KEY.getName() + "*");
			redisService.del(keys.toArray(String[]::new));
			return;
		}
		Optional.ofNullable(userMirrorShopMapper.selectOne(Wrappers.lambdaQuery(UserMirrorShop.class).eq(UserMirrorShop::getUserId, userId).last("limit 1")))
				.ifPresent(um -> {
					String key = RedisService.key.USER_MIRROR_SHOP_ADVERTISE_CACHE_KEY.getName() + um.getCustomId();
					redisService.del(key);
				});

	}

	@Override
	public void refreshUserBenefitsConfigCache() {
		Set<String> keys = redisService.keys(RedisService.key.USER_BENEFITS_KEY.getName() + "*");
		redisService.del(keys.toArray(String[]::new));
	}
}
