InfoController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package com.caimei.modules.info.web;
  2. import java.util.*;
  3. import javax.annotation.Resource;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.caimei.modules.info.dao.InfoDao;
  8. import com.caimei.modules.info.entity.CmInfoDocSyn;
  9. import com.caimei.modules.info.service.CmInfoDocSynService;
  10. import com.caimei.modules.opensearch.GenerateUtils;
  11. import com.caimei.modules.opensearch.CoreServiceUitls;
  12. import com.caimei.modules.product.dao.KeywordFrequencyDao;
  13. import com.caimei.modules.product.entity.SearchFrequencyVo;
  14. import com.caimei.redis.RedisService;
  15. import org.apache.commons.collections.CollectionUtils;
  16. import org.apache.shiro.authz.annotation.RequiresPermissions;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.ui.Model;
  20. import org.springframework.web.bind.annotation.ModelAttribute;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import org.springframework.web.bind.annotation.ResponseBody;
  24. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  25. import com.caimei.modules.info.entity.Info;
  26. import com.caimei.modules.info.entity.InfoType;
  27. import com.caimei.modules.info.service.InfoService;
  28. import com.caimei.modules.info.service.InfoTypeService;
  29. import com.google.common.collect.Maps;
  30. import com.thinkgem.jeesite.common.config.Global;
  31. import com.thinkgem.jeesite.common.persistence.Page;
  32. import com.thinkgem.jeesite.common.utils.StringUtils;
  33. import com.thinkgem.jeesite.common.web.BaseController;
  34. /**
  35. * 信息列表Controller
  36. *
  37. * @author LG
  38. * @version 2016-06-27
  39. */
  40. @Controller
  41. @RequestMapping(value = "${adminPath}/info/info")
  42. public class InfoController extends BaseController {
  43. @Autowired
  44. private InfoService infoService;
  45. @Autowired
  46. private InfoTypeService infoTypeService;
  47. @Autowired
  48. CmInfoDocSynService cmInfoDocSynService;
  49. @Autowired
  50. private CoreServiceUitls coreServiceUitls;
  51. @Autowired
  52. private RedisService redisService;
  53. @Resource
  54. private GenerateUtils generateUtils;
  55. @Autowired
  56. private InfoDao infoDao;
  57. @Autowired
  58. private KeywordFrequencyDao keywordFrequencyDao;
  59. @ModelAttribute
  60. public Info get(@RequestParam(required = false) String id) {
  61. Info entity = null;
  62. if (StringUtils.isNotBlank(id)) {
  63. entity = infoService.get(id);
  64. }
  65. if (entity == null) {
  66. entity = new Info();
  67. }
  68. return entity;
  69. }
  70. @RequiresPermissions("info:info:view")
  71. @RequestMapping(value = {"list", ""})
  72. public String list(Info info, HttpServletRequest request, HttpServletResponse response, Model model) {
  73. if (null != info.getStartPubDate() && !"".equals(info.getStartPubDate()) && !info.getStartPubDate().endsWith("00:00:00")) {
  74. model.addAttribute("startConfirmTime", info.getStartPubDate());
  75. info.setStartPubDate(info.getStartPubDate().trim() + " 00:00:00");
  76. }
  77. if (null != info.getEndPubDate() && !"".equals(info.getEndPubDate()) && !info.getEndPubDate().endsWith("23:59:59")) {
  78. model.addAttribute("endConfirmTime", info.getEndPubDate());
  79. info.setEndPubDate(info.getEndPubDate().trim() + " 23:59:59");
  80. }
  81. Page<Info> page = infoService.findPage(new Page<Info>(request, response), info);
  82. List<Info> list = page.getList();
  83. if (CollectionUtils.isNotEmpty(list)) {
  84. String admin2Server = Global.getConfig("admin2Server");//获取admin2文件服务器地址
  85. for (Info info2 : list) {
  86. String image = info2.getGuidanceImage();
  87. if (StringUtils.isNotBlank(image) && !image.contains("http:") && !image.contains("https:")) {//不以http开头的
  88. info2.setGuidanceImage(admin2Server + "/uploadFile/infoImage/" + image);
  89. }
  90. }
  91. }
  92. InfoType infoType = new InfoType();
  93. infoType.setEnabledStatus("1");
  94. List<InfoType> typeList = infoTypeService.findList(infoType);
  95. List<Info> topList = infoService.findTopList();
  96. model.addAttribute("topLength", topList.size());
  97. model.addAttribute("typeList", typeList);
  98. model.addAttribute("page", page);
  99. if (null != info.getPublishSource() && 2 == info.getPublishSource()) {
  100. return "modules/info/shopInfoList";
  101. } else {
  102. return "modules/info/infoList";
  103. }
  104. }
  105. @RequiresPermissions("info:info:view")
  106. @RequestMapping(value = "list1")
  107. public String list1(Info info, HttpServletRequest request, HttpServletResponse response, Model model) {
  108. info.setEnabledStatus("1");
  109. Page<Info> page = infoService.findPage(new Page<Info>(request, response), info);
  110. /*List<Info> list = page.getList();
  111. if(CollectionUtils.isNotEmpty(list)){
  112. String admin2Server = Global.getConfig("admin2Server");//获取admin2文件服务器地址
  113. for (Info info2 : list) {
  114. String image = info2.getGuidanceImage();
  115. if(StringUtils.isNotBlank(image) && !image.contains("http:") && !image.contains("https:")){//不以http开头的
  116. info2.setGuidanceImage(admin2Server+"/uploadFile/infoImage/"+image);
  117. }
  118. }
  119. }
  120. List<InfoType> typeList = infoTypeService.findList(new InfoType());
  121. model.addAttribute("typeList", typeList);*/
  122. model.addAttribute("page", page);
  123. return "modules/info/infoList1";
  124. }
  125. @RequiresPermissions("info:info:view")
  126. @RequestMapping(value = "form")
  127. public String form(Info info, String ltype, Model model) {
  128. if (StringUtils.isBlank(info.getId())) {
  129. info.setRecommendStatus("0");
  130. info.setEnabledStatus("1");
  131. info.setPubdate(new Date());
  132. }
  133. InfoType infoType = new InfoType();
  134. List<InfoType> typeList = infoTypeService.findList(infoType);
  135. //先从reids中获取keyword值,不存在时在查询数据库
  136. List<SearchFrequencyVo> searchFrequencyVos = new ArrayList<>();
  137. if (redisService.getExpireTime("keyword") > 0) {
  138. searchFrequencyVos = JSONArray.parseArray(redisService.get("keyword").toString(), SearchFrequencyVo.class);
  139. } else {
  140. searchFrequencyVos = keywordFrequencyDao.getKeywordList();
  141. }
  142. // 敏感词
  143. String sensitiveWords = infoDao.getSensitiveWords(3);
  144. model.addAttribute("typeList", typeList);
  145. model.addAttribute("info", info);
  146. model.addAttribute("ltype", ltype);
  147. model.addAttribute("sensitiveWords", sensitiveWords);
  148. model.addAttribute("SearchFrequencyVo", searchFrequencyVos);
  149. return "modules/info/infoForm";
  150. }
  151. @RequiresPermissions("info:info:edit")
  152. @RequestMapping(value = "save")
  153. public String save(Info info, Model model, String ltype, RedirectAttributes redirectAttributes, HttpServletRequest request) {
  154. if (!beanValidator(model, info)) {
  155. return form(info, ltype, model);
  156. }
  157. String content = info.getInfoContent();
  158. int stringLengthByUtf8 = com.caimei.utils.StringUtils.getStringLengthByUtf8(content);
  159. logger.info("信息内容字数" + stringLengthByUtf8);
  160. logger.info("原始传入信息:" + content);
  161. //富文本数量超过text保存最大数
  162. if (stringLengthByUtf8 > 300000) {
  163. List<String> list = new ArrayList<String>();
  164. list.add(0, "信息内容编辑框输入字数超过30万,当前字数:" + stringLengthByUtf8);
  165. addMessage(model, list.toArray(new String[]{}));
  166. return form(info, ltype, model);
  167. }
  168. //新增时才设置随机值
  169. if (null == info.getId() || "".equals(info.getId())) {
  170. if (0 == info.getBasePv() || "0".equals(info.getBasePv().toString())) {
  171. Random random = new Random();
  172. info.setBasePv((long) (random.nextInt(191) + 10));
  173. }
  174. }
  175. boolean flag = false;
  176. if (StringUtils.isBlank(info.getId())) {
  177. flag = true;
  178. }
  179. // 保存成功返回info.id
  180. infoService.save(info, request);
  181. addMessage(redirectAttributes, "保存信息成功");
  182. // 更新索引
  183. coreServiceUitls.updateArticleIndex(Integer.valueOf(info.getId()));
  184. if (flag) {
  185. // 新增文章 百度链接实时推送
  186. generateUtils.pushBaiduLink("https://www.caimei365.com/info/detail-" + info.getId() + "-1.html");
  187. }
  188. if (StringUtils.equals("1", ltype)) {
  189. return "redirect:" + Global.getAdminPath() + "/info/infoLabel/list";
  190. }
  191. info.setPublishSource(null == info.getPublishSource() ? 1 : info.getPublishSource());
  192. return "redirect:" + Global.getAdminPath() + "/info/info/list?repage&publishSource=" + info.getPublishSource();
  193. }
  194. @RequiresPermissions("info:info:delete")
  195. @RequestMapping(value = "delete")
  196. public String delete(Info info, RedirectAttributes redirectAttributes) {
  197. infoService.delete(info);
  198. if (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) {
  199. //清除置顶,将后面置顶位的数据往前移
  200. infoService.moveTopList(info);
  201. // 重新生成静态首页
  202. generateUtils.generateHome();
  203. }
  204. addMessage(redirectAttributes, "删除信息成功");
  205. // 删除索引
  206. coreServiceUitls.deleteArticleIndex(Integer.valueOf(info.getId()));
  207. return "redirect:" + Global.getAdminPath() + "/info/info/?repage&publishSource=1";
  208. }
  209. /**
  210. * 批量修改启用、停用状态
  211. *
  212. * @param ids
  213. * @param request
  214. * @param response
  215. * @return
  216. */
  217. @RequiresPermissions("info:info:edit")
  218. @ResponseBody
  219. @RequestMapping(value = "updateStatus")
  220. public Map<String, Object> updateStatus(String status, String[] ids, String type, HttpServletRequest request, HttpServletResponse response) {
  221. Map<String, Object> map = Maps.newLinkedHashMap();
  222. try {
  223. if (StringUtils.equals("enabledStatus", type)) {
  224. infoService.updateEnabledStatusByIds(status, ids);
  225. } else if (StringUtils.equals("recommendStatus", type)) {
  226. infoService.updateRecommendStatusByIds(status, ids);
  227. }
  228. map.put("success", true);
  229. map.put("msg", "修改成功");
  230. for (String id : ids) {
  231. // 更新索引
  232. coreServiceUitls.updateArticleIndex(Integer.valueOf(id));
  233. Info info = infoService.get(id);
  234. Boolean topFlag = (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) ? true : false;
  235. if (topFlag) {
  236. // 重新生成静态首页
  237. generateUtils.generateHome();
  238. }
  239. }
  240. } catch (Exception e) {
  241. logger.debug(e.toString(), e);
  242. map.put("success", false);
  243. map.put("msg", "修改失败");
  244. }
  245. return map;
  246. }
  247. @RequestMapping(value = "updateTopPosition")
  248. @ResponseBody
  249. public Map<String, Object> updateTopPosition(Integer topPosition, String id, String type) {
  250. Map<String, Object> map = Maps.newLinkedHashMap();
  251. try {
  252. Info info = infoService.get(id);
  253. Boolean topFlag = (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) ? true : false;
  254. String oldTopPosition = info.getTopPosition();
  255. if (StringUtils.equals("setTopPosition", type)) {
  256. //更新被替换掉置顶的文章
  257. Info oldInfo = new Info();
  258. oldInfo.setTopPosition(topPosition.toString());
  259. List<Info> replacedInfoList = infoService.findList(oldInfo);
  260. replacedInfoList.forEach(replacedInfo -> {
  261. replacedInfo.setTopPosition(null);
  262. infoService.update(replacedInfo);
  263. });
  264. //新的置顶直播
  265. info.setTopPosition(topPosition.toString());
  266. map.put("msg", "置顶成功");
  267. } else if (StringUtils.equals("clearTopPosition", type)) {
  268. //清除置顶,将后面置顶位的数据往前移
  269. infoService.moveTopList(info);
  270. //清除置顶位
  271. info.setTopPosition(null);
  272. map.put("msg", "清除成功");
  273. }
  274. infoService.update(info);
  275. //如果新直播原本也是置顶直播,需要把新直播后面的直播往前挪(新直播更新后再一起挪)
  276. if (StringUtils.equals("setTopPosition", type) && topFlag) {
  277. Info oldInfo = new Info();
  278. oldInfo.setTopPosition(oldTopPosition);
  279. infoService.moveTopList(oldInfo);
  280. }
  281. // 重新生成静态首页
  282. generateUtils.generateHome();
  283. map.put("success", true);
  284. } catch (Exception e) {
  285. logger.debug(e.toString(), e);
  286. map.put("success", false);
  287. map.put("msg", "修改失败");
  288. }
  289. return map;
  290. }
  291. @RequestMapping(value = {"infoList_multiselect"})
  292. public String infoPageMulti(Info info, HttpServletRequest request, HttpServletResponse response, Model model) {
  293. info.setEnabledStatus("1");
  294. Page<Info> page = infoService.findPage(new Page<Info>(request, response), info);
  295. model.addAttribute("page", page);
  296. return "modules/info/infoList_multiselect";
  297. }
  298. @RequestMapping(value = "check")
  299. public String checkInfo(Info info, Model model) {
  300. model.addAttribute("info", info);
  301. return "modules/info/checkInfoPage";
  302. }
  303. @RequestMapping(value = "toAuditPage")
  304. public String toAuditPage(Info info, Model model) {
  305. if (StringUtils.isBlank(info.getId())) {
  306. info.setRecommendStatus("0");
  307. info.setEnabledStatus("1");
  308. info.setPubdate(new Date());
  309. }
  310. InfoType infoType = new InfoType();
  311. List<InfoType> typeList = infoTypeService.findList(infoType);
  312. model.addAttribute("typeList", typeList);
  313. model.addAttribute("info", info);
  314. // 敏感词
  315. String sensitiveWords = infoDao.getSensitiveWords(3);
  316. model.addAttribute("sensitiveWords", sensitiveWords);
  317. return "modules/info/auditInfoPage";
  318. }
  319. @RequestMapping(value = "auditInfo")
  320. @ResponseBody
  321. public Boolean auditInfo(Info info) {
  322. infoService.auditInfo(info);
  323. return true;
  324. }
  325. @RequestMapping(value = "offline")
  326. @ResponseBody
  327. public Boolean offline(Integer id) {
  328. infoService.offlineInfo(id);
  329. return true;
  330. }
  331. }