package com.example.hotel.serviceImpl.order;
import com.example.hotel.service.hotel.HotelService;
import com.example.hotel.service.hotel.RoomService;
import com.example.hotel.service.order.OrderService;
import com.example.hotel.service.user.AccountService;
import com.example.hotel.data.order.OrderMapper;
import com.example.hotel.po.Order;
import com.example.hotel.po.User;
import com.example.hotel.vo.OrderVO;
import com.example.hotel.vo.ResponseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class OrderServiceImpl implements OrderService {
private final static String RESERVE_ERROR = "预订失败";
private final static String ROOMNUM_LACK = "预订房间数量剩余不足";
private final static String ANNUL_ERROR = "删除失败";
@Autowired
OrderMapper orderMapper;
@Autowired
HotelService hotelService;
@Autowired
AccountService accountService;
@Autowired
OrderService orderService;
@Autowired
RoomService roomService;
@Override
public ResponseVO addOrder(OrderVO orderVO) {
int reserveRoomNum = orderVO.getRoomNum();
int curNum = hotelService.getRoomCurNum(orderVO.getHotelId(),orderVO.getRoomType());
if(reserveRoomNum>curNum){
return ResponseVO.buildFailure(ROOMNUM_LACK);
}
try {
// 获取日期和时间
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
String curdate = sf.format(date);
orderVO.setCreateDate(curdate);
orderVO.setOrderState("已预订");
User user = accountService.getUserInfo(orderVO.getUserId());
orderVO.setClientName(user.getUserName());
orderVO.setPhoneNumber(user.getPhoneNumber());
Order order = new Order();
BeanUtils.copyProperties(orderVO,order);
orderMapper.addOrder(order);
hotelService.updateRoomInfo(orderVO.getHotelId(),orderVO.getRoomType(),orderVO.getRoomNum()); //这里的update是原有房间数减去这次订单房间数
} catch (Exception e) {
System.out.println(e.getMessage());
return ResponseVO.buildFailure(RESERVE_ERROR);
}
return ResponseVO.buildSuccess(true);
}
@Override
public List<Order> getAllOrders() {
return orderMapper.getAllOrders();
}
@Override
public List<Order> getUserOrders(int userid) {
return orderMapper.getUserOrders(userid);
}
@Override
public ResponseVO annulOrder(int orderid) {
//取消订单逻辑的具体实现(注意可能有和别的业务类之间的交互)
Order order=orderMapper.getOrderById(orderid);
//房间数
int roomNum=order.getRoomNum();
//酒店编号
int hotelId=order.getHotelId();
//房间类型
String roomType=order.getRoomType();
try {
//删除订单
orderMapper.annulOrder(orderid);
//更新相应酒店客房信息,增加剩余房间数
roomService.addRoomNum(hotelId, roomType, roomNum);
}catch (Exception e) {
System.out.println(e.getMessage());
return ResponseVO.buildFailure(ANNUL_ERROR);
}
return ResponseVO.buildSuccess(true);
}
@Override
public int deleteOrder(Integer orderid) {
Order order=orderMapper.getOrderById(orderid);
//房间数
int roomNum=order.getRoomNum();
//酒店编号
int hotelId=order.getHotelId();
//房间类型
String roomType=order.getRoomType();
roomService.addRoomNum(hotelId, roomType, roomNum);
return orderMapper.deleteOrder(orderid);
}
@Override
public List<Order> getHotelOrders(Integer hotelId) {
List<Order> orders = orderService.getAllOrders();
return orders.stream().filter(order -> order.getHotelId().equals(hotelId)).collect(Collectors.toList());
}
@Override
public List<Order> getOnSearchOrders(BigInteger phoneNumber) {
return orderMapper.getOnSearchOrders(phoneNumber);
}
}