// Package controller 控制器层
package controller

import (
	"strconv"

	"server/common"
	"server/modules/yx/entity"
	"server/modules/yx/service"

	"github.com/gin-gonic/gin"
)

type YxSchoolMajorController struct {
	service *service.YxSchoolMajorService
}

func NewYxSchoolMajorController() *YxSchoolMajorController {
	return &YxSchoolMajorController{service: service.NewYxSchoolMajorService()}
}

func (ctrl *YxSchoolMajorController) RegisterRoutes(r *gin.RouterGroup) {
	r.GET("/yx-school-majors", ctrl.List)
	r.GET("/yx-school-majors/:id", ctrl.GetByID)
	r.POST("/yx-school-majors", ctrl.Create)
	r.PUT("/yx-school-majors/:id", ctrl.Update)
	r.PATCH("/yx-school-majors/:id", ctrl.UpdateFields)
	r.DELETE("/yx-school-majors/:id", ctrl.Delete)
	r.POST("/yx-school-majors/batch", ctrl.BatchCreate)
	r.DELETE("/yx-school-majors/batch", ctrl.BatchDelete)
}

// List 获取院校专业列表
// @Summary      获取院校专业列表
// @Tags         院校专业
// @Param        page  query  int  false  "页码"
// @Param        size  query  int  false  "每页数量"
// @Success      200  {object}  common.Response
// @Router       /yx-school-majors [get]
func (ctrl *YxSchoolMajorController) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	items, total, err := ctrl.service.List(page, size)
	if err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.SuccessPage(c, items, total, page, size)
}

// GetByID 获取单个院校专业
// @Summary      获取单个院校专业
// @Tags         院校专业
// @Param        id   path  string  true  "ID"
// @Success      200  {object}  common.Response
// @Router       /yx-school-majors/{id} [get]
func (ctrl *YxSchoolMajorController) GetByID(c *gin.Context) {
	item, err := ctrl.service.GetByID(c.Param("id"))
	if err != nil {
		common.Error(c, 404, "未找到")
		return
	}
	common.Success(c, item)
}

// Create 创建院校专业
// @Summary      创建院校专业
// @Tags         院校专业
// @Param        item  body  entity.YxSchoolMajor  true  "院校专业信息"
// @Success      200  {object}  common.Response
// @Router       /yx-school-majors [post]
func (ctrl *YxSchoolMajorController) Create(c *gin.Context) {
	var item entity.YxSchoolMajor
	if err := c.ShouldBindJSON(&item); err != nil {
		common.Error(c, 400, "参数错误")
		return
	}
	if err := ctrl.service.Create(&item); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, item)
}

// Update 更新院校专业
// @Summary      更新院校专业
// @Tags         院校专业
// @Param        id    path  string  true  "ID"
// @Param        item  body  entity.YxSchoolMajor  true  "院校专业信息"
// @Success      200  {object}  common.Response
// @Router       /yx-school-majors/{id} [put]
func (ctrl *YxSchoolMajorController) Update(c *gin.Context) {
	var item entity.YxSchoolMajor
	if err := c.ShouldBindJSON(&item); err != nil {
		common.Error(c, 400, "参数错误")
		return
	}
	item.ID = c.Param("id")
	if err := ctrl.service.Update(&item); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, item)
}

// UpdateFields 动态字段更新
// @Summary      动态字段更新
// @Tags         院校专业
// @Param        id      path  string  true  "ID"
// @Param        fields  body  map[string]interface{}  true  "要更新的字段"
// @Success      200  {object}  common.Response
// @Router       /yx-school-majors/{id} [patch]
func (ctrl *YxSchoolMajorController) UpdateFields(c *gin.Context) {
	var fields map[string]interface{}
	if err := c.ShouldBindJSON(&fields); err != nil {
		common.Error(c, 400, "参数错误")
		return
	}
	if err := ctrl.service.UpdateFields(c.Param("id"), fields); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, nil)
}

// Delete 删除院校专业
// @Summary      删除院校专业
// @Tags         院校专业
// @Param        id   path  string  true  "ID"
// @Success      200  {object}  common.Response
// @Router       /yx-school-majors/{id} [delete]
func (ctrl *YxSchoolMajorController) Delete(c *gin.Context) {
	if err := ctrl.service.Delete(c.Param("id")); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, nil)
}

// BatchCreate 批量创建
// @Summary      批量创建院校专业
// @Tags         院校专业
// @Param        items  body  []entity.YxSchoolMajor  true  "院校专业列表"
// @Success      200  {object}  common.Response
// @Router       /yx-school-majors/batch [post]
func (ctrl *YxSchoolMajorController) BatchCreate(c *gin.Context) {
	var items []entity.YxSchoolMajor
	if err := c.ShouldBindJSON(&items); err != nil {
		common.Error(c, 400, "参数错误")
		return
	}
	if err := ctrl.service.BatchCreate(items); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, nil)
}

// BatchDelete 批量删除
// @Summary      批量删除院校专业
// @Tags         院校专业
// @Param        ids  body  []string  true  "ID列表"
// @Success      200  {object}  common.Response
// @Router       /yx-school-majors/batch [delete]
func (ctrl *YxSchoolMajorController) BatchDelete(c *gin.Context) {
	var ids []string
	if err := c.ShouldBindJSON(&ids); err != nil {
		common.Error(c, 400, "参数错误")
		return
	}
	if err := ctrl.service.BatchDelete(ids); err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, nil)
}
