go项目脚手架
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sorbet/internal/entities/company_department.go

38 lines
1.8 KiB

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:"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:"delete_time" xml:"delete_time" gorm:"comment:删除时间"`
Staffs []*CompanyStaff `json:"staffs" xml:"staffs" gorm:"many2many:company_staff_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(tx *gorm.DB) error {
if c.CompanyID == 0 {
return errors.New("缺少所属公司编号")
}
if utf8.RuneCountInString(c.Name) < 6 {
return errors.New("部门名称至少两个字")
}
return nil
}