package com.izouma.nineth.service;

import com.github.kevinsawicki.http.HttpRequest;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.config.Constants;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.UserRegister;
import com.izouma.nineth.enums.AuthorityName;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.security.Authority;
import com.izouma.nineth.service.storage.StorageService;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class UserServiceTest extends ApplicationTests {

    @Autowired
    private UserService    userService;
    @Autowired
    private UserRepo       userRepo;
    @Autowired
    private StorageService storageService;

    @Test
    public void findByUsernameAndDelFalse1() {
        userRepo.findByUsernameAndDelFalse("admin");
        userRepo.findByUsernameAndDelFalse("admin");
    }

    @Test
    public void searchByPhone() {
        System.out.println(userService.searchByPhone("15077886171"));
    }

    @Test
    public void sss9() throws IOException {
        for (User user : userRepo.findAll()) {
            if (user.getAvatar().startsWith("https://cdn")) {
                HttpRequest request = HttpRequest.get(user.getAvatar());
                File file = File.createTempFile("gw1", "1sadga");
                request.receive(file);
                user.setAvatar(storageService.uploadFromInputStream(new FileInputStream(file), "image/" + RandomStringUtils.randomAlphabetic(12) + ".jpg"));
                userRepo.save(user);
            }
        }
    }

    @Test
    public void a() {
        for (String s : ("13816681152\n" +
                "15564456867\n" +
                "13363659367\n" +
                "18963550881\n" +
                "13931604318\n" +
                "13705395943\n" +
                "13162078752\n" +
                "13589360751\n")
                .split("\n")) {
            String name = "9th_" + RandomStringUtils.randomAlphabetic(8);
            User user = userService.create(UserRegister.builder()
                    .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
                    .username(name)
                    .nickname(name)
                    .password("123456")
                    .avatar(Constants.DEFAULT_AVATAR)
                    .phone(s)
                    .build());
        }
    }

    @Test
    public void batchRegister() {
        userService.batchRegister("13226246698\n" +
                "13793310531\n" +
                "13358006613\n" +
                "18898406893\n" +
                "17615876370\n" +
                "13267002685\n" +
                "15888548080\n" +
                "13678662069\n" +
                "15659009359\n" +
                "13011776866\n" +
                "18810668532\n" +
                "13543322894\n" +
                "13589360750\n" +
                "18582589484\n" +
                "15194185209\n" +
                "15063692500\n" +
                "13605489618\n" +
                "18069783568\n" +
                "15801815684\n" +
                "13285413859\n" +
                "13688419164\n" +
                "15979115671\n" +
                "13799940755\n" +
                "18301015323", "123456");
    }

    @Test
    public void test() {
        PageQuery pageQuery = new PageQuery();
        Map<String, Object> query = pageQuery.getQuery();
        query.put("inviteCode", "JL21FF");
        pageQuery.setSize(5000);
        List<User> phone = userService.all(pageQuery)
                .getContent();
        phone.forEach(user -> System.out.println(user.getPhone() + ":" + user.getCreatedAt()));
        // 2022-01-14T15:53:58 504 第一次查询
        phone.forEach(user -> System.out.print(user.getPhone() + " "));
    }
}