一对多查询创建编辑。
Posted fenghh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一对多查询创建编辑。相关的知识,希望对你有一定的参考价值。
前端传参格式
"interfaces": [
{
"nodeId": 2351,
"ipAddress": "192.168.1.222",
"macAddress": "值班人员"
}]
一对多后端查询编写从controller到service,实体类,SQL。
1 @RequestMapping(value = "/mini/server/zone", method = RequestMethod.GET, produces = "text/html;charset=UTF-8") 2 @ApiOperation(value = "查询分区", httpMethod = "GET", response = String.class) 3 public String listMiniServerZone(@ApiParam(value = "服务器ID", required = false) @RequestParam(required = false, name = "parentId") Integer parentId, 4 @ApiParam(value = "资源池ID") @RequestParam(required = false) Integer poolId) { 5 HashMap<String, Object> paramMap = new HashMap<>(); 6 Optional.ofNullable(parentId).map(t -> paramMap.put("parentId", t)); 7 Optional.ofNullable(poolId).map(t -> paramMap.put("poolId", t)); 8 9 return resourceService.listMiniServerZone(paramMap); 10 }
service
1 /** 2 * 查询分区 3 * @param paramMap 4 * @return 5 */ 6 public String listMiniServerZone(HashMap<String, Object> paramMap) { 7 List<Map<String, Object>> resultList = resourceMapper.selectMiniServerZone(paramMap);
//一对多返回HashMap格式给前端 8 for (Map<String, Object> itemMap : resultList) { 9 List<Map<String, Object>> wwnList = resourceMapper.selectMiniServerZoneWwn(itemMap); 10 itemMap.put("wwns", wwnList); 11 12 List<Map<String, Object>> interfaceList = resourceMapper.selectMiniServerZoneInterface(itemMap); 13 itemMap.put("interfaces", interfaceList); 14 } 15 String result = JsonHelper.toJson(resultList); 16 return result; 17 }
创建
@RequestMapping(value = "/mini/server/zone", method = RequestMethod.POST) @ApiOperation(value = "添加分区", httpMethod = "POST", response = String.class) public Result createMiniServerZone(@ApiParam(value = "添加分区", required = true) @RequestBody MiniServerZoneRequest miniServerZoneRequest) { HashMap<String, Object> paramMap = miniServerZoneRequest.parseToMap(); return resourceService.insertMiniServerZone(getCurrentLoginId(), paramMap); }
/** * 添加分区 * @param currentLoginId * @return */ public Result insertMiniServerZone(String currentLoginId, HashMap<String, Object> paramMap) { Result result = new Result(); try {
// id自动随机生成 UUID uuid = UUID.randomUUID(); int nodeId = CommonTools.genterateRnId(uuid.toString(),""); paramMap.put("nodeId", nodeId); resourceMapper.insertMiniServerZone(paramMap); //一对多后端获取 先获取到wwns List<HashMap<String, Object>> wwnValues = (List<HashMap<String, Object>>) paramMap.get("wwns"); for (HashMap<String, Object> item : wwnValues) { item.put("nodeId", nodeId); resourceMapper.insertMiniServerZoneWwn(item); } List<HashMap<String, Object>> fieldValues = (List<HashMap<String, Object>>) paramMap.get("interfaces"); for (HashMap<String, Object> paramter : fieldValues) { paramter.put("nodeId", nodeId); if (paramter.containsKey("ipAddress")) { paramter.put("ipAddressLong", CommonTools.parseIpToLong((String) paramter.get("ipAddress"))); } resourceMapper.insertMiniServerZoneInterface(paramter); } result.setResultCode(EResultCode.SUCCESS); result.setResultMsg("分区添加成功"); } catch (Exception e) { logger.error("update updatePowerZone error!", e); result.setResultCode(EResultCode.DATABASE_FAIL); result.setResultMsg("分区添加失败"); } return result; }
编辑
@RequestMapping(value = "/mini/server/zone/{nodeId}", method = RequestMethod.PUT) @ResponseBody @ApiOperation(value = "编辑分区", httpMethod = "PUT", response = String.class) public Result updateMiniServerZone(@ApiParam(value = "nodeId", required = true) @PathVariable Integer nodeId, @ApiParam(value = "修改分区", required = true) @RequestBody MiniServerZoneRequest miniServerZoneRequest) { HashMap<String, Object> paramMap = miniServerZoneRequest.parseToMap(); paramMap.put("nodeId", nodeId); return resourceService.updateMiniServerZone(getCurrentLoginId(), paramMap); /**
* 修改分区 * @param currentLoginId * @return */ public Result updateMiniServerZone(String currentLoginId, HashMap<String, Object> paramMap) { Result result = new Result(); try { resourceMapper.updateMiniServerZone(paramMap);
//修改wwn和interface表时先删除这条记录,然后在添加 resourceMapper.deleteMiniServerZoneInterface(paramMap); resourceMapper.deleteMiniServerZoneWwn(paramMap); List<HashMap<String, Object>> wwnValues = (List<HashMap<String, Object>>) paramMap.get("wwns"); for (HashMap<String, Object> item : wwnValues) { item.put("nodeId", paramMap.get("nodeId")); resourceMapper.insertMiniServerZoneWwn(item); } List<HashMap<String, Object>> fieldValues = (List<HashMap<String, Object>>) paramMap.get("interfaces"); for (HashMap<String, Object> paramter : fieldValues) { paramter.put("nodeId",paramMap.get("nodeId")); if (paramter.containsKey("ipAddress")) { paramter.put("ipAddressLong", CommonTools.parseIpToLong((String) paramter.get("ipAddress"))); } resourceMapper.insertMiniServerZoneInterface(paramter); } result.setResultCode(EResultCode.SUCCESS); result.setResultMsg("分区修改成功"); } catch (Exception e) { logger.error("update updatePowerZone error!", e); result.setResultCode(EResultCode.DATABASE_FAIL); result.setResultMsg("分区修改失败"); } return result; }
实体类
@Data public class MiniServerZoneRequest extends AbstractRequest { @ApiModelProperty(value="nodeId", dataType="integer", hidden=true) private Integer nodeId; private String hostName; private Integer parentId; private String serialNumber; private String unitType; private String openSSH; private String agentId; private String remark; private String name; private String wwn; private String ipAddress; private List<WwnRequest> wwns; //一对多实体类 private List<MiniServerInterface> interfaces;
导入模板
public String importMiniServer(String currentLoginId, String templatePath, String uploadedFilePath) { // 模板文件 File templateFile = new File(uploadDir + File.separator + FileUpDownController.TEMPLATE_BASE_DIR + File.separator + templatePath); // 已上传文件 File uploadedFile = new File(uploadDir + File.separator + uploadedFilePath); Result result = new Result(); // 查询机房数据列表,并转为map方便后面使用 List<HashMap<String, Object>> machineRoomList = ossDatabaseService.directSelect(ApigateMapperConst.SELECT_RESOURCE_MACHINE_ROOMS, new HashMap<>()); // 查询设备类型字典 Map<String, Object> machineTypeDictMap = systemService.listDictMap("IPPOOL_MACHINE_TYPE"); // 查询设备厂家字典 Map<String, Object> manufacturerDictMap = systemService.listDictMap("RESOURCE_MANUFACTURERCODE"); //查询HMC HashMap<String, Object> hmcMap = new HashMap<>(); List<Map<String, Object>> dataList = resourceMapper.selectHMC(hmcMap); Map<String, Object> resourceHmcMap = new HashMap<>(); dataList.forEach(data -> resourceHmcMap.put(data.get("name").toString(), data.get("id"))); // 判断文件是否都存在 if (!templateFile.exists() || !uploadedFile.exists()) { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("模板文件或上传文件不存在"); return JsonHelper.toJson(result); } // 必填字段key List<String> requiredKeys = Arrays.asList("machineRoomName", "column_", "row_", "unum", "height", "machineType", "manufacturerCode","cpuNum","memory"); try { // 从excel读文件 List<Map<String, Object>> datalist = ExcelReaderUtil.readExcel(uploadedFile.getPath(), templateFile.getPath()); logger.debug("[服务器导入]:读取到的excel数据-" + JsonHelper.toJson(datalist)); // 遍历数据,校验数据,处理数据,但是先不入库 for (Map<String, Object> data : datalist) { // 处理数据 校验数据 // 验证必填字段 for (String dataKey : requiredKeys) { if (!data.containsKey(dataKey) || data.get(dataKey) == null || StringUtils.isBlank((String) data.get(dataKey))) { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的必填字段缺失"); return JsonHelper.toJson(result); } } // 处理所属机房 String machineRoomName = (String) data.get("machineRoomName"); val tmpMachineRoomList = machineRoomList.stream().filter(it -> machineRoomName.equals(it.get("name"))).collect(Collectors.toList()); if (tmpMachineRoomList.isEmpty()) { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的所属机房不存在"); return JsonHelper.toJson(result); } else { data.put("machineRoomId", tmpMachineRoomList.get(0).get("id")); data.put("machineRoomAlias", tmpMachineRoomList.get(0).get("alias")); } // 机柜-列 String column_ = (String) data.get("column_"); if (StringUtils.isNotBlank(column_)) { if (column_.toUpperCase().matches("^[A-Z]$")) { data.put("column_", column_.toUpperCase()); } else { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的机柜-列格式不对,请输入1位大写字母"); return JsonHelper.toJson(result); } } // 机柜-行 Object row_ = data.get("row_"); if (row_ != null) { if (row_.toString().length() == 1) { row_ = "0" + row_; } if (row_.toString().matches("^[0-9]{2}$")) { data.put("row_", row_); } else { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的机柜-行格式不对,请输入2位数字"); return JsonHelper.toJson(result); } } if (data.get("hmcName") == null) { data.put("hmcId", 0); }else { Object hmcId = resourceHmcMap.get(data.get("hmcName")); data.put("hmcId", hmcId); } // 机位验证,只能输入数字 Object unum = data.get("unum"); if (unum != null) { try { unum = Integer.parseInt(String.valueOf(unum)); data.put("unum", unum); } catch (Exception e) { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的机位格式不对,只需输入数字"); return JsonHelper.toJson(result); } } // 高度验证,只能输入数字 Object height = data.get("height"); if (height != null) { try { height = Integer.parseInt(String.valueOf(height)); data.put("height", height); } catch (Exception e) { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的高度格式不对,只需输入数字"); return JsonHelper.toJson(result); } } // 验证设备类型 if (data.get("machineType") != null) { Object machineType = data.get("machineType"); if (machineTypeDictMap.containsValue(machineType)) { for (Map.Entry<String, Object> entry : machineTypeDictMap.entrySet()) { if (entry.getValue().toString().equalsIgnoreCase(machineType.toString())) { machineType = entry.getKey(); break; } } data.put("machineType", machineType); } else { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的设备类型不存在"); return JsonHelper.toJson(result); } } // 验证设备厂家 if (data.get("manufacturerCode") != null) { Object manufacturerCode = data.get("manufacturerCode"); String manufacturerId = null; if (manufacturerDictMap.containsValue(manufacturerCode)) { for (Map.Entry<String, Object> entry : manufacturerDictMap.entrySet()) { if (entry.getValue().toString().equalsIgnoreCase(manufacturerCode.toString())) { manufacturerId = entry.getKey(); break; } } data.put("manufacturerCode", manufacturerId); } else { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的设备厂家不存在"); return JsonHelper.toJson(result); } } // 验证CPU核数是否是数字 Object cpuNum = data.get("cpuNum"); if (cpuNum != null) { try { cpuNum = Integer.parseInt(String.valueOf(cpuNum)); data.put("cpuNum", cpuNum); } catch (Exception e) { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的CPU核数格式不对,只需输入数字"); return JsonHelper.toJson(result); } } // 验证内存(MB)是否是数字 Object memory = data.get("memory"); if (memory != null) { try { memory = Integer.parseInt(String.valueOf(memory)); data.put("memory", memory); } catch (Exception e) { result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg("服务器[" + data.get("serialNumber") + "]的内存格式不对,只需输入数字"); return JsonHelper.toJson(result); } } } for (Map<String, Object> data : datalist) { logger.debug("[服务器导入]:发送到EAction的数据-" + JsonHelper.toJson(data)); String json = JsonHelper.toJson(data);//转为String HashMap<String, Object> fromJson = JsonHelper.fromJson(HashMap.class, json);//转为HashMap if (data.get("nodeId") != null) { // 更新服务器 updateMiniServer(currentLoginId,fromJson); } else { // 添加服务器 insertMiniServer(currentLoginId, fromJson); } } } catch (Exception e) { logger.error(e.getCause().getMessage(), e); result.setResultCode(EResultCode.GET_DATA_FAIL); result.setResultMsg(e.getCause().getMessage()); return JsonHelper.toJson(result); } result.setResultCode(EResultCode.SUCCESS); return JsonHelper.toJson(result); }
以上是关于一对多查询创建编辑。的主要内容,如果未能解决你的问题,请参考以下文章