package com.izouma.nineth.service;

import com.alibaba.excel.EasyExcel;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.domain.*;
import com.izouma.nineth.dto.airDrop.AirDropDTO;
import com.izouma.nineth.enums.AirDropType;
import com.izouma.nineth.enums.CollectionType;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.CollectionRepo;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.utils.excel.UploadDataListener;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

import static org.junit.Assert.*;

@Slf4j
public class AirDropServiceTest extends ApplicationTests {
    @Autowired
    AirDropService    airDropService;
    @Autowired
    UserRepo          userRepo;
    @Autowired
    CollectionRepo    collectionRepo;
    @Autowired
    CollectionService collectionService;
    @Autowired
    AssetService      assetService;

    @Test
    public void drop() {
        airDropService.drop(4235490L, 4273750L, 498,
                LocalDateTime.of(2022, 3, 25, 17, 30));
    }

    @Test
    public void drop2() throws ExecutionException, InterruptedException {
        ForkJoinPool customThreadPool = new ForkJoinPool(1000);
        customThreadPool.submit(() -> {
            Long id = 273L;
            Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("暂无藏品"));
            List<AirDropDTO> dtos = EasyExcel.read("/nft/xss1.xlsx")
                    .head(AirDropDTO.class).sheet().doReadSync();
            Set<String> phones = dtos.stream().map(AirDropDTO::getPhone).collect(Collectors.toSet());
            List<Long> userId = userRepo.findAllByPhoneIn(phones)
                    .stream()
                    .map(User::getId)
                    .collect(Collectors.toList());
            AirDrop airDrop = new AirDrop();
            airDrop.setUserIds(userId);
            airDrop.setName("官方空投");
            airDrop.setType(AirDropType.asset);
            airDrop.setSimulateOrder(false);
            airDrop.setIgnoreStockCheck(false);
            airDrop.setCollectionId(collection.getId());
            List<User> users = userRepo.findByIdInAndDelFalse(airDrop.getUserIds());
            users.parallelStream().forEach(user -> {
                try {
                    assetService.createAsset(collection, user, null, null, "空投", collectionService
                            .getNextNumber(collection.getId()));
                    collectionService.decreaseStock(collection.getId(), 1);
                    collectionService.increaseSale(collection.getId(), 1);
                } catch (Exception e) {
                    log.error("空投出错", e);
                }
            });
        }).get();
    }
}