package com.izouma.nineth;

import com.izouma.nineth.domain.IdentityAuth;
import com.izouma.nineth.dto.UserBankCard;
import com.izouma.nineth.repo.IdentityAuthRepo;
import com.izouma.nineth.repo.UserBankCardRepo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

public class AES extends ApplicationTests {

    @Autowired
    private UserBankCardRepo userBankCardRepo;

    @Autowired
    private IdentityAuthRepo identityAuthRepo;

    @Test
    public void aes() throws ExecutionException, InterruptedException {

        new ForkJoinPool(500).submit(() -> {
            int pageNum = 0;
            boolean hasNext = true;
            while (hasNext) {
                Page<UserBankCard> page = userBankCardRepo.findAll(PageRequest.of(pageNum, 1000));
                page.getContent().parallelStream().forEach(i -> {
                    i.setModifiedAt(LocalDateTime.now());
                    userBankCardRepo.save(i);
                });
                pageNum++;
                hasNext = page.hasNext();
            }
        }).get();

        new ForkJoinPool(500).submit(() -> {
            int pageNum = 0;
            boolean hasNext = true;
            while (hasNext) {
                Page<IdentityAuth> page = identityAuthRepo.findAll(PageRequest.of(pageNum, 1000));
                page.getContent().parallelStream().forEach(i -> {
                    i.setModifiedAt(LocalDateTime.now());
                    identityAuthRepo.save(i);
                });
                pageNum++;
                hasNext = page.hasNext();
            }
        }).get();
    }
}
