package com.njcn.device.pms.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.device.pms.api.DistributionMonitorClient;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.pwUser.PwUserMonitorParam;
import com.njcn.device.pms.pojo.po.DistributionMonitor;
import com.njcn.device.pms.pojo.vo.DoubleUserVO;
import com.njcn.device.pms.pojo.vo.pwUser.PwUserMonitorDataVO;
import com.njcn.device.pms.utils.PmsDeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author yzh
 * @date 2022/10/18
 */
@Slf4j
@Component
public class DistributionMonitorClientFallbackFactory implements FallbackFactory<DistributionMonitorClient> {

    @Override
    public DistributionMonitorClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = PmsDeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new DistributionMonitorClient()
        {
            @Override
            public HttpResult<List<DoubleUserVO>> getDoubleUserByDept(@RequestParam("orgId")String orgId) {
                log.error("{}异常，降级处理，异常为：{}", "获取指定组织下的发电用电用户", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }


            @Override
            public HttpResult<List<DistributionMonitor>> distributionMonitorList(List<String> monitorIds) {
                log.error("{}异常，降级处理，异常为：{}", "获取配网监测点", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DistributionMonitor>> distributionMonitorListByCondition(@RequestParam("orgId")String orgId,@RequestParam("monitorSort")String monitorSort) {
                log.error("{}异常，降级处理，异常为：{}", "获取配网所有监测点", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PwPmsMonitorDTO>> pwMonitorAreaDetailInfo(List<String> monitorIds) {
                log.error("{}异常，降级处理，异常为：{}", "根据配网监测点集合获取配网监测点详情", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PwUserMonitorDataVO>> getPwUserIds(PwUserMonitorParam pwUserMonitorParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据部门获取配网用户信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }


        };
    }
}
