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; |
} |
} |
初级程序员
by: 丨Z丨 发表于:2017-03-16 11:27:20 顶(0) | 踩(0) 回复
阿斯蒂芬
回复评论