package org.springblade.app.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springblade.app.vo.AgencyAppLoginVO;
import org.springblade.common.constant.CommonConstant;
import org.springblade.community.entity.Agency;
import org.springblade.community.feign.IAgencyClient;
import org.springblade.community.vo.AgencyVO;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.system.expand.entity.TenantType;
import org.springblade.system.expand.feign.ITenantTypeClient;
import org.springblade.system.expand.vo.TenantTypeVO;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lidexi
 * @date 2021/5/12 9:54
 */
@RestController
@RequestMapping("/agency")
@RequiredArgsConstructor
public class AgencyController {

	private final IAgencyClient agencyClient;
	private final ITenantTypeClient tenantTypeClient;
	private final IUserClient userClient;

	/**
	 *园区小程序登录页获取园区列表（无需鉴权）:返回获取对应园区的token所需的账号密码
	 *
	 */
	@GetMapping("/getAgencyTenantList")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取园区租户列表")
	public R<List<AgencyAppLoginVO>> getAgencyTenantList(@RequestParam Map<String,Object> agency) {

		List<AgencyAppLoginVO> agencyAppLoginVOS = new ArrayList<>();
		TenantType searchTenantType = new TenantType();
		searchTenantType.setTenantType(CommonConstant.TenantType.PARK.getValue());
		List<TenantTypeVO> tenantTypes = tenantTypeClient.list(searchTenantType);

		List<String> tenantIds = tenantTypes.stream().map(TenantType::getTenantId).collect(Collectors.toList());

		for(String tenantId:tenantIds){
			AgencyAppLoginVO agencyAppLoginVO;

			//获取该租户的园区信息
			Agency searchAgency = new Agency();
			searchAgency.setTenantId(tenantId);
			List<AgencyVO> agencyList = agencyClient.listWithOutToken(searchAgency);
			if(agencyList.size()>0){
				agencyAppLoginVO = Objects.requireNonNull(BeanUtil.copy(agencyList.get(0), AgencyAppLoginVO.class));
				agencyAppLoginVO.setPassword(agencyList.get(0).getTenantPassword());
				//获取该租户管理员的账号密码用于小程序获取token

				R<User> admin = userClient.userByAccount(tenantId, "admin");
				if(admin.getData()!=null){
					agencyAppLoginVO.setAccount(admin.getData().getAccount());
					if(agencyAppLoginVO.getAccount() != null){
						agencyAppLoginVOS.add(agencyAppLoginVO);
					}

				}
			}

		}

		return R.data(agencyAppLoginVOS);
	}

	/**
	 *园区小程序登录页获取园区列表（无需鉴权）:返回获取对应园区
	 *
	 */
	@GetMapping("/getAgency")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取园区")
	public R<AgencyAppLoginVO> getAgency(Agency agency) {
		AgencyVO detail = agencyClient.detail(agency);
		if (detail != null) {
			AgencyAppLoginVO agencyAppLoginVO = Objects.requireNonNull(BeanUtil.copy(detail, AgencyAppLoginVO.class));
			agencyAppLoginVO.setPassword(detail.getTenantPassword());
			R<User> admin = userClient.userByAccount(agencyAppLoginVO.getTenantId(), "admin");
			if (admin.getData() != null) {
				agencyAppLoginVO.setAccount(admin.getData().getAccount());
				return R.data(agencyAppLoginVO);
			} else {
				return R.fail("获取园区失败");
			}
		} else {
			return R.fail("园区不存在");
		}
	}
}
