This commit is contained in:
laodaming
2023-11-14 18:39:10 +08:00
parent 4aef652b20
commit 1cc0cc8d72
13 changed files with 74 additions and 388 deletions

View File

@@ -1,29 +0,0 @@
package gmodel
import (
"gorm.io/gorm"
"time"
)
// ldap_department 部门表
type LdapDepartment struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` //
Name *string `gorm:"unique_key;default:'';" json:"name"` //
Remark *string `gorm:"unique_key;default:'';" json:"remark"` //
Creator *string `gorm:"default:'';" json:"creator"` //
Type *string `gorm:"default:'';" json:"type"` //
ParentId *int64 `gorm:"default:0;" json:"parent_id"` // 层级如 10/20/30
Dn *string `gorm:"default:'';" json:"dn"` //
SyncState *int64 `gorm:"default:1;" json:"sync_state"` // 同步状态:1已同步, 2未同步
Sort *int64 `gorm:"default:999;" json:"sort"` // 排序
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` //
Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` //
}
type LdapDepartmentModel struct {
db *gorm.DB
name string
}
func NewLdapDepartmentModel(db *gorm.DB) *LdapDepartmentModel {
return &LdapDepartmentModel{db: db, name: "ldap_department"}
}

View File

@@ -1,47 +0,0 @@
package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
// 获取列表
func (d *LdapDepartmentModel) GetAll(ctx context.Context, sort string) (resp []LdapDepartment, total int64, err error) {
db := d.db.WithContext(ctx).Model(&LdapDepartment{})
if sort != "" {
db = db.Order(sort)
}
if err = db.Count(&total).Error; err != nil {
return nil, 0, err
}
err = db.Find(&resp).Error
return resp, total, err
}
func (d *LdapDepartmentModel) FindOne(ctx context.Context, id int64) (resp *LdapDepartment, err error) {
err = d.db.WithContext(ctx).Model(&LdapDepartment{}).Where("id = ?", id).Take(&resp).Error
return resp, err
}
// 更新
func (d *LdapDepartmentModel) Update(ctx context.Context, id int64, data *LdapDepartment) error {
return d.db.WithContext(ctx).Model(&LdapDepartment{}).Where("id = ?", id).Updates(&data).Error
}
// 创建
func (d *LdapDepartmentModel) Create(ctx context.Context, data *LdapDepartment) error {
return d.db.WithContext(ctx).Model(&LdapDepartment{}).Create(&data).Error
}
func (d *LdapDepartmentModel) CreateOrUpdate(ctx context.Context, id int64, data *LdapDepartment) error {
_, err := d.FindOne(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return d.Create(ctx, data)
}
return err
}
return d.Update(ctx, id, data)
}

View File

@@ -1,19 +0,0 @@
package gmodel
import (
"gorm.io/gorm"
)
// ldap_department_users 部门用户表
type LdapDepartmentUsers struct {
DepartmentId *int64 `gorm:"default:0;" json:"department_id"` //
UserId *int64 `gorm:"default:0;" json:"user_id"` //
}
type LdapDepartmentUsersModel struct {
db *gorm.DB
name string
}
func NewLdapDepartmentUsersModel(db *gorm.DB) *LdapDepartmentUsersModel {
return &LdapDepartmentUsersModel{db: db, name: "ldap_department_users"}
}

View File

@@ -1,2 +0,0 @@
package gmodel
// TODO: 使用model的属性做你想做的

View File

@@ -1,16 +1 @@
package gmodel
import "context"
// TODO: 使用model的属性做你想做的
type GetAllUserWithDepartmentRsp struct {
LdapUsers
LdapDepartmentUsers
}
func (u *LdapUsersModel) GetAllUserWithDepartment(ctx context.Context) (resp []GetAllUserWithDepartmentRsp, err error) {
err = u.db.WithContext(ctx).Table(u.name + " as u").
Joins("inner join `ldap_department_users` as du on u.id = du.user_id ").
Select("u.*,du.*").Find(&resp).Error
return resp, err
}

View File

@@ -111,8 +111,6 @@ type AllModelsGen struct {
FsZipCode *FsZipCodeModel // fs_zip_code 邮编表
LdapApis *LdapApisModel // ldap_apis api表
LdapCasbinRule *LdapCasbinRuleModel // ldap_casbin_rule 权限表
LdapDepartment *LdapDepartmentModel // ldap_department 部门表
LdapDepartmentUsers *LdapDepartmentUsersModel // ldap_department_users 部门用户表
LdapMenus *LdapMenusModel // ldap_menus 菜单表
LdapRoleMenus *LdapRoleMenusModel // ldap_role_menus 角色菜单表
LdapRoles *LdapRolesModel // ldap_roles 角色表
@@ -230,8 +228,6 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
FsZipCode: NewFsZipCodeModel(gdb),
LdapApis: NewLdapApisModel(gdb),
LdapCasbinRule: NewLdapCasbinRuleModel(gdb),
LdapDepartment: NewLdapDepartmentModel(gdb),
LdapDepartmentUsers: NewLdapDepartmentUsersModel(gdb),
LdapMenus: NewLdapMenusModel(gdb),
LdapRoleMenus: NewLdapRoleMenusModel(gdb),
LdapRoles: NewLdapRolesModel(gdb),