package com.erp.common.web.feign;

import com.erp.common.web.dto.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单服务Feign客户端
 * 示例：用户服务调用订单服务
 */
@FeignClient(name = "order-service", path = "/api/orders")
public interface OrderServiceClient {

    /**
     * 创建订单
     */
    @PostMapping
    Result<OrderDTO> createOrder(@RequestBody CreateOrderRequest request);

    /**
     * 获取用户订单列表
     */
    @GetMapping("/user/{userId}")
    Result<PagedResult<OrderDTO>> getUserOrders(
            @PathVariable("userId") Long userId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size,
            @RequestParam(value = "status", required = false) String status
    );

    /**
     * 获取订单详情
     */
    @GetMapping("/{orderId}")
    Result<OrderDTO> getOrderById(@PathVariable("orderId") Long orderId);

    /**
     * 取消订单
     */
    @PostMapping("/{orderId}/cancel")
    Result<Void> cancelOrder(@PathVariable("orderId") Long orderId);

    /**
     * 订单统计
     */
    @GetMapping("/user/{userId}/statistics")
    Result<OrderStatisticsDTO> getUserOrderStatistics(@PathVariable("userId") Long userId);
}

/**
 * 创建订单请求
 */
class CreateOrderRequest {
    private Long userId;
    private List<OrderItemRequest> items;
    private String shippingAddress;
    private String remark;
    
    // getters and setters
}

/**
 * 订单项请求
 */
class OrderItemRequest {
    private Long productId;
    private Integer quantity;
    private BigDecimal price;
    
    // getters and setters
}

/**
 * 订单DTO
 */
class OrderDTO {
    private Long id;
    private String orderNo;
    private Long userId;
    private BigDecimal totalAmount;
    private String status;
    private LocalDateTime createdAt;
    private List<OrderItemDTO> items;
    
    // getters and setters
}

/**
 * 订单项DTO
 */
class OrderItemDTO {
    private Long productId;
    private String productName;
    private Integer quantity;
    private BigDecimal price;
    private BigDecimal subtotal;
    
    // getters and setters
}

/**
 * 订单统计DTO
 */
class OrderStatisticsDTO {
    private Integer totalOrders;
    private BigDecimal totalAmount;
    private Integer pendingOrders;
    private Integer completedOrders;
    private Integer canceledOrders;
    
    // getters and setters
}

/**
 * 分页结果
 */
class PagedResult<T> {
    private List<T> records;
    private Long total;
    private Integer size;
    private Integer current;
    private Integer pages;
    
    // getters and setters
}