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

import com.baomidou.mybatisplus.core.metadata.IPage;
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.User;
import com.ruoyi.test.domain.Vo.UserVo;
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.List;

@Api(tags = "管理端用户管理")
@RestController
@RequestMapping("/gl/users")
public class UserController {
    @Autowired
    private IUserService iUserService;

    /*
        获取学生列表
     */
    @ApiOperation("学生列表")
    @GetMapping("student")
    public R<IPage<User>> selectList(int pagenum){
        return R.ok(iUserService.selectStudentList(pagenum));
    }
    /*获取学生用户名查询查询记录
     *@param name 用户名
     */
    @ApiOperation("学生用户名查询")
    @GetMapping("/seleteStudentName")
    public R<IPage<User>> selectStudent(int pagenum,String name){
        return R.ok(iUserService.selectStudent(pagenum,name));
    }

    /*增加学生用户信息
     *@param name 用户名
     */
    @ApiOperation("学生添加用户")
    @PostMapping("/insertStudent")
    public R<String> inserStudent(@RequestBody UserVo userVo){
        User user = new User();
        user.setAge(userVo.getAge());
        user.setBirthday(userVo.getBirthday());
        user.setGrade(userVo.getGrade());
        user.setSex(userVo.getSex());
        user.setName(userVo.getName());
        user.setTname(userVo.getTname());
        user.setTelephone(userVo.getTelephone());
        user.setRoleKey("2");
        user.setState(0L);
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        user.setZhId(zhid);
        List<User> list = iUserService.selectStudentByZhid(zhid);
        if(list.size()==0){
            if(iUserService.insertUser(user)!=0){
                return  R.ok("添加成功！");
            }else{
                return R.fail("添加失败！");
            }
        }
        return R.fail("该账号已绑定用户了！");
    }



    @ApiOperation("教师列表")
    @GetMapping("/teacher")
    public R<IPage<User>> selectTeacher(int pagenum){
        return R.ok(iUserService.selectTeacherList(pagenum));
    }
    /*获取教师用户名查询查询记录
     *@param name 用户名
     */
    @ApiOperation("教师用户名查询")
    @GetMapping("/seleteTeacherName")
    public R<IPage<User>> selectTeacher(int pagenum,String name){
        return R.ok(iUserService.selectTeacher(pagenum,name));
    }
    /*增加教师用户信息
     *@param name 用户名
     */
    @ApiOperation("老师添加用户")
    @PostMapping("/insertTeacher")
    public R<String> insertTeacher(@RequestBody  UserVo userVo){
        User user = new User();
        user.setAge(userVo.getAge());
        user.setBirthday(userVo.getBirthday());
        user.setGrade(userVo.getGrade());
        user.setSex(userVo.getSex());
        user.setName(userVo.getName());
        user.setTname(userVo.getTname());
        user.setTelephone(userVo.getTelephone());
        user.setRoleKey("3");
        user.setState(0L);
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        user.setZhId(zhid);
        List<User> list = iUserService.selectStudentByZhid(zhid);
        if(list.size()==0){
            if(iUserService.insertUser(user)!=0){
                return  R.ok("添加成功！");
            }else{
                return R.fail("添加失败！");
            }
        }
        return R.fail("该账号已绑定用户了！");
    }

    @ApiOperation("管理员列表")
    @GetMapping("/Gl")
    public R<IPage<User>> selectGl(int pagenum){
        return R.ok(iUserService.selectGlList(pagenum));
    }
    /*获取管理员用户名查询查询记录
     *@param name 用户名
     */
    @ApiOperation("管理员用户名查询")
    @GetMapping("/seleteGlName")
    public R<IPage<User>> selectGl(int pagenum,String name){
        return R.ok(iUserService.selectGl(pagenum,name));
    }
    /*增加管理员用户信息
     *@param name 用户名
     */
    @ApiOperation("管理员添加用户")
    @PostMapping("/insertGl")
    public R<String> insertGl(@RequestBody  UserVo userVo){
        User user = new User();
        user.setAge(userVo.getAge());
        user.setBirthday(userVo.getBirthday());
        user.setGrade(userVo.getGrade());
        user.setSex(userVo.getSex());
        user.setName(userVo.getName());
        user.setTname(userVo.getTname());
        user.setTelephone(userVo.getTelephone());
        user.setRoleKey("4");
        user.setState(0L);
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        user.setZhId(zhid);
        List<User> list = iUserService.selectStudentByZhid(zhid);
        if(list.size()==0){
            if(iUserService.insertUser(user)!=0){
                return  R.ok("添加成功！");
            }else{
                return R.fail("添加失败！");
            }
        }
        return R.fail("该账号已绑定用户了！");
    }
}
