package com.mortals.xhx.feign.device;

import com.mortals.xhx.common.pdu.LoginForm;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.device.DevicePdu;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.common.pdu.device.DeviceReq;
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 2023-02-25
 */
@FeignClient(name = "device-manager", path = "/m", fallbackFactory = DeviceFeignFallbackFactory.class)
public interface IDeviceFeign extends IFeign {


    /**
     * 查看设备列表
     *
     * @param devicePdu
     * @return
     */
    @PostMapping(value = "/device/list")
    Rest<RespData<List<DevicePdu>>> list(@RequestBody DevicePdu devicePdu);


    /**
     * 查看设备
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/device/info")
    Rest<DevicePdu> info(@RequestParam(value = "id") Long id);


    /**
     * 设备保存更新
     *
     * @param devicePdu
     * @return
     */
    @PostMapping(value = "/device/save")
    Rest<RespData<DevicePdu>> save(@RequestBody DevicePdu devicePdu, @RequestHeader("Authorization") String authorization);


    /**
     * 获取token
     *
     * @param loginForm
     * @return
     */
    @PostMapping(value = "/api/getToken")
    Rest<String> getToken(@RequestBody LoginForm loginForm);


    /**
     * 设备回调
     *
     * @param deviceReq
     * @return
     */
    @PostMapping(value = "/api/receive")
    Rest<String> deviceCall(@RequestBody DeviceReq deviceReq, @RequestHeader("Authorization") String authorization);

}


@Slf4j
@Component
class DeviceFeignFallbackFactory implements FallbackFactory<IDeviceFeign> {
    @Override
    public IDeviceFeign create(Throwable t) {
        return new IDeviceFeign() {
            @Override
            public Rest<RespData<List<DevicePdu>>> list(DevicePdu devicePdu) {
                return Rest.fail("暂时无法获取设备列表，请稍后再试!");
            }

            @Override
            public Rest<DevicePdu> info(Long id) {
                return Rest.fail("暂时无法获取设备详细，请稍后再试!");
            }

            @Override
            public Rest<RespData<DevicePdu>> save(DevicePdu devicePdu, String authorization) {
                return Rest.fail("暂时无法保存设备，请稍后再试!");
            }

            @Override
            public Rest<String> getToken(LoginForm loginForm) {
                return Rest.fail("暂时无法获取token，请稍后再试!");
            }

            @Override
            public Rest<String> deviceCall(DeviceReq deviceReq, String authorization) {
                return Rest.fail("暂时无法保存设备，请稍后再试!");
            }
        };
    }
}








