/*
 *      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.party.wrapper;

import org.springblade.common.constant.Constants;
import org.springblade.community.entity.Agency;
import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IAgencyClient;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.enterprise.entity.Enterprise;
import org.springblade.enterprise.feign.IEnterpriseClient;
import org.springblade.party.entity.Branch;
import org.springblade.party.entity.BranchRelation;
import org.springblade.party.service.IBranchService;
import org.springblade.party.vo.BranchRelationVO;
import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-04-19
 */
public class BranchRelationWrapper extends BaseEntityWrapper<BranchRelation, BranchRelationVO>  {

	private IAgencyClient agencyClient;
	private IResidentialClient residentialClient;
	private IEnterpriseClient enterpriseClient;
	private IBranchService branchService;

	private BranchRelationWrapper(IAgencyClient agencyClient,IResidentialClient residentialClient,IEnterpriseClient enterpriseClient,IBranchService branchService){
		this.agencyClient = agencyClient;
		this.residentialClient = residentialClient;
		this.enterpriseClient = enterpriseClient;
		this.branchService = branchService;
	}

	public static BranchRelationWrapper build(IAgencyClient agencyClient,IResidentialClient residentialClient,IEnterpriseClient enterpriseClient,IBranchService branchService) {
		return new BranchRelationWrapper(agencyClient,residentialClient,enterpriseClient,branchService);
 	}

	@Override
	public BranchRelationVO entityVO(BranchRelation branchRelation) {
		BranchRelationVO branchRelationVO = Objects.requireNonNull(BeanUtil.copy(branchRelation, BranchRelationVO.class));
		if (Constants.BRANCH_RELATE_AGENCY == branchRelation.getResourceType()){//园区
			Agency agency = new Agency();
			agency.setId(branchRelation.getResourceId());
			branchRelationVO.setObject(agencyClient.detail(agency));
		}else if (Constants.BRANCH_RELATE_RESIDENTIAL == branchRelation.getResourceType()){//小区
			Residential residential = new Residential();
			residential.setId(branchRelation.getResourceId());
			branchRelationVO.setObject(residentialClient.detail(residential));
		}else if (Constants.BRANCH_RELATE_PARK == branchRelation.getResourceType()){//园区
			Agency agency = new Agency();
			agency.setId(branchRelation.getBranchId());
			branchRelationVO.setObject(agencyClient.detail(agency));
		}else if (Constants.BRANCH_RELATE_ENTERPRISE == branchRelation.getResourceType()){//企业
			Enterprise enterprise = new Enterprise();
			enterprise.setId(branchRelation.getResourceId());
			branchRelationVO.setObject(enterpriseClient.detail(enterprise));
		}

		Branch branch = branchService.getById(branchRelation.getBranchId());
		branchRelationVO.setBranch(branch);
		return branchRelationVO;
	}

}
