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 OrgDivision struct {
	api.Api
}

// GetPage <赛事>获取赛区列表
// @Summary <赛事>获取赛区列表
// @Description <赛事>获取赛区列表
// @Tags <赛事>赛区
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgDivisionGetPageReq true "data"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /api/v1/org-division [get]
// @Security Bearer
func (e OrgDivision) GetPage(c *gin.Context) {
	req := dto.OrgDivisionGetPageReq{}
	s := service.OrgDivision{}
	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.OrgDivision, 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-division/{id} [get]
// @Security Bearer
func (e OrgDivision) Get(c *gin.Context) {
	req := dto.OrgDivisionGetReq{}
	s := service.OrgDivision{}
	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.OrgDivision

	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.OrgDivisionInsertReq true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Router /api/v1/org-division [post]
// @Security Bearer
func (e OrgDivision) Insert(c *gin.Context) {
	req := dto.OrgDivisionInsertReq{}
	s := service.OrgDivision{}
	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.OrgDivisionUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-division/{id} [put]
// @Security Bearer
func (e OrgDivision) Update(c *gin.Context) {
	req := dto.OrgDivisionUpdateReq{}
	s := service.OrgDivision{}
	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-division [delete]
// @Security Bearer
func (e OrgDivision) Delete(c *gin.Context) {
	s := service.OrgDivision{}
	req := dto.OrgDivisionDeleteReq{}
	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(), "删除成功")
}
