package org.springblade.community.feign;

import lombok.AllArgsConstructor;
import org.springblade.community.entity.*;
import org.springblade.community.service.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created By lidexi in 2021/4/27
 **/
@AllArgsConstructor
@RestController
@RequestMapping("/feign/community")
public class CommunityClient implements ICommunityClient{


	private final IAgencyService agencyService;
	private final IResidentialService residentialService;
	private final IBuildingService buildingService;
	private final IUnitService unitService;
	private final IFloorService floorService;
	private final IRoomService roomService;
	  /**
	  	 * 新增住户时为园区-小区-楼栋-单元-楼层-房间加人数1
	   */
	@Override
	@GetMapping("/addPersonNum")
	public boolean addPersonNum(@RequestParam(value = "agencyId", required = false) Long agencyId, @RequestParam(value = "residentialId", required = false) Long residentialId, @RequestParam(value = "buildingId", required = false) Long buildingId, @RequestParam(value = "unitId", required = false) Long unitId, @RequestParam(value = "floorId", required = false) Long floorId, @RequestParam(value = "roomId") Long roomId) {
		//园区
		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);
			}
		}

		return true;
	}

	/**
	 * 新增住户时为园区-小区-楼栋-单元-楼层-房间加人数1
	 */
	@Override
	@GetMapping("/subPersonNum")
	public boolean subPersonNum(@RequestParam(value = "agencyId", required = false) Long agencyId, @RequestParam(value = "residentialId", required = false) Long residentialId, @RequestParam(value = "buildingId", required = false) Long buildingId, @RequestParam(value = "unitId", required = false) Long unitId, @RequestParam(value = "floorId", required = false) Long floorId, @RequestParam(value = "roomId") Long roomId) {
		//园区
		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);
			}
		}

		return true;
	}
}
