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.
66 lines
2.2 KiB
66 lines
2.2 KiB
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// UserOrgInsert 添加用户-机构关联
|
|
func UserOrgInsert(ctx context.Context, db *gorm.DB, uo *UserOrganization) (int64, error) {
|
|
result := db.WithContext(ctx).Create(uo)
|
|
if result.Error != nil {
|
|
return 0, result.Error
|
|
}
|
|
return uo.Id, nil
|
|
}
|
|
|
|
// UserOrgFindOne 查询单条关联
|
|
func UserOrgFindOne(ctx context.Context, db *gorm.DB, userId, orgId int64) (*UserOrganization, error) {
|
|
var uo UserOrganization
|
|
result := db.WithContext(ctx).Where("user_id = ? AND org_id = ?", userId, orgId).First(&uo)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &uo, nil
|
|
}
|
|
|
|
// UserOrgFindByUserId 获取用户的所有机构关联
|
|
func UserOrgFindByUserId(ctx context.Context, db *gorm.DB, userId int64) ([]UserOrganization, error) {
|
|
var list []UserOrganization
|
|
err := db.WithContext(ctx).Where("user_id = ?", userId).Find(&list).Error
|
|
return list, err
|
|
}
|
|
|
|
// UserOrgFindByOrgId 获取机构的所有成员关联
|
|
func UserOrgFindByOrgId(ctx context.Context, db *gorm.DB, orgId int64) ([]UserOrganization, error) {
|
|
var list []UserOrganization
|
|
err := db.WithContext(ctx).Where("org_id = ?", orgId).Find(&list).Error
|
|
return list, err
|
|
}
|
|
|
|
// UserOrgUpdate 更新关联(如改角色)
|
|
func UserOrgUpdate(ctx context.Context, db *gorm.DB, uo *UserOrganization) error {
|
|
return db.WithContext(ctx).Save(uo).Error
|
|
}
|
|
|
|
// UserOrgDelete 删除关联
|
|
func UserOrgDelete(ctx context.Context, db *gorm.DB, userId, orgId int64) error {
|
|
return db.WithContext(ctx).Where("user_id = ? AND org_id = ?", userId, orgId).Delete(&UserOrganization{}).Error
|
|
}
|
|
|
|
// UserOrgDeleteByOrgId 删除机构的所有成员关联
|
|
func UserOrgDeleteByOrgId(ctx context.Context, db *gorm.DB, orgId int64) error {
|
|
return db.WithContext(ctx).Where("org_id = ?", orgId).Delete(&UserOrganization{}).Error
|
|
}
|
|
|
|
// UserOrgCountByOrgId 统计机构成员数
|
|
func UserOrgCountByOrgId(ctx context.Context, db *gorm.DB, orgId int64) (int64, error) {
|
|
var count int64
|
|
err := db.WithContext(ctx).Model(&UserOrganization{}).Where("org_id = ?", orgId).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|