apply_on.go 930 字节
package command

import (
	"fmt"
	"reflect"
	"strings"

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

type ApplyOnCommand struct {
	// 表Id
	TableId int `cname:"表Id" json:"tableId" valid:"Required"`
	// 模块
	Module int `json:"module"`
	// 模块
	Modules []int `json:"modules"`
}

func (c *ApplyOnCommand) Valid(validation *validation.Validation) {
	var module = 0
	for i := range c.Modules {
		module |= c.Modules[i]
	}
	c.Module = module
}

func (c *ApplyOnCommand) ValidateCommand() error {
	valid := validation.Validation{}
	b, err := valid.Valid(c)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(c).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
}