用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

租借自行车RentBikeController

2017-03-08 作者:知行合一心举报

[java]代码库

package com.liang.bike.controller.rent;

import com.alibaba.fastjson.JSONObject;
import com.liang.bike.bean.base.BikeBase;
import com.liang.bike.bean.base.OrderBase;
import com.liang.bike.bean.base.UserBase;
import com.liang.bike.enumeration.BikeStatusEnum;
import com.liang.bike.enumeration.OperateEnum;
import com.liang.bike.service.rent.IRentBikeService;
import com.liang.bike.utill.ReturnUtill;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/22.
 */
@Controller
@RequestMapping("/rent")
public class RentBikeController {

    Logger logger = Logger.getLogger(RentBikeController.class);

    @Autowired
    private IRentBikeService rentBikeService;

    //获取用户信息
    public UserBase getUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (UserBase) session.getAttribute("user");
    }

    /**
     * 生成租借自行车订单
     *
     * @param request
     * @param bikeBase 自行车信息
     * @return
     */


    @RequestMapping("/bike")
    public String rentBike(HttpServletRequest request, Model model, BikeBase bikeBase) {
        UserBase userBase = getUser(request);
        String page = "user/rent";
        try {
            //查询自行车是否存在
            BikeBase bike = rentBikeService.selectBikeByBikeNo(bikeBase);
            model.addAttribute("bike", bike);
            if (bike == null) {
                model.addAttribute("index", OperateEnum.BIKE_BIKE_NOT_EXIST.getIndex());
                model.addAttribute("desc", OperateEnum.BIKE_BIKE_NOT_EXIST.getDesc());
                return page;
            }
            //自行车是否在空闲状态
            String status = bike.getBikeStatus();
            if (status != null && !status.equals(BikeStatusEnum.FREE.getKey())) {
                //如果正在使用自行车的正是当前用户
                if (status.equals(BikeStatusEnum.USING.getKey())) {
                    OrderBase orderBase = new OrderBase();
                    orderBase.setUserId(userBase.getUserId());
                    orderBase.setBikeId(bikeBase.getBikeId());
                    orderBase.setReturnBy("");
                    OrderBase order = rentBikeService.selectOrder(orderBase);
                    if (order != null) {
                        page = "user/running";
                        model.addAttribute("order", order);
                        model.addAttribute("createDate", order.getCreateDate().getTime());
                        return page;
                    }
                }
                model.addAttribute("index", status);
                model.addAttribute("desc", OperateEnum.getOperateDesc(status));

                return page;
            }
            //修改自行车使用状态并生成订单
            rentBikeService.rentBike(bike, userBase);
            //自行车状态描述
        } catch (Exception e) {
            //e.printStackTrace();
            logger.info("租借自行车失败RentBikeController.rentBike" + e.getMessage());
            model.addAttribute("index", OperateEnum.OPERATE_ERROR.getIndex());
            model.addAttribute("desc", OperateEnum.OPERATE_ERROR.getDesc());
            return page;
        }
        model.addAttribute("index", OperateEnum.OPERATE_SUCCESS.getIndex());
        return page;
    }

    /**
     * 完成订单
     *
     * @param request
     * @param model
     * @param orderBase
     * @return
     */
    @RequestMapping("/finish")
    @ResponseBody
    public JSONObject finish(HttpServletRequest request, Model model, OrderBase orderBase) {
        JSONObject json = new JSONObject();
        UserBase userBase = getUser(request);
        orderBase.setReturnBy(userBase.getUserName());
        orderBase.setReturnDate(new Date());
        boolean flag = false;
        try {
            rentBikeService.finishOrder(orderBase);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        json.put("flag", flag);
        return json;
    }
}


分享到:
更多

网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。