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

import org.springblade.common.Constants;
import org.springblade.community.agency.entity.Agency;
import org.springblade.community.agency.service.IAgencyService;
import org.springblade.community.enterprise.service.IEnterpriseService;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.estate.banners.service.IBannersService;
import org.springblade.party.branch.entity.Branch;
import org.springblade.party.branch.service.IBranchService;
import org.springblade.party.relation.entity.BranchRelation;
import org.springblade.party.relation.vo.BranchRelationVO;

import java.util.Objects;

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

	private IAgencyService agencyService;
	private IResidentialService residentialService;
	private IEnterpriseService enterpriseService;
	private IBranchService branchService;

	public BranchRelationWrapper(IAgencyService agencyService, IResidentialService residentialService, IEnterpriseService enterpriseService,IBranchService branchService) {
		this.agencyService = agencyService;
		this.residentialService = residentialService;
		this.enterpriseService = enterpriseService;
		this.branchService = branchService;
	}


	public static BranchRelationWrapper build(IAgencyService agencyService, IResidentialService residentialService, IEnterpriseService enterpriseService, IBranchService branchService) {
		return new BranchRelationWrapper(agencyService,residentialService,enterpriseService,branchService);
	}



	@Override
	public BranchRelationVO entityVO(BranchRelation branchRelation) {
		BranchRelationVO branchRelationVO = Objects.requireNonNull(BeanUtil.copy(branchRelation, BranchRelationVO.class));

		if (Constants.BRANCH_RELATE_AGENCY == branchRelation.getResourceType()) {//社区
			branchRelationVO.setObject(agencyService.getById(branchRelation.getResourceId()));
		} else if (Constants.BRANCH_RELATE_RESIDENTIAL == branchRelation.getResourceType()) {//小区
			branchRelationVO.setObject(residentialService.getById(branchRelation.getResourceId()));
		} else if (Constants.BRANCH_RELATE_PARK == branchRelation.getResourceType()) {//
			branchRelationVO.setObject(agencyService.getById(branchRelation.getBranchId()));
		} else if (Constants.BRANCH_RELATE_ENTERPRISE == branchRelation.getResourceType()) {//企业
			branchRelationVO.setObject(enterpriseService.getById(branchRelation.getResourceId()));
		}

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

}
