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 { @Autowired private NavigationService navigationService; @Autowired private AttachmentService attachmentService; @Resource private ContentDao contentDao; @Resource private AttachmentDao attachmentDao; @Transactional(readOnly = true) public PageInfo 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 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(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 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 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(contents); } public List 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 contents = this.selectByExample(example); return contents; } public List findContentsByNavigationId() { List contents = contentDao.findContentsByNavigationId(); for (Content content : contents) { List 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 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 findContentsByNavigationIdForIndex(Integer id) { return contentDao.findContentsByNavigationIdForIndex(id); } public Integer findContentsCount(FindRequest request) { return contentDao.findContentsCount(request); } /** * 获取门户消息 * * @param request * @return */ public List findContentList(FindRequest request) { return contentDao.findContentList(request); } public List searchGlobal(String keyword) { return contentDao.searchGlobal(keyword); } public List randomPic(Integer num) { List result = new ArrayList<>(); Content query = new Content(); query.setDel_flag(false); List contentList = this.findListByWhere(query); int count = 0; List 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(" pics = new ArrayList(); //String regEx_img = "]*?>"; //图片链接地址 String regEx_img = "]*?>"; 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 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的正则表达式{或]*?>[\\s\\S]*?<\\/script> } String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义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; } }