|
|
@@ -6,11 +6,13 @@ import com.njuzr.eaibackend.dto.UserRegisterDTO;
|
|
|
import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.mapper.UserMapper;
|
|
|
import com.njuzr.eaibackend.po.User;
|
|
|
+import com.njuzr.eaibackend.service.EmailService;
|
|
|
import com.njuzr.eaibackend.service.UserService;
|
|
|
import com.njuzr.eaibackend.utils.ModelMapperUtil;
|
|
|
import com.njuzr.eaibackend.vo.UserVO;
|
|
|
import com.opencsv.CSVReader;
|
|
|
import com.opencsv.exceptions.CsvValidationException;
|
|
|
+import jakarta.mail.MessagingException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
@@ -44,6 +46,8 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
private final UserMapper userMapper;
|
|
|
|
|
|
+ private final EmailService emailService = new EmailService();
|
|
|
+
|
|
|
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
|
|
|
|
@Autowired
|
|
|
@@ -109,6 +113,7 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
/**
|
|
|
* 解析文件,批量创建用户。其中,文件必须包含姓名、邮箱、学号。
|
|
|
+ * TODO 完善邮箱服务(需要公邮和stmp秘钥)
|
|
|
* @param file
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -141,6 +146,7 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
int res = userMapper.batchInsert(users);
|
|
|
if (res == 0) throw new MyException(500, "批量创建失败");
|
|
|
+ processUsers(users);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -169,6 +175,7 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
int res = userMapper.batchInsert(users);
|
|
|
if (res == 0) throw new MyException(500, "批量创建失败");
|
|
|
+ processUsers(users);
|
|
|
}
|
|
|
|
|
|
private int findColumnIndex(Row headerRow, String columnName) {
|
|
|
@@ -189,16 +196,21 @@ public class UserServiceImpl implements UserService {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
-// private void processUsers(List<User> users) {
|
|
|
-// for(User user: users) {
|
|
|
-// // 随机生成密码,并使用BCryptPasswordEncoder加密密码
|
|
|
-// String randomPassword = UUID.randomUUID().toString();
|
|
|
-// String encodedPassword = passwordEncoder.encode(randomPassword);
|
|
|
-// user.setPassword(encodedPassword);
|
|
|
-// }
|
|
|
-// int res = userMapper.batchInsert(users);
|
|
|
-// if (res == 0) throw new MyException(500, "批量创建失败");
|
|
|
-// // TODO 通过邮箱服务,将初始密码发送给指定邮箱
|
|
|
-// }
|
|
|
+ private void processUsers(List<User> users) throws MessagingException {
|
|
|
+ List<String> passwordStage = new ArrayList<>();
|
|
|
+ for(User user: users) {
|
|
|
+ // 随机生成密码,并使用BCryptPasswordEncoder加密密码
|
|
|
+ String randomPassword = UUID.randomUUID().toString();
|
|
|
+ passwordStage.add(randomPassword);
|
|
|
+ String encodedPassword = passwordEncoder.encode(randomPassword);
|
|
|
+ user.setPassword(encodedPassword);
|
|
|
+ }
|
|
|
+ int res = userMapper.batchInsert(users);
|
|
|
+ if (res == 0) throw new MyException(500, "批量创建失败");
|
|
|
+ // TODO 通过邮箱服务,将初始密码发送给指定邮箱
|
|
|
+ for (int i = 0; i < users.size(); i++) {
|
|
|
+ emailService.sendPasswordEmail(users.get(i).getOfficialEmail(), users.get(i).getName(), passwordStage.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|