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

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

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

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

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

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

            @Override
            public AjaxResult remove(Long[] dictIds, String source) {
                return AjaxResult.error("删除字典类型失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult optionselect(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;
            }
        };
    }
}
