ContentBizImpl.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. The MIT License (MIT) * Copyright (c) 2019 铭飞科技
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. * this software and associated documentation files (the "Software"), to deal in
  5. * the Software without restriction, including without limitation the rights to
  6. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7. * the Software, and to permit persons to whom the Software is furnished to do so,
  8. * subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in all
  10. * copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  13. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  14. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  15. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. package net.mingsoft.cms.biz.impl;
  19. import cn.hutool.core.bean.BeanUtil;
  20. import cn.hutool.core.bean.copier.CopyOptions;
  21. import cn.hutool.core.date.DateUtil;
  22. import cn.hutool.core.io.FileUtil;
  23. import net.mingsoft.base.constant.Const;
  24. import net.mingsoft.basic.holder.DataHolder;
  25. import net.mingsoft.basic.util.BasicUtil;
  26. import net.mingsoft.cms.bean.CategoryBean;
  27. import net.mingsoft.cms.bean.ContentBean;
  28. import net.mingsoft.cms.dao.ICategoryDao;
  29. import net.mingsoft.cms.entity.CategoryEntity;
  30. import net.mingsoft.cms.util.CmsParserUtil;
  31. import net.mingsoft.mdiy.bean.AttributeBean;
  32. import net.mingsoft.mdiy.bean.PageBean;
  33. import net.mingsoft.mdiy.entity.ModelEntity;
  34. import net.mingsoft.mdiy.util.ParserUtil;
  35. import org.apache.commons.lang3.StringUtils;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import org.springframework.stereotype.Service;
  40. import net.mingsoft.base.biz.impl.BaseBizImpl;
  41. import net.mingsoft.base.dao.IBaseDao;
  42. import java.io.IOException;
  43. import java.util.*;
  44. import net.mingsoft.cms.biz.IContentBiz;
  45. import net.mingsoft.cms.dao.IContentDao;
  46. /**
  47. * 文章管理持久化层
  48. * @author 铭飞开发团队
  49. * 创建日期:2019-11-28 15:12:32<br/>
  50. * 历史修订:<br/>
  51. */
  52. @Service("cmscontentBizImpl")
  53. public class ContentBizImpl extends BaseBizImpl implements IContentBiz {
  54. /*
  55. * log4j日志记录
  56. */
  57. protected final Logger LOG = LoggerFactory.getLogger(this.getClass());
  58. @Autowired
  59. private IContentDao contentDao;
  60. /**
  61. * 栏目管理业务层
  62. */
  63. @Autowired
  64. private ICategoryDao categoryDao;
  65. @Override
  66. protected IBaseDao getDao() {
  67. // TODO Auto-generated method stub
  68. return contentDao;
  69. }
  70. @Override
  71. public List<CategoryBean> queryIdsByCategoryIdForParser(ContentBean contentBean) {
  72. contentBean.setAppId(BasicUtil.getAppId());
  73. return this.contentDao.queryIdsByCategoryIdForParser(contentBean);
  74. }
  75. @Override
  76. public int getSearchCount(ModelEntity contentModel, List diyList, Map whereMap, int appId, String categoryIds) {
  77. if (contentModel!=null) {
  78. return contentDao.getSearchCount(contentModel.getModelTableName(),diyList,whereMap, appId,categoryIds);
  79. }
  80. return contentDao.getSearchCount(null,null,whereMap, appId,categoryIds);
  81. }
  82. /*
  83. * 任务调度静态化任务
  84. */
  85. public void staticizeTask(Integer appId, String tmpFileName, String generateFileName) {
  86. Date now = new Date();
  87. LOG.info("定时静态化任务", now);
  88. //生成栏目,更新所有栏目
  89. try {
  90. DataHolder.set(ParserUtil.APP_ID, appId);
  91. genernateColumn();
  92. generaterIndex(tmpFileName, generateFileName);
  93. generateArticle(DateUtil.format(now, "yyyy-MM-dd"));
  94. LOG.info("静态化完成", new Date());
  95. } catch (IOException e) {
  96. LOG.info("静态化失败", new Date());
  97. e.printStackTrace();
  98. }
  99. }
  100. //文章
  101. private void generateArticle(String dateTime) throws IOException {
  102. // 网站风格物理路径
  103. List<CategoryBean> articleIdList = null;
  104. List<CategoryEntity> categoryList = null;
  105. AttributeBean attributeBean = new AttributeBean();
  106. ContentBean contentBean = new ContentBean();
  107. contentBean.setBeginTime(dateTime);
  108. Map<String, Object> map = new HashMap<>();
  109. map.put(ParserUtil.APP_ID, BasicUtil.getAppId());
  110. PageBean page = new PageBean();
  111. map.put(ParserUtil.HTML, ParserUtil.HTML);
  112. map.put(ParserUtil.URL, BasicUtil.getUrl());
  113. map.put(ParserUtil.PAGE, page);
  114. CategoryEntity categoryEntity = new CategoryEntity();
  115. categoryList = categoryDao.query(categoryEntity);
  116. for(CategoryEntity category : categoryList){
  117. contentBean.setContentCategoryId(category.getId());
  118. // 分类是列表
  119. if(category.getCategoryType().equals("1")){
  120. // 判断模板文件是否存在
  121. if (!FileUtil.exist(ParserUtil.buildTempletPath(category.getCategoryListUrl())) || StringUtils.isEmpty(category.getCategoryListUrl())) {
  122. LOG.error("模板不存在:{}",category.getCategoryUrl());
  123. continue;
  124. }
  125. // 获取文章列表表属性
  126. ParserUtil.read(category.getCategoryListUrl(),map, page,attributeBean);
  127. contentBean.setFlag(attributeBean.getFlag());
  128. contentBean.setNoflag(attributeBean.getNoflag());
  129. contentBean.setOrder(attributeBean.getOrder());
  130. contentBean.setOrderBy(attributeBean.getOrderby());
  131. }
  132. articleIdList = queryIdsByCategoryIdForParser(contentBean);
  133. // 有符合条件的就更新
  134. if (articleIdList.size() > 0) {
  135. CmsParserUtil.generateBasic(articleIdList);
  136. }
  137. }
  138. }
  139. //栏目
  140. private void genernateColumn() throws IOException {
  141. List<CategoryEntity> columns = new ArrayList<>();
  142. // 获取所有的内容管理栏目
  143. CategoryEntity categoryEntity=new CategoryEntity();
  144. Integer appId = (Integer) DataHolder.get(ParserUtil.APP_ID);
  145. categoryEntity.setAppId(appId);
  146. columns = categoryDao.query(categoryEntity);
  147. List<CategoryBean> articleIdList = null;
  148. // 1、设置模板文件夹路径
  149. // 获取栏目列表模版
  150. for (CategoryEntity column : columns) {
  151. ContentBean contentBean = new ContentBean();
  152. contentBean.setContentCategoryId(column.getId());
  153. // 分类是列表
  154. if(column.getCategoryType().equals("1")) {
  155. // 判断模板文件是否存在
  156. if (!FileUtil.exist(ParserUtil.buildTempletPath(column.getCategoryListUrl()))) {
  157. LOG.error("模板不存在:{}", column.getCategoryUrl());
  158. continue;
  159. }
  160. //获取模板中列表标签中的条件
  161. Map<String, Object> map = new HashMap<>();
  162. map.put(ParserUtil.APP_ID, BasicUtil.getAppId());
  163. PageBean page = new PageBean();
  164. map.put(ParserUtil.HTML, ParserUtil.HTML);
  165. map.put(ParserUtil.URL, BasicUtil.getUrl());
  166. map.put(ParserUtil.PAGE, page);
  167. AttributeBean attributeBean = new AttributeBean();
  168. // 获取文章列表模板标签属性
  169. ParserUtil.read(column.getCategoryListUrl(), map, page, attributeBean);
  170. contentBean.setFlag(attributeBean.getFlag());
  171. contentBean.setNoflag(attributeBean.getNoflag());
  172. contentBean.setOrder(attributeBean.getOrder());
  173. contentBean.setOrderBy(attributeBean.getOrderby());
  174. }
  175. articleIdList = contentDao.queryIdsByCategoryIdForParser(contentBean);
  176. // 判断列表类型
  177. switch (column.getCategoryType()) {
  178. //TODO 暂时先用字符串代替
  179. case "1": // 列表
  180. CmsParserUtil.generateList(column, articleIdList.size());
  181. break;
  182. case "2":// 单页
  183. if(articleIdList.size()==0){
  184. CategoryBean columnArticleIdBean=new CategoryBean();
  185. CopyOptions copyOptions=CopyOptions.create();
  186. copyOptions.setIgnoreError(true);
  187. BeanUtil.copyProperties(column,columnArticleIdBean,copyOptions);
  188. articleIdList.add(columnArticleIdBean);
  189. }
  190. CmsParserUtil.generateBasic(articleIdList);
  191. break;
  192. }
  193. }
  194. }
  195. //主页
  196. private void generaterIndex(String templatePath, String targetPath) throws IOException {
  197. if (!FileUtil.exist(ParserUtil.buildTempletPath())) {
  198. LOG.info("模板文件不存在");
  199. return;
  200. }
  201. Integer appId = (Integer) DataHolder.get(ParserUtil.APP_ID);
  202. Map<String, Object> map = new HashMap<String, Object>();
  203. map.put(ParserUtil.IS_DO, false);
  204. CategoryEntity column = new CategoryEntity();
  205. //内容管理栏目编码
  206. map.put(ParserUtil.COLUMN, column);
  207. //如果单站点,就废弃站点地址
  208. if (ParserUtil.IS_SINGLE) {
  209. map.put(ParserUtil.URL, BasicUtil.getUrl());
  210. }
  211. //设置生成的路径
  212. map.put(ParserUtil.HTML, ParserUtil.HTML);
  213. //设置站点编号
  214. map.put(ParserUtil.APP_ID, appId);
  215. String read = ParserUtil.read(templatePath, map);
  216. FileUtil.writeString(read, ParserUtil.buildHtmlPath(targetPath), Const.UTF8);
  217. }
  218. }