系统中有一个表中,人员存放的是多个
并且,存放的不是姓名,而是code,code之间使用逗号分隔
页面,需要显示所有人员的姓名
把数组中的code,作为key放在map中,获取每一个员工姓名
@RequestMapping(method = RequestMethod.GET)
public String list(Page<SamplePreparationInfoExt> page, Model model, @RequestParam Map<String, String> param) {
List<PersonInfoExt> personInfoList = this.personInfoExtService.selectPersonByDeptId(currentUser != null ? currentUser.getDepartmentCode() : null);
List<SamplePreparationInfoExt> SamplePreparationInfoExtList = samplePreparationWebService.selectExtListByMap(page, param);
for (int i = 0; i < SamplePreparationInfoExtList.size(); i++) {
String opertor = SamplePreparationInfoExtList.get(i).getLabSampleAcceptor();
String opertorName = "";
if (opertor == null || opertor.equals("")) {
SamplePreparationInfoExtList.get(i).setLabSampleAcceptor(opertorName);
} else {
int number = StringUtils.countMatches(opertor, ",");
Map<String, String> nameMap = new HashMap<>();
for (int j = 0; j < number + 1; j++) {
String opertorCode = "";
opertorCode = opertor.substring(j * 8 + j, (j + 1) * 8 + j);
nameMap.put(opertorCode, "");
}
if (nameMap.size() == 0) {
SamplePreparationInfoExtList.get(i).setLabSampleAcceptor(opertorName);
} else {
int k = 0;
for (String key : nameMap.keySet()) {
// 获取名字
String name = getOpertorNameByCode(personInfoList, key);
if (k == 0) {
opertorName = name;
} else {
opertorName = opertorName + ',' + name;
}
k = k + 1;
}
SamplePreparationInfoExtList.get(i).setLabSampleAcceptor(opertorName);
}
}
}
setCommonData(model);
page.setResult(SamplePreparationInfoExtList);
model.addAttribute("page", page);
return this.viewName("list");
}
// 获取化验员姓名
public String getOpertorNameByCode(List<PersonInfoExt> personInfoList, String opertorCode) {
String opertorName = "";
for (int i = 0; i < personInfoList.size(); i++) {
if (opertorCode != null) {
if (opertorCode.equals(personInfoList.get(i).getCode())) {
opertorName = personInfoList.get(i).getName();
}
}
}
return opertorName;
}
注意
使用substring截取String,必须保证这个code的位数,必须是固定的
考虑到性能,只有list在不大的情况下,才可以使用
或者尽量过滤list集合中的数据,减少遍历次数