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"
	"strconv"

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

type OrgPlayer struct {
	api.Api
}

func (e OrgPlayer) getTeamId(c *gin.Context) string {
	sOrgTeamUser := service.OrgTeamUser{}
	e.MakeContext(c).MakeOrm().MakeService(&sOrgTeamUser.Service)
	_, teamId := sOrgTeamUser.GetTeamId(user.GetUserId(c))
	return teamId
}

// GetPage <球队>获取球员列表
// @Summary <球队>获取球员列表
// @Description <球队>获取球员列表
// @Tags <球队>球员
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgPlayerGetPageReq true "body"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-player [get]
// @Security Bearer
func (e OrgPlayer) GetPage(c *gin.Context) {
	req := dto.OrgPlayerGetPageReq{}
	s := service.OrgPlayer{}
	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.OrgPlayerGetPageReply, 0)
	var count int64

	req.TeamId = e.getTeamId(c)

	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-player/{id} [get]
// @Security Bearer
func (e OrgPlayer) Get(c *gin.Context) {
	req := dto.OrgPlayerGetReq{}
	s := service.OrgPlayer{}
	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 dto.OrgPlayerGetReply

	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.OrgPlayerInsertReq true "data"
// @Success 200 {string} string  	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/org-player [post]
// @Security Bearer
func (e OrgPlayer) Insert(c *gin.Context) {
	req := dto.OrgPlayerInsertReq{}
	s := service.OrgPlayer{}
	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
	}

	reqUser := dto.OrgPlayerUserInsertReq{}
	sUser := service.OrgPlayerUser{}
	e.MakeContext(c).MakeOrm().MakeService(&sUser.Service)
	_, userId := sUser.IsBeing(req.Username)
	if userId > 0 {
		req.PlayerUserId = strconv.Itoa(userId)
	} else {
		// 设置创建人
		reqUser.SetCreateBy(user.GetUserId(c))
		reqUser.Username = req.Username
		reqUser.Password = dto.Pwd("123456")

		err = sUser.Insert(&reqUser)
		req.PlayerUserId = strconv.Itoa(reqUser.Id)
	}

	// 设置创建人
	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.OrgPlayerUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-player/{id} [put]
// @Security Bearer
func (e OrgPlayer) Update(c *gin.Context) {
	req := dto.OrgPlayerUpdateReq{}
	s := service.OrgPlayer{}
	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)

	reqUser := dto.OrgPlayerUserInsertReq{}
	sUser := service.OrgPlayerUser{}
	e.MakeContext(c).MakeOrm().MakeService(&sUser.Service)
	_, userId := sUser.IsBeing(req.Username)
	if userId > 0 {
		req.PlayerUserId = strconv.Itoa(userId)
	} else {
		// 设置创建人
		reqUser.SetCreateBy(user.GetUserId(c))
		reqUser.Username = req.Username
		reqUser.Password = dto.Pwd("123456")

		err = sUser.Insert(&reqUser)
		req.PlayerUserId = strconv.Itoa(reqUser.Id)
	}

	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-player [delete]
// @Security Bearer
func (e OrgPlayer) Delete(c *gin.Context) {
	s := service.OrgPlayer{}
	req := dto.OrgPlayerDeleteReq{}
	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(), "删除成功")
}

// Update <球队>修改球员状态
// @Summary <球队>修改球员状态
// @Description <球队>修改球员状态
// @Tags <球队>工作人员
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgPlayerUpdateStatusReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-player/update-status [put]
// @Security Bearer
func (e OrgPlayer) UpdateStatus(c *gin.Context) {
	req := dto.OrgPlayerUpdateStatusReq{}
	s := service.OrgPlayer{}
	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)

	reqUpdate := dto.OrgPlayerUpdateReq{}
	reqUpdate.Id = req.Id
	reqUpdate.Status = req.Status
	err = s.Update(&reqUpdate, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改球队人员账户信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.Id, "修改成功")
}
