NewPageFirstNavigationController.java 11 KB

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