package cn.reghao.tnb.content.app.mall.controller;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.content.api.dto.TaobaoItem;
import cn.reghao.tnb.content.app.mall.model.dto.BuyProductDto;
import cn.reghao.tnb.content.app.mall.model.dto.ProductAddDto;
import cn.reghao.tnb.content.app.mall.model.po.Product;
import cn.reghao.tnb.content.app.mall.service.BuyService;
import cn.reghao.tnb.content.app.mall.service.ProductService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2024-04-16 10:53:54
 */
@Tag(name = "商品接口")
@RestController
@RequestMapping("/api/mall/product")
public class ProductController {
    private final ProductService productService;
    private final BuyService buyService;

    public ProductController(ProductService productService, BuyService buyService) {
        this.productService = productService;
        this.buyService = buyService;
    }

    @Operation(summary = "添加淘宝商品", description = "N")
    @PostMapping("/taobao")
    public String addCam(@RequestBody @Validated TaobaoItem taobaoItem) {
        productService.add(taobaoItem);
        return WebResult.success();
    }

    @Operation(summary = "发布商品", description = "N")
    @PostMapping("")
    public String addProduct(@RequestBody @Validated ProductAddDto productAddDto) throws Exception {
        Result result = productService.addProduct(productAddDto);
        return WebResult.result(result);
    }

    @Operation(summary = "获取商品列表", description = "N")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getProducts(@RequestParam("page") int page) {
        PageList<Product> pageList = productService.getPage(page);
        return WebResult.success(pageList);
    }

    @Operation(summary = "获取商品详情", description = "N")
    @GetMapping(value = "/{itemId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @HystrixCommand(
            fallbackMethod = "getProductFallback",
            commandProperties = {
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value = "false"),
                    @HystrixProperty(name="metrics.rollingStats.timeInMilliseconds", value="5000"),
                    @HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="4"),
                    @HystrixProperty(name="circuitBreaker.errorThresholdPercentage", value="50"),
                    @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds", value="3000")
            })
    public String getProduct(@PathVariable("itemId") Long itemId) {
        Product product = productService.getProduct(itemId);
        return WebResult.success(product);
    }

    public String getProductFallback(Long itemId) {
        return WebResult.fallback("fallback");
    }

    @Operation(summary = "购买商品", description = "N")
    @PostMapping("/buy")
    public String buyProduct(@RequestBody @Validated BuyProductDto buyProductDto) {
        long orderId = buyService.buyProduct(buyProductDto);
        return WebResult.success(orderId);
    }
}
