package gn import ( "eta_gn/eta_bridge/global" "eta_gn/eta_bridge/models/eta" "eta_gn/eta_bridge/models/gn" "eta_gn/eta_bridge/utils" "fmt" "time" ) // SyncUser // @Description: 全量同步用户 // @author: Roc // @datetime 2024-01-22 10:28:39 // @return err error //func SyncUser() (err error) { // roleId := global.CONFIG.Gn.DefaultRoleId // if roleId <= 0 { // err = fmt.Errorf("默认角色ID不能为空") // return // } // if global.CONFIG.Gn.DefaultUserPass == "" { // err = fmt.Errorf("默认用户密码不能为空") // return // } // // // 指定的角色 // roleInfo, e := eta.GetSysRoleById(roleId) // if e != nil { // err = fmt.Errorf("获取默认角色信息失败, %v", e) // return // } // // // 获取所有部门和分组 // departmentMap, _, groupMap, _, userMap, e := getAllDepartmentAndGroup() // if e != nil { // err = fmt.Errorf("获取部门/分组/用户失败, %v", e) // return // } // // // 从公共库里面读取部门和用户 // oaUser, e := gn.GetGnOaUser() // if e != nil { // err = fmt.Errorf("获取OA全量用户失败, %v", e) // return // } // // for _, v := range oaUser { // fmt.Printf("正在同步用户: %s\n", v.AddrName) // e = handleUser(v, departmentMap, groupMap, userMap, roleInfo) // if e != nil { // err = fmt.Errorf("同步OA用户失败, %v", e) // return // } // } // 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, e := eta.GetAllDepartment() // if e != nil { // err = fmt.Errorf("获取所有部门失败, %v", e) // 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, e := eta.GetAllSysGroup() // if e != nil { // err = fmt.Errorf("获取所有分组失败, %v", e) // 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, e := eta.GetAllSysUser() // if e != nil { // err = fmt.Errorf("获取所有用户失败, %v", e) // return // } // userMap = make(map[string]*eta.Admin) // for _, admin := range adminList { // userMap[admin.AdminName] = admin // } // return //} //func handleUser(oaUser *gn.OaUser, departmentMap map[string]*eta.SysDepartment, groupMap map[string]*eta.SysGroup, userMap map[string]*eta.Admin, roleInfo *eta.SysRole) (err error) { // if oaUser == nil { // err = fmt.Errorf("OA用户信息不存在") // return // } // // 从部门字段中拆分出部门和分组, 数据格式: 销售业务部/福建办事处/华南销售分公司, 层级不定, 目前最大3级 // oaUser.AddrDepartmentFullName = strings.TrimSpace(oaUser.AddrDepartmentFullName) // departmentArr := strings.Split(oaUser.AddrDepartmentFullName, "/") // //var departmentId, groupId int // departmentLen := len(departmentArr) // departmentInfo := new(eta.SysDepartment) // groupInfo := new(eta.SysGroup) // if departmentLen > 0 { // // 部门是最后那个 // departmentName := departmentArr[departmentLen-1] // depart, ok := departmentMap[departmentName] // if !ok { // // 如果没有的话,那么就创建该部门 // depart = &eta.SysDepartment{ // DepartmentName: departmentName, // OutID: departmentName, // 因为部门是从用户信息中取的, 没有ID这种字段, 所以部门全程作为out_id // CreateTime: time.Now(), // } // if err = depart.Create(); err != nil { // return // } // // departmentMap[departmentName] = depart // } else { // // TODO:是否更新outId // } // departmentInfo = depart // // // 分组信息 // reversed := departmentArr // for i, j := 0, departmentLen-1; i < j; i, j = i+1, j-1 { // reversed[i], reversed[j] = reversed[j], reversed[i] // } // lastOutId := departmentName // 分组的out_id格式: 部门out_id_一级分组out_id_二级分组out_id... // for k, v := range reversed { // if k == 0 { // continue // } // // lastOutId = fmt.Sprintf("%s_%s", lastOutId, v) // group, ok := groupMap[lastOutId] // if !ok { // group = &eta.SysGroup{ // DepartmentId: departmentInfo.DepartmentId, // GroupName: v, // CreateTime: time.Now(), // ParentId: groupInfo.GroupId, // OutID: lastOutId, // } // if err = group.Create(); err != nil { // return // } // // groupMap[lastOutId] = group // } else { // // TODO:是否更新outId // } // groupInfo = group // } // } // // // 同步用户 // userInfo, ok := userMap[oaUser.AddrUserId] // if !ok { // userInfo = &eta.Admin{ // AdminName: oaUser.AddrUserId, // RealName: oaUser.AddrName, // Password: utils.MD5(global.CONFIG.Gn.DefaultUserPass), // Enabled: 1, // Email: "", // CreatedTime: time.Now(), // LastUpdatedTime: time.Now(), // Mobile: oaUser.AddrMobilePhone, // 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: oaUser.AddrJobTitle, // ChartPermission: 0, // EdbPermission: 0, // MysteelChemicalPermission: 0, // OpenId: "", // UnionId: "", // PredictEdbPermission: 0, // Province: "", // ProvinceCode: "", // City: "", // CityCode: "", // EmployeeId: "", // TelAreaCode: "", // OutID: oaUser.AddrUserId, // } // if err = userInfo.Create(); err != nil { // return // } // userMap[oaUser.AddrUserId] = userInfo // } else { // updateColList := make([]string, 0) // // // 部门 // 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 != oaUser.AddrJobTitle { // updateColList = append(updateColList, "Position") // userInfo.Position = oaUser.AddrJobTitle // } // // // 姓名 // if userInfo.RealName != oaUser.AddrName { // updateColList = append(updateColList, "RealName") // userInfo.RealName = oaUser.AddrName // } // // // 手机号 // if userInfo.Mobile != oaUser.AddrMobilePhone { // updateColList = append(updateColList, "Mobile") // userInfo.Mobile = oaUser.AddrMobilePhone // } // // // 外部id // if userInfo.OutID == `` { // updateColList = append(updateColList, "OutID") // userInfo.OutID = oaUser.AddrUserId // } // // if len(updateColList) > 0 { // updateColList = append(updateColList, "LastUpdatedTime") // userInfo.LastUpdatedTime = time.Now() // if err = userInfo.Update(updateColList); err != nil { // return // } // } // } // return //} // AddAccount 创建新账号 func AddAccount(userReq gn.UserEntity) (errMsg string, err error) { adminName := *userReq.UserId // 校验是否存在, 若存在则更新信息, 否则新增 userInfo, e := eta.GetSysUserByAdminName(adminName) if e != nil && !utils.IsErrNoRow(e) { errMsg = "用户不存在" err = fmt.Errorf("获取用户是否存在失败, %v", e) return } // 更新数据 if userInfo != nil && userInfo.AdminId > 0 { if e = UpdateUser(userInfo, userReq); e != nil { errMsg = "操作失败" err = fmt.Errorf("更新用户数据失败, %v", e) return } return } // 新增数据 newUser := new(eta.Admin) newUser.AdminName = adminName newUser.RealName = fmt.Sprint(*userReq.LastName, *userReq.FirstName) newUser.Password = utils.MD5(*userReq.Password) statsMap := map[string]int{gn.UserStatusActive: 1, gn.UserStatusDisable: 0} newUser.Enabled = statsMap[*userReq.UserStatus] newUser.Email = *userReq.Mail newUser.Mobile = *userReq.Mobile newUser.Position = *userReq.TitleName // 部门分组 departmentId, groupId, departmentName, groupName, e := GetDepartmentGroupIdByMdmCode(*userReq.DepartmentNum) if e != nil { errMsg = "操作失败" err = fmt.Errorf("根据MDM编码获取ETA部门失败, %v", e) return } newUser.DepartmentId = departmentId newUser.DepartmentName = departmentName newUser.GroupId = groupId newUser.GroupName = groupName newUser.CreatedTime = time.Now() newUser.LastUpdatedTime = time.Now() newUser.OutID = *userReq.EmpNumber // 这里用工号 // 默认角色 if global.CONFIG.Gn.DefaultRoleId > 0 { roleInfo, tmpErr := eta.GetSysRoleById(global.CONFIG.Gn.DefaultRoleId) if tmpErr == nil { // 默认角色 newUser.RoleId = roleInfo.RoleId newUser.RoleName = roleInfo.RoleName newUser.RoleTypeCode = roleInfo.RoleTypeCode //newUser.RoleType = roleInfo.RoleType } } if e = newUser.Create(); e != nil { errMsg = "操作失败" err = fmt.Errorf("新增用户失败, %v", e) return } return } // UpdateUser 更新用户非null字段 func UpdateUser(originUser *eta.Admin, userReq gn.UserEntity) (err error) { if originUser == nil { err = fmt.Errorf("用户信息有误") return } updateCols := make([]string, 0) // 密码 if userReq.Password != nil { originUser.Password = utils.MD5(*userReq.Password) updateCols = append(updateCols, "Password") } // 部门分组 if userReq.DepartmentNum != nil { departmentId, groupId, departmentName, groupName, e := GetDepartmentGroupIdByMdmCode(*userReq.DepartmentNum) if e != nil { err = fmt.Errorf("根据MDM编码获取ETA部门失败, %v", e) return } originUser.DepartmentId = departmentId originUser.DepartmentName = departmentName originUser.GroupId = groupId originUser.GroupName = groupName updateCols = append(updateCols, "DepartmentId", "DepartmentName", "GroupId", "GroupName") } // 姓名 if userReq.FirstName != nil && userReq.LastName != nil { originUser.RealName = fmt.Sprint(*userReq.LastName, *userReq.FirstName) updateCols = append(updateCols, "RealName") } // 状态 if userReq.UserStatus != nil { statsMap := map[string]int{gn.UserStatusActive: 1, gn.UserStatusDisable: 0} originUser.Enabled = statsMap[*userReq.UserStatus] updateCols = append(updateCols, "Enabled") } // 联系方式 if userReq.Mail != nil { originUser.Email = *userReq.Mail updateCols = append(updateCols, "Email") } if userReq.Mobile != nil { originUser.Mobile = *userReq.Mobile updateCols = append(updateCols, "Mobile") } // 职位 if userReq.TitleName != nil { originUser.Position = *userReq.TitleName updateCols = append(updateCols, "Position") } // 更新数据 originUser.LastUpdatedTime = time.Now() updateCols = append(updateCols, "LastUpdatedTime") if e := originUser.Update(updateCols); e != nil { err = fmt.Errorf("更新用户数据失败, %v", e) return } return } // ModifyAccount 修改账号 func ModifyAccount(userReq gn.UserEntity) (errMsg string, err error) { adminName := *userReq.UserId // 校验是否存在, 不存在返回异常 userInfo, e := eta.GetSysUserByAdminName(adminName) if e != nil { if utils.IsErrNoRow(e) { errMsg = "用户不存在" err = fmt.Errorf("用户不存在") return } errMsg = "操作失败" err = fmt.Errorf("获取用户是否存在失败, %v", e) return } // 更新数据 if e = UpdateUser(userInfo, userReq); e != nil { errMsg = "操作失败" err = fmt.Errorf("更新用户数据失败, %v", e) return } return } // DeleteAccount 删除账号 func DeleteAccount(userId string) (errMsg string, err error) { // 校验是否存在, 不存在返回成功 _, e := eta.GetSysUserByAdminName(userId) if e != nil { if utils.IsErrNoRow(e) { return } errMsg = "操作失败" err = fmt.Errorf("获取用户是否存在失败, %v", e) return } // 删除用户 if e = eta.DeleteSysUserByName(userId); e != nil { errMsg = "操作失败" err = fmt.Errorf("删除用户失败, %v", e) return } return } // SuspendAccount 停用账号 func SuspendAccount(userId string) (errMsg string, err error) { // 校验是否存在, 不存在返回异常 userInfo, e := eta.GetSysUserByAdminName(userId) if e != nil { if utils.IsErrNoRow(e) { errMsg = "用户不存在" err = fmt.Errorf("用户不存在") return } errMsg = "操作失败" err = fmt.Errorf("获取用户是否存在失败, %v", e) return } userInfo.Enabled = 0 userInfo.LastUpdatedTime = time.Now() updateCols := []string{"Enabled", "LastUpdatedTime"} if e := userInfo.Update(updateCols); e != nil { errMsg = "操作失败" err = fmt.Errorf("禁用用户失败, %v", e) return } return } // RestoreAccount 启用账号 func RestoreAccount(userId string) (errMsg string, err error) { // 校验是否存在, 不存在返回异常 userInfo, e := eta.GetSysUserByAdminName(userId) if e != nil { if utils.IsErrNoRow(e) { errMsg = "用户不存在" err = fmt.Errorf("用户不存在") return } errMsg = "操作失败" err = fmt.Errorf("获取用户是否存在失败, %v", e) return } userInfo.Enabled = 1 userInfo.LastUpdatedTime = time.Now() updateCols := []string{"Enabled", "LastUpdatedTime"} if e := userInfo.Update(updateCols); e != nil { errMsg = "操作失败" err = fmt.Errorf("启用用户失败, %v", e) return } return } // ChangePassword 修改密码 func ChangePassword(userId, newPassword string) (errMsg string, err error) { // 校验是否存在, 不存在返回异常 userInfo, e := eta.GetSysUserByAdminName(userId) if e != nil { if utils.IsErrNoRow(e) { errMsg = "用户不存在" err = fmt.Errorf("用户不存在") return } errMsg = "操作失败" err = fmt.Errorf("获取用户是否存在失败, %v", e) return } userInfo.Password = utils.MD5(newPassword) userInfo.LastUpdatedTime = time.Now() userInfo.LastUpdatedPasswordTime = time.Now() updateCols := []string{"Password", "LastUpdatedTime", "LastUpdatedPasswordTime"} if e := userInfo.Update(updateCols); e != nil { errMsg = "操作失败" err = fmt.Errorf("用户密码修改失败, %v", e) return } return }