package com.izouma.nineth.service;

import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.domain.*;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.AssetStatus;
import com.izouma.nineth.enums.OperationType;
import com.izouma.nineth.enums.OrderStatus;
import com.izouma.nineth.repo.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

class AssetServiceTest extends ApplicationTests {
    @Autowired
    private OrderRepo         orderRepo;
    @Autowired
    private BlindBoxItemRepo  blindBoxItemRepo;
    @Autowired
    private AssetService      assetService;
    @Autowired
    private CollectionRepo    collectionRepo;
    @Autowired
    private UserRepo          userRepo;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private AssetMintService  assetMintService;
    @Autowired
    private AssetRepo         assetRepo;
    @Autowired
    private GiftOrderRepo     giftOrderRepo;

    @Test
    void createAsset() {
        BlindBoxItem item = blindBoxItemRepo.findById(1860L).get();
        Order order = orderRepo.findById(1922L).get();
    }


    @Test
    void testCancelConsignment() {
    }

    @Test
    public void testCreateAsset() {
        Order order = orderRepo.findById(4618L).get();
        Collection collection = collectionRepo.findById(order.getCollectionId()).get();
        BlindBoxItem blindBoxItem = blindBoxItemRepo.findById(4250L).get();
        User user = userRepo.findById(order.getUserId()).get();
        assetService.createAsset(collection,blindBoxItem, user, order.getId(), order.getPrice(),  OperationType.SELL.getDescription());
    }

    @Test
    public void setHistory() {
        assetService.setHistory();
    }


    @Test
    public void stat() {
        Map<User, Integer> match = assetService.holdQuery(Arrays.asList(
                        "【熊猫柯斯】-虎虎生威"),
                LocalDateTime.of(2021, 12, 31, 9, 59, 59),
                LocalDateTime.of(2021, 12, 31, 10, 0, 0));
        System.out.println("用户ID,昵称,手机,持有套数");
        for (Map.Entry<User, Integer> e : match.entrySet()) {
            User user = e.getKey();
            System.out.printf("%d,%s,%s,%d%n", user.getId(), user.getNickname(), user.getPhone(), e.getValue());
        }

    }

    @Test
    public void fixstatus() {
        List<Asset> assets = assetRepo.findByStatusIn(Arrays.asList(AssetStatus.GIFTING, AssetStatus.TRADING));
        System.out.println(assets.size());
        for (Asset asset : assets) {
            if (asset.getModifiedAt().isAfter(LocalDateTime.of(2022, 3, 10, 16, 0, 0))) continue;
            if (assetRepo.findByFromAssetId(asset.getId()).isEmpty()) {
                if (asset.getStatus() == AssetStatus.GIFTING) {
                    List<GiftOrder> list = giftOrderRepo.findByAssetIdAndStatusIn(asset.getId(), Arrays.asList(OrderStatus.PROCESSING, OrderStatus.FINISH));
                    if (list.size() == 1) {
                        System.out.println(asset.getId() + "," + list.get(0).getId());
                        User user = userRepo.findById(list.get(0).getToUserId()).get();
                        assetService.transfer(asset, asset.getPrice(), user, OperationType.give.getDescription(), null);
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("@" + asset.getId());
                    }
                } else {
                    List<Order> list = orderRepo.findByAssetIdAndStatusIn(asset.getId(), Arrays.asList(OrderStatus.PROCESSING, OrderStatus.FINISH));
                    if (list.size() == 1) {
                        System.out.println(asset.getId() + "," + list.get(0).getId());
                        Order order = list.get(0);
                        User user = userRepo.findById(list.get(0).getUserId()).get();
                        assetService.transfer(asset, order.getPrice(), user, OperationType.TRANSFER.getDescription(), order.getId());
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("#" + asset.getId());
                    }
                }
            } else {
                System.out.println("*" + asset.getId());
            }
        }
    }

    @Test
    public void all() {
        PageQuery pageQuery = new PageQuery();
        Map<String, Object> query = pageQuery.getQuery();
        query.put("userId", 79151);
        query.put("status", "TRANSFERRED");
        query.put("projectId", 1);
        pageQuery.setSearch("熊");
        System.out.println(assetService.all(pageQuery).getContent());
    }

}