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.OrderItemDto;
import com.mall.order.domain.entity.OrderItem;
import com.mall.order.domain.vo.OrderItemVo;
import com.mall.order.service.OrderItemService;
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-item")
@RequiredArgsConstructor
public class OrderItemController {

    private final OrderItemService orderItemService;

    @Operation(summary = "分页查询订单项信息", description = "分页订单项信息")
    @GetMapping("{page}/{limit}")
    public Result<PageResult<OrderItemVo>> getOrderItemPage(
            @Parameter(name = "page", description = "当前页", required = true)
            @PathVariable("page") Integer page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable("limit") Integer limit,
            OrderItemDto dto) {
        Page<OrderItem> pageParams = new Page<>(page, limit);
        PageResult<OrderItemVo> pageResult = orderItemService.getOrderItemPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @Operation(summary = "添加订单项信息", description = "添加订单项信息")
    @PostMapping()
    public Result<String> addOrderItem(@Valid @RequestBody OrderItemDto dto) {
        orderItemService.addOrderItem(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @Operation(summary = "更新订单项信息", description = "更新订单项信息")
    @PutMapping()
    public Result<String> updateOrderItem(@Valid @RequestBody OrderItemDto dto) {
        orderItemService.updateOrderItem(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

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