package com.huiju.estateagents.property.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.Constant;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.dao.*;
import com.huiju.estateagents.property.model.*;
import com.huiju.estateagents.property.service.BuildingTreeServiceI;
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;

	@Autowired
	private TpBuildingOwnerInfoMapper tpBuildingOwnerInfoMapper;

	
	/**
	 * 获取整个小区的基础数据
	 *
	 * @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, TaUser 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.getOrgId();
		
		//保存数据到表中--拆分数据
		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")) {
			//是否存在同样的名字
			QueryWrapper<TpPhase> tpPhaseQuery = new QueryWrapper<>();
			tpPhaseQuery.eq("community_id",communityId);
			tpPhaseQuery.eq("NAME",nodeNumber);
			TpPhase phase= tpPhaseMapper.selectOne(tpPhaseQuery);
			if (null!=phase){
				responseBean.addError("节点名称已存在");
				return responseBean;
			}
			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")){
			//是否存在同样的名字
			QueryWrapper<TpBuilding> tpBuildingQuery = new QueryWrapper<>();
			tpBuildingQuery.eq("community_id",communityId);
			tpBuildingQuery.eq("phase_id",tpPhase.getId());
			tpBuildingQuery.eq("phase_name",tpPhase.getName());
			tpBuildingQuery.eq("name",nodeNumber);
			TpBuilding building= tpBuildingMapper.selectOne(tpBuildingQuery);
			if (null!=building){
				responseBean.addError("节点名称已存在");
				return responseBean;
			}
		 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")){
			// 校验节点名称
			QueryWrapper<TpUnit> unitQuery=new QueryWrapper<>();
			unitQuery.eq("community_id",selectTpBuilding.getCommunityId());
			unitQuery.eq("phase_id",selectTpBuilding.getPhaseId());
			unitQuery.eq("building_id",selectTpBuilding.getId());
			unitQuery.eq("name",nodeNumber);
			TpUnit unit= tpUnitMapper.selectOne(unitQuery);
			if (null!=unit){
				responseBean.addError("节点名称已存在");
				return responseBean;
			}
		 TpUnit tpUnit= new TpUnit();
				tpUnit.setCommunityId(selectTpBuilding.getCommunityId())
						.setPhaseId(selectTpBuilding.getPhaseId())
						.setPhaseName(selectTpBuilding.getPhaseName())
						.setBuildingName(selectTpBuilding.getName())
						.setBuildingId(selectTpBuilding.getId())
						.setName(nodeNumber)
						.setCreateDate(LocalDateTime.now());
		 	 tpUnitMapper.insert(tpUnit);
		 }

		//查询单元，维护楼层
		 TpUnit selectTpUnit= tpUnitMapper.selectById(id);
		if(type.equals("level")){
			//校验楼层名称
			QueryWrapper<TpLevel> levelQuery=new QueryWrapper<>();
			levelQuery.eq("community_id",selectTpUnit.getCommunityId());
			levelQuery.eq("phase_id",selectTpUnit.getPhaseId());
			levelQuery.eq("building_id",selectTpUnit.getBuildingId());
			levelQuery.eq("unit_id",selectTpUnit.getId());
			levelQuery.eq("name",nodeNumber);
			TpLevel level= tpLevelMapper.selectOne(levelQuery);
			if (null!=level){
				responseBean.addError("节点名称已存在");
				return responseBean;
			}
		 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")){
			 QueryWrapper<TpRoomNo> tpRoomNoQuery= new QueryWrapper<>();
			tpRoomNoQuery.eq("community_id",selectTpLevel.getCommunityId());
			tpRoomNoQuery.eq("phase_id",selectTpLevel.getPhaseId());
			tpRoomNoQuery.eq("building_id",selectTpLevel.getBuildingId());
			tpRoomNoQuery.eq("unit_id",selectTpLevel.getUnitId());
			tpRoomNoQuery.eq("name",nodeNumber);
			TpRoomNo roomNo= tpRoomNoMapper.selectOne(tpRoomNoQuery);
			if (null!=roomNo){
				responseBean.addError("节点名称已存在");
				return  responseBean;
			}
			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");

		//审核是否通过
		  Boolean isA= InfoPass(type,id);
		  if (isA==false){
		  	responseBean.addError("需要先删除相关人员");
		  	return responseBean;
		  }
		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());
			Info.eq("level_name", tpLevel.getName());
			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;
			List<TpLevel> levelevelList = tpLevelMapper.selectList(tpLevel);
			for (TpLevel levelevel:levelevelList) {
				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());
			tpUnit.eq("building_name", tpBuilding.getName());
			//先查询楼层
			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.eq("unit_name", unit.getName());
					//查询房间
					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());
							tpRoomNoQueryWrapper.eq("level_name", level.getName());
							//先删除所有房间
							tpRoomNoMapper.delete(tpRoomNoQueryWrapper);
						}
				}
			  }
			}
			//删除所有楼层
			tpLevelMapper.delete(tpLevel);
			//删除此单元
			tpUnitMapper.delete(tpUnit);
			//删除栋
			tpBuildingMapper.deleteById(id);
		}
		if (type.equals("building")){
			TpPhase phase= tpPhaseMapper.selectById(id);

			QueryWrapper<TpBuilding> building= new QueryWrapper<>();
			building.eq("phase_id", phase.getId());
			building.eq("community_id", phase.getCommunityId());
			building.eq("name", phase.getName());
			List<TpBuilding> tpBuildingList= tpBuildingMapper.selectList(building);
			for (TpBuilding tpBuilding:tpBuildingList){
			//删除单元-楼层-房间
			QueryWrapper<TpUnit> tpUnit = new QueryWrapper<>();
			List<TpUnit> unitList=null;
			if (null!=tpBuilding) {
				tpUnit.eq("community_id", tpBuilding.getCommunityId());
				tpUnit.eq("phase_id", tpBuilding.getPhaseId());
				tpUnit.eq("building_id", tpBuilding.getId());
				tpUnit.eq("building_name", tpBuilding.getName());
				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.eq("unit_name", unit.getName());
					//查询房间
					List<TpLevel> levelList = tpLevelMapper.selectList(tpLevel);
					if (null != levelList) {
						for (TpLevel level : levelList) {
							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());
							tpRoomNoQueryWrapper.eq("level_name", level.getName());
							//先删除所有房间
							tpRoomNoMapper.delete(tpRoomNoQueryWrapper);
						}
					}
					//再删除所有楼层
					tpLevelMapper.delete(tpLevel);
				}
			}
			//删除此单元
			tpUnitMapper.delete(tpUnit);
	}
			//删除栋
			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){
			//插入期/区数据
				TpPhase tpPhase = new TpPhase();
				tpPhase.setName(tpBuildingOwnerInfo.getPhaseName());
				tpPhase.setCommunityId(communityId);
				tpPhase.setCreateDate(LocalDateTime.now());

				QueryWrapper<TpPhase> phaseQueryWrapper = new QueryWrapper<>();
				phaseQueryWrapper.eq("community_id", communityId);
				phaseQueryWrapper.eq("`NAME`", tpBuildingOwnerInfo.getPhaseName());
				TpPhase selectPhase = tpPhaseMapper.selectOne(phaseQueryWrapper);
				// 判断是否存在库里
				if (null != selectPhase) {
					lastPhaseName = selectPhase.getName();
					lastPhaseId = selectPhase.getId();
				} else {
					tpPhaseMapper.insert(tpPhase);
					//重置Last
					lastPhaseName = tpBuildingOwnerInfo.getPhaseName();
					lastBuildingName = "";
					lastUnitName = "";
					lastLevelName = "";
					lastRoomNoName = "";
					lastPhaseId = tpPhase.getId();
				}
			//插入楼栋数据
				TpBuilding tpBuilding = new TpBuilding();
				tpBuilding.setCommunityId(communityId);
				tpBuilding.setPhaseId(lastPhaseId);
				tpBuilding.setPhaseName(lastPhaseName);
				tpBuilding.setName(tpBuildingOwnerInfo.getBuildingName());
				tpBuilding.setCreateDate(LocalDateTime.now());

				QueryWrapper<TpBuilding> buildingQueryWrapper = new QueryWrapper<>();
				buildingQueryWrapper.eq("community_id", communityId);
				buildingQueryWrapper.eq("phase_id", lastPhaseId);
				buildingQueryWrapper.eq("`name`", tpBuildingOwnerInfo.getBuildingName());
				TpBuilding selectBuilding = tpBuildingMapper.selectOne(buildingQueryWrapper);
				if (null != selectBuilding) {
					lastBuildingName = selectBuilding.getName();
					lastBuildingId = selectBuilding.getId();
				} else {
					tpBuildingMapper.insert(tpBuilding);
					lastBuildingName = tpBuildingOwnerInfo.getBuildingName();
					lastBuildingId = tpBuilding.getId();
				}

			//插入单元信息
				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());

				QueryWrapper<TpUnit> unitQueryWrapper = new QueryWrapper<>();
				unitQueryWrapper.eq("community_id", communityId);
				unitQueryWrapper.eq("phase_id", lastPhaseId);
				unitQueryWrapper.eq("building_id", lastBuildingId);
				unitQueryWrapper.eq("`name`",tpBuildingOwnerInfo.getUnitName());
				TpUnit selectUnit = tpUnitMapper.selectOne(unitQueryWrapper);
				// 校验是否存在库里
				if (null != selectUnit) {
					lastUnitName = selectUnit.getName();
					lastUnitId = selectUnit.getId();
				} else {
					tpUnitMapper.insert(tpUnit);
					lastUnitName = tpBuildingOwnerInfo.getUnitName();
					lastUnitId = tpUnit.getId();
				}

			//插入楼层信息

				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());

				QueryWrapper<TpLevel> levelQueryWrapper = new QueryWrapper<>();
				levelQueryWrapper.eq("community_id", communityId);
				levelQueryWrapper.eq("phase_id", lastPhaseId);
				levelQueryWrapper.eq("building_id", lastBuildingId);
				levelQueryWrapper.eq("unit_id", lastUnitId);
				levelQueryWrapper.eq("`name`", tpBuildingOwnerInfo.getLevelName());
				TpLevel selectLevel = tpLevelMapper.selectOne(levelQueryWrapper);
				// 校验是否存在库里
				if (null != selectLevel) {
					lastLevelName = selectLevel.getName();
					lastLevelId = selectLevel.getId();
				} else {
					tpLevelMapper.insert(tpLevel);
					lastLevelName = tpBuildingOwnerInfo.getLevelName();
					lastLevelId = tpLevel.getId();
				}

			//插入房间号信息
				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());

				QueryWrapper<TpRoomNo> roomNoQueryWrapper = new QueryWrapper<>();
				roomNoQueryWrapper.eq("community_id", communityId);
				roomNoQueryWrapper.eq("phase_id", lastPhaseId);
				roomNoQueryWrapper.eq("building_id", lastBuildingId);
				roomNoQueryWrapper.eq("unit_id", lastUnitId);
				roomNoQueryWrapper.eq("level_id", lastLevelId);
				roomNoQueryWrapper.eq("`name`", tpBuildingOwnerInfo.getRoomNoName());
				TpRoomNo selectRoomNo = tpRoomNoMapper.selectOne(roomNoQueryWrapper);
				if (null == selectRoomNo) {
					tpRoomNoMapper.insert(tpRoomNo);
				}

		}
	}
	//删除当前节点判断是否有审核通过的
	private boolean 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 && taUserVerifyList.size()>0) {
				return false;
			}
		}
		if (type.equals("roomNo")) {
			TpLevel tpLevel = tpLevelMapper.selectById(id);
			QueryWrapper<TpBuildingOwnerInfo> tpBuildingOwnerInfo = new QueryWrapper<>();
			if (null!=tpLevel) {
				tpBuildingOwnerInfo.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<TpBuildingOwnerInfo> ifoBuid = tpBuildingOwnerInfoMapper .selectList(tpBuildingOwnerInfo);
			if (null != ifoBuid && ifoBuid.size()>0) {
				return false;
			}
		}
		if (type.equals("level")) {
			TpUnit tpUnit = tpUnitMapper.selectById(id);
			QueryWrapper<TpBuildingOwnerInfo> tpBuildingOwnerInfo = new QueryWrapper<>();
			if (null!=tpUnit) {
				tpBuildingOwnerInfo.eq("community_id", tpUnit.getCommunityId())
						.eq("phase_id", tpUnit.getPhaseId())
						.eq("building_id", tpUnit.getBuildingId())
						.eq("unit_id",tpUnit.getId())
						.eq("verify_status", 1);
				}
			List<TpBuildingOwnerInfo> ifoBuid = tpBuildingOwnerInfoMapper.selectList(tpBuildingOwnerInfo);
			if (null != ifoBuid && ifoBuid.size()>0) {
				return false;
			}
		}
		if (type.equals("unit")) {
			//判断书否有已审核人员
			TpBuilding tpBuilding = tpBuildingMapper.selectById(id);
			QueryWrapper<TpBuildingOwnerInfo> tpBuildingOwnerInfo = new QueryWrapper<>();
			if (null!=tpBuilding) {
				tpBuildingOwnerInfo.eq("community_id", tpBuilding.getCommunityId())
						.eq("phase_id", tpBuilding.getPhaseId())
						.eq("building_id", tpBuilding.getId())
						.eq("verify_status", 1);
			    }
			List<TpBuildingOwnerInfo> ifoBuid = tpBuildingOwnerInfoMapper.selectList(tpBuildingOwnerInfo);
			if (null != ifoBuid && ifoBuid.size()>0) {
				return false;
			}
		}
		if (type.equals("building")) {
			TpPhase phase = tpPhaseMapper.selectById(id);
				QueryWrapper<TpBuildingOwnerInfo> tpBuildingOwnerInfo = new QueryWrapper<>();
			if (null!=phase) {
				tpBuildingOwnerInfo.eq("community_id", phase.getCommunityId())
						.eq("phase_id", phase.getId())
						.eq("verify_status", 1);
				}
			List<TpBuildingOwnerInfo> ifoBuid = tpBuildingOwnerInfoMapper.selectList(tpBuildingOwnerInfo);
			if (null != ifoBuid && ifoBuid.size()>0) {
				return false;
			}
		}
		return true;
	}
}
