InfoController.java 12 KB

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