package org.wfc.user.api.factory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import org.wfc.common.core.constant.HttpStatus;
import org.wfc.common.core.domain.LoginUser;
import org.wfc.common.core.domain.R;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.user.api.RemoteUUserService;
import org.wfc.user.api.domain.UUser;
import org.wfc.user.api.domain.bo.UClientBo;
import org.wfc.user.api.domain.vo.UOrderVo;

import java.util.Collections;

/**
 * 用户服务降级处理
 * 
 * @author wfc
 */
@Component
public class RemoteUUserFallbackFactory implements FallbackFactory<RemoteUUserService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteUUserFallbackFactory.class);

    @Override
    public RemoteUUserService create(Throwable throwable)
    {
        log.error("User service call failed:{}", throwable.getMessage());
        return new RemoteUUserService()
        {
            @Override
            public R<LoginUser<UUser>> getUserInfo(String username, String source)
            {
                return R.fail("Failed to get user:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> registerUserInfo(UUser user, String source)
            {
                return R.fail("Failed to register user:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> checkRepeat(UUser user, String source)
            {
                return R.fail("Check for duplicate failures:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> recordUserLogin(UUser user, String source)
            {
                return R.fail("Failed to record user login information:" + throwable.getMessage());
            }

            @Override
            public TableDataInfo page(Integer pageNum, Integer pageSize, UUser user) {
                TableDataInfo rspData = new TableDataInfo(Collections.emptyList(), 0L);
                rspData.setCode(HttpStatus.SUCCESS);
                rspData.setMsg("Query successful");
                return rspData;
            }

            @Override
            public R<Boolean> addCdrInfoByOmadaApi() {
                return R.fail("Failed to add CDR information:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> recordClientUser(UClientBo clientBo) {
                return R.fail("Failed to save or update device information based on MAC.:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> paySuccess(Long id, String source) {
                return R.fail("pay callback error:" + throwable.getMessage());
            }

            @Override
            public R<UOrderVo> getOrderById(Long id) {
                return R.fail("get order error:" + throwable.getMessage());
            }
        };
    }
}
