xiatian преди 2 години
родител
ревизия
72c954c0a8

+ 31 - 4
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/RandomPaperServiceImpl.java

@@ -7,6 +7,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -25,7 +26,10 @@ import org.springframework.transaction.annotation.Transactional;
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.basic.api.UserCloudService;
+import cn.com.qmth.examcloud.core.basic.api.bean.UserBean;
+import cn.com.qmth.examcloud.core.basic.api.request.GetUserListByIdsReq;
 import cn.com.qmth.examcloud.core.basic.api.request.GetUserReq;
+import cn.com.qmth.examcloud.core.basic.api.response.GetUserListByIdsResp;
 import cn.com.qmth.examcloud.core.basic.api.response.GetUserResp;
 import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
 import cn.com.qmth.examcloud.core.questions.base.Model;
@@ -59,6 +63,7 @@ import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.StructQuest
 import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.StructQuestionCountInfo;
 import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.StructQuestionInfo;
 import cn.com.qmth.examcloud.core.questions.service.cache.RandomPaperCache;
+import cn.com.qmth.examcloud.core.questions.service.util.BatchGetDataUtil;
 import cn.com.qmth.examcloud.core.questions.service.util.PaperUtil;
 import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
 import cn.com.qmth.examcloud.question.commons.core.paper.DefaultQuestionGroup;
@@ -147,14 +152,36 @@ public class RandomPaperServiceImpl implements RandomPaperService {
 			PaperStruct paperStruct = Model.of(paperStructRepo.findById(vo.getPaperStructId()));
 			vo.setPaperStructName(paperStruct.getName());
 			vo.setEnableStr(vo.getEnable() ? "启用" : "禁用");
-			GetUserReq ureq = new GetUserReq();
-			ureq.setUserId(vo.getUpdateBy());
-			GetUserResp ures = userCloudService.getUser(ureq);
-			vo.setUpdateByName(ures.getUserBean().getDisplayName());
 		}
+		fillUserName(paperList, req.getRootOrgId());
+		
 
 		return new PageImpl<>(paperList, pageable, total);
 	}
+	private void fillUserName(List<RandomPaperListVo> dtos, Long rootOrgId) {
+		if (dtos != null && dtos.size() > 0) {
+			List<Long> ids = dtos.stream().map(dto -> dto.getUpdateBy()).distinct().collect(Collectors.toList());
+			List<UserBean> userList = new ArrayList<UserBean>();
+			GetUserListByIdsReq req = new GetUserListByIdsReq();
+			BatchGetDataUtil<UserBean, Long> tool = new BatchGetDataUtil<UserBean, Long>() {
+				@Override
+				public List<UserBean> getData(List<Long> paramList) {
+					req.setRootOrgId(rootOrgId);
+					req.setUserIdList(paramList);
+					GetUserListByIdsResp resp = userCloudService.getUserListByIds(req);
+					return resp.getUserBeanList();
+				}
+
+			};
+			tool.getDataForBatch(userList, ids, 100);
+			Map<Long, UserBean> map = userList.stream()
+					.collect(Collectors.toMap(UserBean::getUserId, account -> account, (key1, key2) -> key2));
+			for (RandomPaperListVo markerBean : dtos) {
+				UserBean userBean = map.get(markerBean.getUpdateBy());
+				markerBean.setUpdateByName(userBean.getDisplayName());
+			}
+		}
+	}
 
 	@Transactional
 	@Override

+ 52 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/util/BatchGetDataUtil.java

@@ -0,0 +1,52 @@
+package cn.com.qmth.examcloud.core.questions.service.util;
+
+import java.util.List;
+
+/**
+ *	多次批量获取数据
+ *	需重写getData方法
+ * @author xiatian
+ * @param <R> 结果类
+ * @param <P> 参数类
+ */
+public  class BatchGetDataUtil<R,P> {
+	/**
+	 * @param resultList 全部结果集合
+	 * @param paramList 全部参数集合
+	 * @param batchSize 每批参数数量
+	 */
+	public final void getDataForBatch(List<R> resultList,List<P> paramList,int batchSize) {
+		if(resultList==null||paramList==null||paramList.size()==0) {
+			return;
+		}
+		if(paramList.size()<=batchSize) {
+			List<R> temlist = getData(paramList);
+			if(temlist!=null&&temlist.size()>0) {
+				resultList.addAll(temlist);
+			}
+		}else {
+			int size = paramList.size();
+			int len=batchSize;
+			int count = (size + len - 1) / len;
+
+			for (int i = 0; i < count; i++) {
+				List<P> subList = paramList.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
+				List<R> temlist = getData(subList);
+				if(temlist!=null&&temlist.size()>0) {
+					resultList.addAll(temlist);
+				}
+			}
+		}
+	}
+	/**
+	 * 	Need Override
+	 * 	每批获取数据方法
+	 * @param <R>
+	 * @param <P>
+	 * @param paramList 获取每批数据时参数
+	 * @return
+	 */
+	public  List<R> getData(List<P> paramList) {
+		return null;
+	}
+}