HomeRefactorService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package platform.modules.home.service;
  2. import com.github.pagehelper.Page;
  3. import com.github.pagehelper.PageHelper;
  4. import com.github.pagehelper.PageInfo;
  5. import io.swagger.models.auth.In;
  6. import org.apache.commons.io.IOUtils;
  7. import org.apache.commons.lang3.StringEscapeUtils;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import platform.common.Constant;
  12. import platform.common.base.model.DictionaryItem;
  13. import platform.common.base.service.DictionaryItemService;
  14. import platform.common.util.ShiroUtils;
  15. import platform.modules.government.dto.FeedbackQuery;
  16. import platform.modules.government.entity.*;
  17. import platform.modules.government.service.*;
  18. import platform.modules.home.dto.HomeRefactorDto;
  19. import platform.modules.home.dto.SearchGlobalDto;
  20. import platform.modules.home.request.FindRequest;
  21. import platform.modules.sys.entity.ActivityDetail;
  22. import platform.modules.sys.entity.ActivityFavourite;
  23. import platform.modules.sys.entity.ActivityFeedback;
  24. import platform.modules.sys.entity.ActivityReview;
  25. import platform.modules.sys.service.*;
  26. import platform.modules.sys.web.ResponseMessage;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.time.LocalDateTime;
  30. import java.time.format.DateTimeFormatter;
  31. import java.util.ArrayList;
  32. import java.util.Base64;
  33. import java.util.List;
  34. import java.util.Objects;
  35. /**
  36. * @author kevin
  37. * @since 2019/5/21 3:45 PM
  38. */
  39. @Service
  40. public class HomeRefactorService {
  41. @Autowired
  42. private ContentService contentService;
  43. @Autowired
  44. private ActivityService activityService;
  45. @Autowired
  46. private ProjectDeclarationService projectDeclarationService;
  47. @Autowired
  48. private ProjectFillingDeclarationService projectFillingDeclarationService;
  49. @Autowired
  50. private DictionaryItemService dictionaryItemService;
  51. @Autowired
  52. private ProjectProvincesDeclarationService projectProvincesDeclarationService;
  53. @Autowired
  54. private AttachmentService attachmentService;
  55. @Autowired
  56. private ActivityReviewService activityReviewService;
  57. @Autowired
  58. private ActivityFeedbackService activityFeedbackService;
  59. @Autowired
  60. private ActivityRegistrationService activityRegistrationService;
  61. @Autowired
  62. private DepartmentService departmentService;
  63. @Autowired
  64. private ActivityFavouriteService activityFavouriteService;
  65. /**
  66. * 门户首页信息
  67. *
  68. * @return
  69. */
  70. public HomeRefactorDto findHomeIndex() {
  71. HomeRefactorDto refactor = new HomeRefactorDto();
  72. FindRequest request = new FindRequest();
  73. request.setPage_size(1);
  74. request.setId(64);
  75. refactor.setMessage(contentService.findContentList(request).get(0));
  76. request.setId(1);
  77. refactor.setActivity(activityService.findContentList(request).getList().get(0));
  78. request.setId(69);
  79. request.setType("");
  80. refactor.setProject(projectDeclarationService.findProjectList(request).getList().get(0));
  81. return refactor;
  82. }
  83. /**
  84. * content列表
  85. *
  86. * @param request
  87. * @return
  88. */
  89. public PageInfo<Content> findContentList(FindRequest request) {
  90. PageHelper.startPage(request.getPage_no(), request.getPage_size());
  91. return new PageInfo<>(contentService.findContentList(request));
  92. }
  93. /**
  94. * 详情
  95. *
  96. * @param id
  97. * @return
  98. */
  99. public Content contentDeatil(Integer id) {
  100. Content content = contentService.findById(id);
  101. if (null == content.getBrowse_count()) {
  102. content.setBrowse_count(0);
  103. }
  104. List<FileDown> fileDowns = new ArrayList<>();
  105. List<Attachment> files = attachmentService.selectByIdAndBusinessId(Constant.Attachment.CONTENT_FILE, content.getId(), null);
  106. if (null != files && files.size() > 0) {
  107. FileDown addFileDown = new FileDown(files.get(0).getId(), files.get(0).getFile_name(), files.get(0).getFile_url(), files.get(0).getDownload_uri());
  108. content.setAddFileDown(addFileDown);
  109. for (Attachment file : files) {
  110. FileDown fileDown = new FileDown(file.getId(), file.getFile_name(), file.getFile_url(), file.getDownload_uri());
  111. fileDowns.add(fileDown);
  112. }
  113. }
  114. content.setFileDowns(fileDowns);
  115. content.setContent(StringEscapeUtils.unescapeHtml4(content.getContent()));
  116. //更新数量
  117. content.setBrowse_count(content.getBrowse_count() + 1);
  118. contentService.updateSelective(content);
  119. return content;
  120. }
  121. /**
  122. * 项目申报详情
  123. *
  124. * @param id
  125. * @return
  126. */
  127. public ResponseMessage findProjectDetail(Integer id, Integer isFilling) {
  128. if (isFilling == 0) {
  129. //项目申报
  130. ProjectDeclaration project = projectDeclarationService.selectById(String.valueOf(id));
  131. if (project != null) {
  132. String sourceName = dictionaryItemService.findNameByTypeAndValue(Constant.DictionaryType.PROJECT_SOURCE, project.getProject_source());
  133. project.setProject_source_name(sourceName);
  134. project.setProject_content(StringEscapeUtils.unescapeHtml4(project.getProject_content()));
  135. //项目来源
  136. String source = project.getProject_source();
  137. if (StringUtils.isNotBlank(source)) {
  138. DictionaryItem sourceItem = dictionaryItemService.findByTypeAndValue(Constant.DictionaryType.PROJECT_SOURCE, source);
  139. if (null != sourceItem) {
  140. project.setProject_source_name(sourceItem.getName());
  141. }
  142. }
  143. if (null == project.getBrowse_count()) {
  144. project.setBrowse_count(0);
  145. }
  146. //更新数量
  147. project.setBrowse_count(project.getBrowse_count() + 1);
  148. projectDeclarationService.updateSelective(project);
  149. }
  150. return ResponseMessage.success("success", project);
  151. } else if (isFilling == 1) {
  152. ProjectProvincesDeclaration provinces = projectProvincesDeclarationService.selectById(String.valueOf(id));
  153. if (null != provinces) {
  154. String sourceName = dictionaryItemService.findNameByTypeAndValue(Constant.DictionaryType.PROJECT_SOURCE, provinces.getProject_source());
  155. provinces.setProject_source_name(sourceName);
  156. provinces.setContent(StringEscapeUtils.unescapeHtml4(provinces.getContent()));
  157. if (null == provinces.getBrowse_count()) {
  158. provinces.setBrowse_count(0);
  159. }
  160. //项目来源
  161. String source = provinces.getProject_source();
  162. if (StringUtils.isNotBlank(source)) {
  163. DictionaryItem sourceItem = dictionaryItemService.findByTypeAndValue(Constant.DictionaryType.PROJECT_SOURCE, source);
  164. if (null != sourceItem) {
  165. provinces.setProject_source_name(sourceItem.getName());
  166. }
  167. }
  168. //更新数量
  169. provinces.setBrowse_count(provinces.getBrowse_count() + 1);
  170. projectProvincesDeclarationService.updateSelective(provinces);
  171. }
  172. return ResponseMessage.success("success", provinces);
  173. }
  174. return ResponseMessage.success("success", null);
  175. }
  176. /**
  177. * 专题活动详情
  178. *
  179. * @param id
  180. * @return
  181. */
  182. public ActivityDetail findByActivityId(Integer id, Integer user_id) {
  183. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  184. //符合条件的载体集合
  185. ActivityDetail activity = activityService.findOne(id.toString());
  186. if (activity != null) {
  187. LocalDateTime localDate = LocalDateTime.parse(activity.getActivity_endtime(), formatter);
  188. if (LocalDateTime.now().isBefore(localDate)) {
  189. activity.setCan_sign_up(true);
  190. }
  191. //活动图片
  192. List<Attachment> attachments = attachmentService.selectByIdAndBusinessId(Constant.Attachment.ACTIVITY, activity.getId(), null);
  193. if (null != attachments && attachments.size() > 0) {
  194. FileDown fileDown = new FileDown(attachments.get(0).getId(), attachments.get(0).getFile_name(), attachments.get(0).getFile_url(), attachments.get(0).getDownload_uri());
  195. activity.setFileDown(fileDown);
  196. }
  197. //报名人数
  198. activity.setSgin_number(activityRegistrationService.getActivityRegistrationNumber(activity.getId()));
  199. //活动回顾
  200. ActivityReview review = activityReviewService.findByActivityId(id);
  201. if (review != null) {
  202. Attachment attachment = attachmentService.findByBizIdAndBizType(review.getId(), Constant.Attachment.REVIEW_ACTIVITY);
  203. if (attachment != null) {
  204. FileDown fileDown = new FileDown(attachment.getId(), attachment.getFile_name(), attachment.getFile_url(),
  205. attachment.getDownload_uri());
  206. review.setFileDown(fileDown);
  207. }
  208. activity.setActivityReview(review);
  209. }
  210. // 综合星级
  211. activity.setAvg_star(activityFeedbackService.findAvgStar(id));
  212. //是否收藏
  213. if (isFavourited(id, user_id)) {
  214. activity.setIs_favourited("1");
  215. } else {
  216. activity.setIs_favourited("0");
  217. }
  218. //浏览数+1
  219. activity.setReading_number(activity.getReading_number() + 1);
  220. activityService.updateSelective(activity);
  221. }
  222. //InputStream is_detail = activityService.getQRcode("1", "350", "activity_id=" + id);
  223. InputStream is_detail = activityService.getQRcode("1", "350", id+"");
  224. try {
  225. byte[] bytes_detail = IOUtils.toByteArray(is_detail);
  226. String encoded_detail = Base64.getEncoder().encodeToString(bytes_detail);
  227. activity.setQr_code("data:image/jpg;base64," + encoded_detail);
  228. } catch (IOException e) {
  229. e.printStackTrace();
  230. }
  231. return activity;
  232. }
  233. /**
  234. * 活动是否收藏
  235. *
  236. * @param id
  237. * @return
  238. */
  239. public boolean isFavourited(Integer id, Integer user_id) {
  240. ActivityFavourite activityFavourite = activityFavouriteService.selectByActivityIdAndUserId(id + "", user_id + "");
  241. if (activityFavourite != null) {
  242. return true;
  243. }
  244. return false;
  245. }
  246. public PageInfo<SearchGlobalDto> searchGlobal(Integer pageNum, Integer pageSize, String keyword) {
  247. PageHelper.startPage(pageNum, pageSize);
  248. List<SearchGlobalDto> res = contentService.searchGlobal(keyword);
  249. for (SearchGlobalDto re : res) {
  250. String title = re.getTitle();
  251. if (title != null) {
  252. title = htmlFilter(title);
  253. if (keyword != null && !Objects.equals(keyword, "")) {
  254. title = title.replace(keyword, "<m>" + keyword + "</m>");
  255. }
  256. }
  257. String content = re.getContent();
  258. if (content != null) {
  259. content = htmlFilter(content);
  260. if (keyword != null && !Objects.equals(keyword, "")) {
  261. content = content.replace(keyword, "<m>" + keyword + "</m>");
  262. }
  263. }
  264. re.setTitle(title);
  265. re.setContent(content);
  266. }
  267. return new PageInfo<>(res);
  268. }
  269. public List<String> searchLatest(Integer size) {
  270. PageInfo<SearchGlobalDto> searchGlobalPage = searchGlobal(1, size, "");
  271. List<SearchGlobalDto> searchGlobalList = searchGlobalPage.getList();
  272. List<String> titles = new ArrayList<>();
  273. for (SearchGlobalDto searchGlobalDto : searchGlobalList) {
  274. titles.add(searchGlobalDto.getTitle());
  275. }
  276. return titles;
  277. }
  278. private String htmlFilter(String html) {
  279. // 过滤文章内容中的html
  280. html = html.replaceAll("</?[^<]+>", "");
  281. // 去除字符串中的空格 回车 换行符 制表符 等
  282. html = html.replaceAll("\\s*|\t|\r|\n", "");
  283. // 去除空格
  284. html = html.replaceAll("&nbsp;", "");
  285. // 去掉其他一些字符
  286. //html = html.replaceAll("\", "");
  287. return html;
  288. }
  289. }