package org.springblade.datamigration.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import lombok.AllArgsConstructor;

import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.SparkSession;
import org.springblade.community.entity.*;
import org.springblade.community.feign.*;
import org.springblade.community.vo.BuildingVO;
import org.springblade.core.tool.api.R;
import org.springblade.datamigration.constant.UrlConstants;
import org.springblade.datamigration.util.TokenUtil;
import org.springblade.datamigration.util.WuyeSystemHttpUtil;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * @author lidexi
 * @date 2021/6/9 17:36
 * 园区服务数据迁移
 */
@RestController
@RequestMapping("/data/migration/community")
@AllArgsConstructor
public class CommunityDataMigrationController implements Serializable {


	private transient final IAgencyClient agencyClient;
	private transient final IResidentialClient residentialClient;
	private transient final IBuildingClient buildingClient;
	private transient final IUnitClient unitClient;
	private transient final IFloorClient floorClient;
	private transient final IRoomClient roomClient;
	private transient static SparkConf sparkConf;
	private transient static SQLContext sqlContext;
	private transient static Properties gridConnectionProperties;
	private transient static Properties localConnectionProperties;

	private static final String GRID_SYSTEM_DB_URL = "jdbc:mysql://139.9.103.171:3306/ycwgdb?useSSL=false&useUnicode=true&" +
		"characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&" +
		"serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true";

	private static final String LOCAL_SYSTEM_DB_URL = "jdbc:mysql://127.0.0.1:3306/smart_community?useSSL=false&useUnicode=true&" +
		"characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&" +
		"serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true";

	private static final String WUYE_SYSTEM_DB_URL = "jdbc:mysql://139.9.103.171:3306/sptg_estate?useSSL=false&useUnicode=true&" +
		"characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&" +
		"serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true";


	static {
//		System.setProperty("hadoop.home.dir", "D:\\hadoop-common-2.2.0-bin-master\\bin");
//		sparkConf = new SparkConf().setAppName("CommunityDataMigrationController").setMaster("local");
//		SparkContext sc = new SparkContext(sparkConf);
//		sqlContext = new SQLContext(sc);
//
//		gridConnectionProperties = new Properties();
//		gridConnectionProperties.put("user", "root");
//		gridConnectionProperties.put("password", "2e564ee5-d9ed-11e9-bab6-0242ac170005");
//		gridConnectionProperties.put("driver", "com.mysql.jdbc.Driver");
//
//		localConnectionProperties = new Properties();
//		localConnectionProperties.put("user", "root");
//		localConnectionProperties.put("password", "lidexi");
//		gridConnectionProperties.put("driver", "com.mysql.jdbc.Driver");
	}

	/**                                        物业系统数据迁移-园区租户的数据以物业系统为准                   **/


	/**
	 * 园区数据迁移
	 *
	 * @return
	 */
	@GetMapping("/agency")
	public R agencyDataMigration() {
		String token = TokenUtil.getWuyeSystemToken();
		Map<String, Object> map = new HashMap<>();
		R r = WuyeSystemHttpUtil.postQuery(JSONUtil.toJsonStr(map), UrlConstants.agencyService.AGENCYURL.getUrl() + "?size=" + Integer.MAX_VALUE + "&current=1", token);
		Map data = (Map) r.getData();
		data = (Map) data.get("data");
		JSONArray records = JSONUtil.parseArray(data.get("records"));

		records.forEach(item -> {
			JSONObject jsonObject = JSONUtil.parseObj(item);
			Long oldId = Long.valueOf(jsonObject.get("id").toString());
			Agency isExist = agencyClient.getById(oldId);
			Agency agency = this.buildAgency(jsonObject);
			if (isExist == null) {//新增
				agencyClient.save(agency);
			} else {
				agencyClient.update(agency);
			}
		});
		return R.data(true);
	}

	/**
	 * 小区数据迁移
	 *
	 * @return
	 */
	@GetMapping("/residential")
	public R residentialDataMigration() {

		String token = TokenUtil.getWuyeSystemToken();
		Map<String, Object> map = new HashMap<>();
		R r = WuyeSystemHttpUtil.postQuery(JSONUtil.toJsonStr(map), UrlConstants.agencyService.RESIDENTIALURL.getUrl() + "?size=" + Integer.MAX_VALUE + "&current=1", token);
		Map data = (Map) r.getData();
		data = (Map) data.get("data");
		JSONArray records = JSONUtil.parseArray(data.get("records"));

		records.forEach(item -> {
			try {
				JSONObject jsonObject = JSONUtil.parseObj(item);
				Long oldId = Long.valueOf(jsonObject.get("id").toString());
				Residential isExist = residentialClient.getById(oldId);
				Residential residential = this.buildResidential(jsonObject);
				if (isExist == null) {//新增
					residentialClient.save(residential);
				} else {
					residentialClient.update(residential);
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(item.toString());
			}

		});
		return R.data(true);
	}

	/**
	 * 楼栋数据迁移
	 *
	 * @return
	 */
	@GetMapping("/building")
	public R buildingDataMigration() {
		String token = TokenUtil.getWuyeSystemToken();
		Map<String, Object> map = new HashMap<>();
		R r = WuyeSystemHttpUtil.postQuery(JSONUtil.toJsonStr(map), UrlConstants.agencyService.BUILDINGURL.getUrl() + "?size=" + Integer.MAX_VALUE + "&current=1", token);
		Map data = (Map) r.getData();
		data = (Map) data.get("data");
		JSONArray records = JSONUtil.parseArray(data.get("records"));

		records.forEach(item -> {
			try {
				JSONObject jsonObject = JSONUtil.parseObj(item);
				Long oldId = Long.valueOf(jsonObject.get("id").toString());
				Building isExist = buildingClient.getById(oldId);
				Building building = this.buildBuilding(jsonObject);
				if (isExist == null) {//新增
					buildingClient.save(building);
				} else {
					buildingClient.update(building);
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(item.toString());
			}

		});
		return R.data(true);
	}


	/**
	 * 单元数据迁移
	 *
	 * @return
	 */
	@GetMapping("/unit")
	public R unitDataMigration() {
		long starttime = System.currentTimeMillis();
		String token = TokenUtil.getWuyeSystemToken();
		Map<String, Object> map = new HashMap<>();
		List<BuildingVO> list = this.buildingClient.list(new Building());
		list.forEach(buildingVO -> {
			R r = WuyeSystemHttpUtil.postQuery(JSONUtil.toJsonStr(map), UrlConstants.agencyService.UNITURL.getUrl() + "?size=" + Integer.MAX_VALUE + "&current=1" + "&buildingId=" + buildingVO.getId(), token);
			Map data = (Map) r.getData();
			JSONArray records = JSONUtil.parseArray(data.get("list"));
			int size = records.size();

			records.forEach(item -> {
				try {
					JSONObject jsonObject = JSONUtil.parseObj(item);
					Long oldId = Long.valueOf(jsonObject.get("id").toString());
					Unit isExist = unitClient.getById(oldId);
					Unit unit = this.buildUnit(jsonObject);
					if (isExist == null) {//新增
						unitClient.save(unit);
					} else {
						unitClient.update(unit);
					}
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println(item.toString());
				}

			});
		});
		long endtime = System.currentTimeMillis();

		return R.data("迁移数据耗时" + (endtime - starttime) + "毫秒");
	}

	/**
	 * 楼层数据迁移
	 *
	 * @return
	 */
	@GetMapping("/floor")
	public R floorDataMigration() {
		long starttime = System.currentTimeMillis();
		String token = TokenUtil.getWuyeSystemToken();
		Map<String, Object> map = new HashMap<>();

		R r = WuyeSystemHttpUtil.postQuery(JSONUtil.toJsonStr(map), UrlConstants.agencyService.FLOORURL.getUrl() + "?size=" + Integer.MAX_VALUE + "&current=1", token);
		Map data = (Map) r.getData();
		data = (Map) data.get("data");
		JSONArray records = JSONUtil.parseArray(data.get("records"));

		records.forEach(item -> {
			try {
				JSONObject jsonObject = JSONUtil.parseObj(item);
				Long oldId = Long.valueOf(jsonObject.get("id").toString());
				Floor isExist = floorClient.getById(oldId);
				Floor floor = this.buildFloor(jsonObject);
				if (isExist == null) {//新增
					floorClient.save(floor);
				} else {
					floorClient.update(floor);
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(item.toString());
			}

		});
		long endtime = System.currentTimeMillis();
		return R.data("楼层总数：" + records.size() + "，迁移数据耗时" + (endtime - starttime) + "毫秒");
	}

	/**
	 * 房间数据迁移
	 *
	 * @return
	 */
	@GetMapping("/room")
	@Deprecated
	public R roomDataMigration() {
		String token = TokenUtil.getWuyeSystemToken();
		Map<String, Object> map = new HashMap<>();
		//要先查出所有的楼层
		R r = WuyeSystemHttpUtil.postQuery(JSONUtil.toJsonStr(map), UrlConstants.agencyService.ROOMURL.getUrl() + "?size=" + Integer.MAX_VALUE + "&current=1", token);
		Map data = (Map) r.getData();
		data = (Map) data.get("data");
		JSONArray records = JSONUtil.parseArray(data.get("records"));

		records.forEach(item -> {
			try {
				JSONObject jsonObject = JSONUtil.parseObj(item);
				Long oldId = Long.valueOf(jsonObject.get("id").toString());
				Room isExist = roomClient.getById(oldId);
				Room room = this.buildRoom(jsonObject);
				if (isExist == null) {//新增
					roomClient.save(room);
				} else {
					roomClient.update(room);
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(item.toString());
			}

		});
		return R.data(true);
	}


	@GetMapping("/roomnew")
	public R roomNewMigration() {
		long starttime = System.currentTimeMillis();

		String floorTable = "community_floor";
		sqlContext.read().jdbc(LOCAL_SYSTEM_DB_URL, floorTable, localConnectionProperties).createOrReplaceTempView("community_floor");
		Dataset floorDataset = sqlContext.sql("SELECT * FROM community_floor");
		JavaRDD<Row> javaRDD = floorDataset.toJavaRDD();
		JavaRDD<Floor> floorJavaRDD = javaRDD.map(new Function<Row, Floor>() {

			@Override
			public Floor call(Row row) throws Exception {
				Floor floor = new Floor();
				floor.setId(row.get(0) != null ? row.getLong(0) : null);
				floor.setAgencyId(row.get(2) != null ? Long.valueOf(row.getLong(2)) : null);
				floor.setAgencyName(row.get(3) != null ? row.getString(3) : null);
				floor.setOrgPosition(row.get(17) != null ? row.getString(17) : null);
				return floor;
			}
		});
		List<Floor> floorList = floorJavaRDD.collect();
		Map<Long, Floor> map = new HashMap<>();
		floorList.stream().forEach(floor -> {
			map.put(floor.getId(), floor);
		});

		//查找的表名
		String table = "estate_room";
		// 读取表中所有数据
		sqlContext.read().jdbc(WUYE_SYSTEM_DB_URL, table, gridConnectionProperties).createOrReplaceTempView("community_room");
		Dataset roomDataset = sqlContext.sql("SELECT * FROM community_room");


		//显示数据
		JavaRDD<Row> javaRDD1 = roomDataset.javaRDD();
		JavaRDD<Room> roomJavaRDD = javaRDD1.map(new Function<Row, Room>() {

			@Override
			public Room call(Row row) throws Exception {
				Room room = new Room();
				room.setId(row.get(0) != null ? row.getLong(0) : null);
				room.setName(row.get(1) != null ? row.getString(1) : null);
				room.setResidentialId(row.get(2) != null ? row.getLong(2) : null);
				room.setResidentialName(row.get(3) != null ? row.getString(3) : null);
				room.setBuildingId(row.get(4) != null ? row.getLong(4) : null);
				room.setBuildingName(row.get(5) != null ? row.getString(5) : null);
				room.setUnitId(row.get(6) != null ? row.getLong(6) : null);
				room.setUnitName(row.get(7) != null ? row.getString(7) : null);
				room.setFloorId(row.get(8) != null ? row.getLong(8) : null);
				room.setFloorName(row.get(9) != null ? row.getString(9) : null);
				Floor floor = map.get(room.getFloorId());
				if (floor != null) {
					room.setRemark(floor.getRemark());
					room.setAgencyName(floor.getAgencyName());
					room.setAgencyId(floor.getAgencyId());
					room.setOrgPosition(floor.getOrgPosition() + "/" + room.getId());
				} else {
					room.setRemark("找不到对应的楼层：" + room.getFloorId());
				}
				room.setArea(row.get(10) != null ? row.getDecimal(10) : null);
				room.setHouseType(row.get(11) != null ? row.getString(11) : null);
				room.setOrientation(row.get(12) != null ? row.getString(12) : null);
				room.setUserCount(row.get(13) != null ? row.getInt(13) : null);
				room.setIsDeleted(row.get(14) != null ? row.getInt(14) : null);
				room.setTenantId("000000");

				return room;
			}

		});

		Dataset<Row> dataFrame = sqlContext.createDataFrame(roomJavaRDD, Room.class);
		dataFrame.createOrReplaceTempView("community");
		SparkSession sparkSession = SparkSession.builder().appName("RoomController").getOrCreate();
		Dataset<Row> roomDataFrame = sparkSession.sql("select id,name,residentialId as residential_id," +
			"residentialName as residential_name,buildingId as building_id,buildingName as building_name," +
			"unitId as unit_id,unitName as unit_name,floorId as floor_id,floorName as floor_name," +
			"area,houseType as house_type,orientation,userCount as user_count,remark," +
			"orgPosition as org_position,isDeleted as is_deleted," +
			"updateUser as update_user,tenantId as tenant_id," +
			"agencyId as agency_id,agencyName as agency_name,createUser as create_user," +
			"status from community");
		roomDataFrame.write().mode("append").jdbc(LOCAL_SYSTEM_DB_URL, "community_room", localConnectionProperties);
		long endtime = System.currentTimeMillis();
		System.out.println("程序执行时间：" + (endtime - starttime) + "ms");
//		sparkSession.close();
		return R.data("房间数量为：" + roomJavaRDD.collect().size() + "，数据迁移耗时：" + (endtime - starttime) / 1000 + "秒");
	}


	/**
	 * 构建园区
	 *
	 * @param jsonObject
	 * @return
	 */
	private Agency buildAgency(JSONObject jsonObject) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Agency agency = new Agency();
		agency.setLatitude(jsonObject.getStr("latitude"));
		agency.setRemark(jsonObject.getStr("remark"));
		agency.setPic(jsonObject.getStr("pic").replace("http://139.9.103.171:1888/miniofile/", ""));
		agency.setPersonPhone(jsonObject.getStr("personPhone"));
		agency.setId(jsonObject.getLong("id"));
		try {
			agency.setCreateTime(sdf.parse(jsonObject.getStr("createDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		agency.setLongitude(jsonObject.getStr("longitude"));
		agency.setRoomCount(jsonObject.getInt("roomCount"));
		agency.setAddress(jsonObject.getStr("address"));
		agency.setCarCount(jsonObject.getInt("carCount"));
		agency.setUnitCount(jsonObject.getInt("unitCount"));
		agency.setRegionStreet(jsonObject.getStr("regionStreet"));
		agency.setRegionCity(jsonObject.getStr("regionCity"));
		agency.setName(jsonObject.getStr("agencyName"));
		agency.setPersonCount(jsonObject.getInt("personCount"));
		agency.setPersonName(jsonObject.getStr("personName"));
		agency.setRegionArea(jsonObject.getStr("regionArea"));
		agency.setResidentialCount(jsonObject.getInt("residentialCount"));
		agency.setRegionProvince(jsonObject.getStr("regionProvince"));
		agency.setOrgPosition("/" + agency.getRegionProvince() + "/" + agency.getRegionCity() + "/" +
			agency.getRegionArea() + "/" + agency.getRegionStreet() + "/" + agency.getId());
		agency.setTenantId("000000");
		agency.setBuildingCount(0);
		agency.setFloorCount(0);
		return agency;
	}

	/**
	 * 构建小区
	 *
	 * @param jsonObject
	 * @return
	 */
	private Residential buildResidential(JSONObject jsonObject) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Residential residential = new Residential();

		residential.setRoomCount(jsonObject.getInt("roomNumber"));
		residential.setLatitude(jsonObject.getStr("latitude"));
		residential.setUnitCount(jsonObject.getInt("unitNumber"));
		residential.setAgencyId(jsonObject.getLong("agencyId"));
		residential.setThirdResidentialId(jsonObject.getStr("qinLinResidentialId"));
		residential.setFloorCount(jsonObject.getInt("floorNumber"));
		residential.setBuildingCount(jsonObject.getInt("buildingNumber"));
		residential.setId(jsonObject.getLong("id"));
		residential.setPersonPhone(jsonObject.getStr("personTel"));
		residential.setUserCount(jsonObject.getInt("userNumber"));
		residential.setLongitude(jsonObject.getStr("longitude"));
		try {
			if (null != jsonObject.get("createDate"))
				residential.setCreateTime(sdf.parse(jsonObject.getStr("createDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		residential.setAddress(jsonObject.getStr("address"));
		residential.setRegionStreet(jsonObject.getStr("regionStreet"));
		residential.setRegionCity(jsonObject.getStr("regionCity"));
		residential.setAgencyName(jsonObject.getStr("agencyName"));
		residential.setPersonName(jsonObject.getStr("personName"));
		residential.setRegionArea(jsonObject.getStr("regionArea"));
		residential.setName(jsonObject.getStr("name"));
		residential.setRegionProvince(jsonObject.getStr("regionProvince"));
		Agency agency = agencyClient.getById(residential.getAgencyId());
		if (agency != null) {
			residential.setOrgPosition(agency.getOrgPosition() + "/" + residential.getId());
			residential.setRemark(agency.getRemark());
		} else {
			residential.setOrgPosition("agencyId对应的园区不存在");
			residential.setRemark("找不到对应的园区" + residential.getAgencyId());
		}
		return residential;
	}


	/**
	 * 构建楼栋
	 *
	 * @param jsonObject
	 * @return
	 */
	private Building buildBuilding(JSONObject jsonObject) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Building building = new Building();

		building.setId(jsonObject.getLong("id"));
		building.setName(jsonObject.getStr("name"));
		building.setUnitCount(jsonObject.getInt("unitNumber"));
		building.setRoomCount(jsonObject.getInt("roomNumber"));
		building.setUserCount(jsonObject.getInt("personNumber"));
		if (jsonObject.getLong("residentialId") != null) {
			building.setResidentialId(jsonObject.getLong("residentialId"));
			Residential residential = residentialClient.getById(building.getResidentialId());
			if (residential != null) {
				building.setResidentialName(residential.getName());
				building.setOrgPosition(residential.getOrgPosition() + "/" + building.getId());
				building.setAgencyId(residential.getAgencyId());
				building.setAgencyName(residential.getAgencyName());
				building.setRemark(residential.getRemark());
			} else {
				building.setResidentialName("找不到对应的小区");
				building.setOrgPosition("找不到对应的小区");
				building.setRemark("找不到对应的小区" + building.getResidentialId());
			}
		}
		building.setFloorCount(jsonObject.getInt("floorNumber"));
		try {
			if (null != jsonObject.get("createDate"))
				building.setCreateTime(sdf.parse(jsonObject.getStr("createDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		building.setPersonName(jsonObject.getStr("personName"));
		building.setPersonPhone(jsonObject.getStr("personTel"));
		building.setTenantId("000000");

		return building;
	}

	/**
	 * 构建单元
	 *
	 * @param jsonObject
	 * @return
	 */
	private Unit buildUnit(JSONObject jsonObject) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Unit unit = new Unit();

		unit.setId(jsonObject.getLong("id"));
		unit.setName(jsonObject.getStr("name"));
		unit.setResidentialId(jsonObject.getLong("residentialId"));
		unit.setResidentialName(jsonObject.getStr("residentialName"));
		unit.setPersonName(jsonObject.getStr("personName"));
		unit.setPersonTel(jsonObject.getStr("personTel"));
		unit.setBuiltYear(jsonObject.getStr("builtYear"));
		unit.setIncludingElevator(jsonObject.getStr("includingElevator"));
		unit.setRoomCount(jsonObject.getInt("roomNumber"));
		unit.setUserCount(jsonObject.getInt("userNumber"));
		unit.setRemark(jsonObject.getStr("remark"));

		try {
			if (null != jsonObject.get("createDate"))
				unit.setCreateTime(sdf.parse(jsonObject.getStr("createDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		unit.setBuildingId(jsonObject.getLong("buildingId"));
		if (jsonObject.getLong("buildingId") != null) {
			Building building = buildingClient.getById(unit.getBuildingId());
			if (building != null) {
				unit.setOrgPosition(building.getOrgPosition() + "/" + unit.getId());
				unit.setBuildingName(building.getName());
				unit.setResidentialName(building.getResidentialName());
				unit.setResidentialId(building.getResidentialId());
				unit.setAgencyId(building.getAgencyId());
				unit.setAgencyName(building.getAgencyName());
				unit.setRemark(building.getRemark());
			} else {
				unit.setBuildingName("楼栋找不到");
				unit.setRemark("找不到对应的楼栋" + unit.getBuildingId());
			}


		}
		unit.setTenantId("000000");
		return unit;
	}

	/**
	 * 构建楼层
	 *
	 * @param jsonObject
	 * @return
	 */
	private Floor buildFloor(JSONObject jsonObject) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Floor floor = new Floor();

		floor.setId(jsonObject.getLong("id"));
		floor.setName(jsonObject.getStr("name"));
		floor.setResidentialId(jsonObject.getLong("residentialId"));
		floor.setResidentialName(jsonObject.getStr("residentialName"));
		floor.setPersonName(jsonObject.getStr("personName"));
		floor.setPersonTel(jsonObject.getStr("personTel"));
		floor.setBuiltYear(jsonObject.getStr("builtYear"));
		floor.setIncludingElevator(jsonObject.getStr("includingElevator"));
		floor.setRoomCount(jsonObject.getInt("roomNumber"));
		floor.setUserCount(jsonObject.getInt("userNumber"));
		floor.setRemark(jsonObject.getStr("remark"));
		try {
			if (null != jsonObject.get("createDate"))
				floor.setCreateTime(sdf.parse(jsonObject.getStr("createDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		floor.setBuildingId(jsonObject.getLong("buildingId"));
		floor.setBuildingName(jsonObject.getStr("buildingName"));
		floor.setUnitId(jsonObject.getLong("unitId"));
		if (floor.getUnitId() != null) {
			Unit unit = this.unitClient.getById(floor.getUnitId());
			if (unit != null) {
				floor.setUnitName(unit.getName());
				floor.setOrgPosition(unit.getOrgPosition() + "/" + floor.getId());
				floor.setBuildingId(unit.getBuildingId());
				floor.setBuildingName(unit.getBuildingName());
				floor.setResidentialId(unit.getResidentialId());
				floor.setResidentialName(unit.getResidentialName());
				floor.setAgencyId(unit.getAgencyId());
				floor.setAgencyName(unit.getAgencyName());
				floor.setRemark(unit.getRemark());
			} else {
				floor.setUnitName("找不到对应的单元");
				floor.setRemark("找不到对应的单元" + floor.getUnitId());
			}
		}
		floor.setTenantId("000000");
		return floor;
	}

	/**
	 * 构建房间
	 *
	 * @param jsonObject
	 * @return
	 */
	private Room buildRoom(JSONObject jsonObject) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Room room = new Room();

		room.setId(jsonObject.getLong("id"));
		room.setName(jsonObject.getStr("name"));
		room.setUnitId(jsonObject.getLong("unitId"));
		room.setUnitName(jsonObject.getStr("unitName"));
		room.setHouseType(jsonObject.getStr("houseType"));
		room.setOrientation(jsonObject.getStr("orientation"));
		room.setArea(jsonObject.getBigDecimal("area"));
		room.setUserCount(jsonObject.getInt("userNumber"));
		room.setRemark(jsonObject.getStr("remark"));
		room.setResidentialId(jsonObject.getLong("residentialId"));
		room.setResidentialName(jsonObject.getStr("residentialName"));

		try {
			if (null != jsonObject.get("createDate"))
				room.setCreateTime(sdf.parse(jsonObject.getStr("createDate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		room.setBuildingId(jsonObject.getLong("buildingId"));
		room.setBuildingName(jsonObject.getStr("buildingName"));
		room.setFloorId(jsonObject.getLong("floorId"));
		if (room.getFloorId() != null) {
			Floor floor = floorClient.getById(room.getFloorId());
			if (floor != null) {
				room.setFloorName(floor.getName());
				room.setOrgPosition(floor.getOrgPosition() + "/" + room.getId());
				room.setUnitName(floor.getUnitName());
				room.setUnitId(floor.getUnitId());
				room.setBuildingName(floor.getBuildingName());
				room.setBuildingId(floor.getBuildingId());
				room.setResidentialId(floor.getResidentialId());
				room.setResidentialName(floor.getResidentialName());
				room.setAgencyId(floor.getAgencyId());
				room.setAgencyName(floor.getAgencyName());
			} else {
				room.setFloorName("找不到对应的楼层");
			}
		}
		room.setTenantId("000000");
		return room;
	}


	/**                                        数字园区单体应用系统数据迁移-园区租户的数据以单体应用为准                   **/

	/**
	 * 物业系统没有的园区/园区数据迁移
	 *
	 * @return
	 */
	@PostMapping("/single/agency")
	public R agency() {
		String agencyTable = "yc_agency";
		sqlContext.read().jdbc(this.GRID_SYSTEM_DB_URL, agencyTable, gridConnectionProperties).createOrReplaceTempView("yc_agency");
		Dataset<Row> agencyDataSet = sqlContext.sql("select * from yc_agency");
		JavaRDD<Row> agencyJavaRDD = agencyDataSet.toJavaRDD();
		JavaRDD<Agency> agencyMapRDD = agencyJavaRDD.map(new Function<Row, Agency>() {
			@Override
			public Agency call(Row row) throws Exception {
				Agency agency = new Agency();
				agency.setId(row.getLong(0));
				agency.setName(row.get(1) != null ? row.getString(1) : null);
				agency.setPersonName(row.get(4) != null ? row.getString(4) : null);
				agency.setPersonPhone(row.get(5) != null ? row.getString(5) : null);
				agency.setCreateUser(row.get(7) != null ? row.getLong(7) : null);
				agency.setCreateDept(row.get(8) != null ? row.getLong(8) : null);
//				agency.setCreateTime(row.get(9) != null ? row.getDate(9) : null);
				agency.setUpdateUser(row.get(10) != null ? row.getLong(10) : null);
//				agency.setUpdateTime(row.get(11) != null ? row.getDate(11) : null);
				agency.setStatus(row.get(12) != null ? row.getInt(12) : null);
				agency.setResidentialCount(row.get(13) != null ? row.getInt(13) : null);
				agency.setIsDeleted(row.get(14) != null ? row.getInt(14) : null);
				agency.setUnitCount(row.get(15) != null ? row.getInt(15) : null);
				agency.setRoomCount(row.get(16) != null ? row.getInt(16) : null);
				agency.setPersonCount(row.get(17) != null ? row.getInt(17) : null);
				agency.setCarCount(row.get(18) != null ? row.getInt(18) : null);
				agency.setRegionProvince(row.get(19) != null ? row.getString(19) : null);
				agency.setRegionCity(row.get(20) != null ? row.getString(20) : null);
				agency.setRegionArea(row.get(21) != null ? row.getString(21) : null);
				agency.setAddress(row.get(22) != null ? row.getString(22) : null);
				agency.setLongitude(row.get(23) != null ? row.getString(23) : null);
				agency.setLatitude(row.get(24) != null ? row.getString(24) : null);
				agency.setPic(row.get(25) != null ? row.getString(25) : null);
				agency.setRegionStreet(row.get(26) != null ? row.getString(26) : null);
				agency.setOrgPosition(row.get(27) != null ? row.getString(27) : null);
				agency.setTenantId(row.get(28) != null ? row.getString(28) : null);
				agency.setOldId(row.get(29) != null ? row.getString(29) : null);
				agency.setTenantPassword(row.get(31) != null ? row.getString(31) : null);
				agency.setBoundaryPath(row.get(32) != null ? row.getString(32) : null);
				return agency;
			}
		});
		List<Agency> agencyList = agencyMapRDD.collect();
		List<Agency> communityAgency = agencyList.stream().filter(agency -> "000000".equals(agency.getTenantId())).collect(Collectors.toList());
		List<Agency> parkAgency = agencyList.stream().filter(agency -> !"000000".equals(agency.getTenantId())).collect(Collectors.toList());

		//园区不需要根据回写gridId 字段
		parkAgency.stream().forEach(agency -> {
			Agency isExist = agencyClient.getById(agency.getId());
			if (isExist != null) {//更新
				agencyClient.update(agency);
			} else {
				agencyClient.save(agency);
			}
		});

		//园区需要根据物业系统的数据回写gridId字段
		communityAgency.stream().forEach(agency -> {
			if (agency.getOldId() != null) {
				Agency isExist = agencyClient.getById(Long.valueOf(agency.getOldId()));//判断物业系统是否有该园区
				if (isExist != null) {
					isExist.setGridId(agency.getId());
					agencyClient.update(isExist);
				} else {//数字园区有的园区而物业系统没有
					agency.setRemark("此园区物业系统没有记录");
					agencyClient.save(agency);
				}
			} else {
				agency.setRemark("此园区物业系统没有记录");
				agencyClient.submit(agency);
			}

		});

		return R.data(true);
	}

	/**
	 * 物业系统没有的小区/区域数据迁移
	 */

	@PostMapping("/single/residential")
	public R residential() {

		String residentialTable = "estate_residential";
		sqlContext.read().jdbc(GRID_SYSTEM_DB_URL, residentialTable, gridConnectionProperties).createOrReplaceTempView("estate_residential");
		Dataset<Row> residentialDataSet = sqlContext.sql("select * from estate_residential");
		JavaRDD<Row> residentialRDD = residentialDataSet.toJavaRDD();
		JavaRDD<Residential> residentialMapRdd = residentialRDD.map(new Function<Row, Residential>() {
			@Override
			public Residential call(Row row) throws Exception {
				Residential residential = new Residential();

				residential.setId(row.getLong(0));
				residential.setName(row.get(2) != null ? row.getString(2) : null);
				residential.setOrgPosition(row.get(7) != null ? row.getString(7) : null);
				residential.setRegionProvince(row.get(12) != null ? row.getString(12) : null);
				residential.setRegionCity(row.get(13) != null ? row.getString(13) : null);
				residential.setRegionArea(row.get(14) != null ? row.getString(14) : null);
				residential.setAddress(row.get(16) != null ? row.getString(16) : null);
				residential.setUnitCount(row.get(17) != null ? row.getInt(17) : null);
				residential.setRoomCount(row.get(18) != null ? row.getInt(18) : null);
				residential.setUserCount(row.get(19) != null ? row.getInt(19) : null);
				residential.setBuildingCount(row.get(20) != null ? row.getInt(20) : null);
				residential.setFloorCount(row.get(21) != null ? row.getInt(21) : null);
				residential.setPersonName(row.get(26) != null ? row.getString(26) : null);
				residential.setPersonPhone(row.get(27) != null ? row.getString(27) : null);
				residential.setRemark(row.get(28) != null ? row.getString(28) : null);
				residential.setStatus(row.get(39) != null ? row.getInt(39) : null);
				residential.setIsDeleted(row.get(40) != null ? row.getInt(40) : null);
				residential.setOldId(row.get(1) != null ? row.getLong(1) : null);

				residential.setAgencyId(row.get(41) != null ? row.getLong(41) : null);
				residential.setLatitude(row.get(43) != null ? row.getString(43) : null);
				residential.setLongitude(row.get(44) != null ? row.getString(44) : null);
				residential.setPic(row.get(45) != null ? row.getString(45).replace("http://139.9.103.171:1888/miniofile/", "") : null);
				residential.setTenantId(row.get(46) != null ? row.getString(46) : null);
				residential.setBoundaryPath(row.get(47) != null ? row.getString(47) : null);
				return residential;

			}
		});
		List<Residential> residentialList = residentialMapRdd.collect();
		List<Residential> communityResidentials = residentialList.stream().filter(residential -> "000000".equals(residential.getTenantId())).collect(Collectors.toList());
		List<Residential> parkResidentials = residentialList.stream().filter(residential -> !"000000".equals(residential.getTenantId())).collect(Collectors.toList());

		parkResidentials.stream().forEach(residential -> {
			Residential isExist = residentialClient.getById(residential.getId());
			if(isExist == null){
				residentialClient.save(residential);
			}else {
				residentialClient.update(residential);
			}

		});
		communityResidentials.forEach(residential -> {
			if(residential.getOldId() != null){
				Residential isExist = residentialClient.getById(residential.getOldId());
				if(isExist != null){
					isExist.setGridId(residential.getId());
					isExist.setTenantId("000000");
				}
				residentialClient.update(isExist);
			}else{
				System.out.println("没有oldId的小区"+ residential.toString());
			}


		});

		return R.data(true);
	}

	/**
	 * 物业系统没有的楼栋数据迁移
	 */

	@PostMapping("/single/building")
	public R building() {

		List<Residential> residentialList = residentialClient.getParkResidential();
		residentialList.forEach(residential -> {
			String buildingTable = "estate_building";
			sqlContext.read().jdbc(GRID_SYSTEM_DB_URL, buildingTable, gridConnectionProperties).createOrReplaceTempView("estate_building");
			Dataset<Row> buildingDataSet = sqlContext.sql("select * from estate_building where residential_id =" + residential.getId());
			JavaRDD<Row> buildingRDD = buildingDataSet.toJavaRDD();
			JavaRDD<Building> buildingJavaRDD = buildingRDD.map(new Function<Row, Building>() {
				@Override
				public Building call(Row row) throws Exception {
					Building building = new Building();
					building.setId(row.getLong(0));
					building.setName(row.get(1) != null ? row.getString(1) : null);
					building.setResidentialId(row.get(2) != null ? row.getLong(2) : null);
					building.setResidentialName(row.get(3) != null ? row.getString(3) : null);
					building.setUnitCount(row.get(4) != null ? row.getInt(4) : null);
					building.setFloorCount(row.get(5) != null ? row.getInt(5) : null);
					building.setRoomCount(row.get(6) != null ? row.getInt(6) : null);
					building.setUserCount(row.get(7) != null ? row.getInt(7) : null);
					building.setStatus(row.get(13) != null ? row.getInt(13) : null);
					building.setIsDeleted(row.get(14) != null ? row.getInt(14) : null);
					building.setOldId(row.get(15) != null ? row.getLong(15) : null);
					building.setPic(row.get(16) != null ? row.getString(16).replace("http://139.9.103.171:1888/miniofile/", "") : null);
					building.setOrgPosition(row.get(17) != null ? row.getString(17) : null);
					building.setTenantId(row.get(18) != null ? row.getString(18) : null);
					building.setAgencyName(residential.getAgencyName());
					building.setAgencyId(residential.getAgencyId());
					return building;
				}
			});
			List<Building> buildingList = buildingJavaRDD.collect();
			buildingList.forEach(building -> {
				if (residential.getRemark().contains("此小区物业系统没有记录")) {
					building.setRemark("此楼栋物业系统没有记录");
				}
				Building isExist = buildingClient.getById(building.getId());
				if(isExist == null){
					buildingClient.save(building);
				}else {
					buildingClient.update(building);
				}

			});

		});
		return R.data(true);
	}

	/**
	 * 物业系统没有的单元数据迁移
	 */

	@PostMapping("/single/unit")
	public R unit() {
		List<Residential> residentialList = residentialClient.getParkResidential();
		residentialList.forEach(residential -> {
			String unitTable = "estate_unit";
			sqlContext.read().jdbc(GRID_SYSTEM_DB_URL, unitTable, gridConnectionProperties).createOrReplaceTempView("estate_unit");
			Dataset<Row> unitDataSet = sqlContext.sql("select * from estate_unit where residential_id =" + residential.getId());
			JavaRDD<Row> unitRDD = unitDataSet.toJavaRDD();
			JavaRDD<Unit> unitJavaRDD = unitRDD.map(new Function<Row, Unit>() {
				@Override
				public Unit call(Row row) throws Exception {
					Unit unit = new Unit();
					unit.setId(row.getLong(0));
					unit.setName(row.get(1) != null ? row.getString(1) : null);
					unit.setAgencyName(residential.getAgencyName());
					unit.setAgencyId(residential.getAgencyId());
					unit.setResidentialId(residential.getId());
					unit.setResidentialName(residential.getName());
					unit.setOrgPosition(row.get(8) != null ? row.getString(8) : null);
					unit.setPersonName(row.get(10) != null ? row.getString(10) : null);
					unit.setPersonTel(row.get(11) != null ? row.getString(11) : null);
					unit.setBuiltYear(row.get(12) != null ? row.getString(12) : null);
					unit.setIncludingElevator(row.get(14) != null ? row.getString(14) : null);
					unit.setRoomCount(row.get(15) != null ? row.getInt(15) : null);
					unit.setFloorCount(row.get(16) != null ? row.getInt(16) : null);
					unit.setUserCount(row.get(17) != null ? row.getInt(17) : null);
					unit.setStatus(row.get(20) != null ? row.getInt(20) : null);
					unit.setIsDeleted(row.get(31) != null ? row.getInt(31) : null);
					unit.setOldId(row.get(32) != null ? row.getLong(32) : null);
					unit.setBuildingId(row.get(33) != null ? row.getLong(33) : null);
					unit.setBuildingName(row.get(34) != null ? row.getString(34) : null);
					unit.setPic(row.get(35) != null ? row.getString(35).replace("http://139.9.103.171:1888/miniofile/", "") : null);
					unit.setTenantId(row.get(36) != null ? row.getString(36) : null);
					return unit;
				}
			});
			List<Unit> unitList = unitJavaRDD.collect();
			unitList.forEach(unit -> {
				if (residential.getRemark().contains("此小区物业系统没有记录")) {
					unit.setRemark("此单元物业系统没有记录");
				}
				if(unitClient.getById(unit.getId()) == null){
					unitClient.save(unit);
				}else{
					unitClient.update(unit);
				}

			});

		});
		return R.data(true);
	}

	/**
	 * 物业系统没有的楼层数据迁移
	 */

	@PostMapping("/single/floor")
	public R floor() {

		List<Residential> residentialList = residentialClient.getParkResidential();
		residentialList.forEach(residential -> {
			String floorTable = "estate_floor";
			sqlContext.read().jdbc(GRID_SYSTEM_DB_URL, floorTable, gridConnectionProperties).createOrReplaceTempView("estate_floor");
			Dataset<Row> floorDataSet = sqlContext.sql("select * from estate_floor where residential_id =" + residential.getId());
			JavaRDD<Row> floorRDD = floorDataSet.toJavaRDD();
			JavaRDD<Floor> floorJavaRDD = floorRDD.map(new Function<Row, Floor>() {
				@Override
				public Floor call(Row row) throws Exception {
					Floor floor = new Floor();
					floor.setId(row.getLong(0));
					floor.setName(row.get(1) != null ? row.getString(1) : null);
					floor.setPersonName(row.get(6) != null ? row.getString(6) : null);
					floor.setPersonTel(row.get(7) != null ? row.getString(7) : null);
					floor.setRoomCount(row.get(11) != null ? row.getInt(11) : 0);
					floor.setUserCount(row.get(12) != null ? row.getInt(12) : 0);
					floor.setUnitId(row.get(21) != null ? row.getLong(21) : null);
					floor.setIsDeleted(row.get(29) != null ? row.getInt(29) : 0);

					return floor;
				}
			});
			List<Floor> floorList = floorJavaRDD.collect();
			floorList.forEach(floor -> {
				Unit unit = unitClient.getById(floor.getUnitId());
				if(unit != null){
					floor.setUnitName(unit.getName());
					floor.setBuildingName(unit.getBuildingName());
					floor.setBuildingId(unit.getBuildingId());
					floor.setResidentialName(unit.getResidentialName());
					floor.setResidentialId(unit.getResidentialId());
					floor.setAgencyName(unit.getAgencyName());
					floor.setAgencyId(unit.getAgencyId());
					floor.setTenantId(unit.getTenantId());
					floor.setOrgPosition(unit.getOrgPosition() + "/"+floor.getId());
				}else {
					floor.setRemark("对应的单元找不到");
				}

				if(floorClient.getById(floor.getId()) == null){
					floorClient.save(floor);
				}else{
					floorClient.update(floor);
				}

			});

		});
		return R.data(true);
	}

	/**
	 * 物业系统没有的房间数据迁移
	 */

	@PostMapping("/single/room")
	public R room() {
		//先查出物业系统没有的园区和园区租户下的小区或区域


		List<Residential> residentialList = residentialClient.getParkResidential();
		residentialList.forEach(residential -> {
			String roomTable = "estate_room";
			sqlContext.read().jdbc(GRID_SYSTEM_DB_URL, roomTable, gridConnectionProperties).createOrReplaceTempView("estate_room");
			Dataset<Row> roomDataSet = sqlContext.sql("select * from estate_room where residential_id =" + residential.getId());
			JavaRDD<Row> roomRDD = roomDataSet.toJavaRDD();
			JavaRDD<Room> roomJavaRDD = roomRDD.map(new Function<Row, Room>() {
				@Override
				public Room call(Row row) throws Exception {
					Room room = new Room();
					room.setId(row.getLong(0));
					room.setName(row.get(1) != null ? row.getString(1) : null);
					room.setArea(row.get(7) != null ? row.getDecimal(7) : null);
					room.setHouseType(row.get(8) != null ? row.getString(8) : null);
					room.setOrientation(row.get(11) != null ? row.getString(11) : null);
					room.setUserCount(row.get(12) != null ? row.getInt(12) : null);
					return room;
				}
			});
			List<Room> roomList = roomJavaRDD.collect();
			roomList.forEach(room -> {
				Floor floor = floorClient.getById(room.getFloorId());
				if(floor != null){
					room.setFloorName(floor.getName());
					room.setUnitName(floor.getUnitName());
					room.setUnitId(floor.getUnitId());
					room.setBuildingName(floor.getBuildingName());
					room.setBuildingId(floor.getBuildingId());
					room.setResidentialId(floor.getResidentialId());
					room.setResidentialName(floor.getResidentialName());
					room.setAgencyId(floor.getAgencyId());
					room.setAgencyName(floor.getAgencyName());
					room.setTenantId(floor.getTenantId());
					room.setOrgPosition(floor.getOrgPosition()+"/"+room.getId());
				}
				if(roomClient.getById(room.getId()) == null){
					roomClient.save(room);
				}else{
					roomClient.update(room);
				}

			});

		});
		return R.data(true);
	}

}
