| | |
| | | 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 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.HanyuPinyinToneType; |
| | | import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.TreeMap; |
| | | |
| | | /** |
| | | * <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(); |
| | | // 根据市区首字母分组排序 |
| | | return newSort(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 (char c : arr) { |
| | | if (c > 128) { |
| | | try { |
| | | pybf.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0]); |
| | | } catch (BadHanyuPinyinOutputFormatCombination e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | pybf.append(c); |
| | | } |
| | | } |
| | | return pybf.toString(); |
| | | } |
| | | |
| | | private Object newSort(List<Region> reginList) { |
| | | // 按照城市名称排序,并分组 |
| | | Map<String, List<RegionVo>> map = new TreeMap<>(); |
| | | for (int i = 1; i <= 26; i++) { |
| | | // 循环找出首字母一样的数据 |
| | | 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.getCityCode()); |
| | | regionVos.add(regionVo); |
| | | } |
| | | map.put(letter, regionVos); |
| | | } |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | } |