package com.izouma.nineth;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.antfinancial.mychain.baas.tool.restclient.RestClient;
import com.izouma.nineth.domain.*;
import com.izouma.nineth.domain.Collection;
import com.izouma.nineth.dto.UserRankDTO;
import com.izouma.nineth.enums.AuthStatus;
import com.izouma.nineth.enums.OrderStatus;
import com.izouma.nineth.repo.*;
import com.izouma.nineth.service.ActivityInviteService;
import com.izouma.nineth.service.AssetService;
import com.izouma.nineth.utils.DateTimeUtils;
import io.netty.util.concurrent.DefaultThreadFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;

import java.awt.print.Pageable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AntChainTest extends ApplicationTests {
    @Autowired
    private RestClient restClient;
    @Autowired
    private ActivityInviteService activityInviteService;
    @Autowired
    private UserRepo userRepo;
    @Autowired
    private AssetRepo assetRepo;
    @Autowired
    private AssetService assetService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private OrderRepo orderRepo;
    @Autowired
    private CollectionRepo collectionRepo;
    @Autowired
    private ActivityPriorityDocumentRepo activityPriorityDocumentRepo;
    public static ExecutorService GLOBAL_EXECUTOR = Executors.newFixedThreadPool(500, new DefaultThreadFactory("Global Thread"));
    @Autowired
    public ActivityInviteRepo activityInviteRepo;
    @Test
    public void testQuery() throws Exception {
        List<OrderRanking> orderRankingList= new ArrayList<>();
        Map<Long, Number> rankMap = new HashMap<>();
        ActivityInvite activityInvite = activityInviteRepo.findRecent();
        List<Map<String,Object>> userIinvitorList = userRepo.userIinvitor(activityInvite.getInviteStartdatetime(), AuthStatus.SUCCESS.name());
        //实名认证邀请排行榜
        userIinvitorList.forEach((item) -> {
            Long inviteUserId = Long.valueOf(item.get("userid").toString());
            Integer num = Integer.valueOf(item.get("num").toString());
            rankMap.put(inviteUserId, num);
        });
        System.err.println("");
        List<UserRankDTO> userRankVos = new ArrayList<>();
        for (Map.Entry<Long, Number> entry : rankMap.entrySet()) {
            Long userId = entry.getKey();
            Number value = entry.getValue();
            if (value == null || value.doubleValue() <= 0) {
                continue;
            }
            Optional<User> tmp = userRepo.findById(userId);
            if (tmp.isPresent()) {
                UserRankDTO userRankVo = new UserRankDTO();
                userRankVo.setUser_id(userId);
                BigDecimal bigDecimal = BigDecimal.valueOf(value.doubleValue());
                BigDecimal amount = bigDecimal.setScale(0, RoundingMode.DOWN);
                userRankVo.setAmount(amount);
                userRankVo.setAvatar(tmp.get().getAvatar());
                userRankVo.setNickname(tmp.get().getNickname());
                userRankVos.add(userRankVo);
            }
        }
        System.err.println("");
    }

    private List<OrderRanking> sss(List<Map<String, BigDecimal>> maps) {
        List<OrderRanking> list = new ArrayList<>();
        maps.forEach(e -> list.add(new OrderRanking(Long.parseLong(e.get("user_id")+""), new BigDecimal(e.get("count")+""))));
        return list;
    }
}
