package test05;

import org.junit.jupiter.api.Test;
import org.opentest4j.TestAbortedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import test05.mapper.*;
import test05.pojo.*;
import test05.utils.JwtUtils;

import java.util.*;


@SpringBootTest
class Test05ApplicationTests {
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    ScoreMapper scoreMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    UserMapper userMapper;

    @Test
    public void courseMapperTest() {

        Course course = new Course();
        course.setName("测试课程");
        course.setCredit(1.0F);
        course.setMajor(2);
        course.setType(2);
        course.setGrade(2022);
        List<Course> selectCourse = courseMapper.selectCourse(course, 0, 10);
        if (!selectCourse.isEmpty()) {
            courseMapper.deleteCourse(course);
        }
        courseMapper.insertCourse(course);
        if (course.getId() == -1) {
            System.out.println("课程添加出阿信错误");
            throw new TestAbortedException();
        }
        selectCourse = courseMapper.selectCourse(new Course(), 0, 10);
        if (!selectCourse.isEmpty()) {
            System.out.println("课程查询出现错误");
        }
        course.setDetail("hello");
        courseMapper.updateCourse(course);
        selectCourse = courseMapper.selectCourse(course, 0, 10);
        if (selectCourse.isEmpty()) {
            System.out.println("课程修改出现错误");
        }
        courseMapper.deleteCourse(course);
        selectCourse = courseMapper.selectCourse(course, 0, 10);
        if (!selectCourse.isEmpty()) {
            System.out.println("课程删除出现错误");
        }
    }

    @Test
    public void studentMapperTest() {
        Student student = new Student();
        student.setCode("2020202");
        student.setMajor(2);
        student.setName("测试学生");
        student.setSex(1);
        student.setGrade(2022);
        //添加学生
        studentMapper.insertStudent(student);
        System.out.println(student.getId());
        if (student.getId() == -1) {
            System.out.println("学生信息添加出现错误");
            throw new TestAbortedException();
        }
        //查询学生
        List<Student> selectResult = studentMapper.selectStudent(new Student(), 0, 10);
        if (selectResult.isEmpty()) {
            System.out.println("学生信息查询出现错误");
            throw new TestAbortedException();
        }
        System.out.println(selectResult);
        //修改学生
        student.setGrade(2023);
        studentMapper.updateStudent(student);
        selectResult = studentMapper.selectStudent(student, 0, 10);
        if (selectResult.size() != 1) {
            System.out.println("学生信息查询出现错误");
            throw new TestAbortedException();
        }
        if (selectResult.getFirst().getGrade() != 2023) {
            System.out.println("学生信息修改出现错误");
            throw new TestAbortedException();
        }
        //删除学生
        studentMapper.deleteStudent(student);
        selectResult = studentMapper.selectStudent(student, 0, 10);
        if (!selectResult.isEmpty()) {
            System.out.println("学生信息删除出现错误");
        }
    }

    @Test
    public void teacherMapperTest() {
        Teacher teacher = new Teacher();
        teacher.setName("测试老师");
        teacher.setEduc(2);
        teacher.setTitle(2);
        teacher.setBirth("20230810");
        teacher.setSex(1);
        //添加老师
        teacherMapper.insertTeacher(teacher);
        System.out.println(teacher.getId());
        if (teacher.getId() == -1) {
            System.out.println("老师信息添加错误");
            throw new TestAbortedException();
        }
        //查询老师
        List<Teacher> selectResult = teacherMapper.selectTeacher(new Teacher(), 0, 10);
        if (selectResult.isEmpty()) {
            System.out.println("查询老师信息出现错误");
            throw new TestAbortedException();
        }
        System.out.println(selectResult);
        teacher.setSex(0);
        //修改老师
        teacherMapper.updateTeacher(teacher);
        selectResult = teacherMapper.selectTeacher(teacher, 0, 10);
        if (selectResult.size() != 1) {
            System.out.println("查询老师信息出现错误");
            throw new TestAbortedException();
        }
        if (selectResult.getFirst().getSex() != 0) {
            System.out.println("老师信息修改出现错误");
            throw new TestAbortedException();
        }
        //删除老师
        teacherMapper.deleteTeacher(teacher);
        selectResult = teacherMapper.selectTeacher(teacher, 0, 10);
        if (!selectResult.isEmpty()) {
            System.out.println("老师信息删除出现错误");
        }
    }

    @Test
    public void userMapperTest() {
        User user;
        user = userMapper.selectUserByName("测试用户");
        if (user == null) {
            user = new User();
        } else {
            userMapper.deleteUser(user);
        }
        user.setPassword("123456");
        user.setName("测试用户");
        userMapper.insertUser(user);
        if (user.getId() == -1) {
            System.out.println("添加用户出现错误");
            throw new TestAbortedException();
        }
        List<User> selectResult = userMapper.selectUser(new User(), 0, 10);
        if (selectResult.isEmpty()) {
            System.out.println("查询用户出现错误");
            throw new TestAbortedException();
        }
        System.out.println(selectResult);
        user.setPassword("654321");
        userMapper.updateUser(user);
        selectResult = userMapper.selectUser(user, 0, 10);
        if (selectResult.size() != 1) {
            System.out.println("查询用户失败");
            throw new TestAbortedException();
        }
        if (!selectResult.getFirst().getPassword().equals(user.getPassword())) {
            System.out.println("修改用户出现错误");
            throw new TestAbortedException();
        }
        User selectUser = userMapper.selectUserByName(user.getName());
        if (selectUser == null) {
            System.out.println("用户定位失败");
            throw new TestAbortedException();
        }
        userMapper.deleteUser(user);
        selectResult = userMapper.selectUser(user, 0, 10);
        if (!selectResult.isEmpty()) {
            System.out.println("删除用户失败");
            throw new TestAbortedException();
        }
    }

    @Test
    public void genJwtTest(){
        Map<String, Object> map = new HashMap<>();
        map.put("id", "1");
        map.put("name", "tom");
        String jwt = JwtUtils.getJwt(map);
        System.out.println(jwt);
        Map<String, Object> result = JwtUtils.parseJwt(jwt);
        System.out.println(result);
    }
}
