package org.springblade.community.eventbus;

import com.google.common.eventbus.Subscribe;
import com.nepxion.eventbus.annotation.EventBus;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.community.entity.*;
import org.springblade.community.feign.IBuildingClient;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.community.service.*;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.stereotype.Service;


/**
 * Created By lidexi in 2021/4/27
 * 园区子线程接收异步事件处理类
 **/
@EventBus(identifier = "communitySubscriber")
@Service
@RequiredArgsConstructor
public class CommunitySubscriber {
	private static final Logger LOG = LoggerFactory.getLogger(CommunitySubscriber.class);

	private final IAgencyService agencyService;
	private final IResidentialService residentialService;
	private final IBuildingService buildingService;
	private final IUnitService unitService;
	private final IFloorService floorService;
	private final IRoomService roomService;

	/**
	 *
	 * @param
	 */
	@Subscribe
	public void addPersonNum(Long agencyId,Long residentialId,Long buildingId,Long unitId,Long floorId,Long roomId) throws InterruptedException {

		//园区
		if(agencyId != null){
			Agency agency = agencyService.getById(agencyId);
			if(agency != null){
				agency.setPersonCount(agency.getPersonCount() == null ? 1 : agency.getPersonCount()+1);
				agencyService.updateById(agency);
			}
		}

		//小区
		if(residentialId != null){
			Residential residential = residentialService.getById(residentialId);
			if(residential != null){
				residential.setUserCount(residential.getUserCount() == null ? 1 : residential.getUserCount()+1);
				residentialService.updateById(residential);
			}
		}



		//楼栋
		if(buildingId != null){
			Building building = buildingService.getById(buildingId);
			if(building != null){
				building.setUserCount(building.getUserCount() == null ? 1 : building.getUserCount()+1);
				buildingService.updateById(building);
			}
		}

		//单元
		if(unitId != null){
			Unit unit = unitService.getById(unitId);
			if(unit != null){
				unit.setUserCount(unit.getUserCount() == null ? 1 : unit.getUserCount()+1);
				unitService.updateById(unit);
			}
		}

		//楼层
		if(floorId != null){
			Floor floor = floorService.getById(floorId);
			if(floor != null){
				floor.setUserCount(floor.getUserCount() == null ? 1 : floor.getUserCount()+1);
				floorService.updateById(floor);
			}
		}

		//房间
		if(roomId != null){
			Room room = roomService.getById(roomId);
			if(room != null){
				room.setUserCount(room.getUserCount() == null ? 1 : room.getUserCount()+1);
				roomService.updateById(room);
			}
		}



		Thread.sleep(2000);
		System.out.println("--------------------------1----------------------");


	}


}
