create_dictionary.go
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
}