update_org.go 1.9 KB
package command

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
	"reflect"
	"strings"

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

type UpdateOrgCommand struct {
	OperateInfo *domain.OperateInfo `json:"-"`
	// 用户ID
	UserId int64 `cname:"用户ID" json:"userId"`
	// 组织ID
	OrgId int64 `cname:"组织ID" json:"orgId`
	// 组织编码
	OrgCode string `cname:"组织编码" json:"orgCode"`
	// 组织名称
	OrgName string `cname:"组织名称" json:"orgName"`
	// 是否是组织(是:1 不是:2)
	IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg"`
	// 父级ID
	ParentId int64 `cname:"父级ID" json:"parentId" valid:"Required"`
}

func (updateOrgCommand *UpdateOrgCommand) Valid(validation *validation.Validation) {
	//validation.SetError("CustomValid", "未实现的自定义认证")
	if len(updateOrgCommand.OrgCode) == 0 {
		validation.SetError("CustomValid", "部门编码不能为空")
		return
	}
	if len(updateOrgCommand.OrgName) == 0 {
		validation.SetError("CustomValid", "部门名称不能为空")
		return
	}
	if updateOrgCommand.ParentId == 0 {
		validation.SetError("CustomValid", "上级部门不能为空")
		return
	}
	if !(updateOrgCommand.IsOrg == domain.IsOrgFlag || updateOrgCommand.IsOrg == domain.IsNotOrgFlag) {
		validation.SetError("CustomValid", "启用状体有误")
		return
	}
}

func (updateOrgCommand *UpdateOrgCommand) ValidateCommand() error {
	valid := validation.Validation{}
	b, err := valid.Valid(updateOrgCommand)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(updateOrgCommand).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
}