package com.mall.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.domain.vo.result.PageResult;
import com.mall.common.domain.vo.result.Result;
import com.mall.common.domain.vo.result.ResultCodeEnum;
import com.mall.order.domain.dto.OrderDto;
import com.mall.order.domain.entity.Order;
import com.mall.order.domain.vo.OrderVo;
import com.mall.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Bunny
 * @since 2025-07-05 18:54:06
 */
@Tag(name = "订单", description = "订单相关接口")
@RestController
@RequestMapping("/api/order/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @Operation(summary = "分页查询订单", description = "分页订单")
    @GetMapping("{page}/{limit}")
    public Result<PageResult<OrderVo>> getOrderPage(
            @Parameter(name = "page", description = "当前页", required = true)
            @PathVariable("page") Integer page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable("limit") Integer limit,
            OrderDto dto) {
        Page<Order> pageParams = new Page<>(page, limit);
        PageResult<OrderVo> pageResult = orderService.getOrderPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @Operation(summary = "添加订单", description = "添加订单")
    @PostMapping()
    public Result<String> addOrder(@Valid @RequestBody OrderDto dto) {
        orderService.addOrder(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @Operation(summary = "更新订单", description = "更新订单")
    @PutMapping()
    public Result<String> updateOrder(@Valid @RequestBody OrderDto dto) {
        orderService.updateOrder(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @Operation(summary = "删除订单", description = "删除订单")
    @DeleteMapping()
    public Result<String> deleteOrder(@RequestBody List<Long> ids) {
        orderService.deleteOrder(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}