package apis

import (
	"github.com/gin-gonic/gin/binding"
	sService "go-admin/app/admin/service"
	sDto "go-admin/app/admin/service/dto"
	"net/http"
	"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 OrgUser struct {
	api.Api
}

// GetPage <赛事>人员管理列表
// @Summary <赛事>人员管理列表
// @Description 获取JSON
// @Tags <赛事>人员管理
// @Param data body dto.OrgUserGetPageReq true "data"
// @Success 200 {string} {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/org-user [get]
// @Security Bearer
func (e OrgUser) GetPage(c *gin.Context) {
	s := service.OrgUser{}
	req := dto.OrgUserGetPageReq{}
	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.OrgUserGetPageReply, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, "查询失败")
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get <赛事>获取人员详情
// @Summary <赛事>获取人员详情
// @Description 获取JSON
// @Tags <赛事>人员管理
// @Param userId path int true "用户编码"
// @Success 200 {string} string   "{"code": 200, "data": [...]}"
// @Router /api/v1/org-user/{userId} [get]
// @Security Bearer
func (e OrgUser) Get(c *gin.Context) {
	s := service.OrgUser{}
	req := dto.OrgUserGetReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object dto.OrgUserGetReply
	//数据权限检查
	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(http.StatusUnprocessableEntity, err, "查询失败")
		return
	}
	e.OK(object, "查询成功")
}

// Insert <赛事>创建人员
// @Summary <赛事>创建人员
// @Description 获取JSON
// @Tags <赛事>人员管理
// @Accept  application/json
// @Product application/json
// @Param data body dto.OrgUserInsertReq true "用户数据"
// @Success 200 {string} string   "{"code": 200, "data": [...]}"
// @Router /api/v1/org-user [post]
// @Security Bearer
func (e OrgUser) Insert(c *gin.Context) {
	s := service.OrgUser{}
	req := dto.OrgUserInsertReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		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.NickName
	reqSysUser.NickNameEn = req.NickNameEn
	reqSysUser.Phone = req.Username
	reqSysUser.RoleId, _ = strconv.Atoi(req.RoleId)
	reqSysUser.Avatar = ""
	reqSysUser.Sex = "0"
	reqSysUser.Email = ""
	reqSysUser.DeptId = 1
	reqSysUser.PostId = 1
	reqSysUser.Remark = "赛事"
	reqSysUser.Status = "2"
	reqSysUser.UserType = "2"

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

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

// Update <赛事>修改人员数据
// @Summary <赛事>修改人员数据
// @Description 获取JSON
// @Tags <赛事>人员管理
// @Accept  application/json
// @Product application/json
// @Param data body dto.OrgUserUpdateReq true "body"
// @Success 200 {string} string   "{"code": 200, "data": [...]}"
// @Router /api/v1/org-user/{userId} [put]
// @Security Bearer
func (e OrgUser) Update(c *gin.Context) {
	s := service.OrgUser{}
	req := dto.OrgUserUpdateReq{}
	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
	}

	_, isBing := s.IsBing(strconv.Itoa(req.UserId), req.Username)

	if isBing > 0 {
		e.Error(111, err, "账户已存在！请更换")
		return
	}

	req.SetUpdateBy(user.GetUserId(c))

	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Logger.Error(err)
		return
	}
	e.OK(req.GetId(), "更新成功")
}

// Delete <赛事>删除人员数据
// @Summary <赛事>删除人员数据
// @Description 删除数据
// @Tags <赛事>人员管理
// @Param userId path int true "userId"
// @Success 200 {string} string   "{"code": 200, "data": [...]}"
// @Router /api/v1/org-user/{userId} [delete]
// @Security Bearer
func (e OrgUser) Delete(c *gin.Context) {
	s := service.OrgUser{}
	req := dto.OrgUserDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// 设置编辑人
	//req. = user.GetUserId(c))

	// 数据权限检查
	p := actions.GetPermissionFromContext(c)

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

// UpdateStatus <赛事>修改人员状态
// @Summary <赛事>修改人员状态
// @Description 获取JSON
// @Tags <赛事>人员管理
// @Accept  application/json
// @Product application/json
// @Param data body dto.OrgUserUpdateStatusReq true "body"
// @Success 200 {string} string   "{"code": 200, "data": [...]}"
// @Router /api/v1/org-user/status [put]
// @Security Bearer
func (e OrgUser) UpdateStatus(c *gin.Context) {
	s := service.OrgUser{}
	req := dto.OrgUserUpdateStatusReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	reqUpdate := dto.OrgUserUpdateReq{}
	reqUpdate.UserId = req.UserId
	reqUpdate.Status = req.Status
	reqUpdate.UpdateBy = user.GetUserId(c)

	err = s.Update(&reqUpdate, p)
	if err != nil {
		e.Logger.Error(err)
		return
	}
	e.OK(1, "更新成功")
}

// ResetPwd <赛事>重置用户密码
// @Summary <赛事>重置用户密码
// @Description 获取JSON
// @Tags <赛事>人员管理
// @Accept  application/json
// @Product application/json
// @Param data body dto.ResetSysUserPwdReq true "body"
// @Success 200 {string} string   "{"code": 200, "data": [...]}"
// @Router /api/v1/org-user/pwd/reset [put]
// @Security Bearer
func (e OrgUser) ResetPwd(c *gin.Context) {
	s := service.OrgUser{}
	req := dto.ResetPwdReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		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.OrgUserUpdateReq{}
	reqUpdate.UserId = req.UserId
	reqUpdate.Password = "123456"
	reqUpdate.UpdateBy = user.GetUserId(c)

	err = s.Update(&reqUpdate, p)
	if err != nil {
		e.Logger.Error(err)
		return
	}
	e.OK("", "更新成功")
}
