package com.njcn.csdevice.api.fallback;

import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.csdevice.api.WlRecordFeignClient;
import com.njcn.csdevice.pojo.param.WlRecordParam;
import com.njcn.csdevice.pojo.po.WlRecord;
import com.njcn.csdevice.pojo.vo.RecordVo;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xy
 */
@Slf4j
@Component
public class WlRecordClientFallbackFactory implements FallbackFactory<WlRecordFeignClient> {
    @Override
    public WlRecordFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new WlRecordFeignClient() {

            @Override
            public HttpResult<Boolean> addBaseData(WlRecord wlRecord) {
                log.error("{}异常，降级处理，异常为：{}","新增原始数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> addBaseDataList(List<WlRecord> wlRecord) {
                log.error("{}异常，降级处理，异常为：{}","批量新增原始数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<WlRecord> findDevBaseData(WlRecordParam.Record param) {
                log.error("{}异常，降级处理，异常为：{}","查询装置基础数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> updateTestRecord(WlRecordParam.UpdateRecord record) {
                log.error("{}异常，降级处理，异常为：{}","修改测试项",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void dayDealNoEndTimeEvent(String date) {
                log.error("{}异常，降级处理，异常为：{}","每日处理没有结束时间的测试基础数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<WlRecord>> getWlAssByWlId(String wlId) {
                log.error("{}异常，降级处理，异常为：{}","根据测试项id获取测试项详细条目",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RecordVo>> findDevBaseDataByLineId(WlRecordParam.lineRecord param) {
                log.error("{}异常，降级处理，异常为：{}","根据监测点id查询装置基础数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<RecordVo.RecordItemVo> getTestRecordById(String testRecordId) {
                log.error("{}异常，降级处理，异常为：{}","根据测试项ID查询测试项信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
