package com.mortals.xhx.feign.error;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.error.ErrorLogPdu;
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-08-17
 */
@FeignClient(name = "log-platform", path = "/logservice", fallbackFactory = ErrorLogFeignFallbackFactory.class)
public interface IErrorLogFeign extends IFeign {


    /**
     * 查看异常日志列表
     *
     * @param errorLogPdu
     * @return
     */
    @PostMapping(value = "/error/log/list")
    Rest<RespData<List<ErrorLogPdu>>> list(@RequestBody ErrorLogPdu errorLogPdu);


    /**
     * 查看异常日志
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/error/log/info")
    Rest<ErrorLogPdu> info(@RequestParam(value = "id") Long id);

    /**
     * 删除异常日志
     *
     * @param ids
     * @return
     */
    @GetMapping(value = "/error/log/delete")
    Rest<Void> delete(Long[] ids,@RequestHeader("Authorization") String authorization);


    /**
     * 异常日志保存更新
     *
     * @param errorLogPdu
     * @return
     */
    @PostMapping(value = "/error/log/save")
    Rest<RespData<ErrorLogPdu>> save(@RequestBody ErrorLogPdu errorLogPdu,@RequestHeader("Authorization") String authorization);


}


@Slf4j
@Component
class ErrorLogFeignFallbackFactory implements FallbackFactory<IErrorLogFeign> {
    @Override
    public IErrorLogFeign create(Throwable t) {
        return new IErrorLogFeign() {
            @Override
            public Rest<RespData<List<ErrorLogPdu>>> list(ErrorLogPdu errorLogPdu) {
               return Rest.fail("暂时无法获取异常日志列表，请稍后再试!");
            }

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

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

            @Override
            public Rest<RespData<ErrorLogPdu>> save(ErrorLogPdu errorLogPdu, String authorization) {
                return Rest.fail("暂时无法保存异常日志，请稍后再试!");
            }
        };
    }
}








