package com.mortals.xhx.feign.user;

import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.user.UserPdu;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.feign.IFeign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息业务 Feign接口
 * @author zxfei
 * @date 2022-07-06
 */
@FeignClient(name = "portal-manager", path = "/zwfw", fallbackFactory = UserFeignFallbackFactory.class)
public interface IUserFeign extends IFeign {


    /**
     * 查看用户信息业务列表
     *
     * @param userPdu
     * @return
     */
    @PostMapping(value = "/user/listPage")
    Rest<RespData<List<UserPdu>>> list(@RequestBody UserPdu userPdu);


    /**
     * 查看用户信息业务
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/user/interinfo")
    Rest<UserPdu> info(@RequestParam(value = "id") Long id);

    /**
     * 删除用户信息业务
     *
     * @param ids
     * @return
     */
    @GetMapping(value = "/user/delete")
    Rest<Void> delete(Long[] ids,@RequestHeader("Authorization") String authorization);


    /**
     * 用户信息业务保存更新
     *
     * @param userPdu
     * @return
     */
    @PostMapping(value = "/user/save")
    Rest<RespData<UserPdu>> save(@RequestBody UserPdu userPdu,@RequestHeader("Authorization") String authorization);


    @PostMapping(value = "/login/login")
    String portalLogin(@RequestBody UserPdu userPdu);

    /**
     * 查询用户列表
     *
     * @param userPdu
     * @return
     */
    @PostMapping("/api/user/findAllUser")
    String findAllUser(@RequestBody UserPdu userPdu);


    /**
     * 获取token
     *
     * @param
     * @return
     */
    @PostMapping(value = "/user/token")
    Rest<String> getToken(@RequestParam(value = "userKey") String userKey);


}


@Slf4j
@Component
class UserFeignFallbackFactory implements FallbackFactory<IUserFeign> {
    @Override
    public IUserFeign create(Throwable t) {
        return new IUserFeign() {
            @Override
            public Rest<RespData<List<UserPdu>>>  list(UserPdu userPdu) {
               return Rest.fail("暂时无法获取用户信息业务列表，请稍后再试!");
            }

            @Override
            public Rest<UserPdu>  info(Long id) {
               return Rest.fail("暂时无法获取用户信息业务详细，请稍后再试!");
            }

            @Override
            public Rest<Void> delete(Long[] ids, String authorization) {
                return Rest.fail("暂时无法删除用户信息业务，请稍后再试!");
            }

            @Override
            public Rest<RespData<UserPdu>> save(UserPdu userPdu, String authorization) {
                return Rest.fail("暂时无法保存用户信息业务，请稍后再试!");
            }

            @Override
            public String portalLogin(UserPdu userPdu) {
                return JSON.toJSONString(Rest.fail("登录失败!"));
            }

            @Override
            public String findAllUser(UserPdu userPdu) {
                return null;
            }

            @Override
            public Rest<String> getToken(String userKey) {
                return Rest.fail("token获取失败");
            }
        };
    }
}








