ContentService.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.request.FindRequest;
  18. import platform.modules.home.response.FindResponse;
  19. import tk.mybatis.mapper.entity.Example;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. /**
  23. * 内容管理Service
  24. *
  25. * @author lhf
  26. * @version 2017-10-26
  27. */
  28. @Service
  29. @Transactional
  30. public class ContentService extends BaseService<Content> {
  31. @Autowired
  32. private NavigationService navigationService;
  33. @Autowired
  34. private AttachmentService attachmentService;
  35. @Autowired
  36. private ContentDao contentDao;
  37. @Autowired
  38. private AttachmentDao attachmentDao;
  39. @Transactional(readOnly = true)
  40. public PageInfo<Content> findPage(Integer pageNum, Integer pageSize, String param, Content searchCondition) throws Exception {
  41. Example example = new Example(Content.class);
  42. Example.Criteria criteria = example.createCriteria();
  43. if (StringUtils.isNotBlank(param)) {
  44. criteria.andLike("title", "%" + WebUtil.covertData(param) + "%");
  45. }
  46. if (null != searchCondition.getNavigation_id()) {
  47. criteria.andEqualTo("navigation_id", searchCondition.getNavigation_id());
  48. }
  49. //倒序
  50. example.orderBy("create_time").desc();
  51. PageHelper.startPage(pageNum, pageSize);
  52. List<Content> contents = this.selectByExample(example);
  53. for (Content content : contents) {
  54. Navigation navigation = navigationService.findById(content.getNavigation_id());
  55. if (null != navigation) {
  56. content.setNavigation_name(navigation.getName());
  57. }
  58. }
  59. return new PageInfo<Content>(contents);
  60. }
  61. public Boolean saveContent(Content Content) throws Exception {
  62. if (this.saveSelective(Content) == 1) {
  63. if (null != Content.getFileDown().getFile_id()) {
  64. attachmentDao.updateAttachment(Constant.Attachment.CONTENT, Content.getId(), Content.getFileDown().getFile_id());
  65. }
  66. if (null != Content.getAddFileDown().getFile_id()) {
  67. attachmentDao.updateAttachment(Constant.Attachment.CONTENT_FILE, Content.getId(), Content.getAddFileDown().getFile_id());
  68. }
  69. return true;
  70. }
  71. return false;
  72. }
  73. public Boolean updateContent(Content Content) throws Exception {
  74. if (this.updateSelective(Content) == 1) {
  75. attachmentDao.deleteByBusiness(Constant.Attachment.CONTENT, Content.getId());
  76. if (null != Content.getFileDown().getFile_id()) {
  77. attachmentDao.updateAttachment(Constant.Attachment.CONTENT, Content.getId(), Content.getFileDown().getFile_id());
  78. }
  79. attachmentDao.deleteByBusiness(Constant.Attachment.CONTENT_FILE, Content.getId());
  80. if (null != Content.getAddFileDown().getFile_id()) {
  81. attachmentDao.updateAttachment(Constant.Attachment.CONTENT_FILE, Content.getId(), Content.getAddFileDown().getFile_id());
  82. }
  83. return true;
  84. }
  85. return false;
  86. }
  87. //首页通知公告
  88. @Transactional(readOnly = true)
  89. public PageInfo<Content> findPageByNavigationId(Integer pageNum, Integer pageSize, String param, Integer navigation_id) throws Exception {
  90. Example example = new Example(Content.class);
  91. Example.Criteria criteria = example.createCriteria();
  92. if (StringUtils.isNotBlank(param)) {
  93. criteria.andLike("title", "%" + WebUtil.covertData(param) + "%");
  94. }
  95. criteria.andEqualTo("navigation_id", navigation_id);
  96. //倒序
  97. example.orderBy("sort").asc();
  98. example.orderBy("create_time").desc();
  99. PageHelper.startPage(pageNum, pageSize);
  100. List<Content> contents = this.selectByExample(example);
  101. for (Content content : contents) {
  102. Navigation navigation = navigationService.findById(content.getNavigation_id());
  103. if (null != navigation) {
  104. content.setNavigation_name(navigation.getName());
  105. }
  106. }
  107. return new PageInfo<Content>(contents);
  108. }
  109. public List<Content> findContentsByNavigationId(Integer navigation_id) {
  110. Example example = new Example(Content.class);
  111. Example.Criteria criteria = example.createCriteria();
  112. criteria.andEqualTo("navigation_id", navigation_id);//通知公告
  113. criteria.andEqualTo("is_start", 1);
  114. criteria.andEqualTo("del_flag", 0);
  115. example.orderBy("is_top").desc();
  116. example.orderBy("sort").asc();
  117. example.orderBy("create_time").desc();
  118. PageHelper.startPage(1, 5);
  119. List<Content> contents = this.selectByExample(example);
  120. return contents;
  121. }
  122. public List<Content> findContentsByNavigationId() {
  123. List<Content> contents = contentDao.findContentsByNavigationId();
  124. for (Content content : contents) {
  125. List<Attachment> attachments = attachmentService.selectByIdAndBusinessId(Constant.Attachment.CONTENT, content.getId(), null);
  126. if (null != attachments && attachments.size() > 0) {
  127. FileDown fileDown = new FileDown(attachments.get(0).getId(), attachments.get(0).getFile_name(), attachments.get(0).getFile_url(), attachments.get(0).getDownload_uri());
  128. content.setFileDown(fileDown);
  129. }
  130. }
  131. return contents;
  132. }
  133. public FindResponse findContents(FindRequest request) {
  134. FindResponse response = new FindResponse();
  135. List<Content> contents = new ArrayList<>();
  136. int lastPageNumber = 1;
  137. int count = contentDao.findContentsCount(request);
  138. if (count > 0) {
  139. // 处理分页参数
  140. if (request.getPage_size() > 0) {
  141. //如果输入的页码大于实际的分页数,将页码设置为最后一页的页码
  142. lastPageNumber = (int) ((count - 1) / request.getPage_size() + 1);
  143. if (request.getPage_number() > lastPageNumber) {
  144. request.setPage_no(lastPageNumber);
  145. }
  146. }
  147. contents = contentDao.findContents(request);
  148. }
  149. response.setContents(contents);
  150. response.setCounts(count);
  151. response.setPage_no(request.getPage_no());
  152. return response;
  153. }
  154. public Integer countContent() {
  155. return contentDao.countContent();
  156. }
  157. public List<Content> findContentsByNavigationIdForIndex(Integer id) {
  158. return contentDao.findContentsByNavigationIdForIndex(id);
  159. }
  160. public Integer findContentsCount(FindRequest request) {
  161. return contentDao.findContentsCount(request);
  162. }
  163. /**
  164. * 获取门户消息
  165. * 、
  166. * @param request
  167. * @return
  168. */
  169. public List<Content> findContentList(FindRequest request) {
  170. return contentDao.findContentList(request);
  171. }
  172. }