package com.mortals.xhx.feign.site;

import com.mortals.xhx.common.pdu.RespData;
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.http.MediaType;
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", fallbackFactory = SiteFeignFallbackFactory.class)
public interface ISiteFeign extends IFeign {


    /**
     * 查看站点列表
     *
     * @param sitePdu
     * @return
     */
    @PostMapping(value = "/site/list")
    Rest<RespData<List<SitePdu>>> list(@RequestBody SitePdu sitePdu);


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

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


    /**
     * 站点保存更新
     *
     * @param sitePdu
     * @return
     */
    @PostMapping(value = "/site/save")
    Rest<RespData<SitePdu>> 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) {
        log.error("异常：", t);
        return new ISiteFeign() {
            @Override
            public Rest<RespData<List<SitePdu>>> list(SitePdu sitePdu) {
                return Rest.fail("暂时无法获取站点列表，请稍后再试!");
            }

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

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

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

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

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








