package com.fdkankan.scene.service.impl;

import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.HttpCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.scene.service.IFdkkLaserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;

@Service
public class IFdkkLaserServiceImpl implements IFdkkLaserService {

    private final String UPDATE_SCENE_STATUS_URL_TEMPLATE="/indoor/%s/scene/useStatus/%s";

    @Value("${4dkk.laserService.host}")
    private String laserHost;

    private RestTemplate restTemplate = new RestTemplate();

    public void updateSceneStatus(String sceneCode,int status){
        String url = laserHost + String.format(UPDATE_SCENE_STATUS_URL_TEMPLATE,sceneCode,status);
        ResponseEntity<Result> response = restTemplate.getForEntity(url, Result.class);
        if(response.getStatusCode() != HttpStatus.OK){
            throw new BusinessException(ErrorCode.FAILURE_CODE_7001);
        }
        if(response.getBody().getCode() != HttpCode.SC_SUCCESS){
            throw new BusinessException(response.getBody().getCode(), response.getBody().getMsg());
        }
    }

    @Data
    public static class Result<T> implements Serializable {
        private static final long serialVersionUID = -1491499610244557029L;
        public static final String SUCCESS_MSG = "操作成功";
        public static int CODE_SUCCESS = 0;
        public static int CODE_FAILURE = -1;
        public static String[] NOOP = new String[]{};

        /**
         * 处理状态：0: 成功, 1: 失败
         */
        private int code;
        /**
         * 消息
         */
        private String msg;
        /**
         * 返回数据
         */
        private T data;
    }
}
