package com.test.controller;

import com.test.bean.Order;
import com.test.bean.Result;
import com.test.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/getAllOrderByPage")
    public Result getAllOrderByPage(@RequestParam(defaultValue = "1")  int page,
                                    @RequestParam(defaultValue = "5") int pageSize) {
        return Result.success(orderService.getAllOrderByPage(page, pageSize));
    }

    @GetMapping
    public Result getOrderById(@RequestParam Long id){
        return Result.success(orderService.getOrderById(id));
    }

    @PostMapping
    public Result createOrder(@RequestBody Order order){
        if (orderService.createOrder(order)){
            return Result.success();
        }
        return Result.error("创建订单失败");

    }

    @GetMapping("/checkout")
    public Result checkoutOrder(@RequestParam Long id){

        Order order = orderService.getOrderById(id);
        if (order.getStatus() == 1){
            return Result.error("订单已支付");
        }
        Order checkoutOrder = orderService.checkoutOrder(id);
        if (checkoutOrder != null ){
            return Result.success(checkoutOrder);
        }
        return Result.error("订单支付失败");

    }
    @PutMapping
    public Result updateOrder(@RequestBody Order order){
        if (orderService.updateOrder(order)){
            return Result.success();
        }
        return Result.error("更新订单失败");
    }
}

