| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- package platform.modules.government.service;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- 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 platform.common.Constant;
- import platform.common.base.service.BaseService;
- import platform.common.util.WebUtil;
- import platform.modules.government.dao.AttachmentDao;
- import platform.modules.government.dao.ContentDao;
- import platform.modules.government.entity.Attachment;
- import platform.modules.government.entity.Content;
- import platform.modules.government.entity.FileDown;
- import platform.modules.government.entity.Navigation;
- import platform.modules.home.dto.SearchGlobalDto;
- import platform.modules.home.request.FindRequest;
- import platform.modules.home.response.FindResponse;
- import tk.mybatis.mapper.entity.Example;
- import javax.annotation.Resource;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 内容管理Service
- *
- * @author lhf
- * @version 2017-10-26
- */
- @Service
- @Transactional
- public class ContentService extends BaseService<Content> {
- @Autowired
- private NavigationService navigationService;
- @Autowired
- private AttachmentService attachmentService;
- @Resource
- private ContentDao contentDao;
- @Resource
- private AttachmentDao attachmentDao;
- @Transactional(readOnly = true)
- public PageInfo<Content> findPage(Integer pageNum, Integer pageSize, String param, Content searchCondition) throws Exception {
- Example example = new Example(Content.class);
- Example.Criteria criteria = example.createCriteria();
- if (StringUtils.isNotBlank(param)) {
- criteria.andLike("title", "%" + WebUtil.covertData(param) + "%");
- }
- if (null != searchCondition.getNavigation_id()) {
- criteria.andEqualTo("navigation_id", searchCondition.getNavigation_id());
- }
- //倒序
- example.orderBy("create_time").desc();
- PageHelper.startPage(pageNum, pageSize);
- List<Content> contents = this.selectByExample(example);
- for (Content content : contents) {
- Navigation navigation = navigationService.findById(content.getNavigation_id());
- if (null != navigation) {
- content.setNavigation_name(navigation.getName());
- }
- }
- return new PageInfo<Content>(contents);
- }
- public Boolean saveContent(Content Content) throws Exception {
- if (this.saveSelective(Content) == 1) {
- if (null != Content.getFileDown().getFile_id()) {
- attachmentDao.updateAttachment(Constant.Attachment.CONTENT, Content.getId(), Content.getFileDown().getFile_id());
- }
- if (null != Content.getAddFileDown().getFile_id()) {
- attachmentDao.updateAttachment(Constant.Attachment.CONTENT_FILE, Content.getId(), Content.getAddFileDown().getFile_id());
- }
- return true;
- }
- return false;
- }
- public Boolean updateContent(Content Content) throws Exception {
- if (this.updateSelective(Content) == 1) {
- attachmentDao.deleteByBusiness(Constant.Attachment.CONTENT, Content.getId());
- if (null != Content.getFileDown().getFile_id()) {
- attachmentDao.updateAttachment(Constant.Attachment.CONTENT, Content.getId(), Content.getFileDown().getFile_id());
- }
- attachmentDao.deleteByBusiness(Constant.Attachment.CONTENT_FILE, Content.getId());
- if (null != Content.getAddFileDown().getFile_id()) {
- attachmentDao.updateAttachment(Constant.Attachment.CONTENT_FILE, Content.getId(), Content.getAddFileDown().getFile_id());
- }
- return true;
- }
- return false;
- }
- //首页通知公告
- @Transactional(readOnly = true)
- public PageInfo<Content> findPageByNavigationId(Integer pageNum, Integer pageSize, String param, Integer navigation_id) throws Exception {
- Example example = new Example(Content.class);
- Example.Criteria criteria = example.createCriteria();
- if (StringUtils.isNotBlank(param)) {
- criteria.andLike("title", "%" + WebUtil.covertData(param) + "%");
- }
- criteria.andEqualTo("navigation_id", navigation_id);
- //倒序
- example.orderBy("sort").asc();
- example.orderBy("create_time").desc();
- PageHelper.startPage(pageNum, pageSize);
- List<Content> contents = this.selectByExample(example);
- for (Content content : contents) {
- Navigation navigation = navigationService.findById(content.getNavigation_id());
- if (null != navigation) {
- content.setNavigation_name(navigation.getName());
- }
- }
- return new PageInfo<Content>(contents);
- }
- public List<Content> findContentsByNavigationId(Integer navigation_id) {
- Example example = new Example(Content.class);
- Example.Criteria criteria = example.createCriteria();
- criteria.andEqualTo("navigation_id", navigation_id);//通知公告
- criteria.andEqualTo("is_start", 1);
- criteria.andEqualTo("del_flag", 0);
- example.orderBy("is_top").desc();
- example.orderBy("sort").asc();
- example.orderBy("create_time").desc();
- PageHelper.startPage(1, 5);
- List<Content> contents = this.selectByExample(example);
- return contents;
- }
- public List<Content> findContentsByNavigationId() {
- List<Content> contents = contentDao.findContentsByNavigationId();
- for (Content content : contents) {
- List<Attachment> attachments = attachmentService.selectByIdAndBusinessId(Constant.Attachment.CONTENT, content.getId(), null);
- if (null != attachments && attachments.size() > 0) {
- FileDown fileDown = new FileDown(attachments.get(0).getId(), attachments.get(0).getFile_name(), attachments.get(0).getFile_url(), attachments.get(0).getDownload_uri());
- content.setFileDown(fileDown);
- }
- }
- return contents;
- }
- public FindResponse findContents(FindRequest request) {
- FindResponse response = new FindResponse();
- List<Content> contents = new ArrayList<>();
- int lastPageNumber = 1;
- int count = contentDao.findContentsCount(request);
- if (count > 0) {
- // 处理分页参数
- if (request.getPage_size() > 0) {
- //如果输入的页码大于实际的分页数,将页码设置为最后一页的页码
- lastPageNumber = (int) ((count - 1) / request.getPage_size() + 1);
- if (request.getPage_number() > lastPageNumber) {
- request.setPage_no(lastPageNumber);
- }
- }
- contents = contentDao.findContents(request);
- }
- response.setContents(contents);
- response.setCounts(count);
- response.setPage_no(request.getPage_no());
- return response;
- }
- public Integer countContent() {
- return contentDao.countContent();
- }
- public List<Content> findContentsByNavigationIdForIndex(Integer id) {
- return contentDao.findContentsByNavigationIdForIndex(id);
- }
- public Integer findContentsCount(FindRequest request) {
- return contentDao.findContentsCount(request);
- }
- /**
- * 获取门户消息
- *
- * @param request
- * @return
- */
- public List<Content> findContentList(FindRequest request) {
- return contentDao.findContentList(request);
- }
- public List<SearchGlobalDto> searchGlobal(String keyword) {
- return contentDao.searchGlobal(keyword);
- }
- public List<Map> randomPic(Integer num) {
- List<Map> result = new ArrayList<>();
- Content query = new Content();
- query.setDel_flag(false);
- List<Content> contentList = this.findListByWhere(query);
- int count = 0;
- List<String> businessIdList = new ArrayList<>();
- long interval = 0;
- Date startTime = new Date();
- while (count < num && interval < 2) {
- Date now = new Date();
- interval = (now.getTime() - startTime.getTime()) / 1000;
- //随机在list里选一个
- int index = (int) (Math.random() * (contentList.size() - 1));
- Content contentEntity = contentList.get(index);
- //排除同一businessid
- boolean idDuplicatedFlag = true;
- for (String businessId : businessIdList) {
- if (Objects.equals(businessId, contentEntity.getId())) {
- idDuplicatedFlag = false;
- }
- }
- if (idDuplicatedFlag) {
- String htmlStr = contentEntity.getContent();
- //排除没有图片标签的
- if (!htmlStr.contains("<img")) {
- continue;
- }
- //排除没有src内容的
- if (!htmlStr.contains("src=")) {
- continue;
- }
- String img = "";
- Pattern p_image;
- Matcher m_image;
- List<String> pics = new ArrayList<String>();
- //String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
- String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
- p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
- m_image = p_image.matcher(htmlStr);
- while (m_image.find()) {
- img = img + "," + m_image.group();
- // Matcher m =
- // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
- Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
- while (m.find()) {
- pics.add(m.group(1));
- }
- }
- Map<String, String> map = new HashMap<>();
- map.put("id", contentEntity.getId() + "");
- map.put("title", contentEntity.getTitle());
- map.put("create_time", contentEntity.getCreate_time());
- map.put("url", pics.get(0));
- /*String textStr = "";
- Pattern p_script;
- Matcher m_script;
- Pattern p_style;
- Matcher m_style;
- Pattern p_html;
- Matcher m_html;
- String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
- String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
- String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
- p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
- m_script = p_script.matcher(htmlStr);
- htmlStr = m_script.replaceAll(""); //过滤script标签
- p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
- m_style = p_style.matcher(htmlStr);
- htmlStr = m_style.replaceAll(""); //过滤style标签
- p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
- m_html = p_html.matcher(htmlStr);
- htmlStr = m_html.replaceAll(""); //过滤html标签
- textStr = htmlStr;
- map.put("content", textStr);*/
- result.add(map);
- businessIdList.add(contentEntity.getId() + "");
- count++;
- }
- }
- // System.out.println("两个时间相差" + interval + "秒");
- return result;
- }
- }
|