请解释这段代码：package com.aia.tss.aitool.service.impl;

import com.aia.tss.aitool.config.AIModelConfig;
import com.aia.tss.aitool.entity.AIModel;
import com.aia.tss.aitool.service.AIService;
import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.Refill;
import io.github.bucket4j.local.LocalBucket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AIServiceImpl implements AIService {

    @Autowired
    private AIModelConfig aiModelConfig;

    public List<AIModel> getModels() {
        return models;
    }

    public List<AIModel> getDocumentModels() {
        return documentModels;
    }

    @Override
    public AIModel tryConsume() {
        for (AIModel model : models) {
            if (model.getBucket().tryConsume(1)) {
                log.debug("{} model remain tokens:{}", model.getName(), model.getBucket().getAvailableTokens());
                return model;
            }
        }
        return null;
    }

    @Override
    public AIModel tryDocumentConsume() {
        for (AIModel model : documentModels) {
            if (model.getBucket().tryConsume(1)) {
                log.debug("{} model remain tokens:{}", model.getName(), model.getBucket().getAvailableTokens());

                return model;
            }
        }
        return null;
    }

    @Override
    public AIModel tryConsume(String usage) {
        for (AIModel model : models) {
            var usages = model.getTargetUsage();
            if (usages.contains(usage) && model.getBucket().tryConsume(1)) {
                log.debug("usage {} model remain tokens:{}", model.getName(), model.getBucket().getAvailableTokens());

                return model;
            }
        }
        return null;
    }

    private List<AIModel> models;
    private List<AIModel> documentModels;

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public AIService aiService() {
        AIServiceImpl aiService = new AIServiceImpl();

        // only use deepseekv2.
        aiService.models = this.aiModelConfig.getModels().stream()
                .filter(model -> model.getTargetUsage().contains("code") && model.getName().equals("deepseekv2"))
                .collect(Collectors.toList());
        aiService.models.sort((x1, x2) -> x1.getPriority() - x2.getPriority());
        int refillSeconds = aiModelConfig.getTokenRefillInSeconds();
        for (AIModel model : aiService.models) {
            int capacity = model.getCapacity();
            Bandwidth limit = Bandwidth.classic(capacity, Refill.greedy(capacity, Duration.ofSeconds(refillSeconds)));
            LocalBucket bucket = Bucket.builder().addLimit(limit).build();
            model.setBucket(bucket);
        }

        // only use llama3.
        aiService.documentModels = this.aiModelConfig.getModels().stream()
                .filter(model -> model.getTargetUsage().contains("document") && model.getName().equals("llama3"))
                .collect(Collectors.toList());
        aiService.documentModels.sort((x1, x2) -> x2.getPriority() - x1.getPriority());
        for (AIModel model : aiService.documentModels) {
            int capacity = model.getCapacity();
            Bandwidth limit = Bandwidth.classic(capacity, Refill.greedy(capacity, Duration.ofSeconds(refillSeconds)));
            LocalBucket bucket = Bucket.builder().addLimit(limit).build();
            model.setBucket(bucket);
        }

        return aiService;
    }

    AIServiceImpl() {

    }
}