[DS-7256][refactor] Optimization for task instances query (#7331)

- assemble a Map<Long, User> outside of the loop instead of the query inside

Co-authored-by: gq47193 <gq47193@ly.com>
This commit is contained in:
mgduoduo 2021-12-15 10:53:52 +08:00 committed by GitHub
parent 6b5c393449
commit bc995752d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,6 +42,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -137,12 +138,13 @@ public class TaskInstanceServiceImpl extends BaseServiceImpl implements TaskInst
exclusionSet.add(Constants.CLASS);
exclusionSet.add("taskJson");
List<TaskInstance> taskInstanceList = taskInstanceIPage.getRecords();
List<Integer> executorIds = taskInstanceList.stream().map(TaskInstance::getExecutorId).distinct().collect(Collectors.toList());
List<User> users = usersService.queryUser(executorIds);
Map<Integer, User> userMap = users.stream().collect(Collectors.toMap(User::getId, v -> v));
for (TaskInstance taskInstance : taskInstanceList) {
taskInstance.setDuration(DateUtils.format2Duration(taskInstance.getStartTime(), taskInstance.getEndTime()));
User executor = usersService.queryUser(taskInstance.getExecutorId());
if (null != executor) {
taskInstance.setExecutorName(executor.getUserName());
if (userMap.containsKey(taskInstance.getExecutorId())) {
taskInstance.setExecutorName(userMap.get(taskInstance.getExecutorId()).getUserName());
}
}
pageInfo.setTotal((int) taskInstanceIPage.getTotal());