44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package schema
|
|
|
|
import (
|
|
"time"
|
|
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/dialect/entsql"
|
|
"entgo.io/ent/schema"
|
|
"entgo.io/ent/schema/edge"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
type Department struct {
|
|
ent.Schema
|
|
}
|
|
|
|
func (Department) Annotations() []schema.Annotation {
|
|
return []schema.Annotation{
|
|
entsql.Annotation{
|
|
Table: "department",
|
|
Options: "COMMENT='部门表,用于存储组织架构中的部门信息'",
|
|
},
|
|
entsql.WithComments(true),
|
|
}
|
|
}
|
|
|
|
func (Department) Fields() []ent.Field {
|
|
return []ent.Field{
|
|
field.Int("id").Positive().Comment("部门ID"),
|
|
field.String("name").MaxLen(20).NotEmpty().Unique().Comment("部门名称,唯一且不超过20个字符"),
|
|
field.Int("parentId").Optional().Comment("父部门ID,用于构建部门层级树"),
|
|
field.Int("order").Optional().Positive().Comment("同级排序序号,正数,越小越靠前"),
|
|
field.Time("createdAt").Default(time.Now).Immutable().Comment("创建时间,记录不可变"),
|
|
field.Time("updatedAt").Default(time.Now).UpdateDefault(time.Now).Comment("更新时间,每次修改自动更新"),
|
|
}
|
|
}
|
|
|
|
func (Department) Edges() []ent.Edge {
|
|
return []ent.Edge{
|
|
edge.To("children", Department.Type).From("parent").Field("parentId").Unique(),
|
|
edge.To("users", User.Type),
|
|
}
|
|
}
|