package org.springblade.app.controller;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springblade.common.constant.CommonConstant;
import org.springblade.community.entity.Residential;
import org.springblade.community.entity.Room;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.community.feign.IRoomClient;
import org.springblade.community.vo.RoomVO;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.estate.entity.ApplyUser;
import org.springblade.estate.feign.IApplyUserClient;
import org.springblade.estate.vo.ApplyUserVO;
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.springblade.system.expand.entity.Conf;
import org.springblade.system.expand.feign.IConfClient;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.*;
import java.util.stream.Collectors;

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

	private final IApplyUserClient applyUserClient;
	private final IRoomClient roomClient;
	private final IHouseUserClient houseUserClient;
	private final IResidentialClient residentialClient;
	private final IConfClient confClient;


	/**
	 * 功能描述: 通过会员id获取会员的房屋列表
	 *
	 * @param map {
	 *            member_id :   会员id,
	 *            org_id    :   机构授权org_id，机构id
	 *            token     :   对接令牌
	 *            }
	 * @return ResultBean<hashMap> 返回结果
	 */
	@PostMapping(value = "/getRoomByMemberIdBak")
	public Map<String, Object> getRoomByMemberIdBak(@RequestBody Map<String, String> map) {

		Map<String, Object> hashMap = new HashMap<>();
		//result_code:0-失败  1-成功
		hashMap.put("result_code", 0);
		try {
			String member_id = map.get("member_id");
			String residentialId = map.get("residential_id");
			if (member_id == null) {
				hashMap.put("result_msg", "member_id不能为空");
				return hashMap;
			}
			//查询会员对应的user
			ApplyUser applyUser = new ApplyUser();
			applyUser.setMemberId(Long.valueOf(member_id));
			if (residentialId != null) {
				applyUser.setResidentialId(Long.valueOf(residentialId));
			}
			applyUser.setAuditStatus(CommonConstant.UserAuditStatus.pass.getValue());
			List<ApplyUserVO> list = this.applyUserClient.list(applyUser);
			if (list == null || list.size() == 0) {
				//hashMap.put("result_msg", "没有查询到与会员对应的住户信息");
				return hashMap;
			} else {
				List<Long> roomIds = new ArrayList<>();
				List<Long> userIds = new ArrayList<>();
				for (ApplyUser au : list) {
					roomIds.add(au.getRoomId());
					userIds.add(au.getUserId());
				}
				//查询会员对应住户
				List<HouseUserVO> users = this.houseUserClient.getListByUserIds(userIds);
				//查询会员对应的房间
				List<RoomVO> rooms = this.roomClient.getListByRoomIds(roomIds);
				if (rooms == null || rooms.size() == 0) {
					hashMap.put("result_msg", "没有相关房屋信息");
				} else {
					List<RoomVO> roomVOS = new ArrayList<>();
					//住户和房屋的关系
					for (Room room : rooms) {
						Residential searchResidential = new Residential();
						searchResidential.setId(room.getResidentialId());
						for (HouseUser user : users) {
							if (user.getRoomId().equals(room.getId())) {
								RoomVO roomVO = Objects.requireNonNull(BeanUtil.copy(room, RoomVO.class));
								Residential residential = this.residentialClient.detail(searchResidential);
								Integer doorNeedAudit = 1;
								if (residential != null) {
									doorNeedAudit = residential.getDoorNeedAudit();
//									roomVO.setDoorNeedAudit(doorNeedAudit);
//									roomVO.setPersonTel(residential.getPersonPhone());
								}
//								roomVO.setRelationshipType(user.getType());
								roomVO.setResidentialId(user.getResidentialId());
								if (StringUtils.isNotBlank(user.getFaceUrl())) {
//									roomVO.setImageUri(user.getFaceUrl());
								}
//								roomVO.setUserId(user.getId());
//								roomVO.setIdcard(user.getIdCard());
//								roomVO.setFaceBeginDate(user.getFaceBeginDate());
//								roomVO.setFaceEndDate(user.getFaceEndDate());
//								roomVO.setFaceEnable(user.getFaceEnable());
								if (user.getFaceEndDate() != null) {
									Conf searchConf = new Conf();
									searchConf.setConfToken("expire.notice.days");
									Conf conf = this.confClient.detail(searchConf);
									if (conf != null) {
										Integer days = Integer.parseInt(conf.getConfValue());
										int compare = DateUtil.compare(DateUtil.offsetDay(new Date(), days), user.getFaceEndDate());
//										roomVO.setFaceValid(compare > 0 ? false : true);
									}
								}
								roomVOS.add(roomVO);
							}
						}

					}
					hashMap.put("list", roomVOS);
					hashMap.put("roomList", rooms);
					hashMap.put("result_code", 1);
					hashMap.put("result_msg", "获取房屋列表成功");
				}
			}
		} catch (Exception e) {
			log.error("异常", e);
			hashMap.put("result_msg", "后台程序异常");
		}
		return hashMap;
	}

	/**
	 * 功能描述: 通过会员id获取会员的房屋列表
	 *
	 * @return ResultBean<hashMap> 返回结果
	 */
	@PostMapping(value = "/getRoomByMemberId")
	public R<List<RoomVO>> getRoomByMemberId(@RequestBody MemberDTO memberDTO) {
		Assert.notNull(memberDTO.getResidentialId(), "小区必传");
		Assert.notNull(memberDTO.getId(), "会员必传");
		List<HouseUserVO> userList = houseUserClient.getListByMember(memberDTO.getId(), memberDTO.getResidentialId(), memberDTO.getRoomId());
		List<Long> roomIdList = userList.stream().map(HouseUser::getRoomId).collect(Collectors.toList());
		List<RoomVO> roomList = roomClient.getListByRoomIds(roomIdList);
		return R.data(roomList);
	}
}
