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

    @Override
    public RemotePostService create(Throwable throwable)
    {
        log.error("用户服务调用失败:{}", throwable.getMessage());
        return new RemotePostService()
        {
            @Override
            public TableDataInfo list(SysPost post, String source) {
                return getDataTableError(new ArrayList<SysPost>());
            }

            @Override
            public AjaxResult getInfo(Long postId, String source) {
                return AjaxResult.error("根据岗位id获取岗位信息失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult add(SysPost post, String source) {
                return AjaxResult.error("新增岗位失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult edit(SysPost post, String source) {
                return AjaxResult.error("修改岗位失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult remove(Long[] postIds, String source) {
                return AjaxResult.error("删除岗位失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult optionselect(String source) {
                return AjaxResult.warn("获取所有岗位列表失败:" + 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;
            }
        };
    }
}
