|
1 | 1 | from module_admin.entity.vo.user_vo import * |
2 | 2 | from module_admin.dao.user_dao import * |
3 | 3 | from utils.pwd_util import * |
4 | | -from utils.common_util import export_list2excel |
| 4 | +from utils.common_util import * |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class UserService: |
@@ -173,6 +173,98 @@ def reset_user_services(cls, result_db: Session, page_object: ResetUserModel): |
173 | 173 |
|
174 | 174 | return CrudUserResponse(**result) |
175 | 175 |
|
| 176 | + @classmethod |
| 177 | + def batch_import_user_services(cls, result_db: Session, user_import: ImportUserModel, current_user: CurrentUserInfoServiceResponse): |
| 178 | + """ |
| 179 | + 批量导入用户service |
| 180 | + :param user_import: 用户导入参数对象 |
| 181 | + :param result_db: orm对象 |
| 182 | + :param current_user: 当前用户对象 |
| 183 | + :return: 批量导入用户结果 |
| 184 | + """ |
| 185 | + header_dict = { |
| 186 | + "部门编号": "dept_id", |
| 187 | + "登录名称": "user_name", |
| 188 | + "用户名称": "nick_name", |
| 189 | + "用户邮箱": "email", |
| 190 | + "手机号码": "phonenumber", |
| 191 | + "用户性别": "sex", |
| 192 | + "帐号状态": "status" |
| 193 | + } |
| 194 | + filepath = get_filepath_from_url(user_import.url) |
| 195 | + df = pd.read_excel(filepath) |
| 196 | + df.rename(columns=header_dict, inplace=True) |
| 197 | + add_error_result = [] |
| 198 | + count = 0 |
| 199 | + try: |
| 200 | + for index, row in df.iterrows(): |
| 201 | + count = count + 1 |
| 202 | + if row['sex'] == '男': |
| 203 | + row['sex'] = '0' |
| 204 | + if row['sex'] == '女': |
| 205 | + row['sex'] = '1' |
| 206 | + if row['sex'] == '未知': |
| 207 | + row['sex'] = '2' |
| 208 | + if row['status'] == '正常': |
| 209 | + row['status'] = '0' |
| 210 | + if row['status'] == '停用': |
| 211 | + row['status'] = '1' |
| 212 | + add_user = UserModel( |
| 213 | + **dict( |
| 214 | + dept_id=row['dept_id'], |
| 215 | + user_name=row['user_name'], |
| 216 | + password=PwdUtil.get_password_hash('123456'), |
| 217 | + nick_name=row['nick_name'], |
| 218 | + email=row['email'], |
| 219 | + phonenumber=row['phonenumber'], |
| 220 | + sex=row['sex'], |
| 221 | + status=row['status'], |
| 222 | + create_by=current_user.user.user_name, |
| 223 | + update_by=current_user.user.user_name |
| 224 | + ) |
| 225 | + ) |
| 226 | + user_info = UserDao.get_user_by_info(result_db, UserModel(**dict(user_name=row['user_name']))) |
| 227 | + if user_info: |
| 228 | + if user_import.is_update: |
| 229 | + edit_user = UserModel( |
| 230 | + **dict( |
| 231 | + user_id=user_info.user_id, |
| 232 | + dept_id=row['dept_id'], |
| 233 | + user_name=row['user_name'], |
| 234 | + nick_name=row['nick_name'], |
| 235 | + email=row['email'], |
| 236 | + phonenumber=row['phonenumber'], |
| 237 | + sex=row['sex'], |
| 238 | + status=row['status'], |
| 239 | + update_by=current_user.user.user_name |
| 240 | + ) |
| 241 | + ).dict(exclude_unset=True) |
| 242 | + UserDao.edit_user_dao(result_db, edit_user) |
| 243 | + else: |
| 244 | + add_error_result.append(f"{count}.用户账号{row['user_name']}已存在") |
| 245 | + else: |
| 246 | + UserDao.add_user_dao(result_db, add_user) |
| 247 | + result_db.commit() |
| 248 | + result = dict(is_success=True, message='\n'.join(add_error_result)) |
| 249 | + except Exception as e: |
| 250 | + result_db.rollback() |
| 251 | + result = dict(is_success=False, message=str(e)) |
| 252 | + |
| 253 | + return CrudUserResponse(**result) |
| 254 | + |
| 255 | + @staticmethod |
| 256 | + def get_user_import_template_services(): |
| 257 | + """ |
| 258 | + 获取用户导入模板service |
| 259 | + :return: 用户导入模板excel的二进制数据 |
| 260 | + """ |
| 261 | + header_list = ["部门编号", "登录名称", "用户名称", "用户邮箱", "手机号码", "用户性别", "帐号状态"] |
| 262 | + selector_header_list = ["用户性别", "帐号状态"] |
| 263 | + option_list = [{"用户性别": ["男", "女", "未知"]}, {"帐号状态": ["正常", "停用"]}] |
| 264 | + binary_data = get_excel_template(header_list=header_list, selector_header_list=selector_header_list, option_list=option_list) |
| 265 | + |
| 266 | + return binary_data |
| 267 | + |
176 | 268 | @staticmethod |
177 | 269 | def export_user_list_services(user_list: List): |
178 | 270 | """ |
|
0 commit comments