package com.ningdatech.kqapi.common.helper.basic;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ningdatech.kqapi.common.constant.BizConst;
import com.ningdatech.kqapi.zzsfw.entity.entity.DscSxAdsShareItemQltQlsxCommonIDVKq;
import com.ningdatech.kqapi.zzsfw.entity.entity.MatterKey;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMattersDeduplicate;
import com.ningdatech.kqapi.zzsfw.service.IDscSxAdsShareItemQltQlsxCommonIDVKqService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMatterDeduplicateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * AbstractMatterCache
 * </p>
 *
 * @author ZPF
 * @since 14:41 2023/3/1
 */
@Slf4j
public abstract class AbstractMatterCacheHelper implements InitializingBean {

    @Autowired
    private IDscSxAdsShareItemQltQlsxCommonIDVKqService kqService;

    @Autowired
    private INdKqZzsfwMatterDeduplicateService deduplicateService;

    protected LoadingCache<MatterKey, NdKqZzsfwMattersDeduplicate> mattersDupCache;

    protected LoadingCache<String, String> mattersUrlCache;

    private Boolean initMatterCache() {
        List<DscSxAdsShareItemQltQlsxCommonIDVKq> allMatters = kqService
                .list(Wrappers.lambdaQuery(DscSxAdsShareItemQltQlsxCommonIDVKq.class)
                .select(DscSxAdsShareItemQltQlsxCommonIDVKq::getRowguid,DscSxAdsShareItemQltQlsxCommonIDVKq::getQlName,
                        DscSxAdsShareItemQltQlsxCommonIDVKq::getWebapplyurl)
                .isNotNull(DscSxAdsShareItemQltQlsxCommonIDVKq::getWebapplyurl));
        if (allMatters.isEmpty()) {
            log.warn("事项数据未初始化");
            return Boolean.FALSE;
        }
        allMatters.forEach(m -> {
            MatterKey key = MatterKey.of(m.getQlName(),m.getWebapplyurl());
            mattersUrlCache.put(m.getQlName(),m.getWebapplyurl());
            if(mattersDupCache.asMap().containsKey(key)){
                NdKqZzsfwMattersDeduplicate dup = mattersDupCache.get(key);
                dup.setCountNum(dup.getCountNum() + 1);
                mattersDupCache.put(key,dup);
            }else{
                mattersDupCache.put(key,NdKqZzsfwMattersDeduplicate.of(m.getQlName(),m.getWebapplyurl()));
            }
        });
        return Boolean.TRUE;
    }

    protected NdKqZzsfwMattersDeduplicate get(String qlName,String url) {
        MatterKey key = MatterKey.of(qlName, url);
        return mattersDupCache.get(key);
    }

    protected String getUrl(String qlName) {
        return mattersUrlCache.get(qlName);
    }

    protected List<NdKqZzsfwMattersDeduplicate> all() {
        return Lists.newArrayList(mattersDupCache.asMap().values());
    }

    @Override
    public void afterPropertiesSet() {
        mattersUrlCache = Caffeine.newBuilder()
                .refreshAfterWrite(7, TimeUnit.DAYS)
                .maximumSize(1024 * 100)
                .build(key -> {
                    DscSxAdsShareItemQltQlsxCommonIDVKq matter = kqService.getOne(Wrappers.lambdaQuery(DscSxAdsShareItemQltQlsxCommonIDVKq.class)
                            .eq(DscSxAdsShareItemQltQlsxCommonIDVKq::getQlName, key)
                            .last(BizConst.LIMIT_1)
                            .isNotNull(DscSxAdsShareItemQltQlsxCommonIDVKq::getWebapplyurl));
                    if (Objects.isNull(matter)) {
                        //查不到直接返回null
                        return null;
                    }
                    return matter.getWebapplyurl();
                });
        mattersDupCache = Caffeine.newBuilder()
                .refreshAfterWrite(7, TimeUnit.DAYS)
                .maximumSize(1024 * 100)
                .build(key -> {
                    NdKqZzsfwMattersDeduplicate dup = deduplicateService.getOne(Wrappers.lambdaQuery(NdKqZzsfwMattersDeduplicate.class)
                            .eq(NdKqZzsfwMattersDeduplicate::getQlName, key)
                            .last(BizConst.LIMIT_1)
                            .isNotNull(NdKqZzsfwMattersDeduplicate::getWebapplyurl));
                    if (Objects.isNull(dup)) {
                        //查不到直接返回null
                        return null;
                    }
                    return dup;
                });
        // 初始化所有事项数据到缓存
//        initMatterCache();
    }

    public Boolean init() {
        mattersUrlCache = Caffeine.newBuilder()
                .refreshAfterWrite(7, TimeUnit.DAYS)
                .maximumSize(1024 * 100)
                .build(key -> {
                    DscSxAdsShareItemQltQlsxCommonIDVKq matter = kqService.getOne(Wrappers.lambdaQuery(DscSxAdsShareItemQltQlsxCommonIDVKq.class)
                            .eq(DscSxAdsShareItemQltQlsxCommonIDVKq::getQlName, key)
                            .last(BizConst.LIMIT_1)
                            .isNotNull(DscSxAdsShareItemQltQlsxCommonIDVKq::getWebapplyurl));
                    if (Objects.isNull(matter)) {
                        //查不到直接返回null
                        return null;
                    }
                    return matter.getWebapplyurl();
                });
        mattersDupCache = Caffeine.newBuilder()
                .refreshAfterWrite(7, TimeUnit.DAYS)
                .maximumSize(1024 * 100)
                .build(key -> {
                    NdKqZzsfwMattersDeduplicate dup = deduplicateService.getOne(Wrappers.lambdaQuery(NdKqZzsfwMattersDeduplicate.class)
                            .eq(NdKqZzsfwMattersDeduplicate::getQlName, key)
                            .last(BizConst.LIMIT_1)
                            .isNotNull(NdKqZzsfwMattersDeduplicate::getWebapplyurl));
                    if (Objects.isNull(dup)) {
                        //查不到直接返回null
                        return null;
                    }
                    return dup;
                });
        // 初始化所有事项数据到缓存
        return initMatterCache();
    }
}
