package typeManage

import (
	"log-server/global"
	"log-server/model/common/request"
	"log-server/model/common/response"
	"log-server/model/typeManage"
	typeManageRes "log-server/model/typeManage/response"
	"log-server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type LoginTypeApi struct{}

//创建登录类型
func (l *LoginTypeApi) CreateLoginType(c *gin.Context) {
	var login typeManage.LoginType
	_ = c.ShouldBindJSON(&login)
	if err := utils.Verify(login, utils.LoginTypeVerify);err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := loginTypeService.CreateLoginType(login);err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}

}

//单个删除登录类型
func (l *LoginTypeApi) DeleteLoginType(c *gin.Context) {
	var login typeManage.LoginType
	_ = c.ShouldBindJSON(&login)
	if err := utils.Verify(login, utils.LoginTypeVerify);err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := loginTypeService.DeleteLoginType(login);err != nil{
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//批量删除
func (l *LoginTypeApi) DeleteLoginTypesByIds(c *gin.Context) {
	var ids request.IdsReq
	_ = c.ShouldBindJSON(&ids)
	if err := loginTypeService.DeleteLoginTypesByIds(ids);err != nil{
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//更改登录类型信息
func (l *LoginTypeApi) UpdateLoginType(c *gin.Context) {
	var login typeManage.LoginType
	_ = c.ShouldBindJSON(&login)
	if err := utils.Verify(login, utils.LoginTypeVerify);err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := loginTypeService.UpdateLoginType(login);err != nil{
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}

//通过id查询登录类型
func (l *LoginTypeApi) GetLoginTypeById(c *gin.Context) {
	var idInfo request.GetById
	_ = c.ShouldBindJSON(&idInfo)
	if err := utils.Verify(idInfo, utils.IdVerify);err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	loginType, err := loginTypeService.GetLoginTypeById(idInfo.ID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(&typeManageRes.LoginTypeResponse {LoginType:loginType}, "获取成功", c)
	}
}

//条件查询登录类型
func (l *LoginTypeApi) GetLoginType(c *gin.Context) {
	//传入查询信息
	var pageInfo typeManage.SearchLoginTypeParams
	_ = c.ShouldBindJSON(&pageInfo)
	//格式校验
	if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	//访问service
	list, total, err := loginTypeService.GetLoginType(pageInfo.LoginType, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		//响应结果
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
