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.request.FindRequest; import platform.modules.home.response.FindResponse; import tk.mybatis.mapper.entity.Example; import java.util.ArrayList; import java.util.List; /** * 内容管理Service * * @author lhf * @version 2017-10-26 */ @Service @Transactional public class ContentService extends BaseService { @Autowired private NavigationService navigationService; @Autowired private AttachmentService attachmentService; @Autowired private ContentDao contentDao; @Autowired 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); } }