package com.mortals.xhx.feign.site;

import com.mortals.xhx.common.pdu.site.SitePdu;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.feign.IFeign;
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.*;

import java.util.List;

/**
 * 站点 Feign接口
 *
 * @author zxfei
 * @date 2022-06-28
 */
@FeignClient(name = "base-manager", path = "/base", fallback = SiteFeignFallbackFactory.class)
public interface ISiteFeign extends IFeign {


    /**
     * 查看站点列表
     *
     * @param sitePdu
     * @return
     */
    @PostMapping(value = "/site/list")
    String list(@RequestBody SitePdu sitePdu);


    /**
     * 查看站点
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/site/info")
    String info(@RequestParam(value = "id") Long id);

    /**
     * 删除站点
     *
     * @param ids
     * @return
     */
    @GetMapping(value = "/site/delete")
    String delete(Long[] ids, @RequestHeader("Authorization") String authorization);


    /**
     * 站点保存更新
     *
     * @param sitePdu
     * @return
     */
    @PostMapping(value = "/site/save")
    String save(@RequestBody SitePdu sitePdu, @RequestHeader("Authorization") String authorization);

    /**
     * 获取站点树
     *
     * @return
     */
    @GetMapping(value = "/site/siteTree")
    String siteTree(@RequestHeader("Authorization") String authorization);

    /**
     * 查询站点及子站点
     *
     * @param sitePdu
     * @return
     */
    @GetMapping(value = "/site/getFlatSitesBySiteId")
    Rest<List<SitePdu>> getFlatSitesBySiteId(@RequestBody SitePdu sitePdu);


}


@Slf4j
@Component
class SiteFeignFallbackFactory implements FallbackFactory<ISiteFeign> {
    @Override
    public ISiteFeign create(Throwable t) {
        return new ISiteFeign() {
            @Override
            public String list(SitePdu sitePdu) {
                return JSON.toJSONString(Rest.fail("暂时无法获取站点列表，请稍后再试!"));
            }

            @Override
            public String info(Long id) {
                return JSON.toJSONString(Rest.fail("暂时无法获取站点详细，请稍后再试!"));
            }

            @Override
            public String delete(Long[] ids, String authorization) {
                return JSON.toJSONString(Rest.fail("暂时无法删除站点，请稍后再试!"));
            }

            @Override
            public String save(SitePdu sitePdu, String authorization) {
                return JSON.toJSONString(Rest.fail("暂时无法保存站点，请稍后再试!"));
            }

            @Override
            public String siteTree(String authorization) {
                return JSON.toJSONString(Rest.fail("暂时无法获取站点树，请稍后再试!"));
            }

            @Override
            public Rest<List<SitePdu>> getFlatSitesBySiteId(SitePdu sitePdu) {
                return  Rest.fail("暂时无法获取站点子站点，请稍后再试!");
            }
        };
    }
}








