json字符串{"1-3": 29},{"8-": 50},{"8-": 50},返回 1-3天 29,大于8天 100

 

业务代码: 入参 json字符串{"1-3": 29},{"8-": 50},{"8-": 50},返回 1-3天 29,大于8天 100

 

 private void mergePostponeDetail(List<CfChargeListVO> chargeInList) {
        // {"1-3": 29},{"8-": 50},{"8-": 50}
        Set<String> keys = new HashSet<>();
        for (CfChargeListVO chargeInVO : chargeInList) {
            if (chargeInVO.getPostponeDetail() == null) {
                chargeInVO.setPostponeDetail("");
            }
            String postponeDetail = chargeInVO.getPostponeDetail();
            String[] substring = postponeDetail.split(",");
            for (String s : substring) {
                JSONObject object = JSONObject.parseObject(s, JSONObject.class);
                if (object == null) {
                    continue;
                }
                Set<String> strings = object.keySet();
                keys.addAll(strings);
            }
        }
        if (keys.size() > 0) {
            keys.addAll(getAllRuleBillDetails(1));
        }
        // 排序
        List<Integer> sortList = new ArrayList<>(5);
        Map<Integer, String> mapsort = new HashMap<>(8);
        Set<String> keysSorted = new LinkedHashSet<>();
        for (String key : keys) {
            String[] split = key.split("-");
            String s = split[0];
            int i = Integer.parseInt(s);
            sortList.add(i);
            mapsort.put(i, key);
        }
        Collections.sort(sortList);
        for (Integer integer : sortList) {
            String s = mapsort.get(integer);
            keysSorted.add(s);
        }
        // 字符串 数字汇总
        for (CfChargeListVO chargeInVO : chargeInList) {
            String postponeDetail = chargeInVO.getPostponeDetail();
            String[] substring = postponeDetail.split(",");
            Map<String, Integer> objectToArray = new LinkedHashMap<>();
            List<Map<String, Integer>> sortArray = new LinkedList<>();
            for (String key : keysSorted) {
                for (String s : substring) {
                    JSONObject object = JSONObject.parseObject(s, JSONObject.class);
                    if (object == null) {
                        object = new JSONObject();
                    }
                    if (!objectToArray.containsKey(key)) {
                        objectToArray.put(key, 0);
                    }
                    if (object.containsKey(key)) {
                        int sum = Integer.parseInt(objectToArray.get(key).toString());
                        int num = Integer.parseInt(object.get(key).toString());
                        objectToArray.put(key, sum + num);
                    }
                }
                Map<String, Integer> jsonObject = new HashMap<>(4);
                String keyAlias = getKeyAlias(key);
                jsonObject.put(keyAlias, objectToArray.get(key));
                sortArray.add(jsonObject);
            }
            chargeInVO.setPostponeDetail(JSONObject.toJSONString(sortArray));
            chargeInVO.setPostponeDetailArray(sortArray);

 

 private String getKeyAlias(String key) {
        String keyAlias = "延期";
        if (StringUtils.isNotBlank(key)) {
            String[] split = key.split("-");
            if (split.length > 0) {
                if (split.length == 1 || StringUtils.isBlank(split[1])) {
                    keyAlias = "延期" + split[0] + "天以上";
                } else {
                    keyAlias = "延期" + key + "天";
                }
            }
        }
        return keyAlias;
    }

 

    /**
     * 延期明细
     */
    private List<Map<String, Integer>> postponeDetailArray;


    /**
     * 延期明细
     */
    private String postponeDetail;

 

posted @ 2021-01-22 15:02  _Phoenix  阅读(91)  评论(0编辑  收藏  举报