package com.dayou.brokerage.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dayou.brokerage.interceptor.BrokerageInterceptor;
import com.dayou.brokerage.validator.BrokerageValidator;
import com.dayou.constants.JwtConstants;
import com.dayou.entity.BrokerageBaseAmount;
import com.dayou.entity.BrokerageMarketerRatio;
import com.dayou.service.IBrokerageBaseAmountService;
import com.dayou.service.IBrokerageMarketerRatioService;
import com.dayou.service.IGlobalConfigService;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

import static com.dayou.common.Constants.MERGE_SYMBOL;

/**
 * 类说明：提成逻辑相关配置类
 *
 * @author: wucl
 * @since: 2023/3/3
 * created with IntelliJ IDEA.
 */
@Configuration
public class BrokerageConfig {

    private static final String allServiceImpl = "execution(* com.dayou.service.impl.*.*(..))";

    @Autowired
    private BrokerageInterceptor brokerageInterceptor;

    @Autowired
    private IBrokerageBaseAmountService brokerageBaseAmountService;

    @Autowired
    private IBrokerageMarketerRatioService marketerRatioService;


    @Bean
    public BrokerageValidator brokerageValidator() {
        BrokerageValidator brokerageValidator = new BrokerageValidator();
        return brokerageValidator;
    }


    @Bean
    public DefaultPointcutAdvisor brokerageAdvisor(){
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(allServiceImpl);
        advisor.setPointcut(pointcut);
        advisor.setAdvice(brokerageInterceptor);
        return advisor;
    }


    @Bean
    @Qualifier("marketerRatioCache")
    public LoadingCache<String,BigDecimal> marketerRatioCache(){
        LoadingCache<String,BigDecimal> cache = CacheBuilder.newBuilder().maximumSize(50)
                .expireAfterWrite(1,TimeUnit.DAYS).build(new CacheLoader<String, BigDecimal>() {
                    @Override
                    public BigDecimal load(String key) throws Exception {
                        String[] ids = key.split(MERGE_SYMBOL);
                        Long itemCate = Long.valueOf(ids[0]);
                        Long itemSource = Long.valueOf(ids[1]);
                        BigDecimal ratio = marketerRatioService.getOne(new LambdaQueryWrapper<BrokerageMarketerRatio>().eq(BrokerageMarketerRatio::getItemCate, itemCate)
                                .eq(BrokerageMarketerRatio::getItemSource, itemSource)).getRatio();
                        return ratio;
                    }
                });
        return cache;
    }

    @Bean
    @Qualifier("baseAmountsCache")
    public LoadingCache<Long,BigDecimal> baseAmountsCache(){
        LoadingCache<Long,BigDecimal> cache = CacheBuilder.newBuilder().maximumSize(100)
                .expireAfterWrite(1,TimeUnit.DAYS).build(new CacheLoader<Long, BigDecimal>() {
                    @Override
                    public BigDecimal load(Long key) throws Exception {
                        return brokerageBaseAmountService.getOne(new LambdaQueryWrapper<BrokerageBaseAmount>().eq(BrokerageBaseAmount::getUserId,key)).getBaseAmount();
                    }
                });
        return cache;
    }
}
