package org.springblade.community.timedtask;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.community.entity.*;
import org.springblade.community.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Map;

@Component
@EnableScheduling
@RestController
@RequestMapping("sync")
public class TimedTask {

	@Autowired
	private IAgencyService agencyService;

	@Autowired
	private IResidentialService residentialService;

	@Autowired
	private IBuildingService buildingService;

	@Autowired
	private IUnitService unitService;

	@Autowired
	private IFloorService floorService;

	@Autowired
	private IRoomService roomService;


	/**
	 * 同步小区数据
	 */
//	@Scheduled(cron =  "0 0 * * * ? ")
	@GetMapping("/syncResidential")
	public  void syncResidential() {
		System.out.println("定时同步小区");
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity responseEntity = null;
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		String url = "http://localhost:9083/grid/estate/syncResidential";
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, reference);

		List<Map> map1 = (List<Map>) responseEntity.getBody();

		map1.parallelStream().forEach(map -> {
			Residential residential = new Residential();
			if(map.get("longitude")!=null){
				residential.setLongitude(map.get("longitude").toString());
			}
			if(map.get("latitude")!=null){
				residential.setLatitude(map.get("latitude").toString());
			}
//			if(map.get("propertyCompanyId") != null){
//				residential.setEstatePid(Long.valueOf(map.get("propertyCompanyId").toString()));
//			}
//			if(map.get("propertyName") != null){
//				residential.setEstateName(map.get("propertyName").toString());
//			}
//			if(map.get("propertyPhone") != null){
//				residential.setEstatePhone(map.get("propertyPhone").toString());
//			}
			residential.setName(map.get("name").toString());
			residential.setRegionProvince(map.get("regionProvince").toString());
			residential.setRegionCity(map.get("regionCity").toString());
			residential.setRegionArea(map.get("regionArea").toString());
			if(map.get("address")!=null){
				residential.setAddress(map.get("address").toString());
			}
			residential.setBuildingCount((Integer) map.get("buildingCount"));
			residential.setUnitCount((Integer) map.get("unitCount"));
			residential.setFloorCount((Integer) map.get("floorCount"));
			residential.setRoomCount((Integer) map.get("roomCount"));
			residential.setUserCount((Integer) map.get("userCount"));
			residential.setOldId(Long.valueOf(map.get("id").toString()));
			Residential one = residentialService.getOne(new QueryWrapper<>(new Residential()).lambda().eq(Residential::getOldId, residential.getOldId()));
			if(one != null){
				residential.setId(one.getId());
				residentialService.updateById(residential);
			}else {
				residentialService.save(residential);
			}
		});
	}

	/**
	 * 定时同步单元
	 */
//	@Scheduled(cron =  "0 0 0 * * ? ")
	@PostMapping("/syncUnit/{residentialOldId}")
	public  void syncUnit(@PathVariable("residentialOldId")Long residentialOldId) {
		System.out.println("同步单元");
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity responseEntity = null;
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		String url = "http://139.9.103.171:9083/grid/unit/syncUnit/"+residentialOldId;
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, reference);

		List<Map> map1 = (List<Map>) responseEntity.getBody();

		map1.parallelStream().forEach(map->{

			Unit unit = new Unit();
			unit.setName(map.get("name").toString());
			//单元关联的小区楼栋id改为新平台id,\

			Residential residential = residentialService.getOne(new QueryWrapper<>(new Residential()).lambda().eq(Residential::getOldId,Long.valueOf(map.get("residentialId").toString())));
			if(residential !=null){
				unit.setResidentialId(residential.getId());
				unit.setResidentialName(residential.getName());
				unit.setAgencyId(residential.getAgencyId());
			}
			Building building = buildingService.getOne(new QueryWrapper<>(new Building()).lambda().eq(Building::getOldId,map.get("buildingId").toString()));
			if(building != null){
				unit.setBuildingId(building.getId());
				unit.setBuildingName(building.getName());
			}
//			unit.setResidentialId(Long.valueOf(map.get("residentialId").toString()));
//			unit.setResidentialName(map.get("residentialName").toString());
			unit.setRoomCount((Integer) map.get("roomCount"));
			unit.setUserCount((Integer)map.get("userCount"));
			unit.setFloorCount((Integer) map.get("floorCount"));
			unit.setOldId(Long.valueOf(map.get("id").toString()));
			Unit one = unitService.getOne(new QueryWrapper<>(new Unit()).lambda().eq(Unit::getOldId, unit.getOldId()));
			if(one != null){
				unit.setId(one.getId());
				unit.setOrgPosition(building.getOrgPosition()+"/"+unit.getId());
				unitService.updateById(unit);
			}else {
				unitService.save(unit);
				unit.setOrgPosition(building.getOrgPosition()+"/"+unit.getId());
				unitService.updateById(unit);
			}
		});
	}

	/**
	 * 定时同步楼层
	 */
	@PostMapping("/syncFloor/{residentialOldId}")
	public  void syncFloor(@PathVariable("residentialOldId")Long residentialOldId) {
		System.out.println("同步楼层");
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity responseEntity = null;
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		String url = "http://139.9.103.171:9083/grid/floor/syncFloor/"+residentialOldId;
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, reference);

		List<Map> map1 = (List<Map>) responseEntity.getBody();

		map1.parallelStream().forEach(map -> {
			Floor floor = new Floor();
			floor.setName(map.get("name").toString());
			//单元关联的小区楼栋id改为新平台id,\
			Residential residential = residentialService.getOne(new QueryWrapper<>(new Residential()).lambda().eq(Residential::getOldId,Long.valueOf(map.get("residentialId").toString())));
			if(residential !=null){
				floor.setResidentialId(residential.getId());
				floor.setResidentialName(residential.getName());
				floor.setAgencyId(residential.getAgencyId());
			}
			Building building = buildingService.getOne(new QueryWrapper<>(new Building()).lambda().eq(Building::getOldId,map.get("buildingId").toString()));
			if(building != null){
				floor.setBuildingId(building.getId());
				floor.setBuildingName(building.getName());
			}
			Unit unit = unitService.getOne(new QueryWrapper<>(new Unit()).lambda().eq(Unit::getOldId,Long.valueOf(map.get("unitId").toString())));
			if(unit != null){
				floor.setUnitId(unit.getId());
				floor.setUnitName(unit.getName());
			}
			floor.setRoomCount((Integer) map.get("roomCount"));
			floor.setUserCount((Integer)map.get("userCount"));
			floor.setOldId(map.get("id").toString());

			Floor one = floorService.getOne(new QueryWrapper<>(new Floor()).lambda().eq(Floor::getOldId, floor.getOldId()));
			if(one != null){
				floor.setId(one.getId());
				floor.setOrgPosition(unit.getOrgPosition()+"/"+floor.getId());
				floorService.updateById(floor);
			}else {
				floorService.save(floor);
				floor.setOrgPosition(unit.getOrgPosition()+"/"+floor.getId());
				floorService.updateById(floor);
			}
		});
	}

	/**
	 * 定时同步房间
	 */
//	@Scheduled(cron =  "0 0 0 * * ? ")
	@PostMapping("/syncRoom/{residentialOldId}")
	public  void syncRoom(@PathVariable("residentialOldId")Long residentialOldId) {
		System.out.println("同步房间");
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity responseEntity = null;
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		String url = "http://139.9.103.171:9083/grid/room/syncRoom/"+residentialOldId;
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, reference);

		List<Map> map1 = (List<Map>) responseEntity.getBody();

		map1.parallelStream().forEach(map -> {
			Room room = new Room();
			room.setName(map.get("name").toString());
			//单元关联的小区楼栋id改为新平台id,\
			Residential residential = residentialService.getOne(new QueryWrapper<>(new Residential()).lambda().eq(Residential::getOldId,Long.valueOf(map.get("residentialId").toString())));
			if(residential !=null){
				room.setResidentialId(residential.getId());
				room.setResidentialName(residential.getName());
				room.setAgencyId(residential.getAgencyId());
			}
			Building building = buildingService.getOne(new QueryWrapper<>(new Building()).lambda().eq(Building::getOldId,map.get("buildingId").toString()));
			if(building != null){
				room.setBuildingId(building.getId());
				room.setBuildingName(building.getName());
			}
			Unit unit = unitService.getOne(new QueryWrapper<>(new Unit()).lambda().eq(Unit::getOldId,Long.valueOf(map.get("unitId").toString())));
			if(unit != null){
				room.setUnitId(unit.getId());
				room.setUnitName(unit.getName());
			}
			Floor floor = floorService.getOne(new QueryWrapper<>(new Floor()).lambda().eq(Floor::getOldId,map.get("floorId").toString()));
			if(floor != null){
				room.setFloorId(floor.getId());
				room.setFloorName(floor.getName());
			}
			room.setOldId(Long.valueOf(map.get("id").toString()));
			room.setUserCount(Integer.valueOf(map.get("personNumber").toString()));
			Room one = roomService.getOne(new QueryWrapper<>(new Room()).lambda().eq(Room::getOldId, room.getOldId()));
			if(one != null){
				room.setId(one.getId());
				room.setOrgPosition(floor.getOrgPosition()+"/"+room.getId());
				roomService.updateById(room);
			}else {
				roomService.save(room);
				room.setOrgPosition(floor.getOrgPosition()+"/"+room.getId());
				roomService.updateById(room);
			}

		});
	}

}
