package com.xhkjedu.suser.controller.gradeclass;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.suser.model.gradeclass.TClass;
import com.xhkjedu.suser.service.gradeclass.ClassService;
import com.xhkjedu.suser.vo.gradeclass.ClassTeacherSubjectVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.suser.vo.gradeclass.ClassVo;
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 javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @classname TClassController
 * @description
 * @date 2020/6/2 14:53
 **/
@RestController
@RequestMapping("/class")
public class ClassController {
    @Resource
    private ClassService classService;

    @PostMapping("/add")
    @Action("班级管理-增加")
    public ResultVo add(@RequestBody TClass tClass) {
        N_Utils.validation(new Object[]{tClass.getGradeid(), "年级id", 1});
        if(tClass.getGradeid()==-1){
            return new ResultVo(1,"年级为空");
        }

        TClass c = classService.getByName(tClass);
        if (c != null) {
            return new ResultVo(1, "班级名称不能重复");
        }
        Integer id = classService.save(tClass);
        return new ResultVo(0, "保存成功", id);
    }

    @PostMapping("/delete")
    @Action("班级管理-删除")
    public ResultVo delete(@RequestBody TClass tClass) {
        N_Utils.validation(new Object[]{tClass.getClassid(), "班级id", 1});
        return classService.deleteById(tClass.getClassid());
    }

    @PostMapping("/update")
    @Action("班级管理-修改")
    public ResultVo update(@RequestBody TClass tClass) {
        N_Utils.validation(new Object[]{tClass.getClassid(), "班级id", 1});
        TClass c = classService.getByName(tClass);
        if (c != null && !c.getClassname().equals(tClass.getClassname())) {
            return new ResultVo(1, "班级名称不能重复");
        }
        classService.update(tClass);
        return new ResultVo(0, "修改成功");
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody TClass tClass) {
        Integer schoolid = tClass.getSchoolid();
        Integer year = tClass.getYear();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1, year, "当前年份", 1});
        List<ClassVo> list = classService.findAll(tClass);
        return new ResultVo(0, "获取列表成功", list);
    }

    @PostMapping("/list_teacher")
    public ResultVo listTeacher(@RequestBody TClass tClass) {
        Integer classid = tClass.getClassid();
        Integer schoolid = tClass.getSchoolid();
        N_Utils.validation(new Object[]{classid, "班级id", 1, schoolid, "学校id", 1});
        List<ClassTeacherSubjectVo> list = classService.listTeacher(classid, schoolid);
        return new ResultVo(0, "获取任教老师列表成功", list);
    }

    //班级下教师，以及教师科目
    @PostMapping("/teachers")
    public ResultVo listTeacherSubject(@RequestBody TClass tClass) {
        Integer classid = tClass.getClassid();
        N_Utils.validation(new Object[]{classid, "班级id", 1});
        List<Map> list = classService.listTeacherSubject(classid);
        return new ResultVo(0, "获取任教老师科目列表成功", list);
    }

    /*
     * @Description 伙伴学校班级
     * @Date 2023/8/29 10:55:01
     * @Author WN
     * @Param [tClass]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_classjtc")
    public ResultVo listSchoolAndClass(@RequestBody TClass tClass) {
        Integer schoolid = tClass.getSchoolid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1});
        List<Map> list = classService.listSchoolAndClass(schoolid);
        return new ResultVo(0, "获取伙伴学校班级成功", list);
    }
}
