目录
1、构建error的Collection
/** * @Description: 构建error 的Collection * @Date: 2024/7/9 * @return: net.sf.json.JSONObject * @param: [errorInfo] **/ public JSONObject buildErrorCollection(String errorInfo){ Object object = null; ErrorInfo errorInfoData = new ErrorInfo(request.getRequestURI(), "0", errorInfo); String resultString = RestfulTool.buildCollection(errorInfoData, request.getRequestURI()); object = JSONObject.fromObject(resultString); return JSONObject.fromObject(object); }
2、调数据湖-数据服务-新增
/** * @Description: 新增数据工具 * @Date: 2024/6/27 * @return: * @param: **/ public <T> String postDataForDataLake(List<T> list, String urlInfo) throws Exception { //发送post请求,新增辅料数据 String collectionJson = RestfulTool.buildCollection(list); String responseBody = null; OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.get("application/json; charset=utf-8"); RequestBody body = RequestBody.create(mediaType, collectionJson); // 从Header获取租户编码tenantCode String tenantCode = getTenantCode(); if (StringUtils.isBlank(tenantCode)){ log.info("tenantCode为空"); throw new BusinessException("","","tenantCode为空"); } //新增url //http://10.238.219.184/dataLakeValueService/rents/mshg/contents/t_wcdm_chemical_dosing/values? String url = "http://" + dataLakeIp + "/dataLakeValueService/rents/" + tenantCode + "/contents" + "/"+ urlInfo + "/values"; //调数据湖-明泉水务专题-加药记录数据服务 Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); responseBody = response.body().string(); return responseBody; }
3、修改
/** * @Description: 修改工具 * @Date: 2024/7/2 * @return: * @param: **/ public <T> String putDataForDataLake(List<T> list, String urlInfo,String codeInfo) throws Exception { //delete,新增辅料数据 String collectionJson = RestfulTool.buildCollection(list); String responseBody = null; OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.get("application/json; charset=utf-8"); RequestBody body = RequestBody.create(mediaType, collectionJson); // 从Header获取租户编码tenantCode String tenantCode = getTenantCode(); if (StringUtils.isBlank(tenantCode)){ log.info("tenantCode为空"); throw new BusinessException("","","tenantCode为空"); } //新增url //http://10.238.219.184/dataLakeValueService/rents/mshg/contents/t_wcdm_chemical_dosing/values? String url = "http://" + dataLakeIp + "/dataLakeValueService/rents/" + tenantCode + "/contents" + "/"+ urlInfo + "/values"; //拼接参数 String code = "code"; String codeValue = codeInfo; url = appendParameterToURL(url, code, codeValue); //调数据湖-明泉水务专题-加药记录数据服务 Request request = new Request.Builder() .url(url) .put(body) .build(); Response response = client.newCall(request).execute(); responseBody = response.body().string(); return responseBody; }
4、自定义分页工具类
/** * @Description 自定义分页工具类$ * @Author baotian.yuan$ * @Date: 2024-06-07$ * @Param: $ * @Return: $ */ public class PaginationUtils { public static <T> List<T> getPage(List<T> list, int total, int top, int skip) { int fromIndex = Math.min(skip, total); // 起始位置不超过总数 int toIndex = Math.min(skip + top, total); // 结束位置不超过总数 if (fromIndex > toIndex) { return new ArrayList<>(); // 空列表,避免IndexOutOfBoundsException } return list.subList(fromIndex, toIndex); } }使用
List<PlantMedicamentRelevancyNewInfo> medicamentManagePageInfo = PaginationUtils.getPage(responseList, responseList.size(), topValue, skipValue); Map<String, Object> map = new HashMap<>(); map.put("medicamentList",medicamentManagePageInfo); map.put("total",responseList.size()); return map;
controller接收
Map<String, Object> plantMedicamentRelvancyData = plantMedicamentService.getPlantMedicamentRelvancyData(plantCode, top, skip); List<PlantMedicamentRelevancyNewInfo> medicamentList = (List<PlantMedicamentRelevancyNewInfo>) plantMedicamentRelvancyData.get("medicamentList"); Object total = plantMedicamentRelvancyData.get("total"); Long totalValue = Long.valueOf(total.toString()); return JSONObject.fromObject(RestfulTool.buildCollection(medicamentList, setPage(totalValue),request.getRequestURI(), PlantMedicamentRelevancyNewInfo.class));