基于SpringBoot+MyBatis的餐饮点餐系统,适用于毕业设计。
package com.example.zxdmeal.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.zxdmeal.model.*;
import com.example.zxdmeal.service.IndexService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/Home")
public class IndexController {
@Autowired
private IndexService indexService;
/**
* 跳转首页
*/
@RequestMapping("/")
public String home (Model model){
return "redirect:/Home/index";
}
/**
* 跳转首页
*/
@RequestMapping("/index")
public String index (Model model){
List<Desk> deskList = indexService.getDeskList();
model.addAttribute("deskList",deskList);
return "home/index";
}
/**
* 跳转点餐
*/
@RequestMapping("/desk")
public String desk (Model model, HttpServletRequest request,HttpSession session){
User user = (User) session.getAttribute("user");
int id = Integer.parseInt(request.getParameter("id"));
if (user!=null){
if (id>0){
Desk desk = indexService.getDeskById(id);
List<Catalog> catalogList = indexService.getCatalogList();
ArrayList<List<Product>> productLists = new ArrayList<>();
for (int i = 0;i<catalogList.size();i++){
List<Product> productList = indexService.getProductListByCatalog(catalogList.get(i).getTypename());
productLists.add(productList);
}
model.addAttribute("deskid",id);
model.addAttribute("desk",desk);
model.addAttribute("catalogList",catalogList);
model.addAttribute("productLists",productLists);
return "home/desk";
}else{
return "redirect:/Home/index";
}
}else{
return "redirect:/Home/loginAndRegist?deskid="+id;
}
}
/**
* 去登录注册页面
*/
@RequestMapping("/loginAndRegist")
public String loginAndRegist() {
return "home/loginAndRegist";
}
/**
* 登录方法
*/
@ResponseBody
@RequestMapping(value = "/loginAction",method = RequestMethod.POST)
public JSONObject loginAction(@RequestBody String request, HttpSession session) {
JSONObject jsonObject = JSONObject.parseObject(request);
Map<String,Object> map = (Map<String,Object>)jsonObject;
String userName = String.valueOf(map.get("userName"));
String password = String.valueOf(map.get("password"));
User user = new User();
user = indexService.LoginAction(userName,password);
if (user!=null){
session.setAttribute("user",user);
System.out.println(user);
jsonObject.put("status", "success");
}else{
jsonObject.put("status", "fail");
}
return jsonObject;
}
/**
* 退出登录方法
*/
@RequestMapping("/loginOut")
public String loginOut(HttpSession session){
session.removeAttribute("user");
return "redirect:/Home/index";
}
/**
* 注册方法
*/
@ResponseBody
@RequestMapping(value = "/registAction",method = RequestMethod.POST)
public JSONObject registAction(@RequestBody String request, HttpSession session){
JSONObject jsonObject = JSONObject.parseObject(request);
Map<String,Object> map = (Map<String,Object>)jsonObject;
String username = String.valueOf(map.get("username"));
String password = String.valueOf(map.get("password"));
String email = String.valueOf(map.get("email"));
User u = new User();
u.setEmail(email);
u.setUsername(username);
u.setPassword(password);
User u1 = indexService.getUserByUsername(username);
System.out.println(u1);
if (null == u1){
try {
int status = indexService.RegistAction(u);
if(status>0){
jsonObject.put("status", "success");
}else{
jsonObject.put("status", "fail");
}
}catch (Exception e){
jsonObject.put("status", "fail");
}
}else{
jsonObject.put("status", "redo");
}
return jsonObject;
}
/**
* 购买
*/
@ResponseBody
@RequestMapping(value = "/buy",method = RequestMethod.POST)
public JSONObject buy(@RequestBody String request, HttpSession session) {
User user = (User) session.getAttribute("user");
JSONObject jsonObject = JSONObject.parseObject(request);
Map<String,Object> map = (Map<String,Object>)jsonObject;
int userid = Integer.parseInt(String.valueOf(map.get("userid")));
int deskid = Integer.parseInt(String.valueOf(map.get("deskid")));
int sum = Integer.parseInt(String.valueOf(map.get("sum")));
String items = String.valueOf(map.get("items"));
Timestamp ts=new Timestamp(new Date().getTime());
Orders orders = new Orders();
orders.setCreateat(ts);
orders.setUserid(userid);
orders.setDeskid(deskid);
orders.setItems(items);
orders.setSum(sum);
orders.setStatus(0);
if (user!=null){
Desk desk_usess = indexService.getDeskByIdUess(deskid);
if (desk_usess != null){
try {
System.out.println(indexService.buy(orders));
System.out.println(indexService.updateDesk(deskid));
jsonObject.put("status", "success");
}catch (Exception e){
jsonObject.put("status", "fail");
}
}else{
jsonObject.put("status", "redo");
}
}else{
jsonObject.put("status", "unlogin");
}
return jsonObject;
}
@RequestMapping("/myorders")
public String myorders (HttpServletRequest request, HttpSession session, @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, Model model){
User user = (User) session.getAttribute("user");
if (user!=null){
PageHelper.startPage(pageNo,pageSize);
PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(indexService.getOrdersByUserid(user.getId()));
System.out.println(pageInfo);
model.addAttribute(pageInfo);
return "home/myorders";
}else{
return "redirect:/Home/loginAndRegist";
}
}
/**
* 订单详情
*/
@RequestMapping("/myordersdetail")
public String myordersdetail(HttpSession session,HttpServletRequest request,Model model){
int id = Integer.parseInt(request.getParameter("id"));
User user = (User) session.getAttribute("user");
if (user!=null){
Orders orders = indexService.myordersdetail(id);
ArrayList ordersList = new ArrayList<>();
String[] itemsLists = orders.getItems().split(";");
for (int i = 0;i<itemsLists.length;i++){
String itemsList = itemsLists[i];
String[] items = itemsList.split(":");
Item item = new Item();
item.setPic(items[4]);
item.setPrice(Integer.parseInt(items[3]));
item.setNum(Integer.parseInt(items[2]));
item.setTitle(items[1]);
item.setId(Integer.parseInt(items[0]));
System.out.println(item);
ordersList.add(item);
}
model.addAttribute("orders",orders);
model.addAttribute("ordersList",ordersList);
return "home/myorders_detail";
}else {
return "redirect:/Home/loginAndRegist";
}
}
/**
* 查看用户
*/
@RequestMapping("/myinfo")
public String myinfo(HttpServletRequest request,HttpSession session, Model model){
User user = (User) session.getAttribute("user");
if (user==null){
return "redirect:/Home/loginAndRegist";
}
if (user!=null){
return "home/user_info";
}else{
return "redirect:/Home/loginAndRegist";
}
}
/**
* 修改用户信息
*/
@RequestMapping("/myinfoAction")
public String myinfoAction(HttpServletRequest request,HttpSession session, Model model){
User user = (User) session.getAttribute("user");
String email = request.getParameter("email");
String password = request.getParameter("password");
String truename = request.getParameter("truename");
String sex = request.getParameter("sex");
if (user==null){
return "redirect:/Home/loginAndRegist";
}
if (user!=null){
User u1 = new User();
u1.setId(user.getId());
u1.setPassword(password);
u1.setEmail(email);
u1.setTruename(truename);
u1.setSex(sex);
User u2 = new User();
System.out.println(indexService.myinfoAction(u1));
u2 = indexService.getUserById(user.getId());
session.removeAttribute("user");
session.setAttribute("user",u2);
return "redirect:/Home/myorders";
}else{
return "redirect:/Home/loginAndRegist";
}
}
}