/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.estate.wrapper;

import org.springblade.community.entity.*;
import org.springblade.community.feign.*;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.estate.entity.UserAccessPermissions;
import org.springblade.estate.vo.UserAccessPermissionsVO;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2022-01-07
 */
public class UserAccessPermissionsWrapper extends BaseEntityWrapper<UserAccessPermissions, UserAccessPermissionsVO>  {

	private static IAgencyClient agencyClient;
	private static IResidentialClient residentialClient;
	private static IBuildingClient buildingClient;
	private static IUnitClient unitClient;
	private static IFloorClient floorClient;
	private static IRoomClient roomClient;

	static {
		agencyClient = SpringUtil.getBean(IAgencyClient.class);
		residentialClient = SpringUtil.getBean(IResidentialClient.class);
		buildingClient = SpringUtil.getBean(IBuildingClient.class);
		unitClient = SpringUtil.getBean(IUnitClient.class);
		floorClient = SpringUtil.getBean(IFloorClient.class);
		roomClient = SpringUtil.getBean(IRoomClient.class);
		floorClient = SpringUtil.getBean(IFloorClient.class);
	}

	public static UserAccessPermissionsWrapper build() {
		return new UserAccessPermissionsWrapper();
 	}

	@Override
	public UserAccessPermissionsVO entityVO(UserAccessPermissions userAccessPermissions) {
		UserAccessPermissionsVO userAccessPermissionsVO = Objects.requireNonNull(BeanUtil.copy(userAccessPermissions, UserAccessPermissionsVO.class));
		if (userAccessPermissionsVO.getAgencyId()!=null) {
			Agency agency = agencyClient.getById(userAccessPermissionsVO.getAgencyId());
			if (agency!=null) {
				userAccessPermissionsVO.setAgencyName(agency.getName());
			}
		}

		if(userAccessPermissionsVO.getResidentialId() != null){
			Residential residential = residentialClient.getById(userAccessPermissionsVO.getResidentialId());
			if(residential != null){
				userAccessPermissionsVO.setResidentialName(residential.getName());
			}
		}
		if(userAccessPermissionsVO.getBuildingId() != null){
			Building building = buildingClient.getById(userAccessPermissionsVO.getBuildingId());
			if(building != null){
				userAccessPermissionsVO.setBuildingName(building.getName());
			}
		}
		if(userAccessPermissionsVO.getUnitId() != null){
			Unit unit = unitClient.getById(userAccessPermissionsVO.getUnitId());
			if(unit != null){
				userAccessPermissionsVO.setUnitName(unit.getName());
			}
		}
		if(userAccessPermissionsVO.getFloorId() != null){
			Floor floor = floorClient.getById(userAccessPermissionsVO.getFloorId());
			if(floor != null){
				userAccessPermissionsVO.setFloorName(floor.getName());
			}
		}
		if(userAccessPermissionsVO.getRoomId() != null){
			Room room = roomClient.getById(userAccessPermissionsVO.getRoomId());
			if(room != null){
				userAccessPermissionsVO.setRoomName(room.getName());
			}
		}

		return userAccessPermissionsVO;
	}

}
