| | |
| | | package com.ruoyi.user.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.core.constant.Constants; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.user.entity.Region; |
| | | import com.ruoyi.user.mapper.RegionMapper; |
| | | import com.ruoyi.user.service.RegionService; |
| | | import net.sourceforge.pinyin4j.PinyinHelper; |
| | | import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; |
| | | import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; |
| | | import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; |
| | | import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.text.Collator; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService { |
| | | |
| | | @Override |
| | | public Object urbanArea(String keyword) { |
| | | LambdaQueryChainWrapper<Region> wrapper = this.lambdaQuery() |
| | | .ne(Region::getParentId, Constants.ZERO); |
| | | if (StringUtils.isNotBlank(keyword)) { |
| | | wrapper.like(Region::getName, keyword); |
| | | } |
| | | // 获取所有市区信息 |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 获取汉字串拼音,英文字符不变 |
| | | * |
| | | * @param chinese 汉字串 |
| | | * @return 汉语拼音 |
| | | */ |
| | | public String getFullSpell(String chinese) { |
| | | StringBuilder pybf = new StringBuilder(); |
| | | char[] arr = chinese.toCharArray(); |
| | | 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) { |
| | | try { |
| | | pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]); |
| | | } catch (BadHanyuPinyinOutputFormatCombination e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | pybf.append(arr[i]); |
| | | } |
| | | } |
| | | 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<>(); |
| | | 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); |
| | | } |
| | | map.put(word, letter); |
| | | } |
| | | //System.out.println(JsoN.to]soNString(map)); |
| | | } |
| | | return map; |
| | | } |
| | | } |