年齡標籤合併的問題

1.運用場景

1.1  運營在後臺給資源打年齡標籤,如:0-0.5歲,0.5-1歲,1-2歲,2-3歲,3-6歲,6歲+。當該資源顯示在前端的時候就會把這些標籤全部展示出來,給用戶看到時候就會顯得非常的臃腫。

2.問題分析

2.1 其實上面這些標籤的年齡跨度是連起來的,展示給前端的時候應該合併。如:0-0.5歲,0.5-1歲,1-2歲,2-3歲應該合併成0-3歲。1-2歲,2-3歲,2-6歲,6歲+,應該合併成1歲+。0-0.5歲,0.5-1歲,1-2歲的標籤的話需要合併成:0-2歲。

好了現在的需求明確了就是要寫代碼來合併了。

3.解決方案

 /**
     * 標籤轉換
     *
     * @return
     * @Author: Tian Luhua
     */
    public static List<String> listLabelVo2ListLabel(List<LabelVo> labelVos) {
        if (labelVos == null)
            return null;
        List<String> labels = labelVos.stream().map(LabelVo::getName).filter(item -> !matchPublishLevelLabel(item)).collect(Collectors.toList());

        //1.將年齡標籤和非年齡標籤分組
        Map<Boolean, List<String>> groupLabels = labels.stream().collect(Collectors.groupingBy(Vo2ApiVoUtil::matchAgeLabel, Collectors.toList()));
        List<String> otherLabels = groupLabels.get(false);
        List<String> ageLabels = groupLabels.get(true);
        //2.判斷是否存在年齡標籤
        if (ageLabels != null && ageLabels.size() > 0) {
            List<String> mageAfterLabels = magerAgeLabels(ageLabels);
            if (otherLabels != null) {
                //3.把合併後的年齡標籤和其他的標籤再次合併返回前端
                otherLabels.addAll(mageAfterLabels);
                return otherLabels;
            } else {
                //4.如果沒有其他的標籤就直接返回年齡標籤
                return mageAfterLabels;
            }
        } else {
            return labels;
        }
    }


    /**
     * 合併年齡標籤
     *
     * @return
     * @Author: Tian Luhua
     */
    private static List<String> magerAgeLabels(List<String> ageLabels) {
        //0  年齡標籤只有一個的話就直接返回,多個的話就走合併邏輯
        if (ageLabels.size() > 1) {
            //1.年齡標籤多個
            String firstAgeLabel = ageLabels.get(0);
            String lastAgeLabel = ageLabels.get(ageLabels.size() - 1);
            StringBuilder afterAgeLabel = new StringBuilder();
            if (isAgePlusLabel(lastAgeLabel)) {
                //1.2 年齡標籤>=2,並且有帶+號格式的年齡標籤
                String f = firstAgeLabel.split("-")[0];
                afterAgeLabel.append(f).append("歲+");
            } else {
                //1.1 年齡標籤>=2,沒有有帶+號格式的年齡標籤
                String f = firstAgeLabel.split("-")[0];
                String l = lastAgeLabel.split("-")[1];
                afterAgeLabel.append(f).append("-").append(l);
            }
            ageLabels.clear();
            ageLabels.add(afterAgeLabel.toString());
        }
        return ageLabels;
    }

    /**
     * 判斷是否是年齡標籤
     *
     * @param ageLabel
     * @return
     * @Author: Tian Luhua
     */
    private static boolean matchAgeLabel(String ageLabel) {
        String pattern1 = "[0-9][.][0-9][-][0-9][.][0-9][歲]";
        String pattern2 = "[0-9][-][0-9][.][0-9][歲]";
        String pattern3 = "[0-9][.][0-9][-][0-9][歲]";
        String pattern4 = "[0-9][-][0-9][歲]";
        boolean isMatch1 = Pattern.matches(pattern1, ageLabel);
        boolean isMatch2 = Pattern.matches(pattern2, ageLabel);
        boolean isMatch3 = Pattern.matches(pattern3, ageLabel);
        boolean isMatch4 = Pattern.matches(pattern4, ageLabel);
        if (isMatch1 || isMatch2 || isMatch3 || isMatch4 || isAgePlusLabel(ageLabel)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 檢查帶+號格式的年齡標籤
     *
     * @param ageLabel
     * @return
     * @Author: Tian Luhua
     */
    private static boolean isAgePlusLabel(String ageLabel) {
        String agePlus1 = "[0-9][歲][+]";
        String agePlus2 = "[0-9][.][0-9][歲][+]";
        return Pattern.matches(agePlus1, ageLabel) || Pattern.matches(agePlus2, ageLabel);
    }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章