package apis

import (
    "fmt"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"go-admin/app/operate/models"
	"go-admin/app/operate/service"
	"go-admin/app/operate/service/dto"
	"go-admin/common/actions"
)

type OrgLeague struct {
	api.Api
}

// GetPage 获取联赛列表
// @Summary 获取联赛列表
// @Description 获取联赛列表
// @Tags 联赛
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /api/v1/org-league [get]
// @Security Bearer
func (e OrgLeague) GetPage(c *gin.Context) {
    req := dto.OrgLeagueGetPageReq{}
    s := service.OrgLeague{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
   	if err != nil {
   		e.Logger.Error(err)
   		e.Error(500, err, err.Error())
   		return
   	}

	p := actions.GetPermissionFromContext(c)
	list := make([]models.OrgLeague, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取联赛 失败，\r\n失败信息 %s", err.Error()))
        return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取联赛
// @Summary 获取联赛
// @Description 获取联赛
// @Tags 联赛
// @Param id path string false "id"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-league/{id} [get]
// @Security Bearer
func (e OrgLeague) Get(c *gin.Context) {
	req := dto.OrgLeagueGetReq{}
	s := service.OrgLeague{}
    err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object models.OrgLeague

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取联赛失败，\r\n失败信息 %s", err.Error()))
        return
	}

	e.OK( object, "查询成功")
}

// Insert 创建联赛
// @Summary 创建联赛
// @Description 创建联赛
// @Tags 联赛
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgLeagueInsertReq true "data"
// @Success 200 {string} string 	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/org-league [post]
// @Security Bearer
func (e OrgLeague) Insert(c *gin.Context) {
    req := dto.OrgLeagueInsertReq{}
    s := service.OrgLeague{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))

	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建联赛  失败，\r\n失败信息 %s", err.Error()))
        return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改联赛
// @Summary 修改联赛
// @Description 修改联赛
// @Tags 联赛
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgLeagueUpdateReq true "body"
// @Success 200 {string} string 	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-league/{id} [put]
// @Security Bearer
func (e OrgLeague) Update(c *gin.Context) {
    req := dto.OrgLeagueUpdateReq{}
    s := service.OrgLeague{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }
	req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改联赛 失败，\r\n失败信息 %s", err.Error()))
        return
	}
	e.OK( req.GetId(), "修改成功")
}

// Delete 删除联赛
// @Summary 删除联赛
// @Description 删除联赛
// @Tags 联赛
// @Param ids body []int false "ids"
// @Success 200 {string} string 	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/org-league [delete]
// @Security Bearer
func (e OrgLeague) Delete(c *gin.Context) {
    s := service.OrgLeague{}
    req := dto.OrgLeagueDeleteReq{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除联赛失败，\r\n失败信息 %s", err.Error()))
        return
	}
	e.OK( req.GetId(), "删除成功")
}