package org.springblade.app.controller;

import cn.hutool.core.lang.Assert;
import lombok.RequiredArgsConstructor;
import org.springblade.common.constant.CommonConstant;
import org.springblade.device.feign.IIotDeviceClient;
import org.springblade.device.vo.IotDeviceVO;
import org.springblade.estate.entity.ApplyUser;
import org.springblade.estate.feign.IApplyUserClient;
import org.springblade.estate.vo.ApplyUserVO;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lidexi
 * @date 2021/5/11 16:27
 */
@RestController
@RequestMapping("/app/iotdevice")
@RequiredArgsConstructor
public class IotDeviceController {

	private final IApplyUserClient applyUserClient;
	private final IIotDeviceClient iotDeviceClient;

	@PostMapping("/list")
	public Map<String,Object> getIotDeviceList(@RequestBody Map<String,String> map){
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("result_code",0);

		String member_id = map.get("memberId");
		String residential_id = map.get("residentialId");
		Assert.notNull(member_id,"会员id不能为空");
		Assert.notNull(residential_id,"小区不能为空");

		//获取该会员在该小区下的所有通过的住户审核记录
		ApplyUser searchApplyUser = new ApplyUser();
		searchApplyUser.setResidentialId(Long.valueOf(residential_id));
		searchApplyUser.setMemberId(Long.valueOf(member_id));
		searchApplyUser.setAuditStatus(CommonConstant.UserAuditStatus.pass.getValue());
		List<ApplyUserVO> applyUsers = applyUserClient.list(searchApplyUser);
		//提取房间id
		List<Long> roomIds = applyUsers.stream().map(ApplyUser::getRoomId).collect(Collectors.toList());
		List<IotDeviceVO> iotDeviceVOList = iotDeviceClient.getListByRoomIds(roomIds);
		resultMap.put("iotDevices",iotDeviceVOList);
		return  resultMap;
	}
}
