package xiangyu import ( "errors" "eta/eta_bridge/global" "eta/eta_bridge/models/eta" "eta/eta_bridge/services/xiangyu" "fmt" "strings" "time" ) // SyncUser // @Description: 全量同步用户 // @author: Roc // @datetime 2024-01-22 10:28:39 // @return err error func SyncUser() (err error) { if global.CONFIG.Xiangyu.DefaultRoleId <= 0 { err = errors.New("默认角色ID不能为空") return } // 指定的角色 roleInfo, err := eta.GetSysRoleById(global.CONFIG.Xiangyu.DefaultRoleId) if err != nil { return } // 获取所有部门和分组 departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, err := getAllDepartmentAndGroup() if err != nil { return } // 获取token tokenId, err := xiangyu.Login() if err != nil { return } //tokenId := `228f7024-1966-47bf-86b5-9ec61ac149ed` defer func() { // 获取用户列表 tmpErr := xiangyu.Logout(tokenId) if tmpErr != nil { fmt.Println("退出失败:", tmpErr.Error()) } }() for { // 获取用户列表 userResp, tmpErr := xiangyu.SyncTask(tokenId) if tmpErr != nil { err = tmpErr return } // 没有数据了,那么结束同步了 if userResp == nil || userResp.Out.ObjectCode == `` { return } // 是否同步成功 var isSync bool var etaUserId, message string // 处理用户信息,将象屿用户与eta用户做关联 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 { etaUserId = fmt.Sprint(etUserInfo.AdminId) } } else { message = tmpErr.Error() } // 结束单次同步 err = xiangyu.SyncFinish(tokenId, userResp.Out.ObjectCode, userResp.Out.ObjectType, userResp.Out.Id, etaUserId, message, isSync) if err != nil { return } } return } // PullUser // @Description: 增量同步用户 // @author: Roc // @datetime 2024-01-23 18:01:27 // @return err error func PullUser() (err error) { if global.CONFIG.Xiangyu.DefaultRoleId <= 0 { err = errors.New("默认角色ID不能为空") return } defer func() { if err != nil { global.FILE_LOG.Info("同步增量用户失败:" + err.Error()) } }() // 指定的角色 roleInfo, err := eta.GetSysRoleById(global.CONFIG.Xiangyu.DefaultRoleId) if err != nil { return } // 获取所有部门和分组 departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, err := getAllDepartmentAndGroup() if err != nil { return } // 获取token tokenId, err := xiangyu.Login() if err != nil { return } defer func() { // 获取用户列表 tmpErr := xiangyu.Logout(tokenId) if tmpErr != nil { fmt.Println("退出失败:", tmpErr.Error()) } }() for { // 获取增量用户列表 userResp, tmpErr := xiangyu.PullTask(tokenId) if tmpErr != nil { err = tmpErr return } // 没有数据了,那么结束同步了 if userResp == nil || userResp.Out.ObjectCode == `` { return } // 是否同步成功 var isSync bool var etaUserId, message string // 处理用户信息,将象屿用户与eta用户做关联 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 { etaUserId = fmt.Sprint(etUserInfo.AdminId) } } else { message = tmpErr.Error() } // 结束单次增量同步 err = xiangyu.PullFinish(tokenId, userResp.Out.TaskId, etaUserId, message, isSync) if err != nil { return } } return } // TaskPullUser // @Description: 定时增量同步用户 // @author: Roc // @datetime 2024-01-23 18:03:04 func TaskPullUser() { PullUser() return } // getAllDepartmentAndGroup // @Description: 获取所有的部门和分组 // @author: Roc // @datetime 2024-01-18 17:39:14 // @return departmentMap map[string]*eta.SysDepartment // @return groupMap map[string]*eta.SysGroup // @return 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 { return } // 部门信息 departmentMap = make(map[string]*eta.SysDepartment) departmentNameMap = make(map[string]*eta.SysDepartment) for _, department := range departmentList { if department.OutID == `` { departmentNameMap[department.DepartmentName] = department } else { departmentMap[department.OutID] = department } } // 获取所有的分组 groupList, err := eta.GetAllSysGroup() if err != nil { return } // 部门信息 groupMap = make(map[string]*eta.SysGroup) groupNameMap = make(map[string]*eta.SysGroup) for _, group := range groupList { if group.OutID == `` { groupNameMap[fmt.Sprint(group.DepartmentId, "_", group.GroupName)] = group } else { groupMap[group.OutID] = group } } // 获取所有的用户 adminList, err := eta.GetAllSysUser() if err != nil { return } // 部门信息 userMap = make(map[string]*eta.Admin) for _, admin := range adminList { userMap[admin.AdminName] = admin } return } // handleUser // @Description: 用户信息处理 // @author: Roc // @datetime 2024-01-18 18:00:04 // @param xyUserInfo xiangyu.UserData // @param departmentMap map[string]*eta.SysDepartment // @param groupMap map[string]*eta.SysGroup // @param userMap map[string]*eta.Admin // @return 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 { // 没有外部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() 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 } // 判断部门id是否存在,如果不存在,那么就去创建 groupInfo, ok := groupMap[xyUserInfo.DepartId] if !ok { 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 } } else { // 如果通过名称找到了,那么就更新一下外部id groupInfo.OutID = xyUserInfo.DepartId err = groupInfo.Update([]string{"OutID"}) if err != nil { return } delete(groupNameMap, xyUserInfo.DepartmentName) } groupMap[xyUserInfo.DepartId] = groupInfo } enabled := 1 if xyUserInfo.IsDisabled { enabled = 0 } // 判断用户是否存在,如果不存在,那么就去创建 userInfo, ok := userMap[xyUserInfo.Username] if !ok { userInfo = &eta.Admin{ AdminId: 0, AdminName: xyUserInfo.Username, AdminAvatar: "", RealName: xyUserInfo.FullName, Password: xyUserInfo.Password, //LastUpdatedPasswordTime: time.Time{}, Enabled: enabled, Email: "", //LastLoginTime: time.Time{}, CreatedTime: time.Now(), LastUpdatedTime: time.Now(), //Role: roleInfo., Mobile: xyUserInfo.Mobile, RoleType: 0, RoleId: roleInfo.RoleId, RoleName: roleInfo.RoleName, RoleTypeCode: roleInfo.RoleTypeCode, DepartmentId: departmentInfo.DepartmentId, DepartmentName: departmentInfo.DepartmentName, GroupId: groupInfo.GroupId, GroupName: groupInfo.GroupName, Authority: 0, Position: xyUserInfo.PositionName, //DisableTime: time.Time{}, ChartPermission: 0, EdbPermission: 0, MysteelChemicalPermission: 0, OpenId: "", UnionId: "", PredictEdbPermission: 0, Province: "", ProvinceCode: "", City: "", CityCode: "", EmployeeId: xyUserInfo.EmployeeNo, TelAreaCode: "", OutID: xyUserId, } if strings.Contains(roleInfo.RoleTypeCode, "researcher") { userInfo.Role = "researcher" } else if strings.Contains(userInfo.RoleTypeCode, "seller") { userInfo.Role = "sales" } else { userInfo.Role = "admin" } // 如果没有的话,那么就创建该用户 err = userInfo.Create() if err != nil { return } } else { updateColList := make([]string, 0) //状态 if userInfo.Enabled != enabled { updateColList = append(updateColList, "Enabled") userInfo.Enabled = enabled } // 部门 if userInfo.DepartmentId != departmentInfo.DepartmentId { updateColList = append(updateColList, "DepartmentId", "DepartmentName") userInfo.DepartmentId = departmentInfo.DepartmentId userInfo.DepartmentName = departmentInfo.DepartmentName } // 分组 if userInfo.GroupId != groupInfo.GroupId { updateColList = append(updateColList, "GroupId", "GroupName") userInfo.GroupId = groupInfo.GroupId userInfo.GroupName = groupInfo.GroupName } // 职位 if userInfo.Position != xyUserInfo.PositionName { updateColList = append(updateColList, "Position") userInfo.Position = xyUserInfo.PositionName } // 姓名 if userInfo.RealName != xyUserInfo.FullName { updateColList = append(updateColList, "RealName") userInfo.RealName = xyUserInfo.FullName } // 手机号 if userInfo.Mobile != xyUserInfo.Mobile { updateColList = append(updateColList, "Mobile") userInfo.Mobile = xyUserInfo.Mobile } // 外部id if userInfo.OutID == `` { updateColList = append(updateColList, "OutID") userInfo.OutID = xyUserId } // 工号 if userInfo.EmployeeId != xyUserInfo.EmployeeNo { updateColList = append(updateColList, "EmployeeId") userInfo.OutID = xyUserInfo.EmployeeNo } if len(updateColList) > 0 { updateColList = append(updateColList, "LastUpdatedTime") userInfo.LastUpdatedTime = time.Now() err = userInfo.Update(updateColList) if err != nil { return } } } userMap[xyUserInfo.Username] = userInfo return }