package entities import ( "errors" "gorm.io/gorm" "sorbet/pkg/db" "time" "unicode/utf8" ) // CompanyDepartment 公司部门表 type CompanyDepartment struct { ID uint `json:"id" xml:"id" gorm:"primaryKey;not null;comment:部门编号"` PID *uint `json:"pid" xml:"pid" gorm:"column:pid;comment:上级部门编号"` CompanyID uint `json:"company_id" xml:"company_id" gorm:"comment:所属公司编号"` PrincipalID *uint `json:"principal_id" xml:"principal_id" gorm:"comment:负责人编号(员工)"` Name string `json:"name" xml:"name" gorm:"size:50;not null;comment:部门名称"` Sort int32 `json:"sort" xml:"sort" gorm:"default:0;comment:展示排序"` Version db.Version `json:"-" xml:"-" gorm:"comment:乐观锁"` CreatedAt time.Time `json:"create_time" xml:"create_time" gorm:"<-:false;comment:创建时间"` UpdatedAt time.Time `json:"update_time" xml:"update_time" gorm:"<-:false;comment:更新时间"` DeletedAt gorm.DeletedAt `json:"-" xml:"-" gorm:"comment:删除时间"` Employees []*CompanyEmployee `json:"employees" xml:"employees" gorm:"many2many:company_employee_to_department_relations"` Courses []*FeatureContent `json:"courses" xml:"courses" gorm:"many2many:company_course_to_department_relations"` Children []*CompanyDepartment `json:"children" xml:"children" gorm:"foreignKey:PID"` } func (c *CompanyDepartment) BeforeCreate(_ *gorm.DB) error { if c.CompanyID == 0 { return errors.New("缺少所属公司编号") } if utf8.RuneCountInString(c.Name) < 6 { return errors.New("部门名称至少两个字") } return nil }