package com.bonus.system.api.factory;

import com.bonus.common.core.constant.HttpStatus;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.system.api.RemoteConfigService;
import com.bonus.system.api.domain.SysConfig;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户服务降级处理
 * 
 * @author bonus
 */
@Component
public class RemoteConfigFallbackFactory implements FallbackFactory<RemoteConfigService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteConfigFallbackFactory.class);

    @Override
    public RemoteConfigService create(Throwable throwable)
    {
        log.error("系统服务调用失败:{}", throwable.getMessage());
        return new RemoteConfigService()
        {
            @Override
            public TableDataInfo list(SysConfig config, String source){
               return getDataTableError(new ArrayList<SysConfig>());
            }

            @Override
            public AjaxResult getInfo(Long configId, String source){
                return AjaxResult.error("根据参数编号获取参数配置列表失败:" + throwable.getMessage());
            }
            @Override
            public AjaxResult getConfigKey(String configKey){
                return AjaxResult.error("根据参数键名获取参数配置列表失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult add(SysConfig config, String source){
                return AjaxResult.error("新增参数配置失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult edit(SysConfig config, String source){
                return AjaxResult.error("修改参数配置失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult remove(Long[] configIds, String source){
                return AjaxResult.error("删除参数配置失败:" + throwable.getMessage());
            }

            /**
             * 响应请求分页数据
             */
            private TableDataInfo getDataTableError(List<?> list)
            {
                TableDataInfo rspData = new TableDataInfo();
                rspData.setCode(HttpStatus.ERROR);
                rspData.setRows(list);
                rspData.setMsg("系统错误,请联系管理员");
                rspData.setTotal(new PageInfo(list).getTotal());
                return rspData;
            }

        };
    }
}
