diff --git a/src/main/java/net/mingsoft/cms/action/CategoryAction.java b/src/main/java/net/mingsoft/cms/action/CategoryAction.java index c56c7122..27985eb7 100644 --- a/src/main/java/net/mingsoft/cms/action/CategoryAction.java +++ b/src/main/java/net/mingsoft/cms/action/CategoryAction.java @@ -1,5 +1,7 @@ package net.mingsoft.cms.action; +import com.github.stuxuhai.jpinyin.PinyinFormat; +import com.github.stuxuhai.jpinyin.PinyinHelper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -13,6 +15,7 @@ import net.mingsoft.basic.util.BasicUtil; import net.mingsoft.basic.util.StringUtil; import net.mingsoft.cms.biz.ICategoryBiz; import net.mingsoft.cms.entity.CategoryEntity; +import net.mingsoft.cms.util.PinYinUtil; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -34,11 +37,11 @@ import java.util.List; @Controller("cmsCategoryAction") @RequestMapping("/${ms.manager.path}/cms/category") public class CategoryAction extends BaseAction{ - - + + /** * 注入分类业务层 - */ + */ @Autowired private ICategoryBiz categoryBiz; @@ -49,7 +52,7 @@ public class CategoryAction extends BaseAction{ public String index(HttpServletResponse response,HttpServletRequest request){ return "/cms/category/index"; } - + /** * 查询分类列表 * @param category 分类实体 @@ -89,14 +92,14 @@ public class CategoryAction extends BaseAction{ List categoryList = categoryBiz.query(category); return ResultData.build().success(new EUListBean(categoryList,(int)BasicUtil.endPage(categoryList).getTotal())); } - + /** * 返回编辑界面category_form */ @GetMapping("/form") public String form(@ModelAttribute CategoryEntity category,HttpServletResponse response,HttpServletRequest request,ModelMap model){ if(category.getId()!=null){ - BaseEntity categoryEntity = categoryBiz.getEntity(Integer.parseInt(category.getId())); + BaseEntity categoryEntity = categoryBiz.getEntity(Integer.parseInt(category.getId())); model.addAttribute("categoryEntity",categoryEntity); } model.addAttribute("appId",BasicUtil.getAppId()); @@ -119,7 +122,7 @@ public class CategoryAction extends BaseAction{ CategoryEntity _category = (CategoryEntity)categoryBiz.getEntity(Integer.parseInt(category.getId())); return ResultData.build().success(_category); } - + @ApiOperation(value = "保存分类列表接口") @ApiImplicitParams({ @ApiImplicitParam(name = "categoryTitle", value = "栏目管理名称", required =true,paramType="query"), @@ -171,11 +174,13 @@ public class CategoryAction extends BaseAction{ if(!StringUtil.checkLength(category.getCategoryParentId()+"", 1, 100)){ return ResultData.build().error(getResString("err.length", this.getResString("category.parent.id"), "1", "100")); } + //获取拼音 + category.setAppId(BasicUtil.getAppId()); categoryBiz.saveEntity(category); return ResultData.build().success(category); } - + /** * @param category 分类实体 */ @@ -228,7 +233,7 @@ public class CategoryAction extends BaseAction{ @RequiresPermissions("cms:category:update") public ResultData update(@ModelAttribute @ApiIgnore CategoryEntity category, HttpServletResponse response, HttpServletRequest request) { - //验证栏目管理名称的值是否合法 + //验证栏目管理名称的值是否合法 if(StringUtil.isBlank(category.getCategoryTitle())){ return ResultData.build().error(getResString("err.empty", this.getResString("category.title"))); } @@ -242,6 +247,16 @@ public class CategoryAction extends BaseAction{ if(!StringUtil.checkLength(category.getCategoryParentId()+"", 0, 100)){ return ResultData.build().error(getResString("err.length", this.getResString("category.parent.id"), "1", "100")); } + String pingYin = PinYinUtil.getPingYin(category.getCategoryTitle()); + CategoryEntity categoryEntity=new CategoryEntity(); + categoryEntity.setCategoryPinyin(pingYin); + categoryEntity.setAppId(BasicUtil.getAppId()); + Object categoryBizEntity = categoryBiz.getEntity(categoryEntity); + category.setCategoryPinyin(pingYin); + //如果存在此拼音栏目则拼接上id + if(categoryBizEntity!=null){ + category.setCategoryPinyin(pingYin+category.getId()); + } //判断是否选择子级为所属栏目 CategoryEntity _category = new CategoryEntity(); _category.setCategoryParentId(category.getId()); @@ -258,5 +273,5 @@ public class CategoryAction extends BaseAction{ } - -} \ No newline at end of file + +} diff --git a/src/main/java/net/mingsoft/cms/biz/impl/CategoryBizImpl.java b/src/main/java/net/mingsoft/cms/biz/impl/CategoryBizImpl.java index 02f2c205..de9c8494 100644 --- a/src/main/java/net/mingsoft/cms/biz/impl/CategoryBizImpl.java +++ b/src/main/java/net/mingsoft/cms/biz/impl/CategoryBizImpl.java @@ -23,7 +23,9 @@ package net.mingsoft.cms.biz.impl; import cn.hutool.core.util.ObjectUtil; import net.mingsoft.base.entity.BaseEntity; +import net.mingsoft.basic.util.BasicUtil; import net.mingsoft.cms.entity.CategoryEntity; +import net.mingsoft.cms.util.PinYinUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -45,11 +47,11 @@ import net.mingsoft.cms.dao.ICategoryDao; @Service("cmscategoryBizImpl") public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { - + @Autowired private ICategoryDao categoryDao; - - + + @Override protected IBaseDao getDao() { // TODO Auto-generated method stub @@ -64,22 +66,30 @@ public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { @Override public void saveEntity(CategoryEntity categoryEntity) { // TODO Auto-generated method stub + String pingYin = PinYinUtil.getPingYin(categoryEntity.getCategoryTitle()); + CategoryEntity category=new CategoryEntity(); + category.setCategoryPinyin(pingYin); + category.setAppId(BasicUtil.getAppId()); + Object categoryBizEntity = getEntity(category); setParentId(categoryEntity); + categoryEntity.setCategoryPinyin(pingYin); super.saveEntity(categoryEntity); - //保存链接地址 - String path=ObjectUtil.isNotNull(categoryEntity.getCategoryParentId())?categoryEntity.getCategoryParentId():""; - //判断是否有parentIds - if(StringUtils.isNotBlank(path)){ - categoryEntity.setCategoryPath("/" + path.replaceAll(",", "/") + "/" + categoryEntity.getId()); - } else { - categoryEntity.setCategoryPath("/" + categoryEntity.getId()); + //拼音存在则拼接id + if(categoryBizEntity!=null){ + categoryEntity.setCategoryPinyin(pingYin+categoryEntity.getId()); } + CategoryEntity parentCategory = (CategoryEntity)categoryDao.getEntity(Integer.parseInt(categoryEntity.getCategoryId())); + //保存链接地址 + String path=ObjectUtil.isNotNull(parentCategory)?categoryEntity.getCategoryPath():""; + categoryEntity.setCategoryPath( path+"/" + categoryEntity.getCategoryPinyin()); super.updateEntity(categoryEntity); } private void setParentId(CategoryEntity categoryEntity) { + String path = ""; if(StringUtils.isNotEmpty(categoryEntity.getCategoryId())&&Integer.parseInt(categoryEntity.getCategoryId())>0) { CategoryEntity category = (CategoryEntity)categoryDao.getEntity(Integer.parseInt(categoryEntity.getCategoryId())); + path = category.getCategoryPath(); if(StringUtils.isEmpty(category.getCategoryParentId())) { categoryEntity.setCategoryParentId(category.getId()); } else { @@ -90,13 +100,7 @@ public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { } //保存时先保存再修改链接地址,修改时直接修改 if(StringUtils.isNotBlank(categoryEntity.getId())) { - String path = ObjectUtil.isNotNull(categoryEntity.getCategoryParentId()) ? categoryEntity.getCategoryParentId() : ""; - //判断是否有parentIds - if(StringUtils.isNotBlank(path)){ - categoryEntity.setCategoryPath("/" + path.replaceAll(",", "/") + "/" + categoryEntity.getId()); - } else { - categoryEntity.setCategoryPath("/" + categoryEntity.getId()); - } + categoryEntity.setCategoryPath(path+ "/" + categoryEntity.getCategoryPinyin()); } } @@ -110,13 +114,9 @@ public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { } else { x.setCategoryParentId(categoryEntity.getCategoryParentId()+","+categoryEntity.getId()); } - String path=ObjectUtil.isNotNull(x.getCategoryParentId())?x.getCategoryParentId():""; + String path=categoryEntity.getCategoryPath(); //判断是否有parentIds - if(StringUtils.isNotBlank(path)){ - x.setCategoryPath("/"+path.replaceAll(",","/")+"/"+x.getId()); - } else { - x.setCategoryPath("/"+x.getId()); - } + x.setCategoryPath(path+"/"+x.getCategoryPinyin()); super.updateEntity(x); setChildParentId(x); }); @@ -125,6 +125,15 @@ public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { @Override public void updateEntity(CategoryEntity entity) { setParentId(entity); + String pingYin = PinYinUtil.getPingYin(entity.getCategoryTitle()); + CategoryEntity category=new CategoryEntity(); + category.setCategoryPinyin(pingYin); + category.setAppId(BasicUtil.getAppId()); + CategoryEntity categoryBizEntity = (CategoryEntity)getEntity(category); + //拼音存在则拼接id + if(categoryBizEntity!=null&&!categoryBizEntity.getId().equals(entity.getId())){ + entity.setCategoryPinyin(pingYin+entity.getId()); + } super.updateEntity(entity); setChildParentId(entity); } @@ -146,4 +155,4 @@ public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { deleteEntity(categoryId); } } -} \ No newline at end of file +} diff --git a/src/main/java/net/mingsoft/cms/dao/ICategoryDao.xml b/src/main/java/net/mingsoft/cms/dao/ICategoryDao.xml index 3763ba70..bc375824 100644 --- a/src/main/java/net/mingsoft/cms/dao/ICategoryDao.xml +++ b/src/main/java/net/mingsoft/cms/dao/ICategoryDao.xml @@ -5,6 +5,7 @@ + @@ -35,6 +36,7 @@ insert into cms_category category_title, + category_pinyin, category_id, category_type, category_sort, @@ -60,6 +62,7 @@ #{categoryTitle}, + #{categoryPinyin}, #{categoryId}, #{categoryType}, #{categorySort}, @@ -90,6 +93,7 @@ update cms_category category_title=#{categoryTitle}, + category_pinyin=#{categoryPinyin}, category_id=#{categoryId}, category_parent_id=#{categoryParentId}, category_type=#{categoryType}, @@ -126,6 +130,7 @@ select * from cms_category and category_title=#{categoryTitle} + and category_pinyin=#{categoryPinyin} and category_id=#{categoryId} and category_type=#{categoryType} and category_sort=#{categorySort} @@ -210,6 +215,7 @@ select * from cms_category and category_title=#{categoryTitle} + and category_pinyin=#{categoryPinyin} and category_id=#{categoryId} and category_type=#{categoryType} and category_sort=#{categorySort} diff --git a/src/main/java/net/mingsoft/cms/entity/CategoryEntity.java b/src/main/java/net/mingsoft/cms/entity/CategoryEntity.java index 09bdb902..bb6725b4 100644 --- a/src/main/java/net/mingsoft/cms/entity/CategoryEntity.java +++ b/src/main/java/net/mingsoft/cms/entity/CategoryEntity.java @@ -20,6 +20,10 @@ private static final long serialVersionUID = 1574925152750L; */ private String categoryTitle; /** + * 栏目别名 + */ + private String categoryPinyin; + /** * 所属栏目 */ private String categoryId; @@ -112,6 +116,14 @@ private static final long serialVersionUID = 1574925152750L; this.categoryId = categoryId; } + public String getCategoryPinyin() { + return categoryPinyin; + } + + public void setCategoryPinyin(String categoryPinyin) { + this.categoryPinyin = categoryPinyin; + } + /** * 获取所属栏目 */ @@ -326,4 +338,4 @@ private static final long serialVersionUID = 1574925152750L; public String getCategoryParentId() { return this.categoryParentId; } -} \ No newline at end of file +} diff --git a/src/main/java/net/mingsoft/cms/util/PinYinUtil.java b/src/main/java/net/mingsoft/cms/util/PinYinUtil.java new file mode 100644 index 00000000..2d8a308b --- /dev/null +++ b/src/main/java/net/mingsoft/cms/util/PinYinUtil.java @@ -0,0 +1,36 @@ +package net.mingsoft.cms.util; + +import com.github.stuxuhai.jpinyin.PinyinException; +import com.github.stuxuhai.jpinyin.PinyinFormat; +import com.github.stuxuhai.jpinyin.PinyinHelper; +import org.apache.commons.lang3.StringUtils; + +/** + * @author by 铭飞开源团队 + * @Description TODO + * @date 2020/6/16 14:40 + */ +public class PinYinUtil { + + + /** + * 将字符串中的中文转化为拼音,其他字符不变 + * + * @param inputString + * @return + */ + public static String getPingYin(String inputString) { + if (!StringUtils.isBlank(inputString)) { + try { + return PinyinHelper.convertToPinyinString(inputString,"", PinyinFormat.WITHOUT_TONE); + } catch (PinyinException e) { + e.printStackTrace(); + } + } + return ""; + } + + public static void main(String[] args) throws PinyinException { + System.out.println(getPingYin("哈哈6666")); + } +}