InfoController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.entity.CmInfoDocSyn;
  7. import com.caimei.modules.info.service.CmInfoDocSynService;
  8. import com.caimei.modules.live.entity.NewPageLive;
  9. import com.caimei.modules.opensearch.GenerateUtils;
  10. import com.caimei.modules.opensearch.SearchUitls;
  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 SearchUitls searchUitls;
  47. @Autowired
  48. private RedisService redisService;
  49. @Resource
  50. private GenerateUtils generateUtils;
  51. @ModelAttribute
  52. public Info get(@RequestParam(required=false) String id) {
  53. Info entity = null;
  54. if (StringUtils.isNotBlank(id)){
  55. entity = infoService.get(id);
  56. }
  57. if (entity == null){
  58. entity = new Info();
  59. }
  60. return entity;
  61. }
  62. @RequiresPermissions("info:info:view")
  63. @RequestMapping(value = {"list", ""})
  64. public String list(Info info, HttpServletRequest request, HttpServletResponse response, Model model) {
  65. Page<Info> page = infoService.findPage(new Page<Info>(request, response), info);
  66. List<Info> list = page.getList();
  67. if(CollectionUtils.isNotEmpty(list)){
  68. String admin2Server = Global.getConfig("admin2Server");//获取admin2文件服务器地址
  69. for (Info info2 : list) {
  70. String image = info2.getGuidanceImage();
  71. if(StringUtils.isNotBlank(image) && !image.contains("http:") && !image.contains("https:")){//不以http开头的
  72. info2.setGuidanceImage(admin2Server+"/uploadFile/infoImage/"+image);
  73. }
  74. }
  75. }
  76. List<InfoType> typeList = infoTypeService.findList(new InfoType());
  77. List<Info> topList = infoService.findTopList();
  78. model.addAttribute("topLength", topList.size());
  79. model.addAttribute("typeList", typeList);
  80. model.addAttribute("page", page);
  81. return "modules/info/infoList";
  82. }
  83. @RequiresPermissions("info:info:view")
  84. @RequestMapping(value = "list1")
  85. public String list1(Info info, HttpServletRequest request, HttpServletResponse response, Model model) {
  86. info.setEnabledStatus("1");
  87. Page<Info> page = infoService.findPage(new Page<Info>(request, response), info);
  88. /*List<Info> list = page.getList();
  89. if(CollectionUtils.isNotEmpty(list)){
  90. String admin2Server = Global.getConfig("admin2Server");//获取admin2文件服务器地址
  91. for (Info info2 : list) {
  92. String image = info2.getGuidanceImage();
  93. if(StringUtils.isNotBlank(image) && !image.contains("http:") && !image.contains("https:")){//不以http开头的
  94. info2.setGuidanceImage(admin2Server+"/uploadFile/infoImage/"+image);
  95. }
  96. }
  97. }
  98. List<InfoType> typeList = infoTypeService.findList(new InfoType());
  99. model.addAttribute("typeList", typeList);*/
  100. model.addAttribute("page", page);
  101. return "modules/info/infoList1";
  102. }
  103. @RequiresPermissions("info:info:view")
  104. @RequestMapping(value = "form")
  105. public String form(Info info,String ltype,Model model) {
  106. if(StringUtils.isBlank(info.getId())){
  107. info.setRecommendStatus("0");
  108. info.setEnabledStatus("1");
  109. info.setPubdate(new Date());
  110. }
  111. List<InfoType> typeList = infoTypeService.findList(new InfoType());
  112. model.addAttribute("typeList", typeList);
  113. model.addAttribute("info", info);
  114. model.addAttribute("ltype", ltype);
  115. return "modules/info/infoForm";
  116. }
  117. @RequiresPermissions("info:info:edit")
  118. @RequestMapping(value = "save")
  119. public String save(Info info, Model model,String ltype, RedirectAttributes redirectAttributes, HttpServletRequest request) {
  120. if (!beanValidator(model,info)){
  121. return form(info,ltype, model);
  122. }
  123. String content = info.getInfoContent();
  124. int stringLengthByUtf8 = com.caimei.utils.StringUtils.getStringLengthByUtf8(content);
  125. logger.info("信息内容字数"+stringLengthByUtf8);
  126. logger.info("原始传入信息:"+content);
  127. //富文本数量超过text保存最大数
  128. if (stringLengthByUtf8 > 300000) {
  129. List<String> list = new ArrayList<String>();
  130. list.add(0, "信息内容编辑框输入字数超过30万,当前字数:"+stringLengthByUtf8);
  131. addMessage(model, list.toArray(new String[]{}));
  132. return form(info,ltype, model);
  133. }
  134. //新增时才设置随机值
  135. if (null==info.getId() || "".equals(info.getId())){
  136. if (0==info.getBasePv() || "0".equals(info.getBasePv().toString())){
  137. Random random = new Random();
  138. info.setBasePv((long)(random.nextInt(191)+10));
  139. }
  140. }
  141. infoService.save(info,request);
  142. addMessage(redirectAttributes, "保存信息成功");
  143. // 更新索引
  144. searchUitls.updateArticleIndex(Integer.valueOf(info.getId()));
  145. if(StringUtils.equals("1", ltype))
  146. return "redirect:"+Global.getAdminPath()+"/info/infoLabel/list";
  147. return "redirect:"+Global.getAdminPath()+"/info/info/list?repage";
  148. }
  149. @RequiresPermissions("info:info:delete")
  150. @RequestMapping(value = "delete")
  151. public String delete(Info info, RedirectAttributes redirectAttributes) {
  152. infoService.delete(info);
  153. if (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) {
  154. //清除置顶,将后面置顶位的数据往前移
  155. infoService.moveTopList(info);
  156. // 重新生成静态首页
  157. generateUtils.generateHome();
  158. }
  159. addMessage(redirectAttributes, "删除信息成功");
  160. // 删除索引
  161. searchUitls.deleteArticleIndex(Integer.valueOf(info.getId()));
  162. return "redirect:"+Global.getAdminPath()+"/info/info/?repage";
  163. }
  164. /**
  165. * 批量修改启用、停用状态
  166. * @param ids
  167. * @param request
  168. * @param response
  169. * @return
  170. */
  171. @RequiresPermissions("info:info:edit")
  172. @ResponseBody
  173. @RequestMapping(value="updateStatus")
  174. public Map<String, Object> updateStatus(String status,String[] ids,String type,HttpServletRequest request,HttpServletResponse response){
  175. Map<String, Object> map =Maps.newLinkedHashMap();
  176. try {
  177. if(StringUtils.equals("enabledStatus", type)){
  178. infoService.updateEnabledStatusByIds(status,ids);
  179. }else if (StringUtils.equals("recommendStatus", type)){
  180. infoService.updateRecommendStatusByIds(status,ids);
  181. }
  182. map.put("success",true);
  183. map.put("msg", "修改成功");
  184. for (String id : ids){
  185. // 更新索引
  186. searchUitls.updateArticleIndex(Integer.valueOf(id));
  187. Info info = infoService.get(id);
  188. Boolean topFlag = (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) ? true : false;
  189. if (topFlag) {
  190. // 重新生成静态首页
  191. generateUtils.generateHome();
  192. }
  193. }
  194. } catch (Exception e) {
  195. logger.debug(e.toString(),e);
  196. map.put("success",false);
  197. map.put("msg", "修改失败");
  198. }
  199. return map;
  200. }
  201. @RequestMapping(value = "updateTopPosition")
  202. @ResponseBody
  203. public Map<String, Object> updateTopPosition(Integer topPosition,String id,String type){
  204. Map<String, Object> map = Maps.newLinkedHashMap();
  205. try {
  206. Info info = infoService.get(id);
  207. Boolean topFlag = (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) ? true : false;
  208. String oldTopPosition = info.getTopPosition();
  209. if (StringUtils.equals("setTopPosition", type)) {
  210. //更新被替换掉置顶的文章
  211. Info oldInfo = new Info();
  212. oldInfo.setTopPosition(topPosition.toString());
  213. List<Info> replacedInfoList = infoService.findList(oldInfo);
  214. replacedInfoList.forEach(replacedInfo->{
  215. replacedInfo.setTopPosition(null);
  216. infoService.update(replacedInfo);
  217. });
  218. //新的置顶直播
  219. info.setTopPosition(topPosition.toString());
  220. map.put("msg", "置顶成功");
  221. } else if (StringUtils.equals("clearTopPosition", type)) {
  222. //清除置顶,将后面置顶位的数据往前移
  223. infoService.moveTopList(info);
  224. //清除置顶位
  225. info.setTopPosition(null);
  226. map.put("msg", "清除成功");
  227. }
  228. infoService.update(info);
  229. //如果新直播原本也是置顶直播,需要把新直播后面的直播往前挪(新直播更新后再一起挪)
  230. if (StringUtils.equals("setTopPosition", type) && topFlag) {
  231. Info oldInfo = new Info();
  232. oldInfo.setTopPosition(oldTopPosition);
  233. infoService.moveTopList(oldInfo);
  234. }
  235. // 重新生成静态首页
  236. generateUtils.generateHome();
  237. map.put("success",true);
  238. } catch (Exception e) {
  239. logger.debug(e.toString(),e);
  240. map.put("success",false);
  241. map.put("msg", "修改失败");
  242. }
  243. return map;
  244. }
  245. @RequestMapping(value = { "infoList_multiselect" })
  246. public String infoPageMulti(Info info,HttpServletRequest request, HttpServletResponse response, Model model) {
  247. info.setEnabledStatus("1");
  248. Page<Info> page = infoService.findPage(new Page<Info>(request, response), info);
  249. model.addAttribute("page", page);
  250. return "modules/info/infoList_multiselect";
  251. }
  252. /* @RequiresPermissions("info:info:edit")
  253. @ResponseBody
  254. @RequestMapping(value="updateIndex")
  255. public Map<String, Object> updateIndex(String id, HttpServletRequest request, HttpServletResponse response){
  256. Map<String, Object> map = Maps.newLinkedHashMap();
  257. try {
  258. CmInfoDocSyn cmInfoDocSyn = new CmInfoDocSyn();
  259. cmInfoDocSyn.setInfoId(Integer.parseInt(id));
  260. cmInfoDocSyn = cmInfoDocSynService.get(cmInfoDocSyn);
  261. if (null!=cmInfoDocSyn){
  262. cmInfoDocSyn.setSynFlag(0);
  263. cmInfoDocSynService.updateIndex(cmInfoDocSyn);
  264. }
  265. map.put("success",true);
  266. map.put("msg", "更新成功");
  267. } catch (Exception e) {
  268. logger.debug(e.toString(),e);
  269. map.put("success",false);
  270. map.put("msg", "更新失败");
  271. }
  272. return map;
  273. }
  274. @RequiresPermissions("info:info:edit")
  275. @ResponseBody
  276. @RequestMapping(value="updateAllIndex")
  277. public Map<String, Object> updateAllIndex(HttpServletRequest request, HttpServletResponse response){
  278. Map<String, Object> map = Maps.newLinkedHashMap();
  279. try {
  280. cmInfoDocSynService.updateAllIndex();
  281. map.put("success",true);
  282. map.put("msg", "更新成功");
  283. } catch (Exception e) {
  284. logger.debug(e.toString(),e);
  285. map.put("success",false);
  286. map.put("msg", "更新失败");
  287. }
  288. return map;
  289. }*/
  290. }