row_edit_data.go 1.1 KB
package command

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

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

type RowEditCommand struct {
	// 表Id
	TableId int `cname:"表Id" json:"tableId" valid:"Required"`
	// 数据列
	UpdateList []*domain.FieldValues `json:"updateList"`
	RemoveList []*domain.FieldValues `json:"removeList"`
	AddList    []*domain.FieldValues `json:"addList"`
	Where      domain.Where          `json:"where"`
	DataList   []map[string]string   `json:"dataList"`
}

func (cmd *RowEditCommand) Valid(validation *validation.Validation) {

}

func (cmd *RowEditCommand) ValidateCommand() 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
}