/*
 *      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.Building;
import org.springblade.community.entity.Residential;
import org.springblade.community.entity.Room;
import org.springblade.community.entity.Unit;
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.ApplyUser;
import org.springblade.estate.vo.ApplyUserVO;
import org.springblade.system.expand.feign.IConfClient;

import java.util.Objects;

/**
 * 申请用户表---房屋成员包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-04-23
 */
public class ApplyUserWrapper extends BaseEntityWrapper<ApplyUser, ApplyUserVO>  {

	private static IConfClient confClient;
	private static IResidentialClient residentialClient;
	private static IBuildingClient buildingClient;
	private static IUnitClient unitClient;
	private static IRoomClient roomClient;

	static {
		confClient = SpringUtil.getBean(IConfClient.class);
		residentialClient = SpringUtil.getBean(IResidentialClient.class);
		buildingClient = SpringUtil.getBean(IBuildingClient.class);
		unitClient = SpringUtil.getBean(IUnitClient.class);
		roomClient = SpringUtil.getBean(IRoomClient.class);
	}

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


	@Override
	public ApplyUserVO entityVO(ApplyUser applyUser) {
		ApplyUserVO applyUserVO = Objects.requireNonNull(BeanUtil.copy(applyUser, ApplyUserVO.class));
		applyUserVO.setPassportImgUri(confClient.addPrefix(applyUser.getPassportImgUri()));
		applyUserVO.setEntryImgUri(confClient.addPrefix(applyUser.getEntryImgUri()));
		applyUserVO.setFaceUrl(confClient.addPrefix(applyUser.getFaceUrl()));
		if(applyUser.getResidentialId() != null){
			Residential residential = residentialClient.getById(applyUser.getResidentialId());
			applyUserVO.setResidentialName(residential.getName());
		}
		if(applyUser.getBuildingId() != null){
			Building building = buildingClient.getById(applyUser.getBuildingId());
			applyUserVO.setBuildingName(building.getName());
		}
		if(applyUser.getUnitId() != null){
			Unit unit = unitClient.getById(applyUser.getUnitId());
			applyUserVO.setUnitName(unit.getName());
		}
		if(applyUser.getRoomId() != null){
			Room room = roomClient.getById(applyUser.getRoomId());
			applyUserVO.setRoomName(room.getName());
		}
		return applyUserVO;
	}

}
