本人自用工具类-笔记

目录

1、构建error的Collection

2、调数据湖-数据服务-新增

3、修改

4、自定义分页工具类


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));

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值