审查视图

pkg/application/evaluation_rule/command/rule_create.go 1.4 KB
1 2 3 4 5
package command

import (
	"github.com/beego/beego/v2/core/validation"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
郑周 authored
6
	"unicode/utf8"
7 8 9
)

type CreateRuleCommand struct {
郑周 authored
10 11 12 13 14 15 16
	CompanyId int64         `cname:"公司ID" json:"companyId"`
	CreatorId int64         `cname:"创建人ID" json:"creatorId"`
	Name      string        `cname:"规则名称" json:"name" valid:"Required"`
	Remark    string        `cname:"规则备注" json:"remark"`
	Type      int           `cname:"评估方式" json:"type"`
	Rating    domain.Rating `cname:"评级" json:"rating"`
	Score     domain.Score  `cname:"评分" json:"score"`
17 18 19 20 21 22 23 24 25 26 27 28
}

func (in *CreateRuleCommand) Valid(validation *validation.Validation) {
	if in.CompanyId == 0 {
		validation.SetError("companyId", "公司ID无效")
		return
	}
	if in.CreatorId == 0 {
		validation.SetError("creatorId", "创建人ID无效")
		return
	}
郑周 authored
29
	if utf8.RuneCountInString(in.Name) > 40 {
郑周 authored
30 31 32 33
		validation.SetError("name", "名称最大长度40个字符")
		return
	}
郑周 authored
34
	if utf8.RuneCountInString(in.Remark) > 100 {
郑周 authored
35
		validation.SetError("remark", "备注不能超过100个字符")
36 37 38 39
		return
	}

	if in.Type == domain.EvaluationTypeRating {
郑周 authored
40
		if len(in.Rating.Levels) == 0 {
41 42 43
			validation.SetError("rating", "评级内容不能为空")
			return
		}
郑周 authored
44 45 46
	} else if in.Type == domain.EvaluationTypeScore {
		if len(in.Score.Levels) == 0 {
			validation.SetError("score", "评分内容不能为空")
47 48 49 50
			return
		}
	}
}