package com.pangu.student.service;

import com.pangu.common.core.exception.ServiceException;
import com.pangu.common.core.page.TableDataInfo;
import com.pangu.student.domain.dto.StudentDTO;
import com.pangu.student.domain.entity.Student;
import com.pangu.student.domain.vo.StudentVO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

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

/**
 * 学生服务测试类
 * 
 * @author pangu
 */
@SpringBootTest
@Transactional // 测试完成后回滚
public class StudentServiceTest {

    @Autowired
    private IStudentService studentService;

    @Test
    public void testInsertStudent() {
        StudentDTO dto = new StudentDTO();
        dto.setStudentName("测试学生");
        dto.setStudentNo("TEST001");
        dto.setGender("1");
        dto.setBirthday(LocalDate.of(2015, 3, 15));
        dto.setRegionId(111L);
        dto.setRegionPath("湖北省-武汉市-武昌区");
        dto.setSchoolId(1L);
        dto.setSchoolGradeId(1L);
        dto.setSchoolClassId(1L);
        dto.setMemberId(1L);

        int result = studentService.insertStudent(dto);
        assertEquals(1, result);
    }

    @Test
    public void testInsertStudentWithDuplicateNo() {
        StudentDTO dto = new StudentDTO();
        dto.setStudentName("测试学生");
        dto.setStudentNo("STU20260001"); // 已存在的学号
        dto.setGender("1");
        dto.setRegionId(111L);
        dto.setSchoolId(1L);
        dto.setSchoolGradeId(1L);
        dto.setSchoolClassId(1L);
        dto.setMemberId(1L);

        assertThrows(ServiceException.class, () -> {
            studentService.insertStudent(dto);
        });
    }

    @Test
    public void testSelectStudentList() {
        StudentDTO dto = new StudentDTO();
        dto.setPageNum(1);
        dto.setPageSize(10);

        TableDataInfo result = studentService.selectStudentList(dto);
        assertNotNull(result);
        assertTrue(result.getTotal() > 0);
    }

    @Test
    public void testGetStudentById() {
        StudentVO vo = studentService.getStudentById(1L);
        assertNotNull(vo);
        assertEquals("张小明", vo.getStudentName());
    }

    @Test
    public void testUpdateStudent() {
        StudentDTO dto = new StudentDTO();
        dto.setStudentId(1L);
        dto.setStudentName("张小明（更新）");
        dto.setStudentNo("STU20260001");
        dto.setGender("1");
        dto.setRegionId(111L);
        dto.setSchoolId(1L);
        dto.setSchoolGradeId(1L);
        dto.setSchoolClassId(1L);
        dto.setMemberId(1L);

        int result = studentService.updateStudent(dto);
        assertEquals(1, result);
    }

    @Test
    public void testDeleteStudent() {
        int result = studentService.deleteStudent(1L);
        assertEquals(1, result);
    }

    @Test
    public void testCheckStudentNoUnique() {
        boolean unique = studentService.checkStudentNoUnique("STU20260001", null);
        assertFalse(unique); // 已存在

        unique = studentService.checkStudentNoUnique("STU99999", null);
        assertTrue(unique); // 不存在
    }
}
