package com.platform.service.impl;

import com.platform.enums.ResultCodeEnum;
import com.platform.exception.CommonBaseException;
import com.platform.vos.ZhiHouseUserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class ZhiHouseService {

    @Value("${scenePath}")
    private String zhiHouseHost;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 登录指房宝
     * @param username
     * @param password
     * @return
     */
    public ResponseEntity<String> login(String username, String password) {
        ZhiHouseUserLoginVo userLoginVo = new ZhiHouseUserLoginVo();
        userLoginVo.setUserName(username);
        userLoginVo.setUserPassword(password);
        userLoginVo.setArea("1234");
        String url = zhiHouseHost + "api/platform/login";
        return restTemplate.postForEntity(url, userLoginVo, String.class);
    }

    public ResponseEntity<String> generateSceneEditToken(String token,String userName,String sceneNum) {
        if(ObjectUtils.isEmpty(token)){
            throw new CommonBaseException(ResultCodeEnum.D101);
        }
        Map<String, String> param = new HashMap<>();
        param.put("userName",userName);
        param.put("sceneNum",sceneNum);
        String url = zhiHouseHost + "api/platform/generateSceneEditToken";
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);
        HttpEntity<Object> formEntity = new HttpEntity<>(param, headers);
        ResponseEntity<String> kankanResult = restTemplate.postForEntity(url,formEntity,String.class);
        return kankanResult;
    }
}
