package router

import (
	"exam_system/dao"
	"exam_system/entity"
	"exam_system/result"
	"exam_system/utils"
	"github.com/gin-gonic/gin"
	"strings"
)

func Class(router *RouterPlus) {

	r := router.Group("/admin")
	{

		// 添加班级
		r.POST("/class", AdminAddClass)

		// 获取班级
		r.GET("/class/:id", AdminGetClass)

		// 获取班级列表(通过termid)
		r.GET("/class/list", AdminClassList)

		// 修改班级
		r.PUT("/class", AdminUpdateClass)

		// 删除班级
		r.DELETE("/class/:ids", AdminDeleteClass)
	}

}

func AdminAddClass(c *gin.Context) *result.Result {
	var class entity.Class
	err := c.ShouldBindJSON(&class)
	if err != nil || class.TermId == 0 || class.Name == "" {
		return result.PARAM_ERROR
	}

	return dao.AddClass(&class)
}

func AdminGetClass(c *gin.Context) *result.Result {
	id := c.Param("id")
	if id == "" {
		return result.UNKNOW_ERROR
	}
	return dao.ClassDetail(id)
}

func AdminClassList(c *gin.Context) *result.Result {
	page, size, sort, query, err := utils.Page(c)
	if err != nil {
		return result.PARAM_ERROR
	}
	return dao.ClassList(page, size, sort, query)
}

func AdminUpdateClass(c *gin.Context) *result.Result {
	var class entity.Class
	err := c.ShouldBindJSON(&class)
	if err != nil || class.TermId == 0 || class.Name == "" {
		return result.PARAM_ERROR
	}
	return dao.UpdateClass(&class)
}

func AdminDeleteClass(c *gin.Context) *result.Result {
	idStr := c.Param("ids")
	if idStr == "" {
		return result.PARAM_ERROR
	}

	ids := strings.Split(idStr, ",")

	return dao.DeleteClasss(ids)
}
