package com.luozhihui.project.controller;

import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.entity.Order;
import com.luozhihui.project.mapper.CarRentMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class CarRentController {
    @Resource
    CarRentMapper carRentMapper;
    @PostMapping("/carRent")
    public ApiResponse carRent( Order order) {
        try {
            // 添加订单
            carRentMapper.addorder(order);
            // 更新车辆状态
            carRentMapper.updateCarStatus(Integer.valueOf(order.getCarId()));
            // 返回成功消息
            return new ApiResponse(null, 200, "租车成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 返回内部服务器错误
            return new ApiResponse(null, 500, "服务器内部错误");
        }
    }

    @PostMapping("/returnCar")
    public ApiResponse returnCar(Order order) {
        try {
            // 将订单费用以及还车照片还车时间插入数据表
            carRentMapper.carRented(order);
            // 根据订单号更新订单状态为已完成
            carRentMapper.finishOrder(order.getOrderId());
            // 根据车辆号更新车辆状态为可用
            carRentMapper.endCarStatus(order.getCarId(), order.getParking_id());
            // 返回成功消息
            return new ApiResponse(null, 200, "还车成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 返回内部服务器错误
            return new ApiResponse(null, 500, "服务器内部错误");
        }
    }
}
