package com.liquidnet.client.admin.zhengzai.kylin.utils;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.service.sweet.constant.SweetConstant;
import com.liquidnet.service.sweet.entity.SweetManualArtists;
import com.liquidnet.service.sweet.entity.SweetManualArtistsFive;
import com.liquidnet.service.sweet.entity.SweetManualArtistsMdsk;
import com.liquidnet.service.sweet.entity.SweetManualArtistsTfc;
import com.liquidnet.service.sweet.mapper.SweetManualArtistsFiveMapper;
import com.liquidnet.service.sweet.mapper.SweetManualArtistsMapper;
import com.liquidnet.service.sweet.mapper.SweetManualArtistsMdskMapper;
import com.liquidnet.service.sweet.mapper.SweetManualArtistsTfcMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * admin 修改 kylin 艺人后，失效 sweet 小程序相关 Redis 缓存
 */
@Slf4j
@Component
public class SweetArtistCacheUtils {

    @Autowired
    private RedisDataSourceUtil redisDataSourceUtil;
    @Autowired
    private SweetManualArtistsMapper sweetManualArtistsMapper;
    @Autowired
    private SweetManualArtistsMdskMapper sweetManualArtistsMdskMapper;
    @Autowired
    private SweetManualArtistsTfcMapper sweetManualArtistsTfcMapper;
    @Autowired
    private SweetManualArtistsFiveMapper sweetManualArtistsFiveMapper;

    public void invalidateByArtistId(String artistId) {
        if (artistId == null || artistId.isEmpty()) {
            return;
        }
        try {
            redisDataSourceUtil.getRedisSweetUtil().del(SweetConstant.REDIS_KEY_SWEET_ARTISTS_DETAILS.concat(artistId));

            invalidateManualTimeList(
                    sweetManualArtistsMapper.selectList(
                            Wrappers.lambdaQuery(SweetManualArtists.class).eq(SweetManualArtists::getArtistsId, artistId)
                    ).stream().map(SweetManualArtists::getManualId).collect(Collectors.toSet()),
                    SweetConstant.REDIS_KEY_SWEET_MANUAL_TIME_LIST
            );
            invalidateManualTimeList(
                    sweetManualArtistsMdskMapper.selectList(
                            Wrappers.lambdaQuery(SweetManualArtistsMdsk.class).eq(SweetManualArtistsMdsk::getArtistsId, artistId)
                    ).stream().map(SweetManualArtistsMdsk::getManualId).collect(Collectors.toSet()),
                    SweetConstant.REDIS_KEY_SWEET_MDSK_MANUAL_TIME_LIST
            );
            invalidateManualTimeList(
                    sweetManualArtistsTfcMapper.selectList(
                            Wrappers.lambdaQuery(SweetManualArtistsTfc.class).eq(SweetManualArtistsTfc::getArtistsId, artistId)
                    ).stream().map(SweetManualArtistsTfc::getManualId).collect(Collectors.toSet()),
                    SweetConstant.REDIS_KEY_SWEET_TFC_MANUAL_TIME_LIST
            );
            invalidateManualTimeList(
                    sweetManualArtistsFiveMapper.selectList(
                            Wrappers.lambdaQuery(SweetManualArtistsFive.class).eq(SweetManualArtistsFive::getArtistsId, artistId)
                    ).stream().map(SweetManualArtistsFive::getManualId).collect(Collectors.toSet()),
                    SweetConstant.REDIS_KEY_SWEET_FIVE_MANUAL_TIME_LIST
            );
        } catch (Exception e) {
            log.error("invalidate sweet artist cache failed, artistId={}", artistId, e);
        }
    }

    private void invalidateManualTimeList(Set<String> manualIds, String keyPrefix) {
        if (manualIds == null || manualIds.isEmpty()) {
            return;
        }
        for (String manualId : new HashSet<>(manualIds)) {
            if (manualId != null && !manualId.isEmpty()) {
                redisDataSourceUtil.getRedisSweetUtil().del(keyPrefix.concat(manualId));
            }
        }
    }
}
