/*
 *      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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.community.vo.AgencyVO;
import org.springblade.community.vo.ResidentialVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.enterprise.entity.Enterprise;
import org.springblade.enterprise.feign.IEnterpriseClient;
import org.springblade.enterprise.vo.EnterpriseVO;
import org.springblade.party.entity.Branch;
import org.springblade.party.entity.BranchRelation;
import org.springblade.party.service.IBranchRelationService;
import org.springblade.party.vo.BranchVO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-03-30
 */
public class BranchWrapper extends BaseEntityWrapper<Branch, BranchVO>  {

	private IBranchRelationService branchRelationService;
	private IResidentialClient residentialClient;
	private IAgencyClient agencyClient;
	private IEnterpriseClient enterpriseClient;



//	public static BranchWrapper build() {
//		return new BranchWrapper();
// 	}

	public BranchWrapper(IBranchRelationService branchRelationService,IResidentialClient residentialClient,IAgencyClient agencyClient,IEnterpriseClient enterpriseClient){
		this.branchRelationService = branchRelationService;
		this.agencyClient = agencyClient;
		this.residentialClient = residentialClient;
		this.enterpriseClient = enterpriseClient;
	}

	public static BranchWrapper build(IBranchRelationService branchRelationService,IResidentialClient residentialClient,IAgencyClient agencyClient,IEnterpriseClient enterpriseClient){
		return new BranchWrapper(branchRelationService, residentialClient, agencyClient, enterpriseClient);
	}

	@Override
	public BranchVO entityVO(Branch branch) {
		BranchVO branchVO = Objects.requireNonNull(BeanUtil.copy(branch, BranchVO.class));
//		List<BranchRelation> branchRelationList = branchRelationService.getBaseMapper().selectList(new QueryWrapper<BranchRelation>().eq("branch_id",branch.getId()));
//		branchVO.setRelationList(branchRelationList);
		if (branch!=null){
				List<BranchRelation> branchRelationList = branchRelationService.getBaseMapper().selectList(new QueryWrapper<BranchRelation>().eq("branch_id",branch.getId()));
			if (branchRelationList!=null && branchRelationList.size() > 0){
				List<String> resourceIds = new ArrayList<>();
				List<String> resourceNames = new ArrayList<>();
				for(BranchRelation branchRelation : branchRelationList){
					//园区或者园区
					if (branchRelation.getResourceType() == 1 || branchRelation.getResourceType() == 3){
						Agency agency = new Agency();
						agency.setId(branchRelation.getResourceId());
						AgencyVO agencyVO = agencyClient.detail(agency);
//						branchVO.setResourceName(agencyVO.getName());
						branchVO.setResourceType(branchRelation.getResourceType());
						resourceIds.add(agencyVO.getId().toString());
						resourceNames.add(agencyVO.getName());
					}
					//小区
					if (branchRelation.getResourceType() == 2){
						Residential residential = new Residential();
						residential.setId(branchRelation.getResourceId());
						ResidentialVO residentialVO = residentialClient.detail(residential);
//						branchVO.setResourceName(residentialVO.getName());
						branchVO.setResourceType(branchRelation.getResourceType());
						resourceIds.add(residentialVO.getId().toString());
						resourceNames.add(residentialVO.getName());
					}
					//企业
					if (branchRelation.getResourceType() == 4){
						Enterprise enterprise = new Enterprise();
						enterprise.setId(branchRelation.getResourceId());
						EnterpriseVO enterpriseVO = enterpriseClient.detail(enterprise);
//						branchVO.setResourceName(enterpriseVO.getEnterpriseName());
						branchVO.setResourceType(branchRelation.getResourceType());
						resourceIds.add(enterpriseVO.getId().toString());
						resourceNames.add(enterpriseVO.getEnterpriseName());
					}
				}
				branchVO.setResourceIds(resourceIds);
				branchVO.setResourceNames(StringUtil.join(resourceNames));
			}
		}

		//User createUser = UserCache.getUser(branch.getCreateUser());
		//User updateUser = UserCache.getUser(branch.getUpdateUser());
		//branchVO.setCreateUserName(createUser.getName());
		//branchVO.setUpdateUserName(updateUser.getName());

		return branchVO;
	}

}
