package com.zd.system.api.factory;

import com.zd.common.core.domain.R;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.system.api.exam.RemoteExamService;
import com.zd.system.api.exam.domain.ElResources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 考试服务降级处理
 *
 * @author zhoupan
 */
@Component
public class RemoteExamFallbackFactory implements FallbackFactory<RemoteExamService> {
    private static final Logger log = LoggerFactory.getLogger(RemoteExamFallbackFactory.class);

    @Override
    public RemoteExamService create(Throwable throwable) {
        log.error("考试服务调用失败:{}", throwable.getMessage());
        return new RemoteExamService() {
            @Override
            public TableDataInfo list(Map exam) {
                TableDataInfo dataInfo = new TableDataInfo<>();
                dataInfo.setCode(R.FAIL);
                return dataInfo;
            }

            @Override
            public R<Object> createPaper(Map elPaper) {
                return R.fail("开始考试失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> handPaper(Map elPaper) {
                return R.fail("交卷失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> queryQuDetail(Map paperQu) {
                return R.fail("调用答题过程失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> fillAnswer(Map<String,Object> paperQuMap) {
                return R.fail("提交答案失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> getInfo(Long id) {
                return R.fail("获取试卷管理详细信息失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> recoveryPoints(Map<String,Object> recovery) {
                if(recovery.get("pointsType")!=null && recovery.get("pointsType").equals("2")){
                    return R.fail("移除黑名单失败:" + throwable.getMessage());
                }else if(recovery.get("pointsType")!=null && recovery.get("pointsType").equals("1")){
                    return R.fail("移除负面清单失败:" + throwable.getMessage());
                }
                return R.fail("移除失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> deductionPoints(Map<String,Object> deductionPoint) {
                return R.fail("扣除积分失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> getPointsConfig(Map<String,Object> markconfig) {
                return R.fail("获取积分列表失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> initCreditPoints(Map<String,Object> initMap) {
                return R.fail("初始化积分失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> obtainBonusPoints(Map<String,Object> obtainMap) {
                return R.fail("获取奖励积分失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> getLastMonthViolation(Map<String,Object> obtainMap) {
                return R.fail("获取上月的违规记录列表失败:" + throwable.getMessage());
            }

            @Override
            public R<Map> paperResult(Map elPaper) {
                return R.fail("查询考试结果失败:" + throwable.getMessage());
            }

            @Override
            public TableDataInfo myExamResultList(Map elUserExam) {
                TableDataInfo tableDataInfo = new TableDataInfo();
                tableDataInfo.setMsg("查询当前用户考试记录列表失败:" + throwable.getMessage());
                tableDataInfo.setCode(500);
                return tableDataInfo;
            }

            @Override
            public TableDataInfo queryMyCert(Map<String, Object> userCert) {
                TableDataInfo dataInfo = new TableDataInfo<>();
                dataInfo.setCode(R.FAIL);
                return dataInfo;
            }

            @Override
            public R<Object> getPaperStatistics(Long id) {
                return R.fail("查询当前考题的正确和错误数量失败:" + throwable.getMessage());
            }

            @Override
            public void abandonPaper() {
                log.error("考试定时检查交卷超时失败！");
            }

            @Override
            public TableDataInfo recordMyApp(Map<String, Object> myApp) {
                TableDataInfo dataInfo = new TableDataInfo<>();
                dataInfo.setCode(R.FAIL);
                return dataInfo;
            }

            @Override
            public R<Object> recordMyAppCount() {
                return R.fail("查询我的积分统计失败:" + throwable.getMessage());
            }

            @Override
            public R<Object> findByUserId(Map<String, Object> map) {
               return R.fail("查询用户是否存在积分表失败:" + throwable.getMessage());
            }

            /**
             * 进入/出加奖励分
             *
             * @param map
             * @return
             */
            @Override
            public R addLoginfo(Map map) {
                return R.fail("用户本次加奖励分失败:" + throwable.getMessage());
            }

            /**
             * 获取奖励分配置表信息
             *
             * @param map
             * @return
             */
            @Override
            public R getBonusPointsConfig(Map map) {
                return R.fail("获取奖励分配置失败:" + throwable.getMessage());
            }

            @Override
            public R<List<ElResources>> listAll() {
                return R.fail("获取学习资源列表失败:" + throwable.getMessage());
            }
        };
    }
}
