package org.springblade.app.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.community.entity.Unit;
import org.springblade.community.feign.IUnitClient;
import org.springblade.community.vo.UnitVO;
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/unit")
@Slf4j
public class UnitController {

	private final IUnitClient unitClient;

	/**
	 * 功能描述: 通过楼栋id获取单元列表
	 *
	 * @param map {
	 *            building_id : 楼栋id,
	 *            org_id         : 机构授权org_id，机构id
	 *            token           : 对接令牌
	 *            }
	 * @return ResultBean<hashMap> 返回结果
	 */
	@PostMapping(value = "/getUnitsByBuildingId")
	public Map<String, Object> getUnitsByBuildingId(@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 building_id = Long.valueOf(map.get("building_id"));


			if (building_id == null) {
				hashMap.put("result_msg", "楼栋id不能为空");
				return hashMap;
			}
			Unit unit = new Unit();
			unit.setBuildingId(building_id);
			List<UnitVO> unitVOList = this.unitClient.list(unit);


			hashMap.put("result_code", 1);

			hashMap.put("list", unitVOList);
			if (unitVOList.size() == 0) {
				hashMap.put("result_msg", "没有相关楼栋信息");
			} else {
				hashMap.put("result_msg", "获取楼栋信息成功");
			}


		} catch (Exception e) {
			log.error("异常", e);
			hashMap.put("result_msg", "后台程序异常");
		}
		return hashMap;
	}
}
