package org.springblade.app.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.community.vo.ResidentialVO;
import org.springblade.community.vo.RoomVO;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.person.dto.MemberDTO;
import org.springblade.person.entity.HouseUser;
import org.springblade.person.feign.IHouseUserClient;
import org.springblade.person.vo.HouseUserVO;
import org.springframework.util.Assert;
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;
import java.util.stream.Collectors;

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

	private final IResidentialClient residentialClient;
	private final IHouseUserClient houseUserClient;

	/**
	 * 功能描述: 通过区编号获取小区列表--国家标准省市区编号
	 *
	 * @param map {
	 *            region_area       :   区编号（国家统一省，市，区编号）,
	 *            residential_name  :   小区名字--非必须
	 *            org_id            :   机构授权org_id，机构id
	 *            token             :    对接令牌
	 *            }
	 * @return ResultBean<hashMap> 返回结果
	 */
	@PostMapping(value = "/getByRegionArea")
	public Map<String, Object> getByRegionArea(@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 {
			String region_area = map.get("region_area");
			String residential_name = map.get("residential_name");

			if (StringUtils.isBlank(region_area)) {
				hashMap.put("result_msg", "区编号不能为空");
				return hashMap;
			}
			Map<String,Object> residential = new HashMap<>();
			residential.put("regionArea",region_area);
			residential.put("name",residential_name);
			List<Residential> residentialVOList = this.residentialClient.listWithOutCompany(residential);
			hashMap.put("result_code", 1);
			hashMap.put("list", residentialVOList);
			if (residentialVOList.size() == 0) {
				hashMap.put("result_msg", "没有相关小区信息");
			} else {
				hashMap.put("result_msg", "获取小区信息成功");
			}
		} catch (Exception e) {
			log.error("异常", e);
			hashMap.put("result_msg", "后台程序异常");
		}
		return hashMap;
	}

	/**
	 * 功能描述: 通过会员id获取会员的房屋列表
	 *
	 * @return ResultBean<hashMap> 返回结果
	 */
	@PostMapping(value = "/getResidentialListByMember")
	public R<List<Residential>> getResidentialListByMember(@RequestBody MemberDTO memberDTO) {
		Assert.notNull(memberDTO.getId(), "会员必传");
		HouseUser houseUser = new HouseUser();
		houseUser.setMemberId(memberDTO.getId());
		List<HouseUserVO> userList = houseUserClient.list(houseUser);
		List<Long> residentialIdList = userList.stream().map(HouseUser::getResidentialId).distinct().collect(Collectors.toList());
		List<Residential> residentialList = new ArrayList<>();
		if(residentialIdList.size() > 0){
			residentialList = residentialClient.listByIds(Func.join(residentialIdList));
		}
		return R.data(residentialList);
	}
}
