package com.sptg.gateway.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.sptg.estate.api.entity.MemberAndUser;
import com.sptg.estate.api.feigns.MemberAndUserFeign;
import com.sptg.gateway.entity.SmartDeviceDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created By lidexi in 2021/3/25
 * 社区小程序获取智家设备
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/device")
public class DeviceController {

    private final MemberAndUserFeign memberAndUserFeign;

    @PostMapping("/list")
    public Map<String,Object> getDeviceList(@RequestBody Map<String,String> map){
        Map<String,Object> resultMap = new HashMap<>();
        //result_code:0-失败  1-成功
        resultMap.put("result_code",0);
        Assert.notNull(map.get("memberId"),"会员id不能为空");

        map.get("residentialId");
        MemberAndUser memberAndUser = new MemberAndUser();
        memberAndUser.setMemberId(Long.valueOf(map.get("memberId")));
        List<MemberAndUser> memberAndUserList = memberAndUserFeign.getByCondition(memberAndUser);
        if(memberAndUserList.size()==0){
            resultMap.put("result_msg","会员尚未成为住户");
        }else{
            StringBuilder stringBuilder = new StringBuilder();
            for(MemberAndUser memberAndUser1: memberAndUserList){
                stringBuilder.append(memberAndUser1.getRoomId()+",");
            }

            String roomIds = stringBuilder.toString().substring(0,stringBuilder.toString().length()-1);
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity responseEntity = null;

            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("roomOldIds",roomIds);
            requestMap.put("residentialOldId",map.get("residentialId"));

            HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(requestMap), headers);
            ParameterizedTypeReference<List<SmartDeviceDto>> reference = new ParameterizedTypeReference<List<SmartDeviceDto>>() {
            };
            String url = "http://localhost:9527/openinterface/listen/estate/getSmartDeviceOnRoom";
            URI uri = URI.create(url);
            responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);

          List<SmartDeviceDto> smartDeviceDtos = (List<SmartDeviceDto>)responseEntity.getBody();
          resultMap.put("smartDevices",smartDeviceDtos);
        }
        return resultMap;
    }
}
