package org.springblade.openinterface.estate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.community.room.entity.Room;
import org.springblade.community.room.service.IRoomService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.openinterface.dto.smartdevice.SmartDeviceDto;
import org.springblade.openinterface.wrapper.SmartDeviceAppWrapper;
import org.springblade.smartapplication.smartsmoke.entity.SmartDevice;
import org.springblade.smartapplication.smartsmoke.service.ISmartDeviceService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created By lidexi in 2021/3/25
 * 物业系统调用
 **/
@RestController
@AllArgsConstructor
@RequestMapping("openinterface/listen/estate")
@Api(value = "智家设备接口", tags = "智家设备接口")
public class SmartDeviceComtroller {

	private final ISmartDeviceService smartDeviceService;
	private final IResidentialService residentialService;
	private final IRoomService roomService;
	private final IDictBizService dictBizService;

	@PostMapping("/getSmartDeviceOnRoom")
	public List<SmartDeviceDto> getSmartDevice(@RequestBody Map<String,String> map){
		String roomOldIds = map.get("roomOldIds");
		String residentialOldId = map.get("residentialOldId");

		String[] roomOldIdList = roomOldIds.split(",");

		List<Room> rooms = roomService.getBaseMapper().selectList(new QueryWrapper<>(new Room()).lambda().in(Room::getOldId, roomOldIdList));
		List<Long> roomIds = rooms.stream().map(Room::getId).collect(Collectors.toList());

		Residential residential = residentialService.getOne(new QueryWrapper<>(new Residential()).lambda().eq(Residential::getOldId, residentialOldId));
		if(residential != null){
			List<SmartDevice> smartDevices = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice()).lambda()
				.eq(SmartDevice::getDeviceUsage, 10).in(SmartDevice::getRoomId, roomIds).eq(SmartDevice::getResidentialId,residential.getId()));
			return SmartDeviceAppWrapper.build(dictBizService).listVO(smartDevices);
		}else{
			return new ArrayList<>();
		}
	}
}
