package com.caimei.modules.info.web; import com.caimei.modules.info.dao.InfoDao; import com.caimei.modules.info.entity.CmRelated; import com.caimei.modules.info.entity.Info; import com.caimei.modules.info.entity.InfoType; import com.caimei.modules.info.service.CmInfoDocSynService; import com.caimei.modules.info.service.CmRelatedService; import com.caimei.modules.info.service.InfoService; import com.caimei.modules.info.service.InfoTypeService; import com.caimei.modules.opensearch.CoreServiceUitls; import com.caimei.modules.opensearch.GenerateUtils; import com.caimei.modules.product.dao.KeywordFrequencyDao; import com.caimei.modules.product.entity.SearchFrequencyVo; import com.caimei.redis.RedisService; import com.google.common.collect.Maps; import com.thinkgem.jeesite.common.config.Global; import com.thinkgem.jeesite.common.persistence.Page; import com.thinkgem.jeesite.common.utils.StringUtils; import com.thinkgem.jeesite.common.web.BaseController; import org.apache.commons.collections.CollectionUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.*; /** * 信息列表Controller * * @author LG * @version 2016-06-27 */ @Controller @RequestMapping(value = "${adminPath}/info/info") public class InfoController extends BaseController { @Autowired private InfoService infoService; @Autowired private CmRelatedService cmRelatedService; @Autowired private InfoTypeService infoTypeService; @Autowired CmInfoDocSynService cmInfoDocSynService; @Autowired private CoreServiceUitls coreServiceUitls; @Autowired private RedisService redisService; @Resource private GenerateUtils generateUtils; @Autowired private InfoDao infoDao; @Autowired private KeywordFrequencyDao keywordFrequencyDao; @ModelAttribute public Info get(@RequestParam(required = false) String id) { Info entity = null; if (StringUtils.isNotBlank(id)) { entity = infoService.get(id); } if (entity == null) { entity = new Info(); } return entity; } @RequiresPermissions("info:info:view") @RequestMapping(value = {"list", ""}) public String list(Info info, HttpServletRequest request, HttpServletResponse response, Model model) { if (null != info.getStartPubDate() && !"".equals(info.getStartPubDate()) && !info.getStartPubDate().endsWith("00:00:00")) { model.addAttribute("startConfirmTime", info.getStartPubDate()); info.setStartPubDate(info.getStartPubDate().trim() + " 00:00:00"); } if (null != info.getEndPubDate() && !"".equals(info.getEndPubDate()) && !info.getEndPubDate().endsWith("23:59:59")) { model.addAttribute("endConfirmTime", info.getEndPubDate()); info.setEndPubDate(info.getEndPubDate().trim() + " 23:59:59"); } Page page = infoService.findPage(new Page(request, response), info); List list = page.getList(); if (CollectionUtils.isNotEmpty(list)) { String admin2Server = Global.getConfig("admin2Server");//获取admin2文件服务器地址 for (Info info2 : list) { String image = info2.getGuidanceImage(); if (StringUtils.isNotBlank(image) && !image.contains("http:") && !image.contains("https:")) {//不以http开头的 info2.setGuidanceImage(admin2Server + "/uploadFile/infoImage/" + image); } } } InfoType infoType = new InfoType(); infoType.setType("1"); infoType.setEnabledStatus("1"); List typeList = infoTypeService.findList(infoType); List topList = infoService.findTopList(); model.addAttribute("topLength", topList.size()); model.addAttribute("typeList", typeList); model.addAttribute("page", page); if (null != info.getPublishSource() && 2 == info.getPublishSource()) { return "modules/info/shopInfoList"; } else { return "modules/info/infoList"; } } @RequiresPermissions("info:info:view") @RequestMapping(value = "list1") public String list1(Info info, HttpServletRequest request, HttpServletResponse response, Model model) { info.setEnabledStatus("1"); Page page = infoService.findPage(new Page(request, response), info); /*List list = page.getList(); if(CollectionUtils.isNotEmpty(list)){ String admin2Server = Global.getConfig("admin2Server");//获取admin2文件服务器地址 for (Info info2 : list) { String image = info2.getGuidanceImage(); if(StringUtils.isNotBlank(image) && !image.contains("http:") && !image.contains("https:")){//不以http开头的 info2.setGuidanceImage(admin2Server+"/uploadFile/infoImage/"+image); } } } List typeList = infoTypeService.findList(new InfoType()); model.addAttribute("typeList", typeList);*/ model.addAttribute("page", page); return "modules/info/infoList1"; } @RequiresPermissions("info:info:view") @RequestMapping(value = "form") public String form(Info info, String ltype, Model model) { if (StringUtils.isBlank(info.getId())) { info.setRecommendStatus("0"); info.setEnabledStatus("1"); info.setPubdate(new Date()); } InfoType infoType = new InfoType(); List typeList = infoTypeService.findList(infoType); //先从reids中获取keyword值,不存在时在查询数据库 List searchFrequencyVos = new ArrayList<>(); searchFrequencyVos = keywordFrequencyDao.getKeywordList(); // 敏感词 String sensitiveWords = infoDao.getSensitiveWords(3); model.addAttribute("typeList", typeList); model.addAttribute("info", info); model.addAttribute("ltype", ltype); model.addAttribute("sensitiveWords", sensitiveWords); model.addAttribute("SearchFrequencyVo", searchFrequencyVos); return "modules/info/infoForm"; } @RequiresPermissions("info:info:view") @RequestMapping(value = "relatedForm") public String relatedForm(Info info, String ltype, Model model) { CmRelated cmRelated = new CmRelated(); cmRelated.setAuthorId(info.getId()); cmRelated.setType("1"); info.setCmRelatedList(cmRelatedService.getCmRelatedList(cmRelated)); model.addAttribute("info", info); model.addAttribute("ltype", ltype); return "modules/info/infoRelatedForm"; } /** * 精选推荐,文章数据 */ @RequestMapping(value = "infoSelectedPage") public String infoSelectedList(CmRelated cmRelated, Model model, HttpServletRequest request, HttpServletResponse response) { cmRelated.setType("2"); Page page = cmRelatedService.findPage(new Page(request, response), cmRelated); InfoType infoType = new InfoType(); infoType.setType("1"); infoType.setEnabledStatus("1"); List typeList = infoTypeService.findList(infoType); model.addAttribute("typeList", typeList); model.addAttribute("page", page); return "modules/info/infoSelectedList"; } /** * 选择文章,文章数据 */ @RequestMapping(value = "findInfoPage") public String findInfoPage(Info info, Model model, HttpServletRequest request, HttpServletResponse response) { info.setOnlineStatus(2); Page page = infoService.findPage(new Page(request, response), info); InfoType infoType = new InfoType(); infoType.setType("1"); infoType.setEnabledStatus("1"); List typeList = infoTypeService.findList(infoType); model.addAttribute("typeList", typeList); model.addAttribute("page", page); return "modules/info/addInfoForm"; } /** * 添加精选文章文章数据 */ @RequestMapping(value = "addSelectedInfos") @ResponseBody public Map addSelectedInfos(String infoIds) { for (String relatedId : infoIds.split(",")) { CmRelated cmRelated = new CmRelated(); cmRelated.setRelatedId(relatedId); cmRelated.setType("2"); cmRelated.setSort(0); cmRelated.setDelFlag("0"); cmRelated.setCreateTime(new Date()); cmRelatedService.addCmRelated(cmRelated); } HashMap map = new HashMap<>(2); map.put("success", true); map.put("info", "添加文章成功"); return map; } /** * 添加文章数据 */ @RequestMapping(value = "addInfos") @ResponseBody public Map addInfos(String infoIds) { Info info = new Info(); info.setId("IN=" + infoIds); info.setEnabledStatus("1"); List infoList = infoService.getInfoList(info); HashMap map = new HashMap<>(2); map.put("infoList", infoList); map.put("success", true); map.put("info", "添加文章成功"); return map; } @RequestMapping(value = "relatedSave") public String relatedSave(Info info, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) { Info byInfo = new Info(); byInfo.setId(info.getId()); byInfo.setAutoStatus(info.getAutoStatus()); infoService.updateInfo(byInfo); if ("1".equals(info.getAutoStatus())) { cmRelatedService.delCmRelatedByAuthorId("1", info.getId()); info.getCmRelatedList().forEach(cmRelated -> { cmRelated.setType("1"); cmRelated.setDelFlag("0"); cmRelated.setCreateTime(new Date()); cmRelatedService.addCmRelated(cmRelated); }); } addMessage(redirectAttributes, "保存信息成功"); // 更新索引 coreServiceUitls.updateArticleIndex(Integer.valueOf(info.getId())); return "redirect:" + Global.getAdminPath() + "/info/info/list?repage&publishSource=" + info.getPublishSource(); } @RequestMapping(value = "infoSelectedListSave") public String infoSelectedListSave(Info info, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) { info.getCmRelatedList().forEach(cmRelated -> { cmRelatedService.updateCmRelated(cmRelated); }); addMessage(redirectAttributes, "更新排序成功"); return "redirect:" + Global.getAdminPath() + "/info/info/infoSelectedPage"; } @RequiresPermissions("info:info:edit") @RequestMapping(value = "save") public String save(Info info, Model model, String ltype, RedirectAttributes redirectAttributes, HttpServletRequest request) { if (!beanValidator(model, info)) { return form(info, ltype, model); } String content = info.getInfoContent(); int stringLengthByUtf8 = com.caimei.utils.StringUtils.getStringLengthByUtf8(content); logger.info("信息内容字数" + stringLengthByUtf8); logger.info("原始传入信息:" + content); //富文本数量超过text保存最大数 if (stringLengthByUtf8 > 300000) { List list = new ArrayList(); list.add(0, "信息内容编辑框输入字数超过30万,当前字数:" + stringLengthByUtf8); addMessage(model, list.toArray(new String[]{})); return form(info, ltype, model); } //新增时才设置随机值 if (null == info.getId() || "".equals(info.getId())) { if (0 == info.getBasePv() || "0".equals(info.getBasePv().toString())) { Random random = new Random(); info.setBasePv((long) (random.nextInt(191) + 10)); } } boolean flag = false; if (StringUtils.isBlank(info.getId())) { flag = true; } // 保存成功返回info.id infoService.save(info, request); addMessage(redirectAttributes, "保存信息成功"); // 更新索引 coreServiceUitls.updateArticleIndex(Integer.valueOf(info.getId())); if (flag) { // 新增文章 百度链接实时推送 generateUtils.pushBaiduLink("https://www.caimei365.com/info/detail-" + info.getId() + "-1.html"); } if (StringUtils.equals("1", ltype)) { return "redirect:" + Global.getAdminPath() + "/info/infoLabel/list"; } info.setPublishSource(null == info.getPublishSource() ? 1 : info.getPublishSource()); return "redirect:" + Global.getAdminPath() + "/info/info/list?repage&publishSource=" + info.getPublishSource(); } @RequiresPermissions("info:info:delete") @RequestMapping(value = "delete") public String delete(Info info, RedirectAttributes redirectAttributes) { infoService.delete(info); if (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) { //清除置顶,将后面置顶位的数据往前移 infoService.moveTopList(info); // 重新生成静态首页 generateUtils.generateHome(); } addMessage(redirectAttributes, "删除信息成功"); // 删除索引 coreServiceUitls.deleteArticleIndex(Integer.valueOf(info.getId())); return "redirect:" + Global.getAdminPath() + "/info/info/?repage&publishSource=1"; } @RequiresPermissions("info:info:delete") @RequestMapping(value = "delInfoSelected") public String delete(CmRelated cmRelated, RedirectAttributes redirectAttributes) { cmRelatedService.delCmRelatedById(cmRelated.getId()); addMessage(redirectAttributes, "删除文章成功"); return "redirect:" + Global.getAdminPath() + "/info/info/infoSelectedPage"; } /** * 批量修改启用、停用状态 * * @param ids * @param request * @param response * @return */ @RequiresPermissions("info:info:edit") @ResponseBody @RequestMapping(value = "updateStatus") public Map updateStatus(String status, String[] ids, String type, HttpServletRequest request, HttpServletResponse response) { Map map = Maps.newLinkedHashMap(); try { if (StringUtils.equals("enabledStatus", type)) { infoService.updateEnabledStatusByIds(status, ids); } else if (StringUtils.equals("recommendStatus", type)) { infoService.updateRecommendStatusByIds(status, ids); } map.put("success", true); map.put("msg", "修改成功"); for (String id : ids) { // 更新索引 coreServiceUitls.updateArticleIndex(Integer.valueOf(id)); Info info = infoService.get(id); Boolean topFlag = (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) ? true : false; if (topFlag) { // 重新生成静态首页 generateUtils.generateHome(); } } } catch (Exception e) { logger.debug(e.toString(), e); map.put("success", false); map.put("msg", "修改失败"); } return map; } @RequestMapping(value = "updateTopPosition") @ResponseBody public Map updateTopPosition(Integer topPosition, String id, String type) { Map map = Maps.newLinkedHashMap(); try { Info info = infoService.get(id); Boolean topFlag = (info.getTopPosition() != null && !StringUtils.equals("", info.getTopPosition())) ? true : false; String oldTopPosition = info.getTopPosition(); if (StringUtils.equals("setTopPosition", type)) { //更新被替换掉置顶的文章 Info oldInfo = new Info(); oldInfo.setTopPosition(topPosition.toString()); List replacedInfoList = infoService.findList(oldInfo); replacedInfoList.forEach(replacedInfo -> { replacedInfo.setTopPosition(null); infoService.update(replacedInfo); }); //新的置顶直播 info.setTopPosition(topPosition.toString()); map.put("msg", "置顶成功"); } else if (StringUtils.equals("clearTopPosition", type)) { //清除置顶,将后面置顶位的数据往前移 infoService.moveTopList(info); //清除置顶位 info.setTopPosition(null); map.put("msg", "清除成功"); } infoService.update(info); //如果新直播原本也是置顶直播,需要把新直播后面的直播往前挪(新直播更新后再一起挪) if (StringUtils.equals("setTopPosition", type) && topFlag) { Info oldInfo = new Info(); oldInfo.setTopPosition(oldTopPosition); infoService.moveTopList(oldInfo); } // 重新生成静态首页 generateUtils.generateHome(); map.put("success", true); } catch (Exception e) { logger.debug(e.toString(), e); map.put("success", false); map.put("msg", "修改失败"); } return map; } @RequestMapping(value = {"infoList_multiselect"}) public String infoPageMulti(Info info, HttpServletRequest request, HttpServletResponse response, Model model) { info.setEnabledStatus("1"); Page page = infoService.findPage(new Page(request, response), info); model.addAttribute("page", page); return "modules/info/infoList_multiselect"; } @RequestMapping(value = "check") public String checkInfo(Info info, Model model) { model.addAttribute("info", info); return "modules/info/checkInfoPage"; } @RequestMapping(value = "toAuditPage") public String toAuditPage(Info info, Model model) { if (StringUtils.isBlank(info.getId())) { info.setRecommendStatus("0"); info.setEnabledStatus("1"); info.setPubdate(new Date()); } InfoType infoType = new InfoType(); List typeList = infoTypeService.findList(infoType); model.addAttribute("typeList", typeList); model.addAttribute("info", info); // 敏感词 String sensitiveWords = infoDao.getSensitiveWords(3); model.addAttribute("sensitiveWords", sensitiveWords); return "modules/info/auditInfoPage"; } @RequestMapping(value = "auditInfo") @ResponseBody public Boolean auditInfo(Info info) { infoService.auditInfo(info); return true; } @RequestMapping(value = "offline") @ResponseBody public Boolean offline(Integer id) { infoService.offlineInfo(id); return true; } }