create_company.go 1.5 KB
package command

import (
	"fmt"
	"reflect"
	"strings"
	"time"

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

type CreateCompanyCommand struct {
	// 企业名称
	CompanyName string `cname:"企业名称" json:"companyName" valid:"Required"`
	// 规模
	Scale string `cname:"规模" json:"scale" valid:"Required"`
	// 公司Logo地址
	Logo string `cname:"公司Logo地址" json:"logo" valid:"Required"`
	// 公司地址
	Address string `cname:"公司地址" json:"address" valid:"Required"`
	// 所属行业
	IndustryCategory string `cname:"所属行业" json:"industryCategory" valid:"Required"`
	// 联系人
	Contacts string `cname:"联系人" json:"contacts" valid:"Required"`
	// 注册时间
	RegisteredTime time.Time `cname:"注册时间" json:"registeredTime,omitempty"`
	// 注册状态  1:已注册 2:待认证 3:已认证
	Status int `cname:"注册状态  1:已注册 2:待认证 3:已认证" json:"status,omitempty"`
}

func (createCompanyCommand *CreateCompanyCommand) Valid(validation *validation.Validation) {

}

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