OrderEntryControllor.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package com.lesson.controller;
  2. import com.lesson.model.Menu;
  3. import com.lesson.service.CategoryManager;
  4. import com.lesson.service.MenuManager;
  5. import org.apache.log4j.Logger;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import javax.servlet.http.Cookie;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.HttpSession;
  17. import java.io.IOException;
  18. import java.io.UnsupportedEncodingException;
  19. @Controller
  20. public class OrderEntryControllor {
  21. @Autowired
  22. MenuManager menuManager;
  23. @Autowired
  24. CategoryManager categoryManager;
  25. Logger logger = Logger.getLogger(OrderEntryControllor.class);
  26. /**
  27. * 展示菜品
  28. *
  29. * @param model
  30. * @param mid 菜品id (查询用)
  31. * @param cid 菜品分类id (查询用)
  32. * @param request
  33. * @return
  34. */
  35. @RequestMapping(value = "/showMenus")
  36. public String showMenus(Model model,
  37. @RequestParam(value = "mid", required = false) String mid,
  38. @RequestParam(value = "cid", required = false) String cid,
  39. @RequestParam(value = "useCookie", required = false) String useCookie,
  40. HttpServletRequest request,
  41. HttpServletResponse response) {
  42. logger.info("mid = " + mid);
  43. logger.info("cid = " + cid);
  44. logger.info("useCookie = " + useCookie);
  45. //判断mid cid为空的情形
  46. if (mid == null || mid.equalsIgnoreCase("") || mid.equalsIgnoreCase("all")) {
  47. mid = "%";
  48. }
  49. if (cid == null || cid.equalsIgnoreCase("") || cid.equalsIgnoreCase("all")) {
  50. cid = "%";
  51. }
  52. HttpSession session = request.getSession();
  53. String sessionId = session.getId();
  54. model.addAttribute("menus", menuManager.getMenusByMidCid(mid, cid));
  55. session.setAttribute("categoryManager", categoryManager);
  56. //配置cookie
  57. if(useCookie != null && useCookie.equalsIgnoreCase("on")){
  58. int expire = 3600 * 24 * 30; //如果使用cookie,则将过期时间设为1个月
  59. logger.info("用户选择使用cookie,进入使用cookies的控制逻辑!");
  60. Cookie ckUseCookie = new Cookie("ckUseCookie","on");
  61. Cookie ckCid = new Cookie("ckCid",cid);
  62. ckUseCookie.setMaxAge(expire);
  63. ckCid.setMaxAge(expire);
  64. response.addCookie(ckUseCookie);
  65. response.addCookie(ckCid);
  66. }else{
  67. int expire = -1; //如果使用cookie,则将过期时间设为-1 控制该cookie立刻过期
  68. logger.info("用户没有选择使用cookie,进入不使用cookies的控制逻辑!");
  69. Cookie ckUseCookie =new Cookie("ckUseCookie","");
  70. Cookie ckCid =new Cookie("ckCid","");
  71. ckUseCookie.setMaxAge(expire);
  72. ckCid.setMaxAge(expire);
  73. response.addCookie(ckUseCookie);
  74. response.addCookie(ckCid);
  75. }
  76. logger.info("Session Id = " + sessionId);
  77. return "jsp/menuList.jsp";
  78. }
  79. /**
  80. * 展示编辑菜品页
  81. *
  82. * @param model
  83. * @param mid 菜品id
  84. * @param request
  85. * @return
  86. */
  87. @RequestMapping(value = "/editMenu/{mid}", method = RequestMethod.GET)
  88. public String editMenu(Model model,
  89. @PathVariable String mid,
  90. HttpServletRequest request) {
  91. logger.info("Start editMenu!");
  92. Menu menu = menuManager.getMenuByMid(mid);
  93. HttpSession session = request.getSession();
  94. model.addAttribute("menu", menu);
  95. session.setAttribute("categoryManager", categoryManager);
  96. return "jsp/menuEdit.jsp";
  97. }
  98. /**
  99. * 展示添加菜品页
  100. *
  101. * @param model
  102. * @param request
  103. * @return
  104. */
  105. @RequestMapping(value = "/addMenu", method = RequestMethod.GET)
  106. public String addMenu(Model model, HttpServletRequest request) {
  107. logger.info("Start addMenu!");
  108. HttpSession session = request.getSession();
  109. session.setAttribute("categoryManager", categoryManager);
  110. return "jsp/menuAdd.jsp";
  111. }
  112. /**
  113. * 保存菜品 (新建或更新)
  114. *
  115. * @param model
  116. * @param request
  117. * @param mid -1 代表新建菜品,其他代表更新菜品
  118. * @param cid 分类ID
  119. * @param mname 菜品名称
  120. * @param price 菜品价格
  121. * @return
  122. * @throws UnsupportedEncodingException
  123. */
  124. @RequestMapping(value = "/saveMenu", method = RequestMethod.POST)
  125. public String saveMenu(Model model,
  126. HttpServletRequest request,
  127. @RequestParam(value = "mid", required = true) int mid,
  128. @RequestParam(value = "new_cid", required = true) int cid,
  129. @RequestParam(value = "mname", required = true) String mname,
  130. @RequestParam(value = "price", required = true) float price) throws UnsupportedEncodingException {
  131. if (mname != null && !mname.equalsIgnoreCase("")) {
  132. mname = new String(mname.getBytes("ISO-8859-1"), "utf8");
  133. }
  134. if (mid >= 1) {
  135. logger.info("保存菜品更新!");
  136. logger.info("Request Param: mid = " + mid);
  137. logger.info("Request Param: cid = " + cid);
  138. logger.info("Request Param: mname = " + mname);
  139. logger.info("Request Param: price = " + price);
  140. menuManager.updateMenuByMid(mid, cid, mname, price);
  141. } else if (mid == -1) {
  142. logger.info("添加新菜品!");
  143. logger.info("Request Param: cid = " + cid);
  144. logger.info("Request Param: mname = " + mname);
  145. logger.info("Request Param: price = " + price);
  146. menuManager.addMenu(cid, mname, price);
  147. } else {
  148. logger.error("出错了,mid 不正确!");
  149. }
  150. HttpSession session = request.getSession();
  151. model.addAttribute("menus", menuManager.getAllMenus());
  152. session.setAttribute("categoryManager", categoryManager);
  153. return "jsp/menuList.jsp";
  154. }
  155. /**
  156. * 删除菜品
  157. *
  158. * @param model
  159. * @param mid 菜品id
  160. * @param request
  161. * @return
  162. */
  163. @RequestMapping(value = "/delete/{mid}", method = RequestMethod.GET) //按照ID展示
  164. public String deleteMenu(Model model,
  165. @PathVariable int mid,
  166. HttpServletRequest request,
  167. HttpServletResponse response) {
  168. menuManager.deleteMenuByMid(mid); //删除对应menu
  169. HttpSession session = request.getSession();
  170. model.addAttribute("menus", menuManager.getAllMenus());
  171. session.setAttribute("categoryManager", categoryManager);
  172. return "/showMenus";
  173. }
  174. /**
  175. * 展示所有菜品分类
  176. *
  177. * @param model
  178. * @return
  179. */
  180. @RequestMapping(value = "/showCategories")
  181. public String showCategories(Model model) {
  182. model.addAttribute("categories", categoryManager.getAllCategories());
  183. return "jsp/categoryList.jsp";
  184. }
  185. /**
  186. * 展示添加菜品分类页面
  187. *
  188. * @param model
  189. * @return
  190. */
  191. @RequestMapping(value = "/addCategory", method = RequestMethod.GET)
  192. public String addCategory(Model model) {
  193. logger.info("Start addCategory!");
  194. return "jsp/categoryAdd.jsp";
  195. }
  196. /**
  197. * 修改菜品分类
  198. *
  199. * @param model
  200. * @param cid 菜品分类id
  201. * @return
  202. */
  203. @RequestMapping(value = "/editCategory/{cid}")
  204. public String editCategory(Model model, @PathVariable int cid) {
  205. model.addAttribute("category", categoryManager.getCategoryById(cid));
  206. return "jsp/categoryEdit.jsp";
  207. }
  208. /**
  209. * 保存菜品分类
  210. *
  211. * @param model
  212. * @param cid 菜品分类id
  213. * @param cname 菜品分类名称
  214. * @return
  215. * @throws UnsupportedEncodingException
  216. */
  217. @RequestMapping(value = "/saveCategory", method = RequestMethod.POST)
  218. public String saveCategory(Model model,
  219. @RequestParam(value = "cid", required = true) int cid,
  220. @RequestParam(value = "cname", required = true) String cname) throws UnsupportedEncodingException {
  221. if (cname != null && !cname.equalsIgnoreCase("")) {
  222. cname = new String(cname.getBytes("ISO-8859-1"), "utf8");
  223. }
  224. if (cid >= 1) {
  225. logger.info("更新保存菜品分类!");
  226. logger.info("cid = " + cid);
  227. logger.info("cname = " + cname);
  228. categoryManager.updateCategoryById(cid, cname);
  229. } else if (cid == -1) {
  230. logger.info("添加新菜品分类!");
  231. logger.info("cname = " + cname);
  232. categoryManager.addCategory(cname);
  233. } else {
  234. logger.info("出错了,id 不正确!");
  235. }
  236. model.addAttribute("categories", categoryManager.getAllCategories());
  237. return "jsp/categoryList.jsp";
  238. }
  239. /**
  240. * 删除菜品分类
  241. *
  242. * @param model
  243. * @param cid 菜品分类id
  244. * @return
  245. */
  246. @RequestMapping(value = "/deleteCategory/{cid}")
  247. public String deleteCategoryById(Model model,
  248. @PathVariable int cid,
  249. HttpServletRequest request) {
  250. try {
  251. categoryManager.deleteCategoryById(cid);
  252. } catch (Exception ex) {
  253. String errMsg = ex.getMessage();
  254. logger.info("发生错误无法删除!");
  255. logger.info(errMsg);
  256. if (errMsg.contains("MySQLIntegrityConstraintViolationException")) {
  257. logger.error("存在依赖,不能删除该值");
  258. String cname = categoryManager.getCategoryById(cid).getCname();
  259. HttpSession session = request.getSession();
  260. session.setAttribute("errMsg", "出错啦:\"" + cname + "\"下仍有菜品,不能删除该分类!");
  261. }
  262. }
  263. model.addAttribute("categories", categoryManager.getAllCategories());
  264. return "jsp/categoryList.jsp";
  265. }
  266. }