package com.sckw.example;

import com.sckw.example.model.Student;
import com.sckw.example.service.StudentRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;
import java.util.*;

@SpringBootTest
public class StudentRepositoryTest {

    @Autowired
    private StudentRepository studentRepository;

    /**
     * 插入单条数据
     */
    @Test
    public void insertOne() {
        Student student = new Student(9L, "tom", 18, 88.2d, new Date());
        studentRepository.insert(student);
    }

    /**
     * 插入多条数据
     */
    @Test
    public void insertMany() {
        List<Student> list = new ArrayList<>();
        Long begtime = System.currentTimeMillis();
        for (long i=0; i<1000; i++) {
            Student student1 = new Student(i, "jerry", 19, 38.2d, new Date());
            list.add(student1);
        }
        studentRepository.insert(list);
        Long enttime = System.currentTimeMillis();
        System.out.println("------------------"+(enttime - begtime));
    }

    /**
     * 修改数据
     */
    @Test
    public void update() {
        Optional<Student> op = studentRepository.findById("009");
        Student student = op.get();
        student.setStudentAge(222);
        studentRepository.save(student);
    }

    /**
     * 查询数据
     */
    @Test
    public void query() {
        Long begtime = System.currentTimeMillis();
        //根据Id查询单个对象
        Optional<Student> stuOp = studentRepository.findById("0000");
        System.out.println(stuOp.get());

        //根据字段查询单个对象
        Student student = new Student();
        student.setStudentAge(19);
        Optional<Student> stuOp1 = studentRepository.findOne(Example.of(student));
        System.out.println(stuOp1.get());

        //根据id列表查询多个对象
        Iterable<Student> itStu = studentRepository.findAllById(Arrays.asList(new String[]{"001", "002"}));
        Iterator<Student> itor = itStu.iterator();
//        while (itor.hasNext())
//            System.out.println(itor.next());

        //查询所有
        List<Student> all = studentRepository.findAll();

        //查询所有并排序
        List<Student> all1 = studentRepository.findAll(Sort.by(Sort.Order.desc("studentId"), Sort.Order.asc("studentName")));
//        for (Student stu : all1)
//            System.out.println(stu);

        System.out.println("0------------------:"+all1.size());

        //根据条件查询所有并排序
        Student student1 = new Student();
        student1.setStudentName("tom");
        List<Student> all2 = studentRepository.findAll(Example.of(student1), Sort.by(Sort.Order.desc("studentId"), Sort.Order.asc("studentName")));
//        for (Student stu : all2)
//            System.out.println(stu);



        //根据条件查询所有并排序，且分页
        Pageable pageable = PageRequest.of(0, 2);
        Page<Student> all3 = studentRepository.findAll(pageable);
        List<Student> content = all3.getContent();
//        for (Student stu : content)
//            System.out.println(stu);

        System.out.println("1------------------:"+content.size());
        Long enttime = System.currentTimeMillis();
        System.out.println("2------------------:"+(enttime - begtime));
    }

    /**
     * 其他操作
     */
    @Test
    public void other() {
        //count
        long count = studentRepository.count();
        System.out.println(count);
        Student student = new Student();
        student.setStudentAge(18);
        long count1 = studentRepository.count(Example.of(student));
        System.out.println(count1);

        //exists
        boolean exists = studentRepository.exists(Example.of(student));
        System.out.println(exists);
        boolean b = studentRepository.existsById("001");
        System.out.println(b);
    }


    /**
     * 删除操作
     */
    @Test
    public void remove() {
        //根据id删除单个对象
        studentRepository.deleteById("001");

        //根据字段删除
        Student student = new Student();
        student.setStudentAge(20);
        studentRepository.delete(student);

        //删除所有
        studentRepository.deleteAll();

        //根据字段删除多个
        Student student1 = new Student();
        student1.setStudentName("jerry");
        List<Student> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        studentRepository.deleteAll(list);
    }
}

