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.Class1;
import com.ruoyi.test.domain.DO.StudentListDO;
import com.ruoyi.test.domain.TeacherManageClass;
import com.ruoyi.test.seriver.IClass1Service;
import com.ruoyi.test.seriver.IStudentClassService;
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.*;

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

@Api(tags = "教师端班级管理")
@RestController
@RequestMapping("teacher/class")
public class TeacherClassController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ITeacherManageClassService iTeacherManageClassService;

    @Autowired
    private IClass1Service iClass1Service;

    @Autowired
    private IStudentClassService iStudentClassService;

    @ApiOperation("班级列表")
    @GetMapping("classList")
    public R<IPage<Class1>> classList(int pagenum){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<Class1> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            String grade = teacherManageClass.getGrade();
            String Class1 = teacherManageClass.getClass1();
            list1.add(iClass1Service.classList(grade,Class1));
        }
        Page<Class1> page = new Page<>(pagenum,10);
        IPage<Class1> page1 = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        page1.setRecords(list1);
        return R.ok(page1);
    }

    @ApiOperation("自动更新班级人数")
    @GetMapping("updateClass")
    public R<String> updateClass(Long id){
        Class1 class1 = iClass1Service.selectClass1(id);
        Integer num = iStudentClassService.selectByGradeAndClass(class1.getGrade(), class1.getClass1()).size();
        if(iClass1Service.updateClass(id,num)!=0){
            return R.ok("更新成功！");
        }
        return R.fail("更新失败！");
    }

    @ApiOperation("删除班级")
    @GetMapping("deleteClass")
    public R<String> deleteClass(Long id){
        Class1 class1 = iClass1Service.selectClass1(id);
        String grade = class1.getGrade();
        String Class1 = class1.getClass1();
        if(iTeacherManageClassService.deleteClass(grade,Class1)!=0){
            return R.ok("删除成功！");
        }
        return R.fail("删除失败！");
    }

    //创建后默认加入创建账号的班级中
    @ApiOperation("班级创建")
    @GetMapping("addclass1")
    public R<String> addclass1(String name,String grade,String Class1){
        if(iClass1Service.addclass1(name,grade,Class1)!=0){
            SysUser sysUser = SecurityUtils.getLoginUser().getUser();
            Long zhid = sysUser.getUserId();
            Long teacherid = iUserService.selectByzhid(zhid);
            iTeacherManageClassService.addclass1(teacherid,grade,Class1);
            return R.ok("创建成功！");
        }
        return R.fail("创建失败！");
    }

}
