package org.springblade.app.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.community.entity.Building;
import org.springblade.community.feign.IBuildingClient;
import org.springblade.community.vo.BuildingVO;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created By lidexi in 2021/4/26
 **/
@RestController
@RequiredArgsConstructor
@Api(value = "", tags = "接口")
@RequestMapping(value = "/app/building")
@Slf4j
public class BuildingController {

	private final IBuildingClient buildingClient;

	/**
	 * 功能描述: 通过小区id获取楼栋列表
	 *
	 * @param map {
	 *            residential_id : 小区id,
	 *            org_id         : 机构授权org_id，机构id
	 *            token           : 对接令牌
	 *            }
	 * @return ResultBean<hashMap> 返回结果
	 */
	@PostMapping(value = "/getBuildingsByResidentialId")
	public Map<String, Object> getBuildingsByResidentialId(@RequestBody Map<String, String> map) {

		Map<String, Object> hashMap = new HashMap<>();
		//result_code:0-失败  1-成功
		hashMap.put("result_code", 0);
		hashMap.put("result_msg", "查询失败");
		try {
			Long residential_id = Long.valueOf(map.get("residential_id"));


			if (residential_id == null) {
				hashMap.put("result_msg", "小区id不能为空");
				return hashMap;
			}
			Building building = new Building();
			building.setResidentialId(residential_id);
			List<BuildingVO> list = this.buildingClient.list(building);
			hashMap.put("result_code", 1);

			hashMap.put("list", list);
			if (list.size() == 0) {
				hashMap.put("result_msg", "没有相关楼栋信息");
			} else {
				hashMap.put("result_msg", "获取楼栋信息成功");
			}
		} catch (Exception e) {
			log.error("异常", e);
			hashMap.put("result_msg", "后台程序异常");
		}
		return hashMap;
	}
}
