package com.izouma.nineth.service;

import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.TokenHistory;
import com.izouma.nineth.repo.AssetRepo;
import com.izouma.nineth.repo.TokenHistoryRepo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.*;

class AssetLockServiceTest extends ApplicationTests {
    @Autowired
    private TokenHistoryRepo tokenHistoryRepo;
    @Autowired
    private AssetRepo        assetRepo;

    @Test
    void giveBonus() throws ExecutionException, InterruptedException {
        List<TokenHistory> list = tokenHistoryRepo.findByOperationAndCreatedAtBefore("销毁", LocalDateTime.of(2022, 7, 23, 21, 0, 0));
        List<Long> ssr = Collections.synchronizedList(new ArrayList<>());
        List<Long> sr = Collections.synchronizedList(new ArrayList<>());
        new ForkJoinPool(1000).submit(() -> {
            list.parallelStream().forEach(tokenHistory -> {
                String name = assetRepo.findFirstByTokenId(tokenHistory.getTokenId()).getName();
                if (Pattern.matches(".*僵尸动物园SSR #.*", name)) {
                    ssr.add(tokenHistory.getFromUserId());
                } else if (Pattern.matches(".*僵尸动物园SR #.*", name)) {
                    sr.add(tokenHistory.getFromUserId());
                }
            });
        }).get();
        System.out.println(ssr.size());
        System.out.println(sr.size());
    }
}