package com.community.huiju.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.commom.utils.AccountValidatorUtil;

import com.community.huiju.dao.TpBuildingMapper;
import com.community.huiju.dao.TpBuildingOwnerInfoMapper;
import com.community.huiju.dao.TpLevelMapper;
import com.community.huiju.dao.TpPhaseMapper;
import com.community.huiju.dao.TpRoomNoMapper;
import com.community.huiju.dao.TpUnitMapper;
import com.community.huiju.model.TaUser;
import com.community.huiju.model.TpBuilding;
import com.community.huiju.model.TpBuildingOwnerInfo;
import com.community.huiju.model.TpLevel;
import com.community.huiju.model.TpPhase;
import com.community.huiju.model.TpRoomNo;
import com.community.huiju.model.TpUnit;

import com.community.huiju.dao.*;
import com.community.huiju.model.*;

import com.community.huiju.service.BuildingTreeServiceI;
import jdk.nashorn.internal.ir.IfNode;
import org.apache.ibatis.annotations.Mapper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FXF
 * @date 2019-04-01
 */
@Service("buildingTreeService")
public class BuildingTreeServiceImpl implements BuildingTreeServiceI {
	
	public static final Logger logger = LoggerFactory.getLogger(BuildingOwnerInfoServiceImpl.class);
	
	@Autowired
	private TpPhaseMapper tpPhaseMapper;
	
	@Autowired
	private TpBuildingMapper tpBuildingMapper;
	
	@Autowired
	private TpUnitMapper tpUnitMapper;
	
	@Autowired
	private TpLevelMapper tpLevelMapper;
	
	@Autowired
	private TpRoomNoMapper tpRoomNoMapper;
	
	@Autowired
	private TaUserVerifyMapper taUserVerifyMapper;
	
	/**
	 * 获取整个小区的基础数据
	 *
	 * @param file
	 * @return
	 */
	@Override
	public ResponseBean getExcelData(MultipartFile file) {
		ResponseBean responseBean = new ResponseBean();
		List<TpBuildingOwnerInfo> list = new ArrayList<>();
		if (file == null) {
			responseBean.addError("对象不能为空");
			return responseBean;
		}
		//获取文件的名字
		String originalFilename = file.getOriginalFilename();
		Workbook workbook = null;
		try {
			if (originalFilename.endsWith(Constant.SUFFIX_2003)) {
				workbook = new HSSFWorkbook(file.getInputStream());
			} else if (originalFilename.endsWith(Constant.SUFFIX_2007)) {
				workbook = new XSSFWorkbook(file.getInputStream());
			}
		} catch (Exception e) {
			logger.info(originalFilename);
			e.printStackTrace();
			responseBean.addError("格式错误");
			return responseBean;
		}
		if (workbook == null) {
			logger.info(originalFilename);
			responseBean.addError("格式错误");
			return responseBean;
		} else {
			List<String> temBuildingList = new ArrayList<String>();
			//获取一个sheet也就是一个工作簿
			Sheet sheet = workbook.getSheetAt(0);
			int lastRowNum = sheet.getLastRowNum();
			//从第一行开始第一行一般是标题
			for (int j = 3; j <= lastRowNum; j++) {
				Row row = sheet.getRow(j);
				TpBuildingOwnerInfo tpBuildingOwnerInfo = new TpBuildingOwnerInfo();
				String phase = row.getCell(0).getStringCellValue().trim();
				String building = row.getCell(1).getStringCellValue().trim();
				String unit = row.getCell(2).getStringCellValue().trim();
				String level = row.getCell(3).getStringCellValue().trim();
				String roomNo = row.getCell(4).getStringCellValue().trim();
				
				int currentRow = j+1;
				if (StringUtils.isEmpty(phase)){
					responseBean.addError("第" + currentRow + "行" + "期/区不能为空!");
					return responseBean;
				}
				if (StringUtils.isEmpty(building)){
					responseBean.addError("第" + currentRow + "行" + "期/区不能为空!");
					return responseBean;
				}
				if (StringUtils.isEmpty(unit)){
					responseBean.addError("第" + currentRow + "行" + "期/区不能为空!");
					return responseBean;
				}
				if (StringUtils.isEmpty(level)){
					responseBean.addError("第" + currentRow + "行" + "期/区不能为空!");
					return responseBean;
				}
				if (StringUtils.isEmpty(roomNo)){
					responseBean.addError("第" + currentRow + "行" + "期/区不能为空!");
					return responseBean;
				}
				
				String buildingInfo = phase + building + unit + level + roomNo;
				if (temBuildingList.contains(buildingInfo)){
					logger.info("存在重复房源:{}",buildingInfo);
					responseBean.addError("第"+ currentRow +"行存在重复房源");
					return responseBean;
				}
				
				//把数据塞入temList校验是否有重复的数据
				temBuildingList.add(buildingInfo);
				//构建数据
				tpBuildingOwnerInfo.setPhaseName(phase);
				tpBuildingOwnerInfo.setBuildingName(building);
				tpBuildingOwnerInfo.setUnitName(unit);
				tpBuildingOwnerInfo.setLevelName(level);
				tpBuildingOwnerInfo.setRoomNoName(roomNo);
				list.add(tpBuildingOwnerInfo);
			}
		}
		//构建分页
		Map<String,Object> data = new HashMap<String, Object>();
		data.put("list",list);
		data.put("total",list.size());
		
		responseBean.addSuccess(data);
		return responseBean;
	}
	
	/**
	 * 上传小区的基础数据
	 *
	 * @param file
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean submitExcelData(MultipartFile file, UserElement userElement) {
		ResponseBean responseBean = new ResponseBean();
		responseBean = getExcelData(file);
		if (!responseBean.getCode().equals(Constant.RESPONSE_SUCCESS)){
			return responseBean;
		}
		//获取要上传的数据
		Map<String,Object> map = (Map<String, Object>) responseBean.getData();
		List<TpBuildingOwnerInfo> list = (List<TpBuildingOwnerInfo>) map.get("list");
		
		//入库
		Integer communityId = userElement.getCommunityId();
		
		//保存数据到表中--拆分数据
		saveBuildingTreeData(communityId,list);
		responseBean.addSuccess("success");
		return responseBean;
	}
	
	/**
	 * 获取树形数据
	 *
	 * @param communityId
	 * @param id
	 * @param treeType
	 * @return
	 */
	@Override
	public ResponseBean getTreeList(Integer communityId, Integer id, String treeType) {
		ResponseBean responseBean = new ResponseBean();
		List<Map<String,Object>> resultList = new ArrayList<>();
		//获取区/期树形数据
		if (treeType.equals("phase")){
			QueryWrapper<TpPhase> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("community_id",communityId);
			queryWrapper.orderByAsc("create_date");
			List<TpPhase> list = tpPhaseMapper.selectList(queryWrapper);
			list.stream().forEach(e -> {
				Map<String,Object> map = new HashMap<>();
				map.put("name",e.getName());
				map.put("id",e.getId());
				map.put("type","building");
				resultList.add(map);
			});
		}else if (treeType.equals("building")){
			QueryWrapper<TpBuilding> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("community_id",communityId);
			queryWrapper.eq("phase_id",id);
			queryWrapper.orderByAsc("create_date");
			List<TpBuilding> list = tpBuildingMapper.selectList(queryWrapper);
			list.stream().forEach(e -> {
				Map<String,Object> map = new HashMap<>();
				map.put("name",e.getName());
				map.put("id",e.getId());
				map.put("type","unit");
				resultList.add(map);
			});
		}else if (treeType.equals("unit")){
			QueryWrapper<TpUnit> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("community_id",communityId);
			queryWrapper.eq("building_id",id);
			queryWrapper.orderByAsc("create_date");
			List<TpUnit> list = tpUnitMapper.selectList(queryWrapper);
			list.stream().forEach(e -> {
				Map<String,Object> map = new HashMap<>();
				map.put("name",e.getName());
				map.put("id",e.getId());
				map.put("type","level");
				resultList.add(map);
			});
		}else if (treeType.equals("level")){
			QueryWrapper<TpLevel> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("community_id",communityId);
			queryWrapper.eq("unit_id",id);
			queryWrapper.orderByAsc("create_date");
			List<TpLevel> list = tpLevelMapper.selectList(queryWrapper);
			list.stream().forEach(e -> {
				Map<String,Object> map = new HashMap<>();
				map.put("name",e.getName());
				map.put("id",e.getId());
				map.put("type","roomNo");
				resultList.add(map);
			});
		}else if (treeType.equals("roomNo")){
			QueryWrapper<TpRoomNo> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("community_id",communityId);
			queryWrapper.eq("level_id",id);
			queryWrapper.orderByAsc("create_date");
			List<TpRoomNo> list = tpRoomNoMapper.selectList(queryWrapper);
			list.stream().forEach(e -> {
				Map<String,Object> map = new HashMap<>();
				map.put("name",e.getName());
				map.put("id",e.getId());
				map.put("type","end");
				map.put("leaf",true);
				resultList.add(map);
			});
		}
		responseBean.addSuccess(resultList);
		return responseBean;
	}

	@Override
	public ResponseBean addNode(String parameter, Integer communityId) {
		ResponseBean responseBean= new ResponseBean();
		 JSONObject object= JSONObject.parseObject(parameter);
		 //当前节点id
		 Integer id= object.getInteger("id");
		//当前节点类型
		 String type= object.getString("type");
		 //当前输入的节点内容
		 String nodeNumber= object.getString("nodeNumber");

		//楼栋
		/*查询出期*/
		if(type.equals("phase")) {
			TpPhase tpPhase= new TpPhase();
					tpPhase.setCommunityId(communityId);
					tpPhase.setName(nodeNumber);
					tpPhase.setCreateDate(LocalDateTime.now());
					tpPhaseMapper.insert(tpPhase);
		}
		TpPhase	tpPhase= tpPhaseMapper.selectById(id);
		if(type.equals("building")){
		 TpBuilding tpBuilding= new TpBuilding();
					tpBuilding.setCommunityId(tpPhase.getCommunityId())
						 .setPhaseId(tpPhase.getId())
						 .setPhaseName(tpPhase.getName())
						 .setName(nodeNumber)
						 .setCreateDate(LocalDateTime.now());
		 //判断书否为期

			tpBuildingMapper.insert(tpBuilding);
		}
		//单元
		//查询栋，维护单元

		 TpBuilding selectTpBuilding= tpBuildingMapper.selectById(id);
		if (type.equals("unit")){
		 TpUnit tpUnit= new TpUnit();
				tpUnit.setCommunityId(selectTpBuilding.getCommunityId())
						.setPhaseId(selectTpBuilding.getPhaseId())
						.setBuildingName(selectTpBuilding.getName())
						.setBuildingId(selectTpBuilding.getId())
						.setName(nodeNumber)
						.setCreateDate(LocalDateTime.now());
		//判断是否为单
		 	 tpUnitMapper.insert(tpUnit);
		 }

		//楼层
		//查询单元，维护楼层
		 TpUnit selectTpUnit= tpUnitMapper.selectById(id);
		if(type.equals("level")){
		 TpLevel tpLevel= new TpLevel();
		tpLevel.setId(selectTpUnit.getId())
				.setCommunityId(selectTpUnit.getCommunityId())
				.setPhaseId(selectTpUnit.getPhaseId())
				.setPhaseName(selectTpUnit.getPhaseName())
				.setBuildingId(selectTpUnit.getBuildingId())
				.setBuildingName(selectTpUnit.getBuildingName())
				.setUnitId(selectTpUnit.getId())
				.setUnitName(selectTpUnit.getName())
				.setName(nodeNumber)
				.setCreateDate(LocalDateTime.now());
		//判断是否是楼层

			 tpLevelMapper.insert(tpLevel);
			}

		 //房间号
	 	TpLevel selectTpLevel= tpLevelMapper.selectById(id);
		if(type.equals("roomNo")){
		 TpRoomNo tpRoomNo= new TpRoomNo();
			tpRoomNo.setCommunityId(selectTpLevel.getCommunityId())
							.setPhaseId(selectTpLevel.getPhaseId())
							.setPhaseName(selectTpLevel.getPhaseName())
							.setBuildingId(selectTpLevel.getBuildingId())
							.setBuildingName(selectTpLevel.getBuildingName())
							.setUnitId(selectTpLevel.getUnitId())
							.setUnitName(selectTpLevel.getUnitName())
							.setLevelId(selectTpLevel.getId())
							.setLevelName(selectTpLevel.getName())
							.setName(nodeNumber)
							.setCreateDate(LocalDateTime.now());


			tpRoomNoMapper.insert(tpRoomNo);
		}
		responseBean.addSuccess("成功");
		return responseBean;
	}

	@Override
	public ResponseBean deleteNode(String parameter, Integer communityId) {
		ResponseBean responseBean= new ResponseBean();
		//注意刪除当前节点，根据type判断当前节点的位置，删除时先删除当前节点的最小节点。
		JSONObject  object= JSONObject.parseObject(parameter);
		//当前节点id
		Integer id= object.getInteger("id");
		//当前节点类型
		String type= object.getString("type");
		//当前输入的节点内容
		String nodeNumber= object.getString("nodeNumber");

		//审核是否通过
		InfoPass(type,id);
		if (type.equals("end")) {
			tpRoomNoMapper.deleteById(id);
		}
		//删除楼层
		if(type.equals("roomNo")){
			TpLevel tpLevel= tpLevelMapper.selectById(id);
			QueryWrapper<TpRoomNo> Info = new QueryWrapper<>();
			Info.eq("community_id", tpLevel.getCommunityId());
			Info.eq("phase_id", tpLevel.getPhaseId());
			Info.eq("building_id",tpLevel.getBuildingId());
			Info.eq("unit_id", tpLevel.getUnitId());
			Info.eq("level_id", tpLevel.getId());
			tpRoomNoMapper.delete(Info);
			tpLevelMapper.deleteById(id);
		}

		//删除单元
		if(type.equals("level")){
			TpUnit tpUnit= tpUnitMapper.selectById(id);
			//删除单元下的楼层
			QueryWrapper<TpLevel> tpLevel = new QueryWrapper<>();
			tpLevel.eq("phase_id", tpUnit.getPhaseId());
			tpLevel.eq("community_id", tpUnit.getCommunityId());
			tpLevel.eq("building_id", tpUnit.getBuildingId());
			tpLevel.eq("unit_id", tpUnit.getId());
			//删除单元-楼层-下的房间
			//查询楼层ID
			Integer tpLevelID=null;

			TpLevel levelevel = tpLevelMapper.selectOne(tpLevel);
			QueryWrapper<TpRoomNo> Info = new QueryWrapper<>();
			Info.eq("community_id", tpUnit.getCommunityId());
			Info.eq("phase_id", tpUnit.getPhaseId());
			Info.eq("building_id",tpUnit.getBuildingId());
			Info.eq("unit_id", tpUnit.getId());
			if (null!=levelevel) {
				Info.eq("level_id", levelevel.getId());
			}

			//先删除所有房间
			tpRoomNoMapper.delete(Info);
			//再删除所有楼层
			tpLevelMapper.delete(tpLevel);
			//删除此单元
			tpUnitMapper.deleteById(id);

		}
		//删除楼栋
		if (type.equals("unit")){
			TpBuilding tpBuilding= tpBuildingMapper.selectById(id);
			//删除单元-楼层-房间
			QueryWrapper<TpUnit> tpUnit = new QueryWrapper<>();
			tpUnit.eq("community_id", tpBuilding.getCommunityId());
			tpUnit.eq("phase_id", tpBuilding.getPhaseId());
			tpUnit.eq("building_id", tpBuilding.getId());
			
			//删除单元-楼层-下的房间
			TpLevel tpLevelToo= tpLevelMapper.selectById(id);
				QueryWrapper<TpRoomNo> Info = new QueryWrapper<>();
			Info.eq("community_id", tpLevelToo.getCommunityId());
			Info.eq("phase_id", tpLevelToo.getPhaseId());
			Info.eq("building_id",tpLevelToo.getBuildingId());
			Info.eq("unit_id", tpLevelToo.getUnitId());
			Info.eq("level_id", tpLevelToo.getId());
			//先删除所有房间
			tpRoomNoMapper.delete(Info);
			//先查询楼层
			TpUnit unit= tpUnitMapper.selectOne(tpUnit);
			QueryWrapper<TpLevel> tpLevel = new QueryWrapper<>();
			if (null!=unit) {
				tpLevel.eq("phase_id", unit.getPhaseId());
				tpLevel.eq("community_id", unit.getCommunityId());
				tpLevel.eq("building_id", unit.getBuildingId());
				tpLevel.eq("unit_id", unit.getId());
			}
			//查询房间
			List<TpLevel> levelList= tpLevelMapper.selectList(tpLevel);
			QueryWrapper<TpRoomNo> tpRoomNoQueryWrapper = new QueryWrapper<>();
			if (null!=levelList) {
				for (TpLevel level:levelList) {
					tpRoomNoQueryWrapper.eq("phase_id", level.getPhaseId());
					tpRoomNoQueryWrapper.eq("community_id", level.getCommunityId());
					tpRoomNoQueryWrapper.eq("building_id", level.getBuildingId());
					tpRoomNoQueryWrapper.eq("unit_id", level.getUnitId());
					tpRoomNoQueryWrapper.eq("level_id", level.getId());
					//先删除所有房间
					tpRoomNoMapper.delete(tpRoomNoQueryWrapper);
				}
			}

			//再删除所有楼层
			tpLevelMapper.delete(tpLevel);
			//删除此单元
			tpUnitMapper.delete(tpUnit);
			//判断是否为单
			tpBuildingMapper.deleteById(id);
		}

		if (type.equals("building")){
			TpPhase phase= tpPhaseMapper.selectById(id);

			TpBuilding tpBuilding= tpBuildingMapper.selectById(phase.getId());
			//删除单元-楼层-房间
			QueryWrapper<TpUnit> tpUnit = new QueryWrapper<>();
			if (null!=tpBuilding) {
				tpUnit.eq("community_id", tpBuilding.getCommunityId());
				tpUnit.eq("phase_id", tpBuilding.getPhaseId());
				tpUnit.eq("building_id", tpBuilding.getId());
			}
			//先查询楼层
			List<TpUnit> unitList= tpUnitMapper.selectList(tpUnit);
			QueryWrapper<TpLevel> tpLevel = new QueryWrapper<>();
			if (null!=unitList) {
				for (TpUnit unit:unitList) {
					tpLevel.eq("phase_id", unit.getPhaseId());
					tpLevel.eq("community_id", unit.getCommunityId());
					tpLevel.eq("building_id", unit.getBuildingId());
					tpLevel.eq("unit_id", unit.getId());

					//查询房间
					TpLevel level= tpLevelMapper.selectOne(tpLevel);
					if (null!=level) {
						QueryWrapper<TpRoomNo> tpRoomNoQueryWrapper = new QueryWrapper<>();
						tpRoomNoQueryWrapper.eq("phase_id", level.getPhaseId());
						tpRoomNoQueryWrapper.eq("community_id", level.getCommunityId());
						tpRoomNoQueryWrapper.eq("building_id", level.getBuildingId());
						tpRoomNoQueryWrapper.eq("unit_id", level.getUnitId());
						tpRoomNoQueryWrapper.eq("level_id", level.getId());
						//先删除所有房间
						tpRoomNoMapper.delete(tpRoomNoQueryWrapper);
				}
					//再删除所有楼层
					tpLevelMapper.delete(tpLevel);
			 }
			}
			//删除此单元
			tpUnitMapper.delete(tpUnit);
			//删除栋
			QueryWrapper<TpBuilding> building= new QueryWrapper<>();
			tpLevel.eq("phase_id", phase.getId());
			tpLevel.eq("community_id", phase.getCommunityId());
			tpBuildingMapper.delete(building);
			//删除整个期
			tpPhaseMapper.deleteById(id);
		}
		responseBean.addSuccess("成功");
		return responseBean;
	}

	private void saveBuildingTreeData(Integer communityId, List<TpBuildingOwnerInfo> list) {
		String lastPhaseName = "";
		String lastBuildingName = "";
		String lastUnitName = "";
		String lastLevelName = "";
		String lastRoomNoName = "";
		
		Integer lastPhaseId = null;
		Integer lastBuildingId = null;
		Integer lastUnitId = null;
		Integer lastLevelId = null;
		
		for (TpBuildingOwnerInfo tpBuildingOwnerInfo : list){
			//插入期/区数据
			if (!tpBuildingOwnerInfo.getPhaseName().equals(lastPhaseName)){
				TpPhase tpPhase = new TpPhase();
				tpPhase.setName(tpBuildingOwnerInfo.getPhaseName());
				tpPhase.setCommunityId(communityId);
				tpPhase.setCreateDate(LocalDateTime.now());
				tpPhaseMapper.insert(tpPhase);
				//重置Last
				lastPhaseName = tpBuildingOwnerInfo.getPhaseName();
				lastBuildingName = "";
				lastUnitName = "";
				lastLevelName = "";
				lastRoomNoName = "";
				lastPhaseId = tpPhase.getId();
			}
			//插入楼栋数据
			if (!tpBuildingOwnerInfo.getBuildingName().equals(lastBuildingName)){
				TpBuilding tpBuilding = new TpBuilding();
				tpBuilding.setCommunityId(communityId);
				tpBuilding.setPhaseId(lastPhaseId);
				tpBuilding.setPhaseName(lastPhaseName);
				tpBuilding.setName(tpBuildingOwnerInfo.getBuildingName());
				tpBuilding.setCreateDate(LocalDateTime.now());
				tpBuildingMapper.insert(tpBuilding);
				lastBuildingName = tpBuildingOwnerInfo.getBuildingName();
				lastBuildingId = tpBuilding.getId();
			}
			//插入单元信息
			if (!tpBuildingOwnerInfo.getUnitName().equals(lastUnitName)){
				TpUnit tpUnit = new TpUnit();
				tpUnit.setCommunityId(communityId);
				tpUnit.setPhaseId(lastPhaseId);
				tpUnit.setPhaseName(lastPhaseName);
				tpUnit.setBuildingId(lastBuildingId);
				tpUnit.setBuildingName(lastBuildingName);
				tpUnit.setName(tpBuildingOwnerInfo.getUnitName());
				tpUnit.setCreateDate(LocalDateTime.now());
				tpUnitMapper.insert(tpUnit);
				lastUnitName = tpBuildingOwnerInfo.getUnitName();
				lastUnitId = tpUnit.getId();
			}
			//插入楼层信息
			if (!tpBuildingOwnerInfo.getLevelName().equals(lastLevelName)){
				TpLevel tpLevel = new TpLevel();
				tpLevel.setCommunityId(communityId);
				tpLevel.setPhaseId(lastPhaseId);
				tpLevel.setPhaseName(lastPhaseName);
				tpLevel.setBuildingId(lastBuildingId);
				tpLevel.setBuildingName(lastBuildingName);
				tpLevel.setUnitId(lastUnitId);
				tpLevel.setUnitName(lastUnitName);
				tpLevel.setName(tpBuildingOwnerInfo.getLevelName());
				tpLevel.setCreateDate(LocalDateTime.now());
				tpLevelMapper.insert(tpLevel);
				lastLevelName = tpBuildingOwnerInfo.getLevelName();
				lastLevelId = tpLevel.getId();
			}
			//插入房间号信息
			if (!tpBuildingOwnerInfo.getRoomNoName().equals(lastRoomNoName)){
				TpRoomNo tpRoomNo = new TpRoomNo();
				tpRoomNo.setCommunityId(communityId);
				tpRoomNo.setPhaseId(lastPhaseId);
				tpRoomNo.setPhaseName(lastPhaseName);
				tpRoomNo.setBuildingId(lastBuildingId);
				tpRoomNo.setBuildingName(lastBuildingName);
				tpRoomNo.setUnitId(lastUnitId);
				tpRoomNo.setUnitName(lastUnitName);
				tpRoomNo.setLevelId(lastLevelId);
				tpRoomNo.setLevelName(lastLevelName);
				tpRoomNo.setName(tpBuildingOwnerInfo.getRoomNoName());
				tpRoomNo.setCreateDate(LocalDateTime.now());
				tpRoomNoMapper.insert(tpRoomNo);
			}
		}
	}
	//删除当前节点判断是否有审核通过的
	private ResponseBean InfoPass(String type,Integer id ) {
		ResponseBean responseBean = new ResponseBean();
		if (type.equals("end")) {
			//判断书否有已审核人员
			TpRoomNo info = tpRoomNoMapper.selectById(id);
			QueryWrapper<TaUserVerify> taUserVerify = new QueryWrapper<>();
			taUserVerify.eq("community_id", info.getCommunityId())
					.eq("phase_id", info.getPhaseId())
					.eq("building_id", info.getBuildingId())
					.eq("unit_id", info.getUnitId())
					.eq("level_id", info.getLevelId())
					.eq("room_no_id", id)
					.eq("verify_status", 1);
			List<TaUserVerify> taUserVerifyList = taUserVerifyMapper.selectList(taUserVerify);
			if (null != taUserVerifyList) {
				responseBean.addError("不可删除审核已通过人员");
				return responseBean;
			}
		}
		if (type.equals("roomNo")) {
			TpLevel tpLevel = tpLevelMapper.selectById(id);
			QueryWrapper<TaUserVerify> taUserVerify = new QueryWrapper<>();
			taUserVerify.eq("community_id", tpLevel.getCommunityId())
					.eq("phase_id", tpLevel.getPhaseId())
					.eq("building_id", tpLevel.getBuildingId())
					.eq("unit_id", tpLevel.getUnitId())
					.eq("level_id", tpLevel.getId())
					.eq("verify_status", 1);
			List<TaUserVerify> taUserVerifyList = taUserVerifyMapper.selectList(taUserVerify);
			if (null != taUserVerifyList) {
				responseBean.addError("不可删除审核已通过人员");
				return responseBean;
			}
		}
		if (type.equals("level")) {
			TpUnit tpUnit = tpUnitMapper.selectById(id);
			QueryWrapper<TaUserVerify> taUserVerify = new QueryWrapper<>();
			taUserVerify.eq("community_id", tpUnit.getCommunityId())
					.eq("phase_id", tpUnit.getPhaseId())
					.eq("building_id", tpUnit.getBuildingId())
					.eq("verify_status", 1);
			List<TaUserVerify> taUserVerifyList = taUserVerifyMapper.selectList(taUserVerify);
			if (null != taUserVerifyList) {
				responseBean.addError("不可删除审核已通过人员");
				return responseBean;
			}
		}
		if (type.equals("unit")) {
			//判断书否有已审核人员
			TpBuilding tpBuilding = tpBuildingMapper.selectById(id);
			QueryWrapper<TaUserVerify> taUserVerify = new QueryWrapper<>();
			taUserVerify.eq("community_id", tpBuilding.getCommunityId())
					.eq("phase_id", tpBuilding.getPhaseId())
					.eq("building_id", tpBuilding.getId())
					.eq("verify_status", 1);
			List<TaUserVerify> taUserVerifyList = taUserVerifyMapper.selectList(taUserVerify);
			if (null != taUserVerifyList) {
				responseBean.addError("不可删除审核已通过人员");

				return responseBean;
			}
		}
		if (type.equals("building")) {
			TpPhase phase = tpPhaseMapper.selectById(id);
			QueryWrapper<TaUserVerify> taUserVerify = new QueryWrapper<>();
			taUserVerify.eq("community_id", phase.getCommunityId())
					.eq("phase_id", phase.getId())
					.eq("verify_status", 1);
			List<TaUserVerify> taUserVerifyList = taUserVerifyMapper.selectList(taUserVerify);
			if (null != taUserVerifyList) {
				responseBean.addError("不可删除审核已通过人员");
				return responseBean;
			}
		}
		responseBean.addError("成功");
		return responseBean;
	}
}
