package apis

import (
	"fmt"
	sService "go-admin/app/admin/service"
	sDto "go-admin/app/admin/service/dto"
	"strconv"

	"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/service"
	"go-admin/app/operate/service/dto"
	"go-admin/common/actions"
)

type OrgTeamUser struct {
	api.Api
}

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

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

	sSysUser := sService.SysUser{}
	reqSysUser := sDto.SysUserInsertReq{}
	e.MakeContext(c).MakeOrm().MakeService(&sSysUser.Service)
	reqSysUser.Username = req.Username
	reqSysUser.Password = "123456"
	reqSysUser.NickName = req.Name
	reqSysUser.NickNameEn = req.NameEn
	reqSysUser.Phone = req.Username
	reqSysUser.RoleId = 7
	reqSysUser.Avatar = ""
	reqSysUser.Sex = req.Sex
	reqSysUser.Email = ""
	_, reqSysUser.DeptId = s.GetParentDeptId(req.TeamId)
	reqSysUser.PostId = 7
	reqSysUser.Remark = "球队工作人员"
	reqSysUser.Status = "2"
	reqSysUser.UserType = "4"

	// 设置创建人
	reqSysUser.SetCreateBy(user.GetUserId(c))
	err = sSysUser.Insert(&reqSysUser)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))
	req.UserId = strconv.Itoa(reqSysUser.GetId().(int))
	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.OrgTeamUserUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-team-user/{id} [put]
// @Security Bearer
func (e OrgTeamUser) Update(c *gin.Context) {
	req := dto.OrgTeamUserUpdateReq{}
	s := service.OrgTeamUser{}
	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
	}

	sSysUser := sService.SysUser{}
	reqSysUser := sDto.SysUserUpdateReq{}
	e.MakeContext(c).MakeOrm().MakeService(&sSysUser.Service)
	reqSysUser.Username = req.UserName
	reqSysUser.NickName = req.Name
	reqSysUser.NickNameEn = req.NameEn
	reqSysUser.Phone = req.UserName
	reqSysUser.UserId, _ = strconv.Atoi(req.UserId)

	// 设置创建人
	reqSysUser.SetUpdateBy(user.GetUserId(c))
	err = sSysUser.Update(&reqSysUser, p)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

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

// Update <球队>修改工作人员
// @Summary <球队>修改工作人员
// @Description <球队>修改工作人员
// @Tags <球队>工作人员
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgTeamUserUpdateStatusReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-team-user/update-status [put]
// @Security Bearer
func (e OrgTeamUser) UpdateStatus(c *gin.Context) {
	req := dto.OrgTeamUserUpdateStatusReq{}
	s := service.OrgTeamUser{}
	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, userId := s.GetUserId(req.Id)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改球队人员账户信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	sSysUser := sService.SysUser{}
	reqSysUser := sDto.SysUserUpdateReq{}
	e.MakeContext(c).MakeOrm().MakeService(&sSysUser.Service)
	reqSysUser.Status = req.Status
	reqSysUser.UserId, _ = strconv.Atoi(userId)

	// 设置创建人
	reqSysUser.SetUpdateBy(user.GetUserId(c))
	err = sSysUser.Update(&reqSysUser, p)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

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

// Delete <球队>删除工作人员
// @Summary <球队>删除工作人员
// @Description <球队>删除工作人员
// @Tags <球队>工作人员
// @Param ids body []int false "ids"
// @Success 200 {string} string  	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/org-team-user [delete]
// @Security Bearer
func (e OrgTeamUser) Delete(c *gin.Context) {
	s := service.OrgTeamUser{}
	req := dto.OrgTeamUserDeleteReq{}
	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)

	_, userIds := s.GetUserIds(req.Ids)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除球队人员账户信息失败，\r\n失败信息 %s", err.Error()))
		return
	}

	sSysUser := sService.SysUser{}
	reqSysUser := sDto.SysUserById{}
	e.MakeContext(c).MakeOrm().MakeService(&sSysUser.Service)
	reqSysUser.Ids = userIds
	// 设置编辑人
	reqSysUser.SetUpdateBy(user.GetUserId(c))

	err = sSysUser.Remove(&reqSysUser, p)
	if err != nil {
		e.Logger.Error(err)
		return
	}

	e.OK(req.GetId(), "删除成功")
}
