update_workshop.go 1.2 KB
package command

import (
	"fmt"
	"reflect"
	"strings"

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

type UpdateWorkshopCommand struct {
	// 车间ID
	WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
	// 车间名称
	WorkshopName string `cname:"车间名称" json:"workshopName" valid:"Required"`
	// 负责人ID
	PrincipalId int `cname:"负责人" json:"principalId" valid:"Required"`
	// 负责人 (用户对象)
	//Principal *domain.User `json:"principal,omitempty"`
}

func (updateWorkshopCommand *UpdateWorkshopCommand) Valid(validation *validation.Validation) {
	//validation.SetError("CustomValid", "未实现的自定义认证")
}

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