package typeManage

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"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"
)

type AccountTypeApi struct {
}

//添加账号类型
func (a *AccountTypeApi) CreateAccountType(c *gin.Context) {
	var accountType typeManage.AccountType
	_ = c.ShouldBind(&accountType)
	if err := utils.Verify(accountType, utils.AccountTypeVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := accountTypeService.CreateAccountType(accountType); err != nil{
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

//单个删除账号类型
func (a *AccountTypeApi) DeleteAccountType(c *gin.Context) {
	var accountType typeManage.AccountType
	_ = c.ShouldBindJSON(&accountType)
	//数据校验
	if err := utils.Verify(accountType, utils.AccountTypeVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := accountTypeService.DeleteAccountType(accountType); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//批量删除账号类型
func (a *AccountTypeApi) DeleteAccountTypesByIds(c *gin.Context) {
	var ids request.IdsReq
	_ = c.ShouldBindJSON(&ids)
	if err := accountTypeService.DeleteAccountTypesByIds(ids); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//更改账号类型
func (a *AccountTypeApi) UpdateAccountType(c *gin.Context) {
	var accountType typeManage.AccountType
	_ = c.ShouldBindJSON(&accountType)
	if err := utils.Verify(accountType, utils.AccountTypeVerify); err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := accountTypeService.UpdateAccountType(accountType); err != nil{
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}

//通过id获取单条数据
func (a *AccountTypeApi) GetAccountTypeById(c *gin.Context) {
	var id request.GetById
	_ = c.ShouldBindJSON(&id)
	if err := utils.Verify(id, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if accountType, err := accountTypeService.GetAccountTypeById(id.ID); err != nil{
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(typeManageRes.AccountTypeResponse{AccountType: accountType}, "获取成功", c)
	}
}

//获取所有账号类型列表
func (a *AccountTypeApi) AccountTypeList(c *gin.Context)  {
	//获取前端传值并校验
	var pageInfo typeManage.SearchAccountTypeParams
	_ = c.ShouldBindJSON(&pageInfo)
	//页面信息校验
	if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := accountTypeService.AccountTypeList(pageInfo.AccountType, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); 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)
	}
}