list_menu.go 1.2 KB
package query

import (
	"fmt"

	"github.com/beego/beego/v2/core/validation"
)

type ListMenuQuery struct {
	// 菜单类别 web app
	MenuCategory string `json:"menuCategory,omitempty"`
	// 菜单父级id 0:查询所有 n:parent_id为n的菜单列表
	ParentId int64 `json:"parentId,omitempty"`
	// 菜单名称过滤
	MenuName string `json:"menuName,omitempty"`
	// 结构类型 树型:tree 列表型:list
	StructType string `json:"structType,omitempty"`
	// 查询偏离量
	Offset int `json:"offset"`
	// 查询限制
	Limit int `json:"limit"`

	// web分页
	PageNumber int `json:"pageNumber"`
	PageSize   int `json:"pageSize"`
}

func (listMenuQuery *ListMenuQuery) Valid(validation *validation.Validation) {
	if listMenuQuery.PageSize > 0 {
		listMenuQuery.Limit = listMenuQuery.PageSize
		listMenuQuery.Offset = (listMenuQuery.PageNumber - 1) * listMenuQuery.PageSize
	}
}

func (listMenuQuery *ListMenuQuery) ValidateQuery() error {
	valid := validation.Validation{}
	b, err := valid.Valid(listMenuQuery)
	if err != nil {
		return err
	}
	if !b {
		for _, validErr := range valid.Errors {
			return fmt.Errorf("%s  %s", validErr.Key, validErr.Message)
		}
	}
	return nil
}