update_product_job.go 1.4 KB
package command

import (
	"fmt"
	"reflect"
	"strings"

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

type UpdateProductJobCommand struct {
	// 工位ID
	ProductJobId int `cname:"工位ID" json:"productJobId" valid:"Required"`
	// 工位名称
	JobName string `cname:"工位名称" json:"jobName" valid:"Required"`
	// 车间ID
	WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
	// 生产线ID
	LineId int `cname:"生产线ID" json:"lineId" valid:"Required"`
	// 工段ID
	SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"`
	// 关联设备列表
	RelatedDevices []int `cname:"关联设备列表" json:"relatedDevices,omitempty"`
}

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

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