package fcb.project.manager.core.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import fcb.project.manager.base.utils.FcbUtils;
import fdage.back.sdk.base.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 2 * @Author: Abner
 * 3 * @Date: 2021/1/11 10:10
 * 4
 */
@Api(tags = "api房源数据查询接口")
@Log4j2
@RestController
@RequestMapping("fcb/project/api/query")
@DefaultProperties(defaultFallback = "defaultFail")
public class ApiQueryHouseController {


    @Value("${fcb.client.secret}")
    private String fcbClientSecret;

    @Value("${fcb.client.code}")
    private String fcbClientCode;

    @Autowired
    private FcbUtils fcbUtils;


    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500"),
    },
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "300"),
                    // BlockingQueue的最大队列数，当设为-1，会使用SynchronousQueue，值为正时使用LinkedBlockingQueue。
                    @HystrixProperty(name = "maxQueueSize", value = "200"),
            })
    @ApiOperation(value = "获取房车宝的签名")
    @PostMapping("/authCode")
    public Result<Object> getFcbSign(@RequestBody Map<String, Object> params) {
        return fcbUtils.getFcbSign(params, fcbClientSecret, fcbClientCode);
    }


    private  Result<Object> defaultFail() {
        log.info("触发了熔断器，这里给出默认返回");
        return  Result.failure(3444 , "网络拥挤，请稍后再试");
    }
}
