package org.springblade.resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.service.ITenantService;
import org.springblade.system.client.TenantClient;
import org.springblade.system.client.model.TenantModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hepingguo
 * @version 1.0
 * @description: TODO
 * @date 2023/11/25 11:51
 */
@RestController
@ApiIgnore
public class TenantSource implements TenantClient {

    @Autowired
    private ITenantService tenantService;

    @Override
    public List<TenantModel> getTenantList(String tenantId) {

        List<Tenant> tenantList;
        if (StrUtil.isBlank(tenantId)) {
            tenantList = tenantService.list();
        } else {
            tenantList = tenantService.list(Wrappers.<Tenant>lambdaQuery()
                    .eq(Tenant::getTenantId,tenantId));
        }

        return BeanUtil.copyToList(tenantList, TenantModel.class);
    }

    @Override
    public List<TenantModel> getTenantByTenantIds(List<String> tenantIds) {

        List<Tenant> tenantList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(tenantIds)) {

            tenantList = tenantService.list(Wrappers.<Tenant>lambdaQuery()
                    .in(Tenant::getTenantId,tenantIds));
        }

        return BeanUtil.copyToList(tenantList, TenantModel.class);
    }
}
