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"
	sService "go-admin/app/admin/service"
	sDto "go-admin/app/admin/service/dto"
	"strconv"

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

type OrgTeam struct {
	api.Api
}

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

	p := actions.GetPermissionFromContext(c)

	if req.GetId() == 0 {
		sOrgClubUser := service.OrgClubUser{}
		e.MakeContext(c).MakeOrm().MakeService(&sOrgClubUser.Service)

		_, object.ClubId = sOrgClubUser.GetClubId(user.GetUserId(c))

		reqClub := dto.OrgClubGetReq{}
		sClub := service.OrgClub{}
		e.MakeContext(c).MakeOrm().MakeService(&sClub.Service)
		var objectClub models.OrgClub

		reqClub.Id, _ = strconv.Atoi(object.ClubId)
		err = sClub.Get(&reqClub, p, &objectClub)

		object.ClubName = objectClub.ClubName
	} else {
		err = s.Get(&req, p, &object)
		if err != nil {
			e.Error(500, err, fmt.Sprintf("获取球队失败，\r\n失败信息 %s", err.Error()))
			return
		}
	}

	reply := dto.OrgTeamGetReply{}
	reply.ClubId = object.ClubId
	reply.ClubName = object.ClubName
	reply.TeamName = object.TeamName

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

// Insert <球队>创建球队
// @Summary <球队>创建球队
// @Description <球队>创建球队
// @Tags <球队>球队
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgTeamInsertReq true "data"
// @Success 200 {string} string  	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/org-team [post]
// @Security Bearer
func (e OrgTeam) Insert(c *gin.Context) {
	req := dto.OrgTeamInsertReq{}
	s := service.OrgTeam{}
	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
	}
	userId := user.GetUserId(c)
	// 设置创建人
	req.CreateBy = userId

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

	sSysDept := sService.SysDept{}
	reqSysDept := sDto.SysDeptInsertReq{}
	err = e.MakeContext(c).
		MakeOrm().
		MakeService(&sSysDept.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	_, reqSysDept.ParentId = s.GetParentDeptId(req.ClubId)
	_, reqSysDept.DeptName = s.GetTeamName(req.Id)
	reqSysDept.Leader = reqSysDept.DeptName
	reqSysDept.Status = 2

	// 设置创建人
	reqSysDept.CreateBy = userId
	err = sSysDept.Insert(&reqSysDept)
	if err != nil {
		e.Error(500, err, "创建失败")
		return
	}

	s.InsertTeamDept(req.Id, reqSysDept.DeptId)

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

// Update <球队>修改球队
// @Summary <球队>修改球队
// @Description <球队>修改球队
// @Tags <球队>球队
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgTeamUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-team/{id} [put]
// @Security Bearer
func (e OrgTeam) Update(c *gin.Context) {
	req := dto.OrgTeamUpdateReq{}
	s := service.OrgTeam{}
	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-team [delete]
// @Security Bearer
func (e OrgTeam) Delete(c *gin.Context) {
	s := service.OrgTeam{}
	req := dto.OrgTeamDeleteReq{}
	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(), "删除成功")
}
