| | |
| | | import com.ruoyi.user.entity.Region; |
| | | import com.ruoyi.user.mapper.RegionMapper; |
| | | import com.ruoyi.user.service.RegionService; |
| | | import com.ruoyi.user.vo.RegionVo; |
| | | import net.sourceforge.pinyin4j.PinyinHelper; |
| | | import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; |
| | | import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; |
| | |
| | | import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.text.Collator; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.TreeMap; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | } |
| | | // 获取所有市区信息 |
| | | List<Region> reginList = wrapper.orderByAsc(Region::getId).list(); |
| | | List<String> data = reginList.stream().map(Region::getName).collect(Collectors.toList()); |
| | | Map<String, Object> px = px(data); |
| | | return px; |
| | | // 根据市区首字母分组排序 |
| | | // data.sort(new Comparator<String>() { |
| | | // final Collator collator = Collator.getInstance(Locale.CHINA); |
| | | // |
| | | // @Override |
| | | // public int compare(String o1, String o2) { |
| | | // CollationKey key1 = collator.getCollationKey(o1); |
| | | // CollationKey key2 = collator.getCollationKey(o2); |
| | | // return key1.compareTo(key2); |
| | | // } |
| | | // }); |
| | | // return reginList; |
| | | return newSort(reginList); |
| | | } |
| | | |
| | | /** |
| | |
| | | HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); |
| | | defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); |
| | | defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); |
| | | for (int i = 0; i < arr.length; i++) { |
| | | if (arr[i] > 128) { |
| | | for (char c : arr) { |
| | | if (c > 128) { |
| | | try { |
| | | pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]); |
| | | pybf.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0]); |
| | | } catch (BadHanyuPinyinOutputFormatCombination e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | pybf.append(arr[i]); |
| | | pybf.append(c); |
| | | } |
| | | } |
| | | return pybf.toString(); |
| | | } |
| | | |
| | | public Map<String, Object> px(List<String> list) { |
| | | Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA); |
| | | // 按字母排序 |
| | | Collections.sort(list, com); |
| | | //输出26个字母 |
| | | Map<String, Object> map = new TreeMap<>(); |
| | | private Object newSort(List<Region> reginList) { |
| | | // 按照城市名称排序,并分组 |
| | | Map<String, List<RegionVo>> map = new TreeMap<>(); |
| | | for (int i = 1; i <= 26; i++) { |
| | | //循环找出 首字母一样的数据 |
| | | String word = String.valueOf((char) (96 + i)).toUpperCase(); |
| | | List<String> letter = new ArrayList<>(); |
| | | for (String str : list) { |
| | | // System.out.println("首字母"+zm); |
| | | String zm = getFullSpell(str).substring(0, 1); |
| | | if (word.equals(zm)) { |
| | | letter.add(str); |
| | | // 循环找出首字母一样的数据 |
| | | String letter = String.valueOf((char) (96 + i)).toUpperCase(); |
| | | List<RegionVo> regionVos = new ArrayList<>(); |
| | | for (Region region : reginList) { |
| | | String firstLetter = getFullSpell(region.getName()).substring(0, 1); |
| | | if (letter.equals(firstLetter)) { |
| | | RegionVo regionVo = new RegionVo(region.getName(), region.getCode()); |
| | | regionVos.add(regionVo); |
| | | } |
| | | map.put(word, letter); |
| | | if (!regionVos.isEmpty()) { |
| | | map.put(letter, regionVos); |
| | | } |
| | | } |
| | | //System.out.println(JsoN.to]soNString(map)); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | } |