package com.izouma.nineth.service;

import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.domain.IdentityAuth;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.enums.AuthStatus;
import com.izouma.nineth.repo.IdentityAuthRepo;
import com.izouma.nineth.repo.UserRepo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class IdentityAuthServiceTest extends ApplicationTests {
    @Autowired
    private IdentityAuthService identityAuthService;
    @Autowired
    private IdentityAuthRepo    identityAuthRepo;
    @Autowired
    private UserRepo            userRepo;

    @Test
    public void validate() {
        identityAuthService.validateV2("王启帆", "18362933705", "32111119980516571X");
        identityAuthService.validateV2("王启帆", "18362933705", "321111199805165712");
        identityAuthService.validateV2("王启帆", "18362663633", "32111119980516571X");
    }

    @Test
    public void asdfa() {
        User user = userRepo.findById(8157L).get();
        List<IdentityAuth> list = identityAuthRepo.findByUserId(8157L);
        if (list.size() > 1) {
            IdentityAuth auth = list.stream()
                    .filter(i -> i.getStatus() == AuthStatus.SUCCESS)
                    .findAny().orElse(null);
            if (auth != null) {
                userRepo.setAuthStatus(user.getId(), auth.getStatus(), auth.getId());
                identityAuthRepo.deleteDuplicated(user.getId(), auth.getId());
                return;
            }

            auth = list.stream()
                    .filter(i -> i.getStatus() == AuthStatus.PENDING)
                    .findAny().orElse(null);
            if (auth != null) {
                userRepo.setAuthStatus(user.getId(), auth.getStatus(), auth.getId());
                identityAuthRepo.deleteDuplicated(user.getId(), auth.getId());
                return;
            }

            auth = list.stream()
                    .filter(i -> i.getStatus() == AuthStatus.FAIL)
                    .findAny().orElse(null);
            if (auth != null) {
                userRepo.setAuthStatus(user.getId(), auth.getStatus(), auth.getId());
                identityAuthRepo.deleteDuplicated(user.getId(), auth.getId());
                return;
            }

        } else if (list.size() == 1) {
            userRepo.setAuthStatus(user.getId(), list.get(0).getStatus(), list.get(0).getId());
        }
    }
}