BatchAddInternalUsers batch add internal users
(eid int64, users []InternalUserInfo)
| 79 | |
| 80 | // BatchAddInternalUsers batch add internal users |
| 81 | func (s *UserService) BatchAddInternalUsers(eid int64, users []InternalUserInfo) (*BatchAddInternalUserResult, error) { |
| 82 | // Begin transaction |
| 83 | // 去重收集需要在提交后关联的平台账号(仅手机号) |
| 84 | accountsToLink := make(map[string]struct{}) |
| 85 | |
| 86 | tx := model.DB.Begin() |
| 87 | if tx.Error != nil { |
| 88 | return nil, tx.Error |
| 89 | } |
| 90 | |
| 91 | result := &BatchAddInternalUserResult{ |
| 92 | Success: []BatchAddUserResult{}, |
| 93 | Failed: []BatchAddUserResult{}, |
| 94 | } |
| 95 | |
| 96 | // Process each user |
| 97 | for _, userInfo := range users { |
| 98 | username := userInfo.Username |
| 99 | |
| 100 | // Validate username format |
| 101 | isEmail := helper.IsValidEmail(username) |
| 102 | isMobile := helper.IsValidPhone(username) |
| 103 | |
| 104 | if !isEmail && !isMobile { |
| 105 | result.Failed = append(result.Failed, BatchAddUserResult{ |
| 106 | Username: username, |
| 107 | Message: "invalid username", |
| 108 | }) |
| 109 | continue |
| 110 | } |
| 111 | |
| 112 | // Create user object |
| 113 | user := model.User{ |
| 114 | Username: username, |
| 115 | Nickname: userInfo.Nickname, |
| 116 | Password: userInfo.Password, |
| 117 | Eid: eid, |
| 118 | GroupId: 0, |
| 119 | Type: model.UserTypeInternal, |
| 120 | Role: model.RoleCommonUser, |
| 121 | } |
| 122 | |
| 123 | // Set email or mobile |
| 124 | if isMobile { |
| 125 | user.Mobile = username |
| 126 | } |
| 127 | if isEmail { |
| 128 | user.Email = username |
| 129 | } |
| 130 | |
| 131 | // Validate user struct |
| 132 | if err := common.Validate.Struct(&user); err != nil { |
| 133 | result.Failed = append(result.Failed, BatchAddUserResult{ |
| 134 | Username: username, |
| 135 | Message: err.Error(), |
| 136 | }) |
| 137 | continue |
| 138 | } |
no test coverage detected