package apis

import (
	"fmt"
	"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"

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

type OrgClub struct {
	api.Api
}

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

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

	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 = 1
	reqSysDept.DeptName = req.ClubName
	reqSysDept.Leader = req.ClubContacts
	reqSysDept.Status = 2

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

	sSysUser := sService.SysUser{}
	reqSysUser := sDto.SysUserInsertReq{}
	e.MakeContext(c).MakeOrm().MakeService(&sSysUser.Service)
	reqSysUser.Username = req.ClubUsername
	reqSysUser.Password = "123456"
	reqSysUser.NickName = req.ClubContacts
	reqSysUser.NickNameEn = req.ClubContactsEn
	reqSysUser.Phone = req.ClubMobile
	reqSysUser.RoleId = 8
	reqSysUser.Avatar = ""
	reqSysUser.Sex = "0"
	reqSysUser.Email = ""
	reqSysUser.DeptId = reqSysDept.DeptId
	reqSysUser.PostId = 6
	reqSysUser.Remark = "俱乐部"
	reqSysUser.Status = "2"
	reqSysUser.UserType = "3"

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

	//关联表添加
	reqClubUser := dto.OrgClubUserInsertReq{}
	sClubUser := service.OrgClubUser{}
	e.MakeContext(c).MakeOrm().MakeService(&sClubUser.Service)
	errClubUser := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if errClubUser != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	reqClubUser.UserId = strconv.Itoa(reqSysUser.GetId().(int))
	reqClubUser.ClubId = strconv.Itoa(req.GetId().(int))
	// 设置创建人
	reqClubUser.SetCreateBy(user.GetUserId(c))

	err = sClubUser.Insert(&reqClubUser)
	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.OrgClubUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-club/{id} [put]
// @Security Bearer
func (e OrgClub) Update(c *gin.Context) {
	req := dto.OrgClubUpdateReq{}
	s := service.OrgClub{}
	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
	}

	sOrgClubUser := service.OrgClubUser{}
	e.MakeContext(c).MakeOrm().MakeService(&sOrgClubUser.Service)
	_, userId := sOrgClubUser.GetUserId(req.GetId().(int))

	sSysUser := sService.SysUser{}
	reqSysUser := sDto.SysUserUpdateReq{}
	e.MakeContext(c).MakeOrm().MakeService(&sSysUser.Service)
	reqSysUser.Username = req.ClubUsername
	reqSysUser.NickName = req.ClubContacts
	reqSysUser.NickNameEn = req.ClubContactsEn
	reqSysUser.Phone = req.ClubMobile
	reqSysUser.UserId, _ = strconv.Atoi(userId)

	// 设置创建人
	reqSysUser.SetCreateBy(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(), "修改成功")
}

// Delete <俱乐部>删除俱乐部
// @Summary <俱乐部>删除俱乐部
// @Description <俱乐部>删除俱乐部
// @Tags <俱乐部>俱乐部
// @Param ids body []int false "ids"
// @Success 200 {string} string  	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/org-club [delete]
// @Security Bearer
func (e OrgClub) Delete(c *gin.Context) {
	s := service.OrgClub{}
	req := dto.OrgClubDeleteReq{}
	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
	}

	sOrgClubUser := service.OrgClubUser{}
	e.MakeContext(c).MakeOrm().MakeService(&sOrgClubUser.Service)
	_, userId := sOrgClubUser.GetUserIds(req.GetId().([]int))

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

	err = sSysUser.Remove(&reqSysUser, p)
	if err != nil {
		e.Logger.Error(err)
		return
	}
	e.OK(req.GetId(), "删除成功")
}
