From be9263c0029c209ea275ce6062c1e02f8e930452 Mon Sep 17 00:00:00 2001 From: xierz Date: Fri, 23 Oct 2020 17:56:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=99=E6=80=81=E5=8C=96=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mingsoft/cms/biz/impl/ContentBizImpl.java | 170 +++++++++++++++++- .../net/mingsoft/cms/util/CmsParserUtil.java | 3 + 2 files changed, 172 insertions(+), 1 deletion(-) 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 0039304c..28a15414 100644 --- a/src/main/java/net/mingsoft/cms/biz/impl/ContentBizImpl.java +++ b/src/main/java/net/mingsoft/cms/biz/impl/ContentBizImpl.java @@ -21,14 +21,31 @@ 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.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.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; @@ -43,9 +60,18 @@ import net.mingsoft.cms.dao.IContentDao; @Service("cmscontentBizImpl") public class ContentBizImpl extends BaseBizImpl implements IContentBiz { - + /* + * log4j日志记录 + */ + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); + @Autowired private IContentDao contentDao; + /** + * 栏目管理业务层 + */ + @Autowired + private ICategoryDao categoryDao; @Override @@ -67,4 +93,146 @@ public class ContentBizImpl extends BaseBizImpl implements IContentBiz { } return contentDao.getSearchCount(null,null,whereMap, appId,categoryIds); } + + /* + * 任务调度静态化任务 + */ + public void staticizeTask(Integer appId, String tmpFileName, String generateFileName) { + Date now = new Date(); + LOG.info("定时静态化任务", now); + //生成栏目,更新所有栏目 + try { + DataHolder.set(ParserUtil.APP_ID, appId); + genernateColumn(); + generaterIndex(tmpFileName, generateFileName); + generateArticle(DateUtil.format(now, "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(); + Integer appId = (Integer) DataHolder.get(ParserUtil.APP_ID); + categoryEntity.setAppId(appId); + 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; + } + Integer appId = (Integer) DataHolder.get(ParserUtil.APP_ID); + 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, appId); + 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/util/CmsParserUtil.java b/src/main/java/net/mingsoft/cms/util/CmsParserUtil.java index 8fd6b45f..b3aab933 100644 --- a/src/main/java/net/mingsoft/cms/util/CmsParserUtil.java +++ b/src/main/java/net/mingsoft/cms/util/CmsParserUtil.java @@ -7,6 +7,7 @@ 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; @@ -244,10 +245,12 @@ 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) {