Java项目:ssm民宿管理系统

发布于:2022-12-22 ⋅ 阅读:(369) ⋅ 点赞:(0)

作者主页:夜未央5788

 简介:Java领域优质创作者、Java项目、学习资料、技术互助

文末获取源码

ssm民宿管理系统网页前后端

项目介绍

游客功能:用户注册、登录、登录权限拦截、按名称搜索房间、支付流程、查看订单信息和状态、评论预定过的房间,并自动修改订单状态、查看统计剩余房间数量,数量为0时不可预定
管理员功能:房间分类管理、房间管理、订单管理

高级功能:各种列表清单的导出和打印功能、订单组合条件查询

功能清单

游客功能

+ 用户注册、登录
+ 登录权限拦截
+ 按名称搜索房间
+ 支付流程
+ 查看订单信息和状态
+ 评论预定过的房间,并自动修改订单状态

+ 查看统计剩余房间数量,数量为0时不可预定

管理员功能

> 房间分类管理
+ 类型的删除、修改、查询(准备添加增添功能,即图片上传功能)
+ 类型所含属性的增删改查
+ 房间分类图片上传

> 房间管理
+ 房间增删改导印统
+ 查询已上传房间的所有照片
+ 房间的配套设施属性设置
+ 房间价格(原价、活动价)、名称、描述等属性设置
+ 房间存量统计

> 订单管理
+ 订单信息修改、删除
+ 订单组合条件查询

+ 筛选出超时未评论的订单

高级功能
+ 各种列表清单的导出和打印功能

+ 订单组合条件查询

环境需要

1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.是否Maven项目: 是;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目 
6.数据库:MySql 5.7版本;

技术栈

1. 后端:Spring SpringMVC MyBatis

2. 前端:JSP+bootstrap+jQuery

使用说明

1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 将项目中applicationContext.xml配置文件中的数据库配置改为自己的配置;
3. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行;

4. 运行项目,输入localhost:8080/ 登录

运行截图

 

 

 

 

 

 

 

 

相关代码 

AdminController

package controller;


import model.*;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import service.*;
import util.ExportExcel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;

    private static Logger logger = Logger.getLogger(AdminController.class);
    private ModelAndView mav = new ModelAndView();

    // windows系统参考路径 注:此处必须为项目路径
    private static String imgPath = "C:\\Users\\70953\\Gotrip\\";
    // Mac系统参考路径
//    private static String imgPath = "/Users/hanmeng/Desktop/java源码/SSM/012ssm民宿管理系统/源码/Gotrip/";

    private ModelAndView listProduct(Integer cid) {
        List<Product> products = productService.listProducts(cid);
        mav.addObject("ps",products);
        mav.addObject("c",categoryService.getCategory(cid));
        mav.setViewName("/admin/listProduct");
        return mav;
    }

    //查询所有房间分类
    @RequestMapping(value = "/admin_category_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView listCategory(){
        List<Category> categories = categoryService.listAll();
        mav.addObject("thecs", categories);
        mav.setViewName("admin/listCategory");
        return mav;
    }

    //打印页面
    @RequestMapping(value = "/admin_print_category_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView printListCategory(){
        List<Category> categories = categoryService.listAll();
        mav.addObject("thecs", categories);
        mav.setViewName("admin/printListCategory");
        return mav;
    }

    //导出分类EXCEL文件
    @RequestMapping(value = "/admin_export_category_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public void exportListCategory(HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<Category> categories = categoryService.listAll();
        String []rowsName = new String[]{"id","name"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        for(int i = 0;i < categories.size();i++){
            Object[] objs = new Object[rowsName.length];
            Category category = categories.get(i);
            objs[0] = category.getId();
            System.out.println(objs[0]);
            objs[1] = category.getName();
            System.out.println(objs[1]);
            dataList.add(objs);
        }
        String fileName = "exportCategoriesExcel";
        //执行导出
        ExportExcel.exportExcel(request,response,fileName, rowsName, dataList, "yyyy-MM-dd HH:mm:ss");
    }

    //添加分类
    //特别注意,由于本操作涉及图片保存,更换运行环境时需要重新配置路径
    @RequestMapping(value = "/admin_category_add",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_category_add(@RequestParam("name") String name, @RequestParam("filepath") MultipartFile filepath) throws IOException {
        Category category = new Category();
        category.setName(name);
        Integer cid = categoryService.insert(category);
        // windows系统参考路径 注:此处必须为本地src及target下category的路径
        String path1 = imgPath+"src\\main\\webapp\\img\\category\\"+cid.toString()+".jpg";
        String path2 = imgPath+"target\\Gotrip-1.0-SNAPSHOT\\img\\category\\"+cid.toString()+".jpg";
        // Mac系统参考路径 注:此处必须为本地src及target下category的路径
//        String path1 = imgPath+"src/main/webapp/img/category/"+cid.toString()+".jpg";
//        String path2 = imgPath+"target/Gotrip-1.0-SNAPSHOT/img/category/"+cid.toString()+".jpg";
        filepath.transferTo(new File(path1));
        FileUtils.copyFile(new File(path1),new File(path2));
        mav = listCategory();
        return mav;
    }

    //查询所有订单
    @RequestMapping(value = "/admin_order_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView listOrder(){
        List<Order> orders = orderService.listAll();
        for(Order order:orders){
            order.setOrderItems(orderItemService.getOrderItem(order.getId()));
        }
        mav.addObject("os",orders);
        mav.setViewName("/admin/listOrder");
        return mav;
    }

    @RequestMapping(value = "/admin_print_order_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView printListOrder(){
        mav = listOrder();
        mav.setViewName("/admin/printListOrder");
        return mav;
    }

    //导出订单列表
    @RequestMapping(value = "/admin_export_order_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public void exportListOrder(HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<Order> orders = orderService.listAll();
        String []rowsName = new String[]{"ID", "状态", "金额", "房间数量","买家名称","支付时间"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        for(int i = 0;i < orders.size();i++){
            Object[] objs = new Object[rowsName.length];
            Order order = orders.get(i);
            objs[0] = order.getId();
            objs[1] = order.getStatus();
            objs[2] = order.getTotal();
            objs[3] = order.getTotalNumber();
            objs[4] = order.getUser().getName();
            objs[5] = order.getPayDate();
            dataList.add(objs);
        }
        String fileName = "exportOrderExcel";
        //执行导出
        ExportExcel.exportExcel(request,response,fileName, rowsName, dataList, "yyyy-MM-dd HH:mm:ss");
    }

    //查询所有属性
    @RequestMapping(value = "/admin_property_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView listProperty(@RequestParam(value = "cid") Integer cid,HttpServletRequest request){
        request.getSession().setAttribute("cid",cid);
        List<Property> properties = productService.listProproty(cid);
        mav.addObject("ps",properties);
        mav.addObject("c",categoryService.getCategory(cid));
        mav.setViewName("/admin/listProperty");
        return mav;
    }

    //编辑属性名称页面
    @RequestMapping(value = "/admin_property_edit",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_property_edit(@RequestParam(value = "id") Integer id){
        Property property = productService.getProperty(id);
        mav.addObject("p",property);
        mav.setViewName("/admin/editProperty");
        return mav;
    }

    //提交编辑的属性名称
    @RequestMapping(value = "/admin_property_update",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_property_update(Property record){
        productService.updateProperty(record);
        List<Property> properties = productService.listProproty(record.getCid());
        mav.addObject("ps",properties);
        mav.setViewName("/admin/listProperty");
        return mav;
    }

    //删除属性名称
    @RequestMapping(value = "/admin_property_delete",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_property_delete(Integer id,HttpServletRequest request){
        try{
            productService.deleteProperty(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        List<Property> properties = productService.listProproty((Integer)request.getSession().getAttribute("cid"));
        mav.addObject("ps",properties);
        mav.setViewName("/admin/listProperty");
        return mav;
    }

    //房间管理
    @RequestMapping(value = "/admin_product_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_product_list(Integer cid){
        return listProduct(cid);
    }

    //添加房间
    @RequestMapping(value = "/admin_product_add",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_product_add(Product product,HttpServletRequest request){
        productService.insert(product);
        List<Product> products = productService.listProducts(product.getCid());
        mav.addObject("ps",products);
        mav.addObject("c",categoryService.getCategory(product.getCid()));
        mav.setViewName("/admin/listProduct");
        return mav;
    }

    //删除房间
    @RequestMapping(value = "/admin_product_delete",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_product_delete(Integer id,Integer cid, HttpServletRequest request){
        productService.delete(id);
        return listProduct(cid);
    }

    //房间图片管理页面
    @RequestMapping(value = "/admin_productImage_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_productImage_list(Integer pid, HttpServletRequest request){
        Product product = productService.getProduct(pid);
        product.setCategory(categoryService.getCategory(product.getCid()));
        List<Productimage> productimages = productService.listProductImage(product.getId());
        mav.addObject("p",product);
        mav.addObject("pisSingle",productimages.stream().filter(e -> e.getType().equals("type_single")).collect(Collectors.toList()));
        mav.addObject("pisDetail",productimages.stream().filter(e -> e.getType().equals("type_detail")).collect(Collectors.toList()));
        mav.setViewName("/admin/listProductImage");
        return mav;
    }

    //删除房间图片
    @RequestMapping(value = "/admin_productImage_delete",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_productImage_delete(Integer id,Integer pid, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            productService.deleteProductImage(id);
        } catch (Exception e) {
            e.getMessage();
        }
        return admin_productImage_list(pid,request);
    }

    //添加房间图片
    @RequestMapping(value = "/admin_productImage_add",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_productImage_add(@RequestParam("filepath") MultipartFile filepath,@RequestParam("type") String type,@RequestParam("pid") int pid,HttpServletRequest request) throws IOException {
        Productimage productimage = new Productimage();
        productimage.setPid(pid);
        productimage.setType(type);
        Integer imageId = productService.insertImage(productimage);
        // Mac系统参考路径 注:此处必须为本地src及target下category的路径
        String path1 = "";
        String path2 = "";
        if (type.equals("type_detail")){
            // windows系统参考路径 注:此处必须为本地src及target下productDetail的路径
            path1 = imgPath+"src\\main\\webapp\\img\\productDetail\\"+imageId.toString()+".jpg";
            path2 = imgPath+"target\\Gotrip-1.0-SNAPSHOT\\img\\productDetail\\"+imageId.toString()+".jpg";
            // Mac系统参考路径
//            path1 = imgPath+"src/main/webapp/img/productDetail/"+imageId.toString()+".jpg";
//            path2 = imgPath+"target/Gotrip-1.0-SNAPSHOT/img/productDetail/"+imageId.toString()+".jpg";

            filepath.transferTo(new File(path1));
            FileUtils.copyFile(new File(path1),new File(path2));
        }
        if (type.equals("type_single")){
            // windows系统参考路径
            path1 = imgPath+"src\\main\\webapp\\img\\productSingle\\"+imageId.toString()+".jpg";
            path2 = imgPath+"target\\Gotrip-1.0-SNAPSHOT\\img\\productSingle\\"+imageId.toString()+".jpg";
            String path3 = imgPath+"src\\main\\/webapp\\img\\productSingle_middle\\"+imageId.toString()+".jpg";;
            String path4 = imgPath+"target\\Gotrip-1.0-SNAPSHOT\\img\\productSingle_middle\\"+imageId.toString()+".jpg";
            String path5 = imgPath+"src\\main\\webapp\\img\\productSingle_small\\"+imageId.toString()+".jpg";;
            String path6 = imgPath+"target\\Gotrip-1.0-SNAPSHOT\\img\\productSingle_small\\"+imageId.toString()+".jpg";

            // Mac系统参考路径
//            path1 = imgPath+"src/main/webapp/img/productSingle/"+imageId.toString()+".jpg";
//            path2 = imgPath+"target/Gotrip-1.0-SNAPSHOT/img/productSingle/"+imageId.toString()+".jpg";
//            String path3 = imgPath+"src/main/webapp/img/productSingle_middle/"+imageId.toString()+".jpg";;
//            String path4 = imgPath+"target/Gotrip-1.0-SNAPSHOT/img/productSingle_middle/"+imageId.toString()+".jpg";
//            String path5 = imgPath+"src/main/webapp/img/productSingle_small/"+imageId.toString()+".jpg";;
//            String path6 = imgPath+"target/Gotrip-1.0-SNAPSHOT/img/productSingle_small/"+imageId.toString()+".jpg";

            filepath.transferTo(new File(path1));
            FileUtils.copyFile(new File(path1),new File(path2));
            FileUtils.copyFile(new File(path1),new File(path3));
            FileUtils.copyFile(new File(path1),new File(path4));
            FileUtils.copyFile(new File(path1),new File(path5));
            FileUtils.copyFile(new File(path1),new File(path6));
        }

        return admin_productImage_list(pid,request);
    }

    //更改房间属性选项
    @RequestMapping(value = "/admin_product_editPropertyValue",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_product_editPropertyValue(Integer id, HttpServletRequest request){
        Product product = productService.getProduct(id);
        List<Propertyvalue> values = productService.listProprotyValue(id);
        for(Propertyvalue p: values){
            p.setProperty(productService.getProperty(p.getPtid()));
        }
        mav.addObject("p",product);
        mav.addObject("pvs",values);
        mav.setViewName("/admin/editProductValue");
        return mav;
    }

    //提交更新房间属性值
    @RequestMapping(value = "/admin_product_updatePropertyValue",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String admin_product_updatePropertyValue(Integer id, Integer pvid, String value){
        productService.updatePropertyValue(pvid, value);
        return "success";
    }

    //提交房间修改信息
    @RequestMapping(value = "/admin_product_update",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_product_update(Product product){
        productService.update(product);
        return admin_product_list(product.getCid());
    }

    //编辑房间信息页面
    @RequestMapping(value = "/admin_product_edit",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_product_edit(Integer id, HttpServletRequest request){
        Product product = productService.getProduct(id);
        mav.addObject("p",product);
        mav.setViewName("/admin/editProduct");
        return mav;
    }

    //编辑房间分类页面
    @RequestMapping(value = "/admin_category_edit",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_category_edit(Integer id, HttpServletRequest request){
        Category category = categoryService.getCategory(id);
        mav.addObject("c",category);
        mav.setViewName("/admin/editCategory");
        return mav;
    }

    //查询所有用户
    @RequestMapping(value = "/admin_user_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView listUser(){
        List<User> users = userService.listAll();
        mav.addObject("us",users);
        mav.setViewName("/admin/listUser");
        return mav;
    }

    //打印用户信息
    @RequestMapping(value = "/admin_print_user_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView printLlistUser(){
        List<User> users = userService.listAll();
        mav.addObject("us",users);
        mav.setViewName("/admin/printListUser");
        return mav;
    }

    //导出用户信息
    @RequestMapping(value = "/admin_export_user_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public void exportUserList(HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<User> users = userService.listAll();
        String []rowsName = new String[]{"id","name","password"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        for(int i = 0;i < users.size();i++){
            Object[] objs = new Object[rowsName.length];
            User user = users.get(i);
            objs[0] = user.getId();
            objs[1] = user.getName();
            objs[2] = user.getPassword();
            dataList.add(objs);
        }
        String fileName = "exportUserExcel";
        //执行导出
        ExportExcel.exportExcel(request,response,fileName, rowsName, dataList, "yyyy-MM-dd HH:mm:ss");
    }

    //删除用户
    @RequestMapping(value = "/admin_user_delete",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_user_delete(Integer id, HttpServletRequest request){
        try{
            userService.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.getMessage();
        }
        List<User> users = userService.listAll();
        mav.addObject("us",users);
        mav.setViewName("/admin/listUser");
        return mav;
    }

    //编辑用户信息页面
    @RequestMapping(value = "/admin_user_edit",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_user_edit(Integer id, HttpServletRequest request){
        User user = userService.getUser(id);
        mav.addObject("u",user);
        mav.setViewName("/admin/editUser");
        return mav;
    }

    //提交更新用户信息
    @RequestMapping(value = "/admin_user_update",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_user_update(User user){
        //user = userService.getUser(user.getName());
        userService.update(user);
        List<User> users = userService.listAll();
        mav.addObject("us",users);
        mav.setViewName("/admin/listUser");
        return mav;
    }

    //添加用户
    @RequestMapping(value = "/admin_user_add",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_user_add(User user,HttpServletRequest request){
        userService.insert(user);
        List<User> users = userService.listAll();
        mav.addObject("us",users);
        mav.setViewName("/admin/listUser");
        return mav;
    }

    //修改订单信息页面
    @RequestMapping(value = "/admin_order_edit",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_order_edit(Integer id, HttpServletRequest request){
        Order order = orderService.getOrder(id);
        request.getSession().setAttribute("oid",id);
        mav.addObject("c",order);
        mav.setViewName("/admin/editOrder");
        return mav;
    }

    //条件查询订单
    @RequestMapping(value = "/select_order_list",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView select_order_list(@RequestParam("status") String status, @RequestParam("min") Integer min, @RequestParam("max") Integer max){
        if(status==null || status.equals("")){
            status = "%";
        }
        if(min==null){
            min = -1;
        }
        if(max==null){
            max = Integer.MAX_VALUE;
        }
        List<Order> orders = orderService.selectOrder(status,min,max);
        for(Order order:orders){
            order.setOrderItems(orderItemService.getOrderItem(order.getId()));
        }
        mav.addObject("os",orders);
        mav.setViewName("/admin/listOrder");
        return mav;
    }

    //提交修改订单信息
    @RequestMapping(value = "/admin_order_update",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_order_update(Order order){
        Order oldOrder = orderService.getOrder(order.getId());
        oldOrder.setReceiver(order.getReceiver());
        oldOrder.setTotal(order.getTotal());
        oldOrder.setStatus(order.getStatus());
        orderService.update(oldOrder);
        mav = listOrder();
        mav.setViewName("/admin/listOrder");
        return mav;
    }

    //超时三天未评论的订单
    @RequestMapping(value = "/outdate_order",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView outdate_order(){
        List<Order> orders = orderService.outDateOrder();
        mav.addObject("os",orders);
        mav.setViewName("/admin/listOrder");
        return mav;
    }

    //添加订单
    @RequestMapping(value = "/admin_order_add",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_order_add(Order order,HttpServletRequest request){
        request.getSession().setAttribute("id",order.getId());
        orderService.insert(order);
        mav = listOrder();
        mav.setViewName("/admin/listOrder");
        return mav;
    }

    //删除订单
    @RequestMapping(value = "/admin_order_delete",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_order_delete(Integer id){
        orderService.deleteOrder(id);
        mav = listOrder();
        mav.setViewName("/admin/listOrder");
        return mav;
    }

    //删除房间分类
    @RequestMapping(value = "/admin_category_delete",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_category_delete(Integer id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            categoryService.delete(id);
        } catch (Exception e) {
            e.getMessage();
        }
        return listCategory();
    }

    //添加属性
    @RequestMapping(value = "/admin_property_add",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView admin_property_add(Property property) throws Exception {
        productService.newProperty(property);
        List<Property> properties = productService.listProproty(property.getCid());
        mav.addObject("ps",properties);
        mav.setViewName("/admin/listProperty");
        return mav;
    }

}

GeneralController

package controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import model.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
@RequestMapping("")
public class GeneralController {
    private static Logger logger = Logger.getLogger(OrderController.class);
    private ModelAndView modelAndView = new ModelAndView();

    @Autowired
    UserService userService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    OrderService orderService;
    @Autowired
    ProductService productService;
    @Autowired
    ReviewService reviewService;

    /**
     * 登录
     * @param request
     * @param response
     * @param name:用户名
     * @param password:密码
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/forelogin",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView logIn(HttpServletRequest request, HttpServletResponse response,String name, String password) throws Exception{
        User user = userService.getUser(name);
        if(user != null && user.getPassword().equals(password)){
            //登陆成功
            request.getSession(true).setAttribute("user",user);
            logger.info(request.getPathInfo());
            return home(request);
        }
        return null;
    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/forelogout",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public void logOut(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.getSession().invalidate();
        response.sendRedirect("home.jsp");
    }

    @RequestMapping(value = "/foresearch",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView search(HttpServletRequest request, HttpServletResponse response, String keyword) throws Exception{
        List<Product> ps = productService.listProducts(keyword);

        modelAndView.addObject("ps",ps);
        modelAndView.setViewName("/searchResult");
        return modelAndView;
    }

    @RequestMapping(value = "/foreproduct",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView product(Integer pid) throws Exception{
        Product p = productService.getProduct(pid);
        List<Propertyvalue> pvs = productService.listProprotyValue(p.getId());
        List<Review> reviews = reviewService.listReview(p.getId());
        modelAndView.addObject("pvs",productService.getPropertyvalues(pvs));
        modelAndView.addObject("p", p);
        modelAndView.addObject("reviews",reviewService.getReviews(reviews));
        modelAndView.setViewName("/product");
        return modelAndView;
    }

    @RequestMapping(value = "/forecheckLogin", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public int check(HttpServletRequest request) throws Exception{
        User user = (User) request.getSession().getAttribute("user");
        if(user != null)
            return 1;
        return 0;
    }

    /**
     * 预定房间按钮点击后的操作,此时便创建了order,订单状态:waitConfirm
     * @param request
     * @param orderitem
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/forebuyone",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  ModelAndView forebuyone(HttpServletRequest request, Orderitem orderitem) throws Exception{
        User user = (User) request.getSession().getAttribute("user");
        Map map =  orderItemService.buyOne(user, orderitem);
        request.getSession(true).setAttribute("order",map.get("order"));
        request.getSession(true).setAttribute("pid",orderitem.getPid());
        Product product = productService.getProduct(orderitem.getPid());
        product.setSaleCount(product.getSaleCount()+orderitem.getNumber());
        product.setStock(product.getStock()-orderitem.getNumber());
        productService.update(product);
        modelAndView.addObject("ois",map.get("ois"));
        modelAndView.addObject("total",((Order)map.get("order")).getTotal());
        modelAndView.setViewName("/buy");
        return modelAndView;
    }

    @RequestMapping(value = "/home",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView home(HttpServletRequest request) throws  Exception{
        List<Category> c = categoryService.listTwoCategory();
        for(Category cs: c){
            cs.setProducts(productService.listProducts(cs.getId()));
        }
        modelAndView.addObject("cs",c);
        modelAndView.setViewName("home");
        return modelAndView;
    }

    /**
     * 创建订单;订单状态:waitPay
     * @param order
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/forecreateOrder",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView buy(Order order, HttpServletRequest request) throws  Exception{
        /**确定支付**/
        Order origin_order = (Order)request.getSession().getAttribute("order");
        origin_order.setOrderCode(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        origin_order.setReceiver(order.getReceiver());
        origin_order.setMobile(order.getMobile());
        origin_order.setUserMessage(order.getUserMessage());
        origin_order.setStatus("waitPay");
        origin_order.setPayDate(new Date());
        if(order.getReceiver()==null || order.getReceiver().equals("")){
            User user = (User)request.getSession().getAttribute("user");
            order.setReceiver(user.getName());
        }
        orderService.update(origin_order);
        modelAndView.addObject("o",origin_order);
        modelAndView.setViewName("/confirmPay");
        return modelAndView;
    }

    /**
     * 确认支付,订单状态:waitReview
     * @param oid
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/foreorderConfirmed", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public ModelAndView confirm(Integer oid) throws Exception{
        Order order = orderService.getOrder(oid);
        order.setStatus("waitReview");
        orderService.update(order);
        modelAndView.setViewName("orderConfirmed");
        return modelAndView;
    }

    @RequestMapping(value = "/forebought", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public ModelAndView bought(HttpServletRequest request) throws Exception{
        modelAndView.setViewName("bought");
        User user = (User) request.getSession().getAttribute("user");
        modelAndView.addObject("os", orderService.getOrders(user));
        return modelAndView;
    }

    @RequestMapping(value = "/deleteorder", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public void deleteorder(HttpServletRequest request,HttpServletResponse response, Integer oid) throws Exception{
        orderService.deleteOrder(oid);
        response.sendRedirect("forebought");
    }

    @RequestMapping(value = "/forestory", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public ModelAndView story()throws Exception{
        modelAndView.setViewName("story");
        return modelAndView;
    }

    /**
     * 跳转到确认支付
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/foreconfirmPay",produces = "text/html; charset=UTF-8")
    @ResponseBody
    public ModelAndView confirmPay(int oid) throws Exception{
        Order order = orderService.getOrder(oid);
        order.setOrderItems(orderItemService.getOrderItem(order.getId()));
        modelAndView.addObject("o",order);
        modelAndView.setViewName("confirmPay");
        return modelAndView;
    }

    /**
     * 跳转到支付页面
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/forealipay",produces = "text/html; charset=UTF-8")
    @ResponseBody
    public ModelAndView alipay() throws Exception{
        modelAndView.setViewName("alipay");
        return modelAndView;
    }

    /**
     * 跳转到评价页面
     * @param oid:订单id
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/forereview", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public ModelAndView review(int oid, HttpServletRequest request)throws Exception{
        Order order = orderService.getOrder(oid);
        Product product = productService.getProduct(orderItemService.getOrderItem(oid).get(0).getPid());
        List<Review> reviews = reviewService.listReview(product.getId());
        modelAndView.addObject("re", reviewService.getReviews(reviews));
        modelAndView.addObject("p",product);
        modelAndView.addObject("o",order);
        modelAndView.addObject("u",request.getSession().getAttribute("user"));
        modelAndView.setViewName("review");
        return modelAndView;
    }

    /**
     * 评价;订单状态:reviewed
     * @param review
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/foredoreviewed", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public ModelAndView reviewed(Review review, HttpServletRequest request)throws Exception{
        Date date = new Date();
        review.setCreateDate(date);
        reviewService.insert(review);
        Product product = productService.getProduct(review.getPid());
        product.setReviewCount(product.getReviewCount()+1);
        productService.update(product);
        Order order = orderService.getOrder(review.getOid());
        order.setStatus("reviewed");
        orderService.update(order);
        return review(review.getOid(),request);
    }
}

如果也想学习本系统,下面领取。关注并回复:012ssm 

本文含有隐藏内容,请 开通VIP 后查看