基于SpringBoot+MyBatis的餐饮点餐系统

基于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";
        }
    }
}

QQ:937598186

已标记关键词 清除标记
相关推荐
后台技术选型: <ul style="color:#2F2F2F;"> <li> JDK8 </li> <li> MySQL </li> <li> Spring-boot </li> <li> Spring-data-jpa </li> <li> Lombok </li> <li> Freemarker </li> <li> Bootstrap </li> <li> Websocket </li> </ul> 小程序端技术选型 <ul style="color:#2F2F2F;"> <li> 微信小程序 </li> </ul> <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img alt="" src="https://upload-images.jianshu.io/upload_images/6273713-928017278f465cbd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" /> </div> </div> <div style="font-size:14px;color:#969696;"> <br /> </div> </div> 小程序端 <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img alt="" src="https://upload-images.jianshu.io/upload_images/6273713-8d6c2b81701d32cd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" /> </div> </div> <div style="font-size:14px;color:#969696;"> <br /> </div> </div> <ul style="color:#2F2F2F;"> <li> 扫码点餐 </li> <li> 菜品分类显示 </li> <li> 模拟支付 </li> <li> 评论系统 </li> <li> 购物车 </li> </ul> <p> <span><img alt="" src="https://img-bss.csdn.net/201907270119553529.png" /><br /> </span> </p> <p> <span><img alt="" src="https://img-bss.csdn.net/201907270120098756.png" /><br /> </span> </p> <p> <span><img alt="" src="https://img-bss.csdn.net/201907270120405331.png" /><br /> </span> </p> <p> <span><img alt="" src="https://img-bss.csdn.net/201907270120538298.png" /><img alt="" src="https://img-bss.csdn.net/201907270121012487.png" /><br /> </span> </p>
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页