// Package controller 控制器层
package controller

import (
	"strconv"

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

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

type YxHistoryMajorEnrollController struct {
	service *service.YxHistoryMajorEnrollService
}

func NewYxHistoryMajorEnrollController() *YxHistoryMajorEnrollController {
	return &YxHistoryMajorEnrollController{service: service.NewYxHistoryMajorEnrollService()}
}

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

// @Summary      获取历年招生列表
// @Tags         历年招生
// @Param        page  query  int  false  "页码"
// @Param        size  query  int  false  "每页数量"
// @Success      200  {object}  common.Response
// @Router       /yx-history-enrolls [get]
func (ctrl *YxHistoryMajorEnrollController) 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)
}

// @Summary      获取单个历年招生记录
// @Tags         历年招生
// @Param        id   path  string  true  "ID"
// @Success      200  {object}  common.Response
// @Router       /yx-history-enrolls/{id} [get]
func (ctrl *YxHistoryMajorEnrollController) GetByID(c *gin.Context) {
	item, err := ctrl.service.GetByID(c.Param("id"))
	if err != nil {
		common.Error(c, 404, "未找到")
		return
	}
	common.Success(c, item)
}

// @Summary      创建历年招生记录
// @Tags         历年招生
// @Param        item  body  entity.YxHistoryMajorEnroll  true  "历年招生信息"
// @Success      200  {object}  common.Response
// @Router       /yx-history-enrolls [post]
func (ctrl *YxHistoryMajorEnrollController) Create(c *gin.Context) {
	var item entity.YxHistoryMajorEnroll
	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)
}

// @Summary      更新历年招生记录
// @Tags         历年招生
// @Param        id    path  string  true  "ID"
// @Param        item  body  entity.YxHistoryMajorEnroll  true  "历年招生信息"
// @Success      200  {object}  common.Response
// @Router       /yx-history-enrolls/{id} [put]
func (ctrl *YxHistoryMajorEnrollController) Update(c *gin.Context) {
	var item entity.YxHistoryMajorEnroll
	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)
}

// @Summary      动态字段更新
// @Tags         历年招生
// @Param        id      path  string  true  "ID"
// @Param        fields  body  map[string]interface{}  true  "要更新的字段"
// @Success      200  {object}  common.Response
// @Router       /yx-history-enrolls/{id} [patch]
func (ctrl *YxHistoryMajorEnrollController) 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)
}

// @Summary      删除历年招生记录
// @Tags         历年招生
// @Param        id   path  string  true  "ID"
// @Success      200  {object}  common.Response
// @Router       /yx-history-enrolls/{id} [delete]
func (ctrl *YxHistoryMajorEnrollController) 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)
}

// @Summary      批量创建历年招生记录
// @Tags         历年招生
// @Param        items  body  []entity.YxHistoryMajorEnroll  true  "历年招生列表"
// @Success      200  {object}  common.Response
// @Router       /yx-history-enrolls/batch [post]
func (ctrl *YxHistoryMajorEnrollController) BatchCreate(c *gin.Context) {
	var items []entity.YxHistoryMajorEnroll
	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)
}

// @Summary      批量删除历年招生记录
// @Tags         历年招生
// @Param        ids  body  []string  true  "ID列表"
// @Success      200  {object}  common.Response
// @Router       /yx-history-enrolls/batch [delete]
func (ctrl *YxHistoryMajorEnrollController) 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)
}
