package com.mortals.xhx.feign.base;

import com.alibaba.fastjson.JSON;
import com.mortals.xhx.common.code.ApiRespCodeEnum;
import com.mortals.xhx.common.pdu.dept.DeptPdu;
import com.mortals.xhx.feign.IFeign;
import com.mortals.xhx.feign.base.pdu.SitePdu;
import com.mortals.xhx.feign.rsp.ApiResp;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = "base-manager", path = "/base", fallbackFactory = BaseManagerFeignFallbackFactory.class)
public interface IApiBaseManagerFeign extends IFeign {

    /**
     * 根据条件查询站点列表
     * @param query
     * @return
     */
    @PostMapping(value = "/site/list/noPage")
    String getSitesByQuery(@RequestBody SitePdu query);

    /**
     * 根据条件查询站点列表
     * @param query
     * @return
     */
    @PostMapping(value = "/dept/interlist")
    String getDeptByQuery(@RequestBody DeptPdu query);
}

@Slf4j
@Component
class BaseManagerFeignFallbackFactory implements FallbackFactory<IApiBaseManagerFeign> {

    @Override
    public IApiBaseManagerFeign create(Throwable throwable) {
        return new IApiBaseManagerFeign() {

            @Override
            public String getSitesByQuery(SitePdu query) {
                ApiResp<String> failResp = new ApiResp<>();
                failResp.setCode(ApiRespCodeEnum.FAILED.getValue());
                failResp.setMsg("暂时无法获取站点列表，请稍后再试!");
                return JSON.toJSONString(failResp);
            }

            @Override
            public String getDeptByQuery(DeptPdu query) {
                ApiResp<String> failResp = new ApiResp<>();
                failResp.setCode(ApiRespCodeEnum.FAILED.getValue());
                failResp.setMsg("暂时无法获取站点部门列表，请稍后再试!");
                return JSON.toJSONString(failResp);
            }
        };
    }
}