Browse Source

Merge remote-tracking branch 'origin/xy_data'

Roc 1 year ago
parent
commit
a8f16ac4d2
1 changed files with 66 additions and 33 deletions
  1. 66 33
      logic/xiangyu/user.go

+ 66 - 33
logic/xiangyu/user.go

@@ -27,7 +27,7 @@ func SyncUser() (err error) {
 	}
 
 	// 获取所有部门和分组
-	departmentMap, groupMap, userMap, err := getAllDepartmentAndGroup()
+	departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, err := getAllDepartmentAndGroup()
 	if err != nil {
 		return
 	}
@@ -66,7 +66,7 @@ func SyncUser() (err error) {
 		var etaUserId, message string
 
 		//  处理用户信息,将象屿用户与eta用户做关联
-		tmpErr = handleUser(userResp.Out.Data, userResp.Out.Id, departmentMap, groupMap, userMap, roleInfo)
+		tmpErr = handleUser(userResp.Out.Data, userResp.Out.Id, departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, roleInfo)
 		if tmpErr == nil {
 			isSync = true
 			if etUserInfo, ok := userMap[userResp.Out.Data.Username]; ok {
@@ -109,7 +109,7 @@ func PullUser() (err error) {
 	}
 
 	// 获取所有部门和分组
-	departmentMap, groupMap, userMap, err := getAllDepartmentAndGroup()
+	departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, err := getAllDepartmentAndGroup()
 	if err != nil {
 		return
 	}
@@ -147,7 +147,7 @@ func PullUser() (err error) {
 		var etaUserId, message string
 
 		//  处理用户信息,将象屿用户与eta用户做关联
-		tmpErr = handleUser(userResp.Out.Data, userResp.Out.Id, departmentMap, groupMap, userMap, roleInfo)
+		tmpErr = handleUser(userResp.Out.Data, userResp.Out.Id, departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, roleInfo)
 		if tmpErr == nil {
 			isSync = true
 			if etUserInfo, ok := userMap[userResp.Out.Data.Username]; ok {
@@ -183,7 +183,7 @@ func TaskPullUser() {
 // @return departmentMap map[string]*eta.SysDepartment
 // @return groupMap map[string]*eta.SysGroup
 // @return err error
-func getAllDepartmentAndGroup() (departmentMap map[string]*eta.SysDepartment, groupMap map[string]*eta.SysGroup, userMap map[string]*eta.Admin, err error) {
+func getAllDepartmentAndGroup() (departmentMap, departmentNameMap map[string]*eta.SysDepartment, groupMap, groupNameMap map[string]*eta.SysGroup, userMap map[string]*eta.Admin, err error) {
 	// 获取所有的部门
 	departmentList, err := eta.GetAllDepartment()
 	if err != nil {
@@ -192,8 +192,13 @@ func getAllDepartmentAndGroup() (departmentMap map[string]*eta.SysDepartment, gr
 
 	// 部门信息
 	departmentMap = make(map[string]*eta.SysDepartment)
+	departmentNameMap = make(map[string]*eta.SysDepartment)
 	for _, department := range departmentList {
-		departmentMap[department.OutID] = department
+		if department.OutID == `` {
+			departmentNameMap[department.DepartmentName] = department
+		} else {
+			departmentMap[department.OutID] = department
+		}
 	}
 
 	// 获取所有的分组
@@ -204,8 +209,13 @@ func getAllDepartmentAndGroup() (departmentMap map[string]*eta.SysDepartment, gr
 
 	// 部门信息
 	groupMap = make(map[string]*eta.SysGroup)
+	groupNameMap = make(map[string]*eta.SysGroup)
 	for _, group := range groupList {
-		groupMap[group.OutID] = group
+		if group.OutID == `` {
+			groupNameMap[fmt.Sprint(group.DepartmentId, "_", group.GroupName)] = group
+		} else {
+			groupMap[group.OutID] = group
+		}
 	}
 
 	// 获取所有的用户
@@ -232,22 +242,34 @@ func getAllDepartmentAndGroup() (departmentMap map[string]*eta.SysDepartment, gr
 // @param groupMap map[string]*eta.SysGroup
 // @param userMap map[string]*eta.Admin
 // @return err error
-func handleUser(xyUserInfo xiangyu.UserData, xyUserId string, departmentMap map[string]*eta.SysDepartment, groupMap map[string]*eta.SysGroup, userMap map[string]*eta.Admin, roleInfo *eta.SysRole) (err error) {
+func handleUser(xyUserInfo xiangyu.UserData, xyUserId string, departmentMap, departmentNameMap map[string]*eta.SysDepartment, groupMap, groupNameMap map[string]*eta.SysGroup, userMap map[string]*eta.Admin, roleInfo *eta.SysRole) (err error) {
 	// 判断公司id是否存在,如果不存在,那么就去创建
 	departmentInfo, ok := departmentMap[xyUserInfo.CompanyId]
 	if !ok {
-		departmentInfo = &eta.SysDepartment{
-			DepartmentId:   0,
-			DepartmentName: xyUserInfo.CompanyName,
-			OutID:          xyUserInfo.CompanyId,
-			CreateTime:     time.Now(),
-			Sort:           0,
-		}
+		// 没有外部id的部门,那么用名称去匹配
+		departmentInfo, ok = departmentNameMap[xyUserInfo.CompanyName]
+		if !ok {
+			// 如果没有的话,那么就创建该部门
+			departmentInfo = &eta.SysDepartment{
+				DepartmentId:   0,
+				DepartmentName: xyUserInfo.CompanyName,
+				OutID:          xyUserInfo.CompanyId,
+				CreateTime:     time.Now(),
+				Sort:           0,
+			}
+			err = departmentInfo.Create()
 
-		// 如果没有的话,那么就创建该部门
-		err = departmentInfo.Create()
-		if err != nil {
-			return
+			if err != nil {
+				return
+			}
+		} else {
+			// 如果通过名称找到了,那么就更新一下外部id
+			departmentInfo.OutID = xyUserInfo.CompanyId
+			err = departmentInfo.Update([]string{"OutID"})
+			if err != nil {
+				return
+			}
+			delete(departmentNameMap, xyUserInfo.CompanyName)
 		}
 
 		departmentMap[xyUserInfo.CompanyId] = departmentInfo
@@ -256,22 +278,33 @@ func handleUser(xyUserInfo xiangyu.UserData, xyUserId string, departmentMap map[
 	// 判断部门id是否存在,如果不存在,那么就去创建
 	groupInfo, ok := groupMap[xyUserInfo.DepartId]
 	if !ok {
-		groupInfo = &eta.SysGroup{
-			GroupId:      0,
-			DepartmentId: departmentInfo.DepartmentId,
-			GroupName:    xyUserInfo.DepartmentName,
-			CreateTime:   time.Now(),
-			ParentId:     0,
-			Sort:         0,
-			OutID:        xyUserInfo.DepartId,
-		}
+		groupInfo, ok = groupNameMap[fmt.Sprint(departmentInfo.DepartmentId, "_", xyUserInfo.DepartmentName)]
+		if !ok {
+			// 如果通过名称还是没有找到,那么就创建一个分组
+			groupInfo = &eta.SysGroup{
+				GroupId:      0,
+				DepartmentId: departmentInfo.DepartmentId,
+				GroupName:    xyUserInfo.DepartmentName,
+				CreateTime:   time.Now(),
+				ParentId:     0,
+				Sort:         0,
+				OutID:        xyUserInfo.DepartId,
+			}
 
-		// 如果没有的话,那么就创建该分组
-		err = groupInfo.Create()
-		if err != nil {
-			return
+			// 如果没有的话,那么就创建该分组
+			err = groupInfo.Create()
+			if err != nil {
+				return
+			}
+		} else {
+			// 如果通过名称找到了,那么就更新一下外部id
+			groupInfo.OutID = xyUserInfo.DepartId
+			err = groupInfo.Update([]string{"OutID"})
+			if err != nil {
+				return
+			}
+			delete(groupNameMap, xyUserInfo.DepartmentName)
 		}
-
 		groupMap[xyUserInfo.DepartId] = groupInfo
 	}