/*
 *      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.person.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.person.entity.HouseUser;
import org.springblade.person.vo.HouseUserVO;
import org.springblade.system.expand.feign.IConfClient;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-04-14
 */
public class HouseUserWrapper extends BaseEntityWrapper<HouseUser, HouseUserVO>  {

	public static IConfClient confClient;
	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 {
		confClient = SpringUtil.getBean(IConfClient.class);
		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);
	}

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

	@Override
	public HouseUserVO entityVO(HouseUser houseUser) {
		HouseUserVO houseUserVO = Objects.requireNonNull(BeanUtil.copy(houseUser, HouseUserVO.class));
		houseUserVO.setFaceUrl(confClient.addPrefix(houseUser.getFaceUrl()));
		StringBuilder sb = new StringBuilder();
		StringBuilder address = new StringBuilder();
		if(houseUser.getResidentialId() != null){
			Agency agency = agencyClient.getById(houseUser.getAgencyId());
			if(agency != null){
				sb.append(agency.getName());
				houseUserVO.setAgencyName(agency.getName());
				sb.append("|");
			}
			Residential residential = residentialClient.getById(houseUser.getResidentialId());
			if(residential != null){
				sb.append(residential.getName());
				houseUserVO.setResidentialName(residential.getName());
				address.append(residential.getName());
			}
			Building building = buildingClient.getById(houseUser.getBuildingId());
			if(building != null){
				sb.append("|").append(building.getName());
				houseUserVO.setBuildingName(building.getName());
				address.append("-").append(building.getName());
			}
			Unit unit = unitClient.getById(houseUser.getUnitId());
			if(unit != null){
				sb.append("|").append(unit.getName());
				houseUserVO.setUnitName(unit.getName());
				address.append("-").append(unit.getName());
			}
			Floor floor = floorClient.getById(houseUser.getFloorId());
			if(floor != null){
				sb.append("|").append(floor.getName());
				houseUserVO.setFloorName(floor.getName());
			}
			Room room = roomClient.getById(houseUser.getRoomId());
			if(room != null){
				sb.append("|").append(room.getName());
				houseUserVO.setRoomName(room.getName());
				address.append("-").append(room.getName());
			}
		}
		houseUserVO.setLiveAddress(sb.toString());
		houseUserVO.setAddress(address.toString());

		return houseUserVO;
	}

}
