package com.erp.supplier.client;

import lombok.Data;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * 商品服务Feign客户端
 * 用于获取商品信息
 */
@FeignClient(name = "product-service", url = "${product.service.url:http://product-service}")
public interface ProductClient {

    /**
     * 获取商品信息
     */
    @GetMapping("/api/products/{id}")
    ProductClient.ApiResult<ProductInfo> getProduct(@PathVariable("id") Long id);

    /**
     * 检查商品是否存在
     */
    @GetMapping("/api/products/{id}/exists")
    ProductClient.ApiResult<Boolean> productExists(@PathVariable("id") Long id);

    @Data
    class ProductInfo {
        private Long id;
        private String productCode;
        private String productName;
        private String spec;
        private String unit;
        private java.math.BigDecimal price;
        private String categoryName;
    }

    @Data
    class ApiResult<T> {
        private int code;
        private T data;
        private String message;

        public static <T> ApiResult<T> success(T data) {
            ApiResult<T> result = new ApiResult<>();
            result.setCode(200);
            result.setData(data);
            result.setMessage("success");
            return result;
        }

        public static <T> ApiResult<T> error(int code, String message) {
            ApiResult<T> result = new ApiResult<>();
            result.setCode(code);
            result.setMessage(message);
            return result;
        }
    }
}
