From 4682b0ca51454606f3f9e6b363697b6ff4025efe Mon Sep 17 00:00:00 2001 From: sgjj <995959152@qq.com> Date: Thu, 29 Oct 2020 14:39:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9B=AA=E8=8A=B1=E4=B8=BB?= =?UTF-8?q?=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/5.2-patch.sql | 5 +- pom.xml | 6 + src/main/java/net/mingsoft/MSApplication.java | 2 +- .../mingsoft/cms/action/CategoryAction.java | 26 +-- .../mingsoft/cms/action/ContentAction.java | 26 +-- .../mingsoft/cms/action/web/MCmsAction.java | 31 ++- .../net/mingsoft/cms/bean/CategoryBean.java | 6 +- .../net/mingsoft/cms/biz/ICategoryBiz.java | 2 +- .../net/mingsoft/cms/biz/IContentBiz.java | 3 +- .../cms/biz/impl/CategoryBizImpl.java | 83 +++++++- .../mingsoft/cms/biz/impl/ContentBizImpl.java | 190 +----------------- .../net/mingsoft/cms/dao/ICategoryDao.java | 9 +- .../net/mingsoft/cms/dao/IContentDao.java | 10 +- .../java/net/mingsoft/cms/dao/IContentDao.xml | 2 +- .../mingsoft/cms/entity/CategoryEntity.java | 41 +++- .../mingsoft/cms/entity/ContentEntity.java | 6 +- .../net/mingsoft/cms/util/CmsParserUtil.java | 9 +- .../java/net/mingsoft/config/WebConfig.java | 25 +++ .../interceptor/DMInnerInterceptor.java | 61 ++++++ .../interceptor/MysqlInnerInterceptor.java | 45 +++++ .../WEB-INF/manager/cms/category/form.ftl | 2 +- 21 files changed, 327 insertions(+), 263 deletions(-) create mode 100644 src/main/java/net/mingsoft/interceptor/DMInnerInterceptor.java create mode 100644 src/main/java/net/mingsoft/interceptor/MysqlInnerInterceptor.java diff --git a/doc/5.2-patch.sql b/doc/5.2-patch.sql index e3265eda..fa4f156e 100644 --- a/doc/5.2-patch.sql +++ b/doc/5.2-patch.sql @@ -11,4 +11,7 @@ ALTER TABLE `mdiy_dict` ADD COLUMN `dict_enable` varchar(11) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '1' COMMENT '启用状态' AFTER `is_child`; ALTER TABLE `cms_category` ADD COLUMN `leaf` bigint(1) NULL DEFAULT NULL COMMENT '是否是叶子节点' AFTER `category_pinyin`; -ALTER TABLE `cms_category` ADD COLUMN `top_id` int(11) NULL DEFAULT NULL COMMENT '顶级id' AFTER `leaf`; \ No newline at end of file +ALTER TABLE `cms_category` ADD COLUMN `top_id` bigint(20) NULL DEFAULT NULL COMMENT '顶级id' AFTER `leaf`; +ALTER TABLE `cms_category` MODIFY COLUMN `id` bigint(20) UNSIGNED NOT NULL FIRST ; +ALTER TABLE `cms_content` MODIFY COLUMN `id` bigint(20) UNSIGNED NOT NULL FIRST ; +ALTER TABLE `system_log` MODIFY COLUMN `id` bigint(20) UNSIGNED NOT NULL FIRST ; \ No newline at end of file diff --git a/pom.xml b/pom.xml index 46747c7c..35e845d2 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,12 @@ + + dm + dm.jdbc.driver + 7.0.1 + + net.mingsoft ms-mpeople diff --git a/src/main/java/net/mingsoft/MSApplication.java b/src/main/java/net/mingsoft/MSApplication.java index 8e385616..7ac61580 100644 --- a/src/main/java/net/mingsoft/MSApplication.java +++ b/src/main/java/net/mingsoft/MSApplication.java @@ -13,7 +13,7 @@ import java.util.Locale; @SpringBootApplication @ComponentScan(basePackages = {"net.mingsoft"}) -@MapperScan(basePackages={"**.dao"}) +@MapperScan(basePackages={"**.dao","**.mapper"}) @ServletComponentScan(basePackages = {"net.mingsoft"}) public class MSApplication { public static void main(String[] args) { diff --git a/src/main/java/net/mingsoft/cms/action/CategoryAction.java b/src/main/java/net/mingsoft/cms/action/CategoryAction.java index e79c3eb8..647e2c9e 100644 --- a/src/main/java/net/mingsoft/cms/action/CategoryAction.java +++ b/src/main/java/net/mingsoft/cms/action/CategoryAction.java @@ -4,16 +4,15 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; -import net.mingsoft.base.entity.BaseEntity; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.annotation.LogAnn; import net.mingsoft.basic.bean.EUListBean; import net.mingsoft.basic.constant.e.BusinessTypeEnum; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.PinYinUtil; import net.mingsoft.basic.util.StringUtil; import net.mingsoft.cms.biz.ICategoryBiz; import net.mingsoft.cms.entity.CategoryEntity; -import net.mingsoft.basic.util.PinYinUtil; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -25,6 +24,7 @@ import springfox.documentation.annotations.ApiIgnore; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; + /** * 分类管理控制层 * @author 铭飞开发团队 @@ -34,7 +34,7 @@ import java.util.List; @Api(value = "分类接口") @Controller("cmsCategoryAction") @RequestMapping("/${ms.manager.path}/cms/category") -public class CategoryAction extends BaseAction{ +public class CategoryAction extends BaseAction { /** @@ -84,23 +84,19 @@ public class CategoryAction extends BaseAction{ }) @RequestMapping("/list") @ResponseBody - public ResultData list(@ModelAttribute @ApiIgnore CategoryEntity category,HttpServletResponse response, HttpServletRequest request,@ApiIgnore ModelMap model,BindingResult result) { + public ResultData list(@ModelAttribute @ApiIgnore CategoryEntity category, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model, BindingResult result) { category.setAppId(BasicUtil.getAppId()); BasicUtil.startPage(); List categoryList = categoryBiz.query(category); - return ResultData.build().success(new EUListBean(categoryList,(int)BasicUtil.endPage(categoryList).getTotal())); + 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())); - model.addAttribute("categoryEntity",categoryEntity); - } - model.addAttribute("appId",BasicUtil.getAppId()); + public String form(@ModelAttribute CategoryEntity category, HttpServletResponse response, HttpServletRequest request, ModelMap model){ + model.addAttribute("appId", BasicUtil.getAppId()); return "/cms/category/form"; } @@ -112,12 +108,12 @@ public class CategoryAction extends BaseAction{ @ApiImplicitParam(name = "id", value = "编号", required =true,paramType="query") @GetMapping("/get") @ResponseBody - public ResultData get(@ModelAttribute @ApiIgnore CategoryEntity category,HttpServletResponse response, HttpServletRequest request,@ApiIgnore ModelMap model){ + public ResultData get(@ModelAttribute @ApiIgnore CategoryEntity category, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model){ if(category.getId()==null) { return ResultData.build().error(); } category.setAppId(BasicUtil.getAppId()); - CategoryEntity _category = (CategoryEntity)categoryBiz.getEntity(Integer.parseInt(category.getId())); + CategoryEntity _category = (CategoryEntity)categoryBiz.getById(category.getId()); return ResultData.build().success(_category); } @@ -187,7 +183,7 @@ public class CategoryAction extends BaseAction{ @ResponseBody @LogAnn(title = "删除分类", businessType = BusinessTypeEnum.DELETE) @RequiresPermissions("cms:category:del") - public ResultData delete(@RequestBody List categorys,HttpServletResponse response, HttpServletRequest request) { + public ResultData delete(@RequestBody List categorys, HttpServletResponse response, HttpServletRequest request) { for(int i = 0;i contents,HttpServletResponse response, HttpServletRequest request) { + public ResultData delete(@RequestBody List contents, HttpServletResponse response, HttpServletRequest request) { int[] ids = new int[contents.size()]; for(int i = 0;i{ map.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1")); @@ -132,7 +129,7 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { } catch (IOException e) { e.printStackTrace(); } - return content; + this.outString(resp, content); } /** @@ -140,9 +137,8 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { * @param req * @param resp */ - @ResponseBody @GetMapping("/list.do") - public String list(HttpServletRequest req, HttpServletResponse resp) { + public void list(HttpServletRequest req, HttpServletResponse resp) { Map map = BasicUtil.assemblyRequestMap(); map.forEach((k,v)->{ map.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1")); @@ -156,7 +152,7 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { List columnArticles = contentBiz.queryIdsByCategoryIdForParser(contentBean); //判断栏目下是否有文章 if(columnArticles.size()==0){ - return ""; + this.outJson(resp, false); } //设置分页类 PageBean page = new PageBean(); @@ -189,7 +185,7 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { } catch (IOException e) { e.printStackTrace(); } - return content; + this.outString(resp, content); } /** @@ -197,17 +193,18 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { * @param id 文章编号 */ @GetMapping("/view.do") - @ResponseBody - public String view(String orderby,String order,HttpServletRequest req, HttpServletResponse resp) { + public void view(String orderby,String order,HttpServletRequest req, HttpServletResponse resp) { //参数文章编号 ContentEntity article = (ContentEntity) contentBiz.getEntity(BasicUtil.getInt(ParserUtil.ID)); if(ObjectUtil.isNull(article)){ - throw new BusinessException(this.getResString("err.empty", this.getResString("id"))) ; + this.outJson(resp, null,false,getResString("err.empty", this.getResString("id"))); + return; } if(StringUtils.isNotBlank(order)){ //防注入 if(!order.toLowerCase().equals("asc")&&!order.toLowerCase().equals("desc")){ - throw new BusinessException(this.getResString("err.error", this.getResString("order"))); + this.outJson(resp, null,false,getResString("err.error", this.getResString("order"))); + return; } } @@ -242,7 +239,7 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { ModelEntity contentModel = null; for (int artId = 0; artId < articleIdList.size();) { //如果不是当前文章则跳过 - if(articleIdList.get(artId).getArticleId() != Integer.parseInt(article.getId())){ + if(!articleIdList.get(artId).getArticleId().equals(article.getId())){ artId++; continue; } @@ -292,7 +289,7 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { } catch (IOException e) { e.printStackTrace(); } - return content; + this.outString(resp, content); } @@ -306,7 +303,7 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { */ @RequestMapping(value = "search") @ResponseBody - public String search(HttpServletRequest request, HttpServletResponse response) throws IOException { + public void search(HttpServletRequest request, HttpServletResponse response) throws IOException { Map map = new HashMap<>(); // 读取请求字段 @@ -450,7 +447,7 @@ public class MCmsAction extends net.mingsoft.cms.action.BaseAction { } catch (IOException e) { e.printStackTrace(); } - return content; + this.outString(response, content); } // 清除路径中的转义字符 diff --git a/src/main/java/net/mingsoft/cms/bean/CategoryBean.java b/src/main/java/net/mingsoft/cms/bean/CategoryBean.java index 1c402a02..6ee3c4f4 100644 --- a/src/main/java/net/mingsoft/cms/bean/CategoryBean.java +++ b/src/main/java/net/mingsoft/cms/bean/CategoryBean.java @@ -13,16 +13,16 @@ public class CategoryBean extends CategoryEntity { /** * 文章编号 */ - private int articleId; + private String articleId; - public int getArticleId() { + public String getArticleId() { return articleId; } - public void setArticleId(int articleId) { + public void setArticleId(String articleId) { this.articleId = articleId; } } \ No newline at end of file diff --git a/src/main/java/net/mingsoft/cms/biz/ICategoryBiz.java b/src/main/java/net/mingsoft/cms/biz/ICategoryBiz.java index 8fb29953..202cbd7c 100644 --- a/src/main/java/net/mingsoft/cms/biz/ICategoryBiz.java +++ b/src/main/java/net/mingsoft/cms/biz/ICategoryBiz.java @@ -12,7 +12,7 @@ import java.util.List; * 创建日期:2019-11-28 15:12:32
* 历史修订:
*/ -public interface ICategoryBiz extends IBaseBiz { +public interface ICategoryBiz extends IBaseBiz { /** * 查询当前分类下的所有子分类 diff --git a/src/main/java/net/mingsoft/cms/biz/IContentBiz.java b/src/main/java/net/mingsoft/cms/biz/IContentBiz.java index 2873c7ce..341e79b9 100644 --- a/src/main/java/net/mingsoft/cms/biz/IContentBiz.java +++ b/src/main/java/net/mingsoft/cms/biz/IContentBiz.java @@ -3,6 +3,7 @@ package net.mingsoft.cms.biz; import net.mingsoft.base.biz.IBaseBiz; import net.mingsoft.cms.bean.CategoryBean; import net.mingsoft.cms.bean.ContentBean; +import net.mingsoft.cms.entity.ContentEntity; import net.mingsoft.mdiy.entity.ModelEntity; import java.util.List; @@ -15,7 +16,7 @@ import java.util.Map; * 创建日期:2019-11-28 15:12:32
* 历史修订:
*/ -public interface IContentBiz extends IBaseBiz { +public interface IContentBiz extends IBaseBiz { /** * 根据文章属性查询 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 7986a27b..f9812beb 100644 --- a/src/main/java/net/mingsoft/cms/biz/impl/CategoryBizImpl.java +++ b/src/main/java/net/mingsoft/cms/biz/impl/CategoryBizImpl.java @@ -21,18 +21,21 @@ The MIT License (MIT) * Copyright (c) 2019 铭飞科技 package net.mingsoft.cms.biz.impl; +import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; import net.mingsoft.base.biz.impl.BaseBizImpl; import net.mingsoft.base.dao.IBaseDao; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.PinYinUtil; import net.mingsoft.cms.biz.ICategoryBiz; import net.mingsoft.cms.dao.ICategoryDao; import net.mingsoft.cms.dao.IContentDao; import net.mingsoft.cms.entity.CategoryEntity; -import net.mingsoft.basic.util.PinYinUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -43,7 +46,8 @@ import java.util.List; * 历史修订:
*/ @Service("cmscategoryBizImpl") -public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { + @Transactional(rollbackFor = RuntimeException.class) +public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { @Autowired @@ -74,25 +78,36 @@ public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { Object categoryBizEntity = getEntity(category); setParentId(categoryEntity); categoryEntity.setCategoryPinyin(pingYin); - super.saveEntity(categoryEntity); + //更新新的父级 + if(StrUtil.isNotBlank(categoryEntity.getCategoryId())&&!"0".equals(categoryEntity.getCategoryId())){ + CategoryEntity parent = getById(categoryEntity.getCategoryId()); + //如果之前是叶子节点就更新 + if(parent.getLeaf()){ + parent.setLeaf(false); + updateById(parent); + } + } + categoryEntity.setLeaf(false); + super.save(categoryEntity); //拼音存在则拼接id if(categoryBizEntity!=null){ categoryEntity.setCategoryPinyin(pingYin+categoryEntity.getId()); } CategoryEntity parentCategory = null; if (StringUtils.isNotBlank(categoryEntity.getCategoryId())) { - parentCategory = (CategoryEntity)categoryDao.getEntity(Integer.parseInt(categoryEntity.getCategoryId())); + parentCategory = (CategoryEntity)getById(categoryEntity.getCategoryId()); } //保存链接地址 String path=ObjectUtil.isNotNull(parentCategory)?parentCategory.getCategoryPath():""; categoryEntity.setCategoryPath( path+"/" + categoryEntity.getCategoryPinyin()); - super.updateEntity(categoryEntity); + setTopId(categoryEntity); + super.updateById(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())); + if(StringUtils.isNotEmpty(categoryEntity.getCategoryId())&&Long.parseLong(categoryEntity.getCategoryId())>0) { + CategoryEntity category = (CategoryEntity)getById(categoryEntity.getCategoryId()); path = category.getCategoryPath(); if(StringUtils.isEmpty(category.getCategoryParentId())) { categoryEntity.setCategoryParentId(category.getId()); @@ -138,7 +153,9 @@ public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { if(categoryBizEntity!=null&&!categoryBizEntity.getId().equals(entity.getId())){ entity.setCategoryPinyin(pingYin+entity.getId()); } - super.updateEntity(entity); + setParentLeaf(entity); + setTopId(entity); + super.updateById(entity); setChildParentId(entity); } @@ -165,4 +182,54 @@ public class CategoryBizImpl extends BaseBizImpl implements ICategoryBiz { contentDao.deleteEntityByCategoryIds(ids); } } + + /** + * 设置父级叶子节点 + * @param entity + */ + private void setParentLeaf(CategoryEntity entity){ + Assert.notNull(entity); + CategoryEntity categoryEntity = getById(entity.getId()); + //如果父级不为空并且修改了父级则需要更新父级 + if(!entity.getCategoryId().equals(categoryEntity.getId())){ + //更新旧的父级 + if(StrUtil.isNotBlank(categoryEntity.getCategoryId())&&!"0".equals(categoryEntity.getCategoryId())){ + CategoryEntity parent = getById(categoryEntity.getCategoryId()); + //如果修改了父级则需要判断父级是否还有子节点 + boolean leaf = parent.getLeaf(); + //查找不等于当前更新的分类子集,有则不是叶子节点 + parent.setLeaf(count(lambdaQuery().eq(CategoryEntity::getCategoryId,parent.getId()).ne(CategoryEntity::getId,entity.getId()))==0); + if(leaf!=parent.getLeaf()){ + updateById(parent); + } + + } + //更新新的父级 + if(StrUtil.isNotBlank(entity.getCategoryId())&&!"0".equals(entity.getCategoryId())){ + CategoryEntity parent = getById(entity.getCategoryId()); + //如果之前是叶子节点就更新 + if(parent.getLeaf()){ + parent.setLeaf(false); + updateById(parent); + } + } + } + } + + /** + * 设置顶级id + * @param entity + */ + private void setTopId(CategoryEntity entity){ + String categoryParentId = entity.getCategoryParentId(); + if(StrUtil.isNotBlank(categoryParentId)){ + String[] ids = categoryParentId.split(","); + //如果有ParentId就取第一个 + if(ids.length>0){ + entity.setTopId(ids[0]); + return; + } + } + entity.setTopId("0"); + } } diff --git a/src/main/java/net/mingsoft/cms/biz/impl/ContentBizImpl.java b/src/main/java/net/mingsoft/cms/biz/impl/ContentBizImpl.java index 615be95c..966b921b 100644 --- a/src/main/java/net/mingsoft/cms/biz/impl/ContentBizImpl.java +++ b/src/main/java/net/mingsoft/cms/biz/impl/ContentBizImpl.java @@ -21,35 +21,20 @@ The MIT License (MIT) * Copyright (c) 2019 铭飞科技 package net.mingsoft.cms.biz.impl; -import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.bean.copier.CopyOptions; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.io.FileUtil; -import net.mingsoft.base.constant.Const; -import net.mingsoft.basic.holder.DataHolder; +import net.mingsoft.base.biz.impl.BaseBizImpl; +import net.mingsoft.base.dao.IBaseDao; import net.mingsoft.basic.util.BasicUtil; import net.mingsoft.cms.bean.CategoryBean; import net.mingsoft.cms.bean.ContentBean; -import net.mingsoft.cms.dao.ICategoryDao; -import net.mingsoft.cms.entity.CategoryEntity; -import net.mingsoft.cms.util.CmsParserUtil; -import net.mingsoft.mdiy.bean.AttributeBean; -import net.mingsoft.mdiy.bean.PageBean; +import net.mingsoft.cms.biz.IContentBiz; +import net.mingsoft.cms.dao.IContentDao; +import net.mingsoft.cms.entity.ContentEntity; import net.mingsoft.mdiy.entity.ModelEntity; -import net.mingsoft.mdiy.util.ParserUtil; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import net.mingsoft.base.biz.impl.BaseBizImpl; -import net.mingsoft.base.dao.IBaseDao; - -import java.io.IOException; -import java.util.*; -import net.mingsoft.cms.biz.IContentBiz; -import net.mingsoft.cms.dao.IContentDao; +import java.util.List; +import java.util.Map; /** * 文章管理持久化层 @@ -58,20 +43,11 @@ import net.mingsoft.cms.dao.IContentDao; * 历史修订:
*/ @Service("cmscontentBizImpl") -public class ContentBizImpl extends BaseBizImpl implements IContentBiz { - - /* - * log4j日志记录 - */ - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); +public class ContentBizImpl extends BaseBizImpl implements IContentBiz { + @Autowired private IContentDao contentDao; - /** - * 栏目管理业务层 - */ - @Autowired - private ICategoryDao categoryDao; @Override @@ -93,152 +69,4 @@ public class ContentBizImpl extends BaseBizImpl implements IContentBiz { } return contentDao.getSearchCount(null,null,whereMap, appId,categoryIds); } - - /* - * 任务调度静态化任务 - */ - public void staticizeTask(Integer appId, String tmpFileName, String generateFileName) { - LOG.info("定时静态化任务", new Date()); - try { - //将任务采集传过来的appId导入到线程变量中 - //当前线程使用appId时优先使用此数据 - DataHolder.set(ParserUtil.APP_ID, appId); - //调用三种静态化 - genernateColumn(); - generaterIndex(tmpFileName, generateFileName); - //生成文章日期默认为执行日期的上一天 - generateArticle(DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd")); - LOG.info("静态化完成", new Date()); - } catch (IOException e) { - LOG.info("静态化失败", new Date()); - e.printStackTrace(); - } - } - - /* - * 生成文章逻辑 - */ - private void generateArticle(String dateTime) throws IOException { - // 网站风格物理路径 - List articleIdList = null; - List categoryList = null; - AttributeBean attributeBean = new AttributeBean(); - ContentBean contentBean = new ContentBean(); - contentBean.setBeginTime(dateTime); - Map map = new HashMap<>(); - map.put(ParserUtil.APP_ID, BasicUtil.getAppId()); - PageBean page = new PageBean(); - map.put(ParserUtil.HTML, ParserUtil.HTML); - map.put(ParserUtil.URL, BasicUtil.getUrl()); - map.put(ParserUtil.PAGE, page); - - CategoryEntity categoryEntity = new CategoryEntity(); - categoryList = categoryDao.query(categoryEntity); - for(CategoryEntity category : categoryList){ - contentBean.setContentCategoryId(category.getId()); - // 分类是列表 - if(category.getCategoryType().equals("1")){ - // 判断模板文件是否存在 - if (!FileUtil.exist(ParserUtil.buildTempletPath(category.getCategoryListUrl())) || StringUtils.isEmpty(category.getCategoryListUrl())) { - LOG.error("模板不存在:{}",category.getCategoryUrl()); - continue; - } - // 获取文章列表表属性 - ParserUtil.read(category.getCategoryListUrl(),map, page,attributeBean); - contentBean.setFlag(attributeBean.getFlag()); - contentBean.setNoflag(attributeBean.getNoflag()); - contentBean.setOrder(attributeBean.getOrder()); - contentBean.setOrderBy(attributeBean.getOrderby()); - } - articleIdList = queryIdsByCategoryIdForParser(contentBean); - // 有符合条件的就更新 - if (articleIdList.size() > 0) { - CmsParserUtil.generateBasic(articleIdList); - } - } - } - - /* - * 生成栏目逻辑 - */ - private void genernateColumn() throws IOException { - List columns = new ArrayList<>(); - // 获取所有的内容管理栏目 - CategoryEntity categoryEntity=new CategoryEntity(); - categoryEntity.setAppId(BasicUtil.getAppId()); - columns = categoryDao.query(categoryEntity); - List articleIdList = null; - // 1、设置模板文件夹路径 - // 获取栏目列表模版 - for (CategoryEntity column : columns) { - ContentBean contentBean = new ContentBean(); - contentBean.setContentCategoryId(column.getId()); - // 分类是列表 - if(column.getCategoryType().equals("1")) { - // 判断模板文件是否存在 - if (!FileUtil.exist(ParserUtil.buildTempletPath(column.getCategoryListUrl()))) { - LOG.error("模板不存在:{}", column.getCategoryUrl()); - continue; - } - //获取模板中列表标签中的条件 - Map map = new HashMap<>(); - map.put(ParserUtil.APP_ID, BasicUtil.getAppId()); - PageBean page = new PageBean(); - map.put(ParserUtil.HTML, ParserUtil.HTML); - map.put(ParserUtil.URL, BasicUtil.getUrl()); - map.put(ParserUtil.PAGE, page); - AttributeBean attributeBean = new AttributeBean(); - // 获取文章列表模板标签属性 - ParserUtil.read(column.getCategoryListUrl(), map, page, attributeBean); - contentBean.setFlag(attributeBean.getFlag()); - contentBean.setNoflag(attributeBean.getNoflag()); - contentBean.setOrder(attributeBean.getOrder()); - contentBean.setOrderBy(attributeBean.getOrderby()); - } - articleIdList = contentDao.queryIdsByCategoryIdForParser(contentBean); - // 判断列表类型 - switch (column.getCategoryType()) { - //TODO 暂时先用字符串代替 - case "1": // 列表 - CmsParserUtil.generateList(column, articleIdList.size()); - break; - case "2":// 单页 - if(articleIdList.size()==0){ - CategoryBean columnArticleIdBean=new CategoryBean(); - CopyOptions copyOptions=CopyOptions.create(); - copyOptions.setIgnoreError(true); - BeanUtil.copyProperties(column,columnArticleIdBean,copyOptions); - articleIdList.add(columnArticleIdBean); - } - CmsParserUtil.generateBasic(articleIdList); - break; - } - } - } - - /* - * 生成主页逻辑 - */ - private void generaterIndex(String templatePath, String targetPath) throws IOException { - if (!FileUtil.exist(ParserUtil.buildTempletPath())) { - LOG.info("模板文件不存在"); - return; - } - Map map = new HashMap(); - map.put(ParserUtil.IS_DO, false); - CategoryEntity column = new CategoryEntity(); - //内容管理栏目编码 - map.put(ParserUtil.COLUMN, column); - //如果单站点,就废弃站点地址 - if (ParserUtil.IS_SINGLE) { - map.put(ParserUtil.URL, BasicUtil.getUrl()); - } - //设置生成的路径 - map.put(ParserUtil.HTML, ParserUtil.HTML); - //设置站点编号 - map.put(ParserUtil.APP_ID, BasicUtil.getAppId()); - String read = ParserUtil.read(templatePath, map); - FileUtil.writeString(read, ParserUtil.buildHtmlPath(targetPath), Const.UTF8); - } - } \ No newline at end of file diff --git a/src/main/java/net/mingsoft/cms/dao/ICategoryDao.java b/src/main/java/net/mingsoft/cms/dao/ICategoryDao.java index 388c3ae0..a59075fd 100644 --- a/src/main/java/net/mingsoft/cms/dao/ICategoryDao.java +++ b/src/main/java/net/mingsoft/cms/dao/ICategoryDao.java @@ -1,12 +1,11 @@ package net.mingsoft.cms.dao; import net.mingsoft.base.dao.IBaseDao; -import java.util.*; - import net.mingsoft.cms.entity.CategoryEntity; -import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Component; +import java.util.List; + /** * 分类持久层 * @author 铭飞开发团队 @@ -14,13 +13,13 @@ import org.springframework.stereotype.Component; * 历史修订:
*/ @Component("cmsCategoryDao") -public interface ICategoryDao extends IBaseDao { +public interface ICategoryDao extends IBaseDao { /** * 查询当前分类下面的所有子分类 * @param category 必须存在categoryId categoryParentId * @return */ - public List queryChildren(CategoryEntity category); + public List queryChildren(CategoryEntity category); } \ No newline at end of file diff --git a/src/main/java/net/mingsoft/cms/dao/IContentDao.java b/src/main/java/net/mingsoft/cms/dao/IContentDao.java index 645dc1af..f82e909f 100644 --- a/src/main/java/net/mingsoft/cms/dao/IContentDao.java +++ b/src/main/java/net/mingsoft/cms/dao/IContentDao.java @@ -1,19 +1,21 @@ package net.mingsoft.cms.dao; import net.mingsoft.base.dao.IBaseDao; -import java.util.*; - import net.mingsoft.cms.bean.CategoryBean; import net.mingsoft.cms.bean.ContentBean; +import net.mingsoft.cms.entity.ContentEntity; import org.apache.ibatis.annotations.Param; +import java.util.List; +import java.util.Map; + /** * 文章持久层 * @author 铭飞开发团队 * 创建日期:2019-11-28 15:12:32
* 历史修订:
*/ -public interface IContentDao extends IBaseDao { +public interface IContentDao extends IBaseDao { /** * 查询文章编号集合 @@ -32,7 +34,7 @@ public interface IContentDao extends IBaseDao { * list[2]:是否是等值查询 list[3]:字段的值 * @return 文章实体总数 */ - int getSearchCount(@Param("tableName") String tableName, @Param("diyList") List diyList,@Param("map") Map map, + int getSearchCount(@Param("tableName") String tableName, @Param("diyList") List diyList, @Param("map") Map map, @Param("websiteId") int websiteId, @Param("ids") String ids); /** diff --git a/src/main/java/net/mingsoft/cms/dao/IContentDao.xml b/src/main/java/net/mingsoft/cms/dao/IContentDao.xml index 78b13c1b..59f786aa 100644 --- a/src/main/java/net/mingsoft/cms/dao/IContentDao.xml +++ b/src/main/java/net/mingsoft/cms/dao/IContentDao.xml @@ -245,7 +245,7 @@ and ct.update_by=#{updateBy} and update_date=#{updateDate} - )ct ORDER BY date_format(ct.content_datetime,'%Y-%m-%d') desc,content_sort desc + )ct ORDER BY ct.content_datetime desc,content_sort desc diff --git a/src/main/java/net/mingsoft/cms/entity/CategoryEntity.java b/src/main/java/net/mingsoft/cms/entity/CategoryEntity.java index e91f8b0a..6033b9f4 100644 --- a/src/main/java/net/mingsoft/cms/entity/CategoryEntity.java +++ b/src/main/java/net/mingsoft/cms/entity/CategoryEntity.java @@ -4,16 +4,20 @@ import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.annotation.JSONField; -import org.springframework.format.annotation.DateTimeFormat; +import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import net.mingsoft.base.entity.BaseEntity; +import org.springframework.format.annotation.DateTimeFormat; + import java.util.Date; + /** * 分类实体 * @author 铭飞开发团队 * 创建日期:2019-11-28 15:12:32
* 历史修订:
*/ +@TableName("cms_category") public class CategoryEntity extends BaseEntity { private static final long serialVersionUID = 1574925152750L; @@ -98,6 +102,31 @@ private static final long serialVersionUID = 1574925152750L; */ private String categoryParentId; + /** + * 叶子节点 + */ + private Boolean leaf; + + /** + * 顶级id + */ + private String topId; + + public Boolean getLeaf() { + return leaf; + } + + public void setLeaf(Boolean leaf) { + this.leaf = leaf; + } + + public String getTopId() { + return topId; + } + + public void setTopId(String topId) { + this.topId = topId; + } /** * 设置栏目管理名称 @@ -395,6 +424,14 @@ private static final long serialVersionUID = 1574925152750L; * 获取栏目图片 (标签使用) */ public String getTypelitpic() { - return categoryImg; + if(StrUtil.isNotBlank(categoryImg)){ + try{ + JSONArray objects = JSON.parseArray(categoryImg); + return objects.getJSONObject(0).getString("path"); + }catch (Exception e){ + + } + } + return ""; } } diff --git a/src/main/java/net/mingsoft/cms/entity/ContentEntity.java b/src/main/java/net/mingsoft/cms/entity/ContentEntity.java index 59850a83..4ef89683 100644 --- a/src/main/java/net/mingsoft/cms/entity/ContentEntity.java +++ b/src/main/java/net/mingsoft/cms/entity/ContentEntity.java @@ -1,16 +1,20 @@ package net.mingsoft.cms.entity; import com.alibaba.fastjson.annotation.JSONField; -import org.springframework.format.annotation.DateTimeFormat; +import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import net.mingsoft.base.entity.BaseEntity; +import org.springframework.format.annotation.DateTimeFormat; + import java.util.Date; + /** * 文章实体 * @author 铭飞开发团队 * 创建日期:2019-11-28 15:12:32
* 历史修订:
*/ +@TableName("cms_content") public class ContentEntity extends BaseEntity { private static final long serialVersionUID = 1574925152617L; diff --git a/src/main/java/net/mingsoft/cms/util/CmsParserUtil.java b/src/main/java/net/mingsoft/cms/util/CmsParserUtil.java index b3aab933..7dfa8dc3 100644 --- a/src/main/java/net/mingsoft/cms/util/CmsParserUtil.java +++ b/src/main/java/net/mingsoft/cms/util/CmsParserUtil.java @@ -7,7 +7,6 @@ import freemarker.core.ParseException; import freemarker.template.MalformedTemplateNameException; import freemarker.template.TemplateNotFoundException; import net.mingsoft.base.constant.Const; -import net.mingsoft.basic.holder.DataHolder; import net.mingsoft.basic.util.BasicUtil; import net.mingsoft.basic.util.SpringUtil; import net.mingsoft.cms.bean.CategoryBean; @@ -161,15 +160,15 @@ public class CmsParserUtil extends ParserUtil { Map contentModelMap = new HashMap(); ModelEntity contentModel = null; // 记录已经生成了文章编号 - List generateIds = new ArrayList<>(); - ExecutorService pool=SpringUtil.getBean(ExecutorService.class); + List generateIds = new ArrayList<>(); + ExecutorService pool= SpringUtil.getBean(ExecutorService.class); // 生成文章 for (int artId = 0; artId < articleIdList.size();) { String writePath = null; //设置分页类 PageBean page = new PageBean(); // 文章编号 - int articleId = articleIdList.get(artId).getArticleId(); + String articleId = articleIdList.get(artId).getArticleId(); // 文章的栏目路径 String articleColumnPath = articleIdList.get(artId).getCategoryPath(); // 该文章相关分类 @@ -245,12 +244,10 @@ public class CmsParserUtil extends ParserUtil { HashMap cloneMap = CollUtil.newHashMap(); cloneMap.putAll(parserParams); HttpServletRequest request = SpringUtil.getRequest(); - Integer appId = (Integer) DataHolder.get("appId"); pool.execute(() -> { String content = null; try { SpringUtil.setRequest(request); - DataHolder.set("appId", appId); content = CmsParserUtil.generate(columnUrl, cloneMap); FileUtil.writeString(content, finalWritePath, Const.UTF8); } catch (IOException e) { diff --git a/src/main/java/net/mingsoft/config/WebConfig.java b/src/main/java/net/mingsoft/config/WebConfig.java index 03617d31..c889d46a 100644 --- a/src/main/java/net/mingsoft/config/WebConfig.java +++ b/src/main/java/net/mingsoft/config/WebConfig.java @@ -1,11 +1,17 @@ package net.mingsoft.config; import java.io.File; +import java.sql.SQLException; import java.util.List; import java.util.concurrent.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils; +import net.mingsoft.interceptor.DMInnerInterceptor; +import net.mingsoft.interceptor.MysqlInnerInterceptor; import org.springframework.aop.Advisor; import net.mingsoft.basic.filter.XSSEscapeFilter; import org.springframework.aop.support.DefaultPointcutAdvisor; @@ -37,6 +43,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import net.mingsoft.basic.interceptor.ActionInterceptor; import net.mingsoft.basic.util.BasicUtil; +import javax.sql.DataSource; + @Configuration public class WebConfig implements WebMvcConfigurer { @@ -59,6 +67,23 @@ public class WebConfig implements WebMvcConfigurer { } + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor(DataSource dataSource) { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + try { + //mysql 添加转换sql + DbType dbType = JdbcUtils.getDbType(dataSource.getConnection().getMetaData().getURL()); + if(DbType.MYSQL==dbType){ + interceptor.addInnerInterceptor(new MysqlInnerInterceptor()); + }else if(DbType.DM==dbType){ + interceptor.addInnerInterceptor( new DMInnerInterceptor()); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return interceptor; + } /** * 增加对rest api鉴权的spring mvc拦截器 */ diff --git a/src/main/java/net/mingsoft/interceptor/DMInnerInterceptor.java b/src/main/java/net/mingsoft/interceptor/DMInnerInterceptor.java new file mode 100644 index 00000000..a66b663b --- /dev/null +++ b/src/main/java/net/mingsoft/interceptor/DMInnerInterceptor.java @@ -0,0 +1,61 @@ +package net.mingsoft.interceptor; + +import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; +import com.baomidou.mybatisplus.core.toolkit.PluginUtils; +import com.baomidou.mybatisplus.core.toolkit.TableNameParser; +import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.executor.statement.StatementHandler; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.SqlCommandType; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class DMInnerInterceptor implements InnerInterceptor { + + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { + PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); + if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; + mpBs.sql(this.changeTable(mpBs.sql())); + } + + @Override + public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { + PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); + MappedStatement ms = mpSh.mappedStatement(); + SqlCommandType sct = ms.getSqlCommandType(); + if (sct == SqlCommandType.INSERT || sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { + if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; + PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); + mpBs.sql(this.changeTable(mpBs.sql())); + } + } + + protected String changeTable(String sql) { + TableNameParser parser = new TableNameParser(sql); + List names = new ArrayList<>(); + parser.accept(names::add); + StringBuilder builder = new StringBuilder(); + int last = 0; + for (TableNameParser.SqlToken name : names) { + int start = name.getStart(); + if (start != last) { + builder.append(sql, last, start); + String value = name.getValue(); + builder.append(String.format("\"%s\"",value)); + } + last = name.getEnd(); + } + if (last != sql.length()) { + builder.append(sql.substring(last)); + } + return builder.toString(); + } +} diff --git a/src/main/java/net/mingsoft/interceptor/MysqlInnerInterceptor.java b/src/main/java/net/mingsoft/interceptor/MysqlInnerInterceptor.java new file mode 100644 index 00000000..0389e4a4 --- /dev/null +++ b/src/main/java/net/mingsoft/interceptor/MysqlInnerInterceptor.java @@ -0,0 +1,45 @@ +package net.mingsoft.interceptor; + +import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; +import com.baomidou.mybatisplus.core.toolkit.PluginUtils; +import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.executor.statement.StatementHandler; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.SqlCommandType; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import java.sql.Connection; +import java.sql.SQLException; + +/** + * Mysql 适配通用拦截器,主要处理不支持的sql语句 + */ +public class MysqlInnerInterceptor implements InnerInterceptor { + + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { + PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); + if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; + mpBs.sql(this.changeSql(mpBs.sql())); + } + + @Override + public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { + PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); + MappedStatement ms = mpSh.mappedStatement(); + SqlCommandType sct = ms.getSqlCommandType(); + if (sct == SqlCommandType.INSERT || sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { + if (InterceptorIgnoreHelper.willIgnoreDynamicTableName(ms.getId())) return; + PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); + mpBs.sql(this.changeSql(mpBs.sql())); + } + } + + protected String changeSql(String sql) { + //替换所有"为` + return sql.replaceAll("\"","`"); + } +} diff --git a/src/main/webapp/WEB-INF/manager/cms/category/form.ftl b/src/main/webapp/WEB-INF/manager/cms/category/form.ftl index ef0fc28e..c1d0f32a 100644 --- a/src/main/webapp/WEB-INF/manager/cms/category/form.ftl +++ b/src/main/webapp/WEB-INF/manager/cms/category/form.ftl @@ -249,7 +249,7 @@ // 栏目管理名称 categoryTitle: '', // 所属栏目 - categoryId: '', + categoryId: null, // 栏目管理属性 categoryType: '1', // 自定义顺序