package com.xhkjedu.suser.controller.gradeclass;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.suser.model.gradeclass.TClassStudent;
import com.xhkjedu.suser.service.gradeclass.ClassStudentService;
import com.xhkjedu.suser.vo.gradeclass.ClassImportVo;
import com.xhkjedu.suser.vo.gradeclass.ClassStudentVo;
import com.xhkjedu.suser.vo.gradeclass.ClassVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ywx
 * @classname TClassStudentController
 * @description 
 * @date 2020/6/2 15:53
 **/
@RestController
@RequestMapping("/class_student")
public class ClassStudentController {
    @Resource
    private ClassStudentService classStudentService;

    @PostMapping("/batch_add")
    @Action("班级学生-批量增加")
    public ResultVo batchAdd(@RequestBody List<TClassStudent> students) {
        try {
            classStudentService.batchAdd(students);
            return new ResultVo(0, "保存成功");
        } catch (Exception e) {
            return new ResultVo(1,e.getMessage());
        }
    }

    //班级学生批量添加-取消的删除
    @PostMapping("/batch_add2")
    @Action("班级学生-批量增加")
    public ResultVo batchAdd2(@RequestBody ClassImportVo vo) {
        try {
            Integer classid = vo.getClassid();
            List<Integer> studentIds = vo.getStudentIds();
            classStudentService.batchAdd2(classid, studentIds);
            return new ResultVo(0, "保存成功");
        } catch (Exception e) {
            return new ResultVo(1,e.getMessage());
        }
    }

    @PostMapping("/add")
    @Action("班级学生-增加")
    public ResultVo add(@RequestBody TClassStudent tClassStudent) {
        Integer classid = tClassStudent.getClassid();
        N_Utils.validation(new Object[]{classid,"班级id",1});
        try {
            Integer delStuId = classStudentService.save(tClassStudent);
            return new ResultVo(0, "保存成功", delStuId);
        } catch (Exception e) {
            return new ResultVo(1,e.getMessage());
        }
    }

    @PostMapping("/delete")
    @Action("班级学生-删除")
    public ResultVo delete(@RequestBody TClassStudent tClassStudent) {
        Integer classid = tClassStudent.getClassid();
        Integer studentid = tClassStudent.getStudentid();
        N_Utils.validation(new Object[]{studentid,"学生id",1,classid,"班级id",1});
        classStudentService.deleteById(classid, studentid);
        return new ResultVo(0,"移除成功");
    }

    @PostMapping("/update")
    @Action("班级学生-修改")
    public ResultVo update(@RequestBody TClassStudent tClassStudent) {
        N_Utils.validation(new Object[]{tClassStudent.getStudentid(),"学生id",1,tClassStudent.getStudentno(),"学号",2});
        return classStudentService.update(tClassStudent);
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody TClassStudent tClassStudent) {
        Integer classid = tClassStudent.getClassid();
        N_Utils.validation(new Object[]{classid, "班级id", 1});
        Integer page = tClassStudent.getPage();
        if (N_Utils.isEmptyInteger(page)) {
            List<ClassStudentVo> list = classStudentService.findAll(tClassStudent);
            return new ResultVo(0,"获取列表成功",list);
        } else {
            Integer pageSize = tClassStudent.getPageSize();
            N_Utils.validation(new Object[]{pageSize, "显示条数", 1});
            PageHelper.startPage(page, pageSize);
            List<ClassStudentVo> list = classStudentService.findAll(tClassStudent);
            PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
            return new ResultVo(0, "获取列表成功", pageResult);
        }
    }

    @PostMapping("/import_user")
    @Action("班级学生-导入")
    public ResultVo importUser(TClassStudent tClassStudent,MultipartFile file) {
        return classStudentService.importUser(file, tClassStudent);
    }

    //提高班学生导入
    @PostMapping("/import_user2")
    @Action("班级学生-导入")
    public ResultVo importUser2(TClassStudent tClassStudent,MultipartFile file) {
        return classStudentService.importUser2(file, tClassStudent);
    }

    //学生转班
    @PostMapping("/move_class")
    @Action("班级学生-转班")
    public ResultVo moveClass(@RequestBody TClassStudent tClassStudent) {
        Integer oldclassid = tClassStudent.getOldclassid();
        Integer classid = tClassStudent.getClassid();
        Integer studentid = tClassStudent.getStudentid();
        N_Utils.validation(new Object[]{oldclassid,"原班级id",1,classid,"新班级id",1,studentid,"学生id",1});
        try {
            classStudentService.moveClass2(oldclassid, classid, studentid);
            return new ResultVo(0,"转班成功");
        } catch (Exception e) {
            return new ResultVo(1,e.getMessage());
        }
    }

    @PostMapping("/remove_bind")
    @Action("班级学生-解绑设备")
    public ResultVo moveBind(@RequestBody TClassStudent tClassStudent) {
        Integer studentid = tClassStudent.getStudentid();
        N_Utils.validation(new Object[]{studentid,"学生id",1});
        classStudentService.removeBind(studentid);
        return new ResultVo(0,"解除mac绑定成功");
    }

    /**
     *功能描述 获取学生所在班级
     * @author WN
     * @date 2020/10/20
     * @param  * @param cs
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/stu_class")
    public ResultVo stuClass(@RequestBody ClassStudentVo cs) {
        Integer studentid = cs.getStudentid();
        N_Utils.validation(new Object[]{studentid,"学生id",1});
        List<ClassVo> classlst = classStudentService.listClassByStudentId(studentid);
        return new ResultVo(0,"获取学生所在班级成功",classlst);
    }

    @PostMapping("/list_no_class_stu")
    public ResultVo listNoClassStu(@RequestBody TClassStudent student) {
        Integer page = student.getPage();
        Integer pageSize = student.getPageSize();
        Integer schoolid = student.getSchoolid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,schoolid,"学校id",1});
        if(N_Utils.isEmpty(student.getFieldname())) {
            student.setFieldname("studentname");
            student.setFieldorder("asc");
        }
        classStudentService.setOrderField(student);
        classStudentService.setYear(student);
        PageHelper.startPage(page, pageSize);
        List<ClassStudentVo> list = classStudentService.listNoClassStu(student);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取没有班级学生列表成功",pageResult);
    }

    @PostMapping("/list_grade")
    public ResultVo listGrade(@RequestBody ClassStudentVo student) {
        Integer studentid = student.getStudentid();
        N_Utils.validation(new Object[]{studentid,"学生id",1});
        List<Integer> list = classStudentService.listGrade(studentid);
        return new ResultVo(0,"获取学生所在年级列表成功",list);
    }

    //获取全校学生列表
    @PostMapping("/all")
    public ResultVo listAllSchool(@RequestBody TClassStudent student) {
        Integer page = student.getPage();
        Integer pageSize = student.getPageSize();
        Integer schoolid = student.getSchoolid();
        Integer classtype = student.getClasstype();
        N_Utils.validation(new Object[]{page, "显示页码", 1, pageSize, "显示条数", 1, schoolid, "学校id", 1, classtype, "班级类型", 1});
        classStudentService.setYear(student);
        PageHelper.startPage(page, pageSize);
        List<ClassStudentVo> list = classStudentService.listAllStudents(student);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取全校学生列表成功", pageResult);
    }

    @PostMapping("/del_tgStu")
    @Action("班级学生-删除学生提高班")
    public ResultVo delTgStu(@RequestBody ClassStudentVo student) {
        String loginname = student.getLoginname();
        N_Utils.validation(new Object[]{loginname, "学生ids", 2});
        classStudentService.delTgStu(loginname);
        return new ResultVo(0, "删除学生所有提高班和所在提高班直播课成功");
    }

    @PostMapping("/batch_remove_bind")
    @Action("班级学生-批量解绑设备")
    public ResultVo batchMoveBind(@RequestBody List<Integer> studentids) {
        if (N_Utils.isListEmpty(studentids)) return new ResultVo(1, "学生不能为空");
        classStudentService.batchRemoveBind(studentids);
        return new ResultVo(0, "批量解除mac绑定成功");
    }

    @PostMapping("/batch_delete")
    @Action("班级学生-批量移除")
    public ResultVo batchDelete(@RequestBody List<TClassStudent> students) {
        if (N_Utils.isListEmpty(students)) return new ResultVo(1, "学生不能为空");
        classStudentService.batchDelete(students);
        return new ResultVo(0, "批量移除成功");
    }

    @PostMapping("/batch_move_class")
    @Action("班级学生-批量转班")
    public ResultVo batchMoveClass(@RequestBody List<TClassStudent> students) {
        if (N_Utils.isListEmpty(students)) return new ResultVo(1, "学生不能为空");
        classStudentService.batchMoveClass(students);
        return new ResultVo(0, "批量转班成功");
    }
}
