package dao

// 部门
type Dept struct {
	ID          int       `gorm:"primary_key;type:int" json:"id"`      //部门id
	DeptName    string    `gorm:"type:varchar(12)" json:"dept_name"`   //部门名称
	ParentId    int       `gorm:"type:int" json:"parent_id"`           //父部门id
	IsDeleted   int       `gorm:"type:int;default:0" json:"isDeleted"` //是否删除
	Remark      string    `gorm:"type:varchar(45)" json:"remark"`      //备注
	Responsible string    `gorm:"type:varchar(25)" json:"responsible"` //负责人"
	Phone       string    `gorm:"type:varchar(25)" json:"phone"`       //联系电话"
	SysUsers    []SysUser `gorm:"-" json:"users"`                      //用户们
}

func (*Dept) TableName() string {
	return "dept"
}

func (d *Dept) GetDepts(uid int) ([]Dept, error) {
	var depts []Dept
	err := Db.Raw("SELECT * from dept where id != (SELECT dept_id  from sys_users where id = ?) and parent_id != 0 and is_deleted = 0 ", uid).Scan(&depts).Error
	return depts, err
}

func (d *Dept) GetChildDepts() (depts []Dept, err error) {
	err = Db.Model(&Dept{}).Where("parent_id != ? and is_deleted = 0", 0).Find(&depts).Error
	return depts, err
}
