品牌的拼音生成报错问题解决

This commit is contained in:
liyj 2025-06-16 17:39:25 +08:00
parent 108ab7443f
commit fe8ff2ab33
2 changed files with 55 additions and 4 deletions

View File

@ -0,0 +1,53 @@
package com.suisung.mall.common.utils;
import com.github.promeg.pinyinhelper.Pinyin;
public class TinyPinyinUtils{
/**
* 获取汉字全拼
* @param chinese 中文字符串
* @return 拼音字符串小写
*/
public static String getFullPinyin(String chinese) {
if (chinese == null || chinese.trim().isEmpty()) {
return "";
}
return Pinyin.toPinyin(chinese, " ").toLowerCase();
}
/**
* 获取汉字首字母
* @param chinese 中文字符串
* @return 首字母字符串大写
*/
public static String getFirstLetters(String chinese) {
if (chinese == null || chinese.trim().isEmpty()) {
return "";
}
StringBuilder firstLetters = new StringBuilder();
for (char c : chinese.toCharArray()) {
if (Pinyin.isChinese(c)) {
String pinyin = Pinyin.toPinyin(c);
if (!pinyin.isEmpty()) {
firstLetters.append(pinyin.charAt(0));
}
} else {
firstLetters.append(c);
}
}
return firstLetters.toString();
}
public static void main(String[] args) {
String text = "其他品牌";
System.out.println("全拼: " + getFullPinyin(text));
// 输出: SHANGPINSHUJUQUANXIANKONGZHI
System.out.println("首字母: " + getFirstLetters(text));
System.out.println("首字母: " + getFullPinyin(text).charAt(0));
// 输出: SPSJQXKZ
}
}

View File

@ -15,6 +15,7 @@ import com.suisung.mall.common.modules.base.ShopBaseProductCategory;
import com.suisung.mall.common.utils.CheckUtil;
import com.suisung.mall.common.utils.FilterUtils;
import com.suisung.mall.common.utils.I18nUtil;
import com.suisung.mall.common.utils.TinyPinyinUtils;
import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
import com.suisung.mall.shop.base.mapper.ShopBaseProductBrandMapper;
import com.suisung.mall.shop.base.service.ShopBaseProductBrandService;
@ -118,11 +119,8 @@ public class ShopBaseProductBrandServiceImpl extends BaseServiceImpl<ShopBasePro
@Transactional
@Override
public boolean saveOrUpdateBrand(ShopBaseProductBrand shopBaseProductBrand) {
//shopBaseProductBrand.setBrand_name_pinyin("qi ta pin pai");
shopBaseProductBrand.setBrand_name_pinyin(PinyinUtil.getPinyin(shopBaseProductBrand.getBrand_name()));
shopBaseProductBrand.setBrand_name_pinyin(TinyPinyinUtils.getFullPinyin(shopBaseProductBrand.getBrand_name()));
shopBaseProductBrand.setBrand_initial(Convert.toStr(shopBaseProductBrand.getBrand_name_pinyin().charAt(0)));
//shopBaseProductBrand.setBrand_initial("q");
Integer store_id = shopBaseProductBrand.getStore_id();
boolean isPlatform = true;