search_file.go 1.3 KB
package query

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"reflect"
	"strings"

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

type SearchFileQuery struct {
	// 查询偏离量
	// Offset int `cname:"查询偏离量" json:"offset"`
	// 查询限制
	Limit int `cname:"查询限制" json:"limit"`
	// 页码
	// PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
	// 页数
	FileName string          `cname:"文件名称" json:"fileName,omitempty"`
	PageSize int             `cname:"页数" json:"pageSize,omitempty"`
	LastId   int             `cname:"最后一条记录ID" json:"lastId"`
	FileType domain.FileType `cname:"文件类型" json:"fileType" valid:"Required"`
	Context  *domain.Context
}

func (cmd *SearchFileQuery) Valid(validation *validation.Validation) {
	cmd.Limit = cmd.PageSize
}

func (cmd *SearchFileQuery) ValidateQuery() error {
	valid := validation.Validation{}
	b, err := valid.Valid(cmd)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(cmd).Elem()
		for _, validErr := range valid.Errors {
			field, isExist := elem.FieldByName(validErr.Field)
			if isExist {
				return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
			} else {
				return fmt.Errorf(validErr.Message)
			}
		}
	}
	return nil
}