NewPageFirstNavigationController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package com.caimei.modules.newhome.web;
  2. import com.caimei.modules.hehe.util.UploadPicUtils;
  3. import com.caimei.modules.newhome.entity.NewPageFirstNavigation;
  4. import com.caimei.modules.newhome.service.NewPageFirstNavigationService;
  5. import com.caimei.modules.opensearch.GenerateUtils;
  6. import com.caimei.redis.RedisService;
  7. import com.google.common.collect.Maps;
  8. import com.thinkgem.jeesite.common.config.Global;
  9. import com.thinkgem.jeesite.common.persistence.Page;
  10. import com.thinkgem.jeesite.common.utils.StringUtils;
  11. import com.thinkgem.jeesite.common.web.BaseController;
  12. import org.apache.commons.collections.CollectionUtils;
  13. import org.apache.shiro.authz.annotation.RequiresPermissions;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.ui.Model;
  16. import org.springframework.web.bind.annotation.ModelAttribute;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  21. import javax.annotation.Resource;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.Map;
  28. import static com.caimei.modules.newhome.web.NewPageQualitySupplierController.isInteger;
  29. /**
  30. * 新首页一级导航栏Controller
  31. *
  32. * @author lwt
  33. * @version 2019-03-14
  34. */
  35. @Controller
  36. @RequestMapping(value = "${adminPath}/newhome/newPageFirstNavigation")
  37. public class NewPageFirstNavigationController extends BaseController {
  38. @Resource
  39. private NewPageFirstNavigationService newPageFirstNavigationService;
  40. @Resource
  41. private RedisService redisService;
  42. @Resource
  43. private GenerateUtils generateUtils;
  44. @ModelAttribute
  45. public NewPageFirstNavigation get(@RequestParam(required = false) String id) {
  46. NewPageFirstNavigation entity = null;
  47. if (StringUtils.isNotBlank(id)) {
  48. entity = newPageFirstNavigationService.get(id);
  49. }
  50. if (entity == null) {
  51. entity = new NewPageFirstNavigation();
  52. }
  53. return entity;
  54. }
  55. @RequiresPermissions("newhome:newPageFirstNavigation:view")
  56. @RequestMapping(value = {"list", ""})
  57. public String list(NewPageFirstNavigation newPageFirstNavigation, HttpServletRequest request, HttpServletResponse response, Model model) {
  58. newPageFirstNavigation.setStatisticsType("4");
  59. Page<NewPageFirstNavigation> page = newPageFirstNavigationService.findPage(new Page<NewPageFirstNavigation>(request, response), newPageFirstNavigation);
  60. model.addAttribute("page", page);
  61. model.addAttribute("newPageFirstNavigation", newPageFirstNavigation);
  62. return "modules/newhome/newPageFirstNavigationList";
  63. }
  64. @RequiresPermissions("newhome:newPageFirstNavigation:view")
  65. @RequestMapping(value = "form")
  66. public String form(NewPageFirstNavigation newPageFirstNavigation, Model model) {
  67. model.addAttribute("newPageFirstNavigation", newPageFirstNavigation);
  68. return "modules/newhome/newPageFirstNavigationForm";
  69. }
  70. @RequiresPermissions("newhome:newPageFirstNavigation:edit")
  71. @RequestMapping(value = "save")
  72. public String save(NewPageFirstNavigation newPageFirstNavigation, Model model, RedirectAttributes redirectAttributes) {
  73. if (!beanValidator(model, newPageFirstNavigation)) {
  74. return form(newPageFirstNavigation, model);
  75. }
  76. //判断如果已经存在10条数据则不允许添加(产品控制数量显示)
  77. NewPageFirstNavigation navigation = new NewPageFirstNavigation();
  78. navigation.setType(newPageFirstNavigation.getType());
  79. List<NewPageFirstNavigation> list = newPageFirstNavigationService.findList(navigation);
  80. if (StringUtils.isEmpty(newPageFirstNavigation.getId())) {
  81. if (CollectionUtils.isNotEmpty(list) && list.size() >= 10) {
  82. addMessage(model, "最多添加10个主菜单!删除旧菜单后才能继续添加");
  83. return form(newPageFirstNavigation, model);
  84. }
  85. }
  86. //上传图片
  87. String image = newPageFirstNavigation.getIcon();
  88. image = UploadPicUtils.saveImageToServer(image);
  89. newPageFirstNavigation.setIcon(image);
  90. if (StringUtils.isEmpty(newPageFirstNavigation.getId())) {
  91. newPageFirstNavigation.setCreateDate(new Date());
  92. newPageFirstNavigationService.insert(newPageFirstNavigation);
  93. } else {
  94. newPageFirstNavigation.setUpdateDate(new Date());
  95. newPageFirstNavigationService.update(newPageFirstNavigation);
  96. }
  97. addMessage(redirectAttributes, "保存成功");
  98. cleanRedisCache();// 清理redis缓存
  99. return "redirect:" + Global.getAdminPath() + "/newhome/newPageFirstNavigation/?repage&type="+newPageFirstNavigation.getType();
  100. }
  101. @RequiresPermissions("newhome:newPageFirstNavigation:delete")
  102. @RequestMapping(value = "delete")
  103. public String delete(NewPageFirstNavigation newPageFirstNavigation, RedirectAttributes redirectAttributes) {
  104. Date date = new Date();
  105. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  106. String str = sdf.format(date);
  107. newPageFirstNavigation.setDelFlag(str);
  108. newPageFirstNavigationService.delete(newPageFirstNavigation);
  109. addMessage(redirectAttributes, "删除成功");
  110. cleanRedisCache();// 清理redis缓存
  111. return "redirect:" + Global.getAdminPath() + "/newhome/newPageFirstNavigation/?repage&type="+newPageFirstNavigation.getType();
  112. }
  113. @RequiresPermissions("newhome:newPageFirstNavigation:edit")
  114. @ResponseBody
  115. @RequestMapping(value = "updateEnabledStatus")
  116. public Map<String, Object> updateEnabledStatus(String enabledStatus, String[] ids, String www, String[] supplierIDs, HttpServletRequest request, HttpServletResponse response) {
  117. Map<String, Object> map = Maps.newLinkedHashMap();
  118. try {
  119. newPageFirstNavigationService.updateEnabledStatusByIds(enabledStatus, ids);
  120. map.put("success", true);
  121. map.put("msg", "修改成功");
  122. cleanRedisCache();// 清理缓存
  123. } catch (Exception e) {
  124. logger.debug(e.toString(), e);
  125. map.put("success", false);
  126. map.put("msg", "修改失败");
  127. }
  128. return map;
  129. }
  130. @RequiresPermissions("newhome:newPageFirstNavigation:edit")
  131. @ResponseBody
  132. @RequestMapping(value = "updateCrmEnabledStatusByIds")
  133. public Map<String, Object> updateCrmEnabledStatusByIds(String crmEnabledStatus, String[] ids, String crm, String[] supplierIDs, HttpServletRequest request, HttpServletResponse response) {
  134. Map<String, Object> map = Maps.newLinkedHashMap();
  135. try {
  136. newPageFirstNavigationService.updateCrmEnabledStatusByIds(crmEnabledStatus, ids);
  137. map.put("success", true);
  138. map.put("msg", "修改成功");
  139. cleanRedisCache();// 清理缓存
  140. } catch (Exception e) {
  141. logger.debug(e.toString(), e);
  142. map.put("success", false);
  143. map.put("msg", "修改失败");
  144. }
  145. return map;
  146. }
  147. /**
  148. * 批量更新排序值
  149. */
  150. @RequestMapping(value = "batchSaveSort")
  151. @ResponseBody
  152. public Map<String, Object> batchSaveSort(String sortList) {
  153. Map<String, Object> map = Maps.newLinkedHashMap();
  154. try {
  155. String[] newPageLists = sortList.split(",");
  156. for (String list : newPageLists) {
  157. String[] split = list.split("-");
  158. if (split.length == 1 || split.length < 1) {
  159. String id = split[0];
  160. String sort = null;
  161. newPageFirstNavigationService.saveSort(sort, id);
  162. } else {
  163. String id = split[0];
  164. String sort = split[1];
  165. if (isInteger(sort)) {
  166. if (StringUtils.equals("0", sort)) {
  167. map.put("success", false);
  168. map.put("msg", "排序值只能填写大于等于1的整数");
  169. return map;
  170. }
  171. newPageFirstNavigationService.saveSort(sort, id);
  172. } else {
  173. map.put("success", false);
  174. map.put("msg", "排序值只能为数字");
  175. return map;
  176. }
  177. }
  178. }
  179. cleanRedisCache();
  180. map.put("success", true);
  181. map.put("msg", "更新排序成功");
  182. return map;
  183. } catch (Exception e) {
  184. map.put("success", false);
  185. map.put("msg", "更新排序失败");
  186. return map;
  187. }
  188. }
  189. /**
  190. * 生成静态首页
  191. */
  192. @RequestMapping(value = "updateStaticHome")
  193. @ResponseBody
  194. public Map<String, Object> updateStaticHome(){
  195. Map<String, Object> map = Maps.newLinkedHashMap();
  196. try {
  197. //首页缓存
  198. redisService.removePattern("getNavMenu*");
  199. redisService.removePattern("getClassify*");
  200. redisService.removePattern("getCommodityClassify*");
  201. redisService.removePattern("getHomeData*");
  202. redisService.removePattern("getHomeCommodityData*");
  203. //清除产品仪器缓存
  204. redisService.removePattern("instrumentData*");
  205. redisService.removePattern("insCommodityData*");
  206. // 重新生成静态首页
  207. generateUtils.generateHome();
  208. // 重新生成静态产品仪器页
  209. generateUtils.generateProductType(286);
  210. generateUtils.generateProductType(287);
  211. map.put("success", true);
  212. map.put("msg", "手动生成静态首页成功");
  213. return map;
  214. } catch (Exception e) {
  215. map.put("success", false);
  216. map.put("msg", "手动生成静态首页失败");
  217. return map;
  218. }
  219. }
  220. /**
  221. * 有数据变动时需要清除缓存
  222. */
  223. public void cleanRedisCache() {
  224. //首页导航栏缓存
  225. String getNavMenu = "getNavMenu*";
  226. redisService.removePattern(getNavMenu);
  227. // 重新生成静态首页
  228. generateUtils.generateHome();
  229. // 重新生成静态产品仪器页
  230. generateUtils.generateProductType(286);
  231. generateUtils.generateProductType(287);
  232. }
  233. }