ContentBizImpl.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.basic.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.entity.ContentEntity;
  31. import net.mingsoft.cms.util.CmsParserUtil;
  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<IContentDao, ContentEntity> 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. return this.contentDao.queryIdsByCategoryIdForParser(contentBean);
  73. }
  74. @Override
  75. public int getSearchCount(ModelEntity contentModel, List diyList, Map whereMap, int appId, String categoryIds) {
  76. if (contentModel!=null) {
  77. return contentDao.getSearchCount(contentModel.getModelTableName(),diyList,whereMap, appId,categoryIds);
  78. }
  79. return contentDao.getSearchCount(null,null,whereMap, appId,categoryIds);
  80. }
  81. /*
  82. * 任务调度静态化任务
  83. */
  84. public void staticizeTask(Integer appId, String tmpFileName, String generateFileName) {
  85. LOG.info("定时静态化任务", new Date());
  86. try {
  87. //将任务采集传过来的appId导入到线程变量中
  88. //当前线程使用appId时优先使用此数据
  89. DataHolder.set(ParserUtil.APP_ID, appId);
  90. //调用三种静态化
  91. genernateColumn();
  92. generaterIndex(tmpFileName, generateFileName);
  93. //生成文章日期默认为执行日期的上一天
  94. generateArticle(DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"));
  95. LOG.info("静态化完成", new Date());
  96. } catch (IOException e) {
  97. LOG.info("静态化失败", new Date());
  98. e.printStackTrace();
  99. }
  100. }
  101. /*
  102. * 生成文章逻辑
  103. */
  104. private void generateArticle(String dateTime) throws IOException {
  105. // 网站风格物理路径
  106. List<CategoryBean> articleIdList = null;
  107. List<CategoryEntity> categoryList = null;
  108. ContentBean contentBean = new ContentBean();
  109. contentBean.setBeginTime(dateTime);
  110. Map<String, Object> map = new HashMap<>();
  111. if(BasicUtil.getWebsiteApp() != null){
  112. map.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  113. }
  114. PageBean page = new PageBean();
  115. map.put(ParserUtil.HTML, ParserUtil.HTML);
  116. map.put(ParserUtil.URL, BasicUtil.getUrl());
  117. map.put(ParserUtil.PAGE, page);
  118. CategoryEntity categoryEntity = new CategoryEntity();
  119. categoryList = categoryDao.query(categoryEntity);
  120. for(CategoryEntity category : categoryList){
  121. contentBean.setCategoryId(category.getId());
  122. // 分类是列表
  123. if(category.getCategoryType().equals("1")){
  124. // 判断模板文件是否存在
  125. if (!FileUtil.exist(ParserUtil.buildTempletPath(category.getCategoryListUrl())) || StringUtils.isEmpty(category.getCategoryListUrl())) {
  126. LOG.error("模板不存在:{}",category.getCategoryUrl());
  127. continue;
  128. }
  129. }
  130. articleIdList = queryIdsByCategoryIdForParser(contentBean);
  131. // 有符合条件的就更新
  132. if (articleIdList.size() > 0) {
  133. CmsParserUtil.generateBasic(articleIdList);
  134. }
  135. }
  136. }
  137. /*
  138. * 生成栏目逻辑
  139. */
  140. private void genernateColumn() throws IOException {
  141. List<CategoryEntity> columns = new ArrayList<>();
  142. // 获取所有的内容管理栏目
  143. CategoryEntity categoryEntity=new CategoryEntity();
  144. columns = categoryDao.query(categoryEntity);
  145. List<CategoryBean> articleIdList = null;
  146. // 1、设置模板文件夹路径
  147. // 获取栏目列表模版
  148. for (CategoryEntity column : columns) {
  149. ContentBean contentBean = new ContentBean();
  150. contentBean.setCategoryId(column.getId());
  151. // 分类是列表
  152. if(column.getCategoryType().equals("1")) {
  153. // 判断模板文件是否存在
  154. if (!FileUtil.exist(ParserUtil.buildTempletPath(column.getCategoryListUrl()))) {
  155. LOG.error("模板不存在:{}", column.getCategoryUrl());
  156. continue;
  157. }
  158. //获取模板中列表标签中的条件
  159. Map<String, Object> map = new HashMap<>();
  160. if(BasicUtil.getWebsiteApp() != null){
  161. map.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  162. }
  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. }
  168. articleIdList = contentDao.queryIdsByCategoryIdForParser(contentBean);
  169. // 判断列表类型
  170. switch (column.getCategoryType()) {
  171. //TODO 暂时先用字符串代替
  172. case "1": // 列表
  173. CmsParserUtil.generateList(column, articleIdList.size());
  174. break;
  175. case "2":// 单页
  176. if(articleIdList.size()==0){
  177. CategoryBean columnArticleIdBean=new CategoryBean();
  178. CopyOptions copyOptions=CopyOptions.create();
  179. copyOptions.setIgnoreError(true);
  180. BeanUtil.copyProperties(column,columnArticleIdBean,copyOptions);
  181. articleIdList.add(columnArticleIdBean);
  182. }
  183. CmsParserUtil.generateBasic(articleIdList);
  184. break;
  185. }
  186. }
  187. }
  188. /*
  189. * 生成主页逻辑
  190. */
  191. private void generaterIndex(String templatePath, String targetPath) throws IOException {
  192. if (!FileUtil.exist(ParserUtil.buildTempletPath())) {
  193. LOG.info("模板文件不存在");
  194. return;
  195. }
  196. Map<String, Object> map = new HashMap<String, Object>();
  197. map.put(ParserUtil.IS_DO, false);
  198. CategoryEntity column = new CategoryEntity();
  199. //内容管理栏目编码
  200. map.put(ParserUtil.COLUMN, column);
  201. //如果单站点,就废弃站点地址
  202. if (ParserUtil.IS_SINGLE) {
  203. map.put(ParserUtil.URL, BasicUtil.getUrl());
  204. }
  205. //设置生成的路径
  206. map.put(ParserUtil.HTML, ParserUtil.HTML);
  207. //设置站点编号
  208. if(BasicUtil.getWebsiteApp() !=null){
  209. map.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  210. }
  211. String read = ParserUtil.read(templatePath, map);
  212. FileUtil.writeString(read, ParserUtil.buildHtmlPath(targetPath), net.mingsoft.base.constant.Const.UTF8);
  213. }
  214. }