update_dictionary.go 1.2 KB
package command

import (
	"fmt"

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

type UpdateDictionaryCommand struct {
	// 字典编号 主键
	DictionaryId int64 `json:"dictionaryId" valid:"Required"`
	// 字典编码
	DictCode string `json:"dictCode" valid:"Required;MaxSize(30)"`
	// 字典名称
	DictName string `json:"dictName" valid:"Required;MaxSize(30)"`
	// 备注信息
	Describe string `json:"describe" valid:"Required;MaxSize(100)"`
	// 字典值列表
	DictItems []DictionaryItem `json:"dictItems"`
}

func (updateDictionaryCommand *UpdateDictionaryCommand) Valid(v *validation.Validation) {
	//检查items
	sortmap := make(map[int]struct{})
	for _, item := range updateDictionaryCommand.DictItems {
		if _, ok := sortmap[item.Sort]; ok {
			v.SetError("sort", "明细项顺序值重复")
		}
		sortmap[item.Sort] = struct{}{}
	}
}

func (updateDictionaryCommand *UpdateDictionaryCommand) ValidateCommand() error {
	valid := validation.Validation{}
	b, err := valid.Valid(updateDictionaryCommand)
	if err != nil {
		return err
	}
	if !b {
		for _, validErr := range valid.Errors {
			return fmt.Errorf("%s  %s", validErr.Key, validErr.Message)
		}
	}
	return nil
}