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

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

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

            @Override
            public AjaxResult add(SysNotice notice, String source) {
                return AjaxResult.error("新增公告失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult edit(SysNotice notice, String source) {
                return AjaxResult.error("修改公告失败:" + throwable.getMessage());
            }

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