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 OrgMatchEvaluate 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-match-evaluate [get]
// @Security Bearer
func (e OrgMatchEvaluate) GetPage(c *gin.Context) {
	req := dto.OrgMatchEvaluateGetPageReq{}
	s := service.OrgMatchEvaluate{}
	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([]dto.OrgMatchEvaluateGetPageReply, 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(), "查询成功")
}

// 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-match-evaluate/get-match [get]
// @Security Bearer
func (e OrgMatchEvaluate) GetPageMatch(c *gin.Context) {
	req := dto.OrgMatchEvaluateGetPageReq{}
	s := service.OrgMatchEvaluate{}
	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([]dto.OrgMatchEvaluateGetPageReply, 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-match-evaluate/{id} [get]
// @Security Bearer
func (e OrgMatchEvaluate) Get(c *gin.Context) {
	req := dto.OrgMatchEvaluateGetReq{}
	s := service.OrgMatchEvaluate{}
	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.OrgMatchEvaluate

	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, "查询成功")
}

// Update <赛事>赛后评价影像维护
// @Summary <赛事>赛后评价影像维护
// @Description <赛事>赛后评价影像维护
// @Tags <赛事>赛后评价
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgMatchEvaluateUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-match-evaluate/{id} [put]
// @Security Bearer
func (e OrgMatchEvaluate) Update(c *gin.Context) {
	req := dto.OrgMatchEvaluateUpdateReq{}
	s := service.OrgMatchEvaluate{}
	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(), "修改成功")
}

// Update <赛事>赛后评价影像维护
// @Summary <赛事>赛后评价影像维护
// @Description <赛事>赛后评价影像维护
// @Tags <赛事>赛后评价
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgMatchEvaluateUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-match-evaluate/status/{id} [put]
// @Security Bearer
func (e OrgMatchEvaluate) Status(c *gin.Context) {
	req := dto.OrgMatchEvaluateUpdateReq{}
	s := service.OrgMatchEvaluate{}
	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(), "修改成功")
}
