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.RemoteDictDataService;
import com.bonus.system.api.domain.SysDictData;
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 RemoteDictDataFallbackFactory implements FallbackFactory<RemoteDictDataService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteDictDataFallbackFactory.class);

    @Override
    public RemoteDictDataService create(Throwable throwable)
    {
        log.error("用户服务调用失败:{}", throwable.getMessage());
        return new RemoteDictDataService()
        {

            @Override
            public TableDataInfo list(SysDictData dictData, String source) {
                return getDataTableError(new ArrayList<SysDictData>());
            }

            @Override
            public AjaxResult getInfo(Long dictCode, String source) {
                return AjaxResult.error("根据条件获取字典数据失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult dictType(String dictType, String source) {
                return AjaxResult.error("根据字典类型获取字典数据失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult add(SysDictData dict, String source) {
                return AjaxResult.error("新增字典数据失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult edit(SysDictData dict, String source) {
                return AjaxResult.error("修改字典数据失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult remove(Long[] dictCodes, 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;
            }
        };
    }
}
