package schema import ( "entgo.io/ent" "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/mixins" "git.noahlan.cn/noahlan/ntool/nrandom" ) type Department struct { ent.Schema } func (Department) Fields() []ent.Field { return []ent.Field{ field.String("name").Comment("Department name | 部门名称"), field.Int64("leader_id").Optional().Comment("Department leader | 部门负责人"), field.String("remark").Optional().Comment("Remark | 备注"), field.Int64("parent_id").Optional().Comment("Parent ID | 直接父级ID"), } } func (Department) Mixin() []ent.Mixin { return []ent.Mixin{ mixins.NewDistributedIDMixin(func() int64 { return nrandom.SnowflakeId() }), mixins.TimeMixin{}, mixins.StatusMixin{}, mixins.OptimisticLockMixin{}, } } func (Department) Edges() []ent.Edge { return []ent.Edge{ // one to many at same type (tree data structure) edge.To("children", Department.Type). From("parent"). Field("parent_id"). Unique(), // 用户从属关系 edge.From("user", User.Type). Ref("departments"). Unique(), // to one // 负责人关系 edge.To("leader", User.Type). Field("leader_id"). Unique(), } } func (Department) Indexes() []ent.Index { return nil } func (Department) Annotations() []schema.Annotation { return []schema.Annotation{ entsql.WithComments(true), entsql.Annotation{Table: "sys_department"}, schema.Comment("系统部门表"), } }