create_dictionary.go 1.7 KB
package command

import (
	"fmt"

	"github.com/beego/beego/v2/core/validation"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
)

type DictionaryItem struct {
	// 项编码
	ItemCode string `json:"itemCode" valid:"Required;MaxSize(30)"`
	// 项标签
	ItemLabel string `json:"itemLabel" valid:"Required;MaxSize(30)"`
	// 值
	ItemValue string `json:"itemValue" valid:"Required;MaxSize(30)"`
	// 是否可见【1:不可以】【2:可以】
	IsShow int `json:"isShow"`
	// 显示序号
	Sort int `json:"sort"`
}

type CreateDictionaryCommand struct {
	// 字典编码
	DictCode string `json:"dictCode" valid:"Required;MaxSize(30)"`
	// 字典名称
	DictName string `json:"dictName" valid:"Required;MaxSize(30)"`
	// 备注信息
	Desc string `json:"desc" valid:"MaxSize(100)"`
	// 字典值列表
	DictItems []DictionaryItem `json:"dictItems"`
}

// 实现了接口 validation.ValidFormer
func (createDictionaryCommand *CreateDictionaryCommand) Valid(v *validation.Validation) {
	//检查items
	sortmap := make(map[int]struct{})
	for _, item := range createDictionaryCommand.DictItems {
		if _, ok := sortmap[item.Sort]; ok {
			v.SetError("Sort", "明细项顺序值重复")
		}
		sortmap[item.Sort] = struct{}{}
		vOk := domain.DictionaryItemShow(item.IsShow).Valid()
		if !vOk {
			v.SetError("IsShow", "明细项是否显示值异常")
		}
	}
}

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