package com.liang.bike.service.rent; |
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.dao.base.*; |
import com.liang.bike.enumeration.BikeStatusEnum; |
import org.springframework.beans.factory.annotation.Autowired; |
import org.springframework.stereotype.Service; |
import org.springframework.transaction.annotation.Transactional; |
import java.util.Date; |
import java.util.HashMap; |
import java.util.Map; |
/** |
* Created by Administrator on 2017/2/23. |
*/ |
@Service |
public class RentBikeServiceImpl implements IRentBikeService { |
@Autowired |
private UserMapper userMapper; |
@Autowired |
private BikeMapper bikeMapper; |
@Autowired |
private BikeBaseMapper bikeBaseMapper; |
@Autowired |
private OrderBaseMapper orderBaseMapper; |
@Autowired |
private OrderMapper orderMapper; |
public UserBase selectUserByUserPhone(UserBase userBase) { |
return userMapper.selectUserByUserPhone(userBase); |
} |
public BikeBase selectBikeByBikeNo(BikeBase bikeBase) { |
return bikeMapper.selectBikeByBikeNo(bikeBase); |
} |
/** |
* 修改自行车使用状态及使用次数 |
* |
* @param params |
* @return |
*/ |
public int updateBikeByStatusTotal(Map<String, Object> params) { |
return bikeMapper.updateBikeStatusTotal(params); |
} |
public void generateOrder(OrderBase orderBase) { |
orderBaseMapper.insertSelective(orderBase); |
} |
/** |
* 租借自行车 |
* |
* @param bikeBase |
* @return |
*/ |
@Transactional |
public void rentBike(BikeBase bikeBase, UserBase userBase) throws Exception { |
//修改自行车使用状态 |
Map<String, Object> params = new HashMap<String, Object>(); |
Long bikeNo = bikeBase.getBikeNo(); |
String bikeStatus = BikeStatusEnum.USING.getKey(); |
Long useTotal = bikeBase.getUseTotal(); |
Long useTotalAddOne = useTotal + 1 ; |
String updateBy = userBase.getUserPhone(); |
Date updateDate = new Date(); |
params.put( "bikeNo" , bikeNo); |
params.put( "bikeStatus" , bikeStatus); |
params.put( "useTotal" , useTotal); |
params.put( "useTotalAddOne" , useTotalAddOne); |
params.put( "updateBy" , updateBy); |
params.put( "updateDate" , updateDate); |
int i = this .updateBikeByStatusTotal(params); |
if (i == 0 ) { |
throw new Exception( "租借自行车失败RentBikeServiceImpl.rentBike" ); |
} |
OrderBase orderBase = new OrderBase(); |
orderBase.setUserId(userBase.getUserId()); |
orderBase.setBikeId(bikeBase.getBikeId()); |
orderBase.setCreateBy(userBase.getUserPhone()); |
orderBase.setCreateDate( new Date()); |
orderBase.setUpdateBy(userBase.getUserPhone()); |
orderBase.setUpdateDate( new Date()); |
orderBase.setReturnBy( "" ); |
this .generateOrder(orderBase); |
} |
@Override |
public OrderBase selectOrder(OrderBase orderBase) { |
return orderMapper.selectOrder(orderBase); |
} |
@Override |
@Transactional |
public void finishOrder(OrderBase orderBase) { |
orderBaseMapper.updateByPrimaryKeySelective(orderBase); |
BikeBase bikeBase = new BikeBase(); |
bikeBase.setBikeStatus(BikeStatusEnum.FREE.getKey()); |
bikeBase.setBikeId(orderBase.getBikeId()); |
bikeBaseMapper.updateByPrimaryKeySelective(bikeBase); |
} |
} |