package org.springblade.app.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.plugins.Page;
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.common.constant.OperationConstant;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.estate.dto.GuestRecordDTO;
import org.springblade.estate.entity.ApplyUser;
import org.springblade.estate.entity.GuestAuthorize;
import org.springblade.estate.entity.GuestRecord;
import org.springblade.estate.feign.IApplyUserClient;
import org.springblade.estate.feign.IGuestAuthorizeClient;
import org.springblade.estate.feign.IGuestRecordClient;
import org.springblade.estate.vo.ApplyUserVO;
import org.springblade.estate.vo.GuestAuthorizeVO;
import org.springblade.estate.vo.GuestRecordVO;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

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

	private final IApplyUserClient applyUserClient;
	private final IGuestRecordClient guestRecordClient;
	private final IGuestAuthorizeClient guestAuthorizeClient;



	@GetMapping("list")
	public R list(GuestRecord guestRecord){
		List<GuestRecordVO> list = guestRecordClient.list(guestRecord);
		return R.data(list);
	}

	@PostMapping("page")
	public R page(@RequestBody GuestRecordDTO guestRecordDTO){
		BladePage<GuestRecordVO> page = guestRecordClient.page(guestRecordDTO);
		return R.data(page);
	}


	@GetMapping("count")
	public R count(GuestRecord guestRecord){
		return R.data(guestRecordClient.count(guestRecord));
	}


	@PostMapping("submit")
	public R submit(@RequestBody GuestRecord guestRecord){
		//园区
		guestRecord.setTenantType(OperationConstant.ZERO);
		if (Objects.isNull(guestRecord.getId())) {
			//新增
			Long guestAuthId = guestRecord.getGuestAuthId();
			Assert.notNull(guestAuthId);
			GuestAuthorize ga = new GuestAuthorize();
			ga.setId(guestAuthId);
			GuestAuthorizeVO authorizeVO = guestAuthorizeClient.detail(ga);
			Assert.notNull(authorizeVO, "找不到申请记录");
			guestRecord.setAgencyId(authorizeVO.getAgencyId());
			guestRecord.setAgencyName(authorizeVO.getAgencyName());
			guestRecord.setResidentialId(authorizeVO.getResidentialId());
			guestRecord.setResidentialName(authorizeVO.getResidentialName());
			guestRecord.setBuildingId(authorizeVO.getBuildingId());
			guestRecord.setBuildingName(authorizeVO.getBuildingName());
			guestRecord.setUnitId(authorizeVO.getUnitId());
			guestRecord.setUnitName(authorizeVO.getUnitName());
			guestRecord.setRoomId(authorizeVO.getRoomId());
			guestRecord.setRoomName(authorizeVO.getRoomName());

			authorizeVO.setGuestTel(guestRecord.getGuestTel());
			authorizeVO.setGuestName(guestRecord.getGuestName());
			guestAuthorizeClient.submit(authorizeVO);
		}
		return R.status(this.guestRecordClient.submit(guestRecord));
	}


	/**
	 * 功能描述: 通过会员id获取访客登记信息记录
	 *
	 * @param map {
	 *            member_id     :   会员id,
	 *            org_id        :   机构授权org_id，机构id
	 *            token         :   对接令牌
	 *            }
	 * @return ResultBean<hashMap> 返回结果
	 */
	@PostMapping(value = "/getListByMemberId")
	public Map<String, Object> getListByMemberId(@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 {
			GuestRecordDTO guestRecordDTO = new GuestRecordDTO();
			String member_id = map.get("member_id");
			String residential_id = map.get("residential_id");

			if (StringUtils.isBlank(member_id)) {
				hashMap.put("result_msg", "会员id不能为空");
				return hashMap;
			}
			if (StringUtils.isBlank(residential_id)) {
				hashMap.put("result_msg", "小区id不能为空");
				return hashMap;
			}
			ApplyUser applyUser = new ApplyUser();
			applyUser.setMemberId(Long.valueOf(member_id));
			applyUser.setAuditStatus(CommonConstant.UserAuditStatus.pass.getValue());
			List<ApplyUserVO> list = this.applyUserClient.list(applyUser);
			if (list == null || list.size() == 0) {
				hashMap.put("result_msg", "没有查询到与会员对应的住户信息");
			} else {
				List<Long> userIds = list.stream().map(ApplyUser::getUserId).collect(Collectors.toList());

				guestRecordDTO.setUserIds(userIds);
				String current = map.get("current");//当前页
				String size = map.get("size");
				String id = map.get("id");
				String guestName = map.get("guestName");
				String guestTel = map.get("guestTel");
				String auditStatus = map.get("auditStatus");


				if(StringUtils.isNotBlank(id)){
					guestRecordDTO.setId(Long.valueOf(id));
				}

				if(StringUtils.isNotBlank(guestName)){
					guestRecordDTO.setGuestName(guestName);
				}

				if(StringUtils.isNotBlank(guestTel)){
					guestRecordDTO.setGuestTel(guestTel);
				}

				if(StringUtils.isNotBlank(auditStatus)){
					guestRecordDTO.setAuditStatus(Integer.valueOf(auditStatus));
				}
				guestRecordDTO.setResidentialId(Long.valueOf(residential_id));

				Page<GuestRecord> page = new Page<>();
				page.setCurrent(Integer.valueOf(current != null ? current : "1"));
				page.setSize(Integer.valueOf(size != null ? size : "10"));

				guestRecordDTO.setPage(page);
				BladePage<GuestRecordVO> bladePage = this.guestRecordClient.page(guestRecordDTO);
				if (bladePage!=null) {
					hashMap.put("result_code", 1);
					List<GuestRecordVO> guestRecords = bladePage.getRecords();
					Integer total = guestRecords.size();
					hashMap.put("list",guestRecords);
					hashMap.put("total",total);
					if (guestRecords.size() == 0) {
						hashMap.put("result_msg", "没有查询到相关数据");
					} else {
						hashMap.put("result_msg", "获取相关数据成功");
					}
				}
			}
		} catch (Exception e) {
			log.error("异常", e);
			hashMap.put("result_msg", "后台程序异常");
		}
		return hashMap;
	}
}
