package com.izouma.nineth.service;

import com.alibaba.fastjson.JSONObject;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.domain.UserBalance;
import com.izouma.nineth.domain.WithdrawApply;
import com.izouma.nineth.dto.UserBankCard;
import com.izouma.nineth.enums.WithdrawStatus;
import com.izouma.nineth.repo.UserBalanceRepo;
import com.izouma.nineth.repo.UserBankCardRepo;
import com.izouma.nineth.repo.WithdrawApplyRepo;
import com.izouma.nineth.utils.SnowflakeIdWorker;
import lombok.AllArgsConstructor;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

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

public class WithdrawApplyServiceTest extends ApplicationTests {
    @Autowired
    private WithdrawApplyRepo    withdrawApplyRepo;
    @Autowired
    private UserBankCardRepo     userBankCardRepo;
    @Autowired
    private SandPayService       sandPayService;
    @Autowired
    private SnowflakeIdWorker    snowflakeIdWorker;
    @Autowired
    private WithdrawApplyService withdrawApplyService;
    @Autowired
    private UserBalanceRepo      userBalanceRepo;

    @Test
    public void test() {
        List<WithdrawApply> list = withdrawApplyRepo.findAll().stream()
                .filter(i -> i.getCreatedAt().isAfter(LocalDate.of(2022, 7, 7).atStartOfDay())
                        && i.getCreatedAt().isBefore(LocalDate.of(2022, 7, 8).atStartOfDay())
                        && "账户余额不足".equals(i.getReason())
                )
                .collect(Collectors.toList());
        list.parallelStream().forEach(i -> {
            UserBalance userBalance = userBalanceRepo.findByUserId(i.getUserId()).get();
            if (userBalance.getBalance().compareTo(i.getAmount()) >= 0) {
                try {
                    WithdrawApply apply = withdrawApplyService.apply(i.getUserId(), i.getAmount());
                    apply.setReason("retry");
                    withdrawApplyRepo.save(apply);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(list.size());
    }
}