ContentService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package platform.modules.government.service;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import platform.common.Constant;
  9. import platform.common.base.service.BaseService;
  10. import platform.common.util.WebUtil;
  11. import platform.modules.government.dao.AttachmentDao;
  12. import platform.modules.government.dao.ContentDao;
  13. import platform.modules.government.entity.Attachment;
  14. import platform.modules.government.entity.Content;
  15. import platform.modules.government.entity.FileDown;
  16. import platform.modules.government.entity.Navigation;
  17. import platform.modules.home.dto.SearchGlobalDto;
  18. import platform.modules.home.request.FindRequest;
  19. import platform.modules.home.response.FindResponse;
  20. import tk.mybatis.mapper.entity.Example;
  21. import javax.annotation.Resource;
  22. import java.util.*;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. /**
  26. * 内容管理Service
  27. *
  28. * @author lhf
  29. * @version 2017-10-26
  30. */
  31. @Service
  32. @Transactional
  33. public class ContentService extends BaseService<Content> {
  34. @Autowired
  35. private NavigationService navigationService;
  36. @Autowired
  37. private AttachmentService attachmentService;
  38. @Resource
  39. private ContentDao contentDao;
  40. @Resource
  41. private AttachmentDao attachmentDao;
  42. @Transactional(readOnly = true)
  43. public PageInfo<Content> findPage(Integer pageNum, Integer pageSize, String param, Content searchCondition) throws Exception {
  44. Example example = new Example(Content.class);
  45. Example.Criteria criteria = example.createCriteria();
  46. if (StringUtils.isNotBlank(param)) {
  47. criteria.andLike("title", "%" + WebUtil.covertData(param) + "%");
  48. }
  49. if (null != searchCondition.getNavigation_id()) {
  50. criteria.andEqualTo("navigation_id", searchCondition.getNavigation_id());
  51. }
  52. //倒序
  53. example.orderBy("create_time").desc();
  54. PageHelper.startPage(pageNum, pageSize);
  55. List<Content> contents = this.selectByExample(example);
  56. for (Content content : contents) {
  57. Navigation navigation = navigationService.findById(content.getNavigation_id());
  58. if (null != navigation) {
  59. content.setNavigation_name(navigation.getName());
  60. }
  61. }
  62. return new PageInfo<Content>(contents);
  63. }
  64. public Boolean saveContent(Content Content) throws Exception {
  65. if (this.saveSelective(Content) == 1) {
  66. if (null != Content.getFileDown().getFile_id()) {
  67. attachmentDao.updateAttachment(Constant.Attachment.CONTENT, Content.getId(), Content.getFileDown().getFile_id());
  68. }
  69. if (null != Content.getAddFileDown().getFile_id()) {
  70. attachmentDao.updateAttachment(Constant.Attachment.CONTENT_FILE, Content.getId(), Content.getAddFileDown().getFile_id());
  71. }
  72. return true;
  73. }
  74. return false;
  75. }
  76. public Boolean updateContent(Content Content) throws Exception {
  77. if (this.updateSelective(Content) == 1) {
  78. attachmentDao.deleteByBusiness(Constant.Attachment.CONTENT, Content.getId());
  79. if (null != Content.getFileDown().getFile_id()) {
  80. attachmentDao.updateAttachment(Constant.Attachment.CONTENT, Content.getId(), Content.getFileDown().getFile_id());
  81. }
  82. attachmentDao.deleteByBusiness(Constant.Attachment.CONTENT_FILE, Content.getId());
  83. if (null != Content.getAddFileDown().getFile_id()) {
  84. attachmentDao.updateAttachment(Constant.Attachment.CONTENT_FILE, Content.getId(), Content.getAddFileDown().getFile_id());
  85. }
  86. return true;
  87. }
  88. return false;
  89. }
  90. //首页通知公告
  91. @Transactional(readOnly = true)
  92. public PageInfo<Content> findPageByNavigationId(Integer pageNum, Integer pageSize, String param, Integer navigation_id) throws Exception {
  93. Example example = new Example(Content.class);
  94. Example.Criteria criteria = example.createCriteria();
  95. if (StringUtils.isNotBlank(param)) {
  96. criteria.andLike("title", "%" + WebUtil.covertData(param) + "%");
  97. }
  98. criteria.andEqualTo("navigation_id", navigation_id);
  99. //倒序
  100. example.orderBy("sort").asc();
  101. example.orderBy("create_time").desc();
  102. PageHelper.startPage(pageNum, pageSize);
  103. List<Content> contents = this.selectByExample(example);
  104. for (Content content : contents) {
  105. Navigation navigation = navigationService.findById(content.getNavigation_id());
  106. if (null != navigation) {
  107. content.setNavigation_name(navigation.getName());
  108. }
  109. }
  110. return new PageInfo<Content>(contents);
  111. }
  112. public List<Content> findContentsByNavigationId(Integer navigation_id) {
  113. Example example = new Example(Content.class);
  114. Example.Criteria criteria = example.createCriteria();
  115. criteria.andEqualTo("navigation_id", navigation_id);//通知公告
  116. criteria.andEqualTo("is_start", 1);
  117. criteria.andEqualTo("del_flag", 0);
  118. example.orderBy("is_top").desc();
  119. example.orderBy("sort").asc();
  120. example.orderBy("create_time").desc();
  121. PageHelper.startPage(1, 5);
  122. List<Content> contents = this.selectByExample(example);
  123. return contents;
  124. }
  125. public List<Content> findContentsByNavigationId() {
  126. List<Content> contents = contentDao.findContentsByNavigationId();
  127. for (Content content : contents) {
  128. List<Attachment> attachments = attachmentService.selectByIdAndBusinessId(Constant.Attachment.CONTENT, content.getId(), null);
  129. if (null != attachments && attachments.size() > 0) {
  130. FileDown fileDown = new FileDown(attachments.get(0).getId(), attachments.get(0).getFile_name(), attachments.get(0).getFile_url(), attachments.get(0).getDownload_uri());
  131. content.setFileDown(fileDown);
  132. }
  133. }
  134. return contents;
  135. }
  136. public FindResponse findContents(FindRequest request) {
  137. FindResponse response = new FindResponse();
  138. List<Content> contents = new ArrayList<>();
  139. int lastPageNumber = 1;
  140. int count = contentDao.findContentsCount(request);
  141. if (count > 0) {
  142. // 处理分页参数
  143. if (request.getPage_size() > 0) {
  144. //如果输入的页码大于实际的分页数,将页码设置为最后一页的页码
  145. lastPageNumber = (int) ((count - 1) / request.getPage_size() + 1);
  146. if (request.getPage_number() > lastPageNumber) {
  147. request.setPage_no(lastPageNumber);
  148. }
  149. }
  150. contents = contentDao.findContents(request);
  151. }
  152. response.setContents(contents);
  153. response.setCounts(count);
  154. response.setPage_no(request.getPage_no());
  155. return response;
  156. }
  157. public Integer countContent() {
  158. return contentDao.countContent();
  159. }
  160. public List<Content> findContentsByNavigationIdForIndex(Integer id) {
  161. return contentDao.findContentsByNavigationIdForIndex(id);
  162. }
  163. public Integer findContentsCount(FindRequest request) {
  164. return contentDao.findContentsCount(request);
  165. }
  166. /**
  167. * 获取门户消息
  168. *
  169. * @param request
  170. * @return
  171. */
  172. public List<Content> findContentList(FindRequest request) {
  173. return contentDao.findContentList(request);
  174. }
  175. public List<SearchGlobalDto> searchGlobal(String keyword) {
  176. return contentDao.searchGlobal(keyword);
  177. }
  178. public List<Map> randomPic(Integer num) {
  179. List<Map> result = new ArrayList<>();
  180. Content query = new Content();
  181. query.setDel_flag(false);
  182. List<Content> contentList = this.findListByWhere(query);
  183. int count = 0;
  184. List<String> businessIdList = new ArrayList<>();
  185. long interval = 0;
  186. Date startTime = new Date();
  187. while (count < num && interval < 2) {
  188. Date now = new Date();
  189. interval = (now.getTime() - startTime.getTime()) / 1000;
  190. //随机在list里选一个
  191. int index = (int) (Math.random() * (contentList.size() - 1));
  192. Content contentEntity = contentList.get(index);
  193. //排除同一businessid
  194. boolean idDuplicatedFlag = true;
  195. for (String businessId : businessIdList) {
  196. if (Objects.equals(businessId, contentEntity.getId())) {
  197. idDuplicatedFlag = false;
  198. }
  199. }
  200. if (idDuplicatedFlag) {
  201. String htmlStr = contentEntity.getContent();
  202. //排除没有图片标签的
  203. if (!htmlStr.contains("<img")) {
  204. continue;
  205. }
  206. //排除没有src内容的
  207. if (!htmlStr.contains("src=")) {
  208. continue;
  209. }
  210. String img = "";
  211. Pattern p_image;
  212. Matcher m_image;
  213. List<String> pics = new ArrayList<String>();
  214. //String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
  215. String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
  216. p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
  217. m_image = p_image.matcher(htmlStr);
  218. while (m_image.find()) {
  219. img = img + "," + m_image.group();
  220. // Matcher m =
  221. // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
  222. Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
  223. while (m.find()) {
  224. pics.add(m.group(1));
  225. }
  226. }
  227. Map<String, String> map = new HashMap<>();
  228. map.put("id", contentEntity.getId() + "");
  229. map.put("title", contentEntity.getTitle());
  230. map.put("create_time", contentEntity.getCreate_time());
  231. map.put("url", pics.get(0));
  232. /*String textStr = "";
  233. Pattern p_script;
  234. Matcher m_script;
  235. Pattern p_style;
  236. Matcher m_style;
  237. Pattern p_html;
  238. Matcher m_html;
  239. String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
  240. String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
  241. String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
  242. p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
  243. m_script = p_script.matcher(htmlStr);
  244. htmlStr = m_script.replaceAll(""); //过滤script标签
  245. p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
  246. m_style = p_style.matcher(htmlStr);
  247. htmlStr = m_style.replaceAll(""); //过滤style标签
  248. p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
  249. m_html = p_html.matcher(htmlStr);
  250. htmlStr = m_html.replaceAll(""); //过滤html标签
  251. textStr = htmlStr;
  252. map.put("content", textStr);*/
  253. result.add(map);
  254. businessIdList.add(contentEntity.getId() + "");
  255. count++;
  256. }
  257. }
  258. // System.out.println("两个时间相差" + interval + "秒");
  259. return result;
  260. }
  261. }