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.common.pdu.product.ProductPdu;
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/interlist")
    Rest<RespData<List<DevicePdu>>> list(@RequestBody DevicePdu devicePdu);


    /**
     * 查看设备
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/device/interinfo")
    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);


    /**
     * 查看设备列表
     *
     * @param productPdu
     * @return
     */
    @PostMapping(value = "/product/interlist")
    Rest<RespData<List<ProductPdu>>> list(@RequestBody ProductPdu productPdu);


}


@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) {
                return Rest.fail("暂时无法保存设备，请稍后再试!");
            }

            @Override
            public Rest<RespData<List<ProductPdu>>> list(ProductPdu productPdu) {
                return  Rest.fail("请稍后再试!");
            }
        };
    }
}








