Skip to content

Commit

Permalink
improve: 重构飞书通讯录同步算法
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Nov 8, 2023
1 parent 8d5182e commit b0a812f
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 197 deletions.
5 changes: 4 additions & 1 deletion internal/db/dao/User.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ncuhome/GeniusAuthoritarian/internal/db/dto"
"github.com/ncuhome/GeniusAuthoritarian/pkg/departments"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type User struct {
Expand Down Expand Up @@ -57,7 +58,9 @@ func (a *User) FirstPhoneByID(tx *gorm.DB) error {

func (a *User) GetUnscopedByPhoneSlice(tx *gorm.DB, phone []string) ([]User, error) {
var t []User
return t, tx.Model(a).Unscoped().Where("phone IN ?", phone).Find(&t).Error
return t, tx.Model(a).Unscoped().Where("phone IN ?", phone).
Clauses(clause.OrderBy{Expression: clause.Expr{SQL: "FIELD(phone,?)", Vars: []interface{}{phone}, WithoutParentheses: true}}).
Find(&t).Error
}

func (a *User) GetNotInPhoneSlice(tx *gorm.DB, phone []string) ([]User, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/db/dao/UserGroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func (a *UserGroups) GetUserGroupsLimited(tx *gorm.DB, groups []string) ([]BaseG
Find(&t).Error
}

func (a *UserGroups) GetAllUnfrozen(tx *gorm.DB) ([]UserGroups, error) {
func (a *UserGroups) GetAllNotFrozen(tx *gorm.DB) ([]UserGroups, error) {
var t []UserGroups
tx = tx.Model(a)
tx = a.sqlJoinUsers(tx)
return t, tx.Find(&t).Error
return t, tx.Order("uid,gid").Find(&t).Error
}

func (a *UserGroups) DeleteByIDSlice(tx *gorm.DB, ids []uint) error {
Expand Down
33 changes: 26 additions & 7 deletions internal/pkg/feishu/User.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
package feishu

import (
"github.com/ncuhome/GeniusAuthoritarian/internal/db/dao"
"github.com/ncuhome/GeniusAuthoritarian/pkg/feishuApi"
)

type UserSync interface {
IsInvalid() bool
Departments(groupMap map[string]uint) []uint
Model() *dao.User
}

func NewUser(data *feishuApi.User) *User {
return &User{
data: data,
Data: data,
}
}

type User struct {
data *feishuApi.User
departments []uint
ID uint // 辅助字段,初始为空
Data *feishuApi.User
}

func (u User) Model() dao.User {
return dao.User{
Name: u.Data.Name,
Phone: u.Data.Mobile,
}
}

func (u User) IsInvalid() bool {
return !u.data.Status.IsActivated || u.data.Status.IsFrozen || u.data.Status.IsResigned || u.data.Mobile == "" || // 账号未异常
u.data.EmployeeType != 1 // 仅允许正式员工状态账号
return !u.Data.Status.IsActivated || u.Data.Status.IsFrozen || u.Data.Status.IsResigned || u.Data.Mobile == "" || // 账号未异常
u.Data.EmployeeType != 1 // 仅允许正式员工状态账号
}

func (u User) Departments(groupMap map[string]uint) []uint {
var departments = make([]uint, len(u.data.DepartmentIds))
var departments = make([]uint, len(u.Data.DepartmentIds))
var validLength int
for _, groupOpenID := range u.data.DepartmentIds {
for _, groupOpenID := range u.Data.DepartmentIds {
id, ok := groupMap[groupOpenID]
if !ok {
continue
Expand All @@ -38,3 +47,13 @@ func (u User) Departments(groupMap map[string]uint) []uint {
}
return departments[:validLength]
}

func (u User) DepartmentModels(uid uint, groupMap map[string]uint) []dao.UserGroups {
departments := u.Departments(groupMap)
models := make([]dao.UserGroups, len(departments))
for i, gid := range departments {
models[i].UID = uid
models[i].GID = gid
}
return models
}
Loading

0 comments on commit b0a812f

Please sign in to comment.