package com.ruoyi.web.controller.test.teacher;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.test.domain.DO.StudentListDO;
import com.ruoyi.test.domain.StudentClass;
import com.ruoyi.test.domain.TeacherManageClass;
import com.ruoyi.test.domain.User;
import com.ruoyi.test.seriver.IStudentClassService;
import com.ruoyi.test.seriver.IClassExamPaperService;
import com.ruoyi.test.seriver.ITeacherManageClassService;
import com.ruoyi.test.seriver.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "教师端用户管理")
@RestController
@RequestMapping("/teacher/users")
public class TeacherUserController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ITeacherManageClassService iTeacherManageClassService;

    @Autowired
    private IStudentClassService iStudentClassService;

    @Autowired
    private IClassExamPaperService iClassExamPaperService;

    @ApiOperation("学生列表")
    @GetMapping("studentList")
    public R<IPage<StudentListDO>> studentList(int pagenum){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<StudentClass> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            String grade = teacherManageClass.getGrade();
            String Class = teacherManageClass.getClass1();
            list1.addAll(iStudentClassService.selectByGradeAndClass(grade,Class));
        }
        List<StudentListDO> listDOS = new ArrayList<>();
        for(StudentClass studentClass : list1){
            StudentListDO studentListDO = new StudentListDO();
            studentListDO.setId(studentClass.getStudentid());
            studentListDO.setGrade(studentClass.getGrade());
            studentListDO.setClass1(studentClass.getClass1());
            studentListDO.setAddtime(studentClass.getAddtime());
            User user = iUserService.user(studentClass.getStudentid());
            studentListDO.setName(user.getName());
            studentListDO.setTname(user.getTname());
            studentListDO.setSex(user.getSex());
            studentListDO.setTelephone(user.getTelephone());
            listDOS.add(studentListDO);
        }
        Page<StudentListDO> page = new Page<>(pagenum,10);
        IPage<StudentListDO> page1 = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        page1.setRecords(listDOS);
        return R.ok(page1);
    }

    @ApiOperation("学生列表(用户名查询)")
    @GetMapping("studentListByName")
    public R<IPage<StudentListDO>> studentListByName(int pagenum,String name){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<StudentClass> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            String grade = teacherManageClass.getGrade();
            String Class = teacherManageClass.getClass1();
            list1.addAll(iStudentClassService.selectByGradeAndClass(grade,Class));
        }
        List<StudentListDO> listDOS = new ArrayList<>();
        for(StudentClass studentClass : list1){
            User user = iUserService.user(studentClass.getStudentid());
            if(!user.getName().equals(name)){
                continue;
            }
            StudentListDO studentListDO = new StudentListDO();
            studentListDO.setId(studentClass.getStudentid());
            studentListDO.setGrade(studentClass.getGrade());
            studentListDO.setClass1(studentClass.getClass1());
            studentListDO.setAddtime(studentClass.getAddtime());
            studentListDO.setName(user.getName());
            studentListDO.setTname(user.getTname());
            studentListDO.setSex(user.getSex());
            studentListDO.setTelephone(user.getTelephone());
            listDOS.add(studentListDO);
        }
        Page<StudentListDO> page = new Page<>(pagenum,10);
        IPage<StudentListDO> page1 = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        page1.setRecords(listDOS);
        return R.ok(page1);
    }

    @ApiOperation("学生列表(班级查询)")
    @GetMapping("studentListByClass")
    public R<IPage<StudentListDO>> studentListByClass(int pagenum,String Class){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<StudentClass> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            String grade = teacherManageClass.getGrade();
            String Class1 = teacherManageClass.getClass1();
            if(Class1.equals(Class)){
                list1.addAll(iStudentClassService.selectByGradeAndClass(grade,Class));
            }
        }
        List<StudentListDO> listDOS = new ArrayList<>();
        for(StudentClass studentClass : list1){
            User user = iUserService.user(studentClass.getStudentid());
            StudentListDO studentListDO = new StudentListDO();
            studentListDO.setId(studentClass.getStudentid());
            studentListDO.setGrade(studentClass.getGrade());
            studentListDO.setClass1(studentClass.getClass1());
            studentListDO.setAddtime(studentClass.getAddtime());
            studentListDO.setName(user.getName());
            studentListDO.setTname(user.getTname());
            studentListDO.setSex(user.getSex());
            studentListDO.setTelephone(user.getTelephone());
            listDOS.add(studentListDO);
        }
        Page<StudentListDO> page = new Page<>(pagenum,10);
        IPage<StudentListDO> page1 = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        page1.setRecords(listDOS);
        return R.ok(page1);
    }
    @ApiOperation("从学生列表删除")
    @GetMapping("deleteStudent")
    public R<String> deleteStudent(Long id){
        if(iStudentClassService.deleteStudentClass(id)!=0){
            return R.ok("删除成功！");
        }
        return R.fail("删除失败！");
    }
}
