package com.cf.imes.module.report.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bstek.datasource.bean.PreviewParams;
import com.bstek.ureport.definition.dataset.TenantParams;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.report.config.CfReportProperties;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourcePreviewReqVO;
import com.cf.imes.module.report.dal.dataobject.dataset.ReportDatasetDO;
import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO;
import com.cf.imes.module.report.dal.mysql.dataset.ReportDatasetMapper;
import com.cf.imes.module.report.dal.mysql.datasource.ReportDatasourceMapper;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 租户参数工具类
 *
 * @author Gqr
 * @since 2024/11/27 10:32
 */
@Component
public class TenantParamsUtils {

    @Resource
    private ReportDatasourceMapper datasourceMapper;

    @Resource
    private ReportDatasetMapper datasetMapper;

    @Resource
    private CfReportProperties cfReportProperties;

    /**
     * 根据数据源id构造租户对象
     *
     * @param reqVO
     */
    public PreviewParams prepareTenantParam(ReportDatasourcePreviewReqVO reqVO) {
        PreviewParams previewParams = BeanUtils.toBean(reqVO, PreviewParams.class);
        Long datasourceId = reqVO.getDatasourceId();
        TenantParams tenantParams = new TenantParams();
        Long userOrganId = SecurityFrameworkUtils.getUserOrganId();
        // 根据前端传递的数据源id，判断是不是内置数据源，是就把所有其下的datasetname传给ureport
        ReportDatasourceDO datasourceDO = datasourceMapper.selectNormalDatasourceById(datasourceId, userOrganId);
        if (ObjectUtil.isNotNull(datasourceDO) && ObjectUtil.equal(datasourceDO.getBuildinType(), ReportTemplateTypeEnum.SYSTEM)) {
            List<ReportDatasetDO> datasetDOList = datasetMapper.selectList(new LambdaQueryWrapperX<ReportDatasetDO>().in(ReportDatasetDO::getDatasourceId, datasourceId));
            if (CollUtil.isNotEmpty(datasetDOList)) {
                tenantParams.setBuildinDsNames(datasetDOList.stream().map(ReportDatasetDO::getName).collect(Collectors.toList()));
            }
        }
        // 用户组织id
        tenantParams.setOrganId(userOrganId);
        // imes多租户业务表表名列表
        tenantParams.setTenantTableNames(cfReportProperties.getTenantTableNames());
        previewParams.setTenantParams(tenantParams);
        return previewParams;
    }

    /**
     * 往parameters中增加租户相关参数
     *
     * @param parameters
     */
    public void prepareTenantParam(Map<String, Object> parameters) {
        Long userOrganId = SecurityFrameworkUtils.getUserOrganId();
        // 根据前端传递的数据集id，判断是不是内置数据源下的，是就把name传给ureport
        List<Long> datasetIds = (List) parameters.get("datasetIds");
        if (CollUtil.isNotEmpty(datasetIds)) {
            // 内置数据集名称列表
            List<String> buildinDsNames = new ArrayList<>();
            // 遍历获取所有上级数据源
            List<ReportDatasetDO> reportDatasetDOS = datasetMapper.selectNormalDatasetList(datasetIds.stream().collect(Collectors.toSet()));
            for (ReportDatasetDO datasetDO : reportDatasetDOS) {
                Long datasourceId = datasetDO.getDatasourceId();
                // 查询数据源，如果是内置数据源则把数据集的name传给ureport
                // 机构下 + 内置数据源查询
                ReportDatasourceDO datasourceDO = datasourceMapper.selectNormalDatasourceById(datasourceId, userOrganId);
                if (ObjectUtil.isNotNull(datasourceDO) && ObjectUtil.equal(datasourceDO.getBuildinType(), ReportTemplateTypeEnum.SYSTEM)) {
                    buildinDsNames.add(datasetDO.getName());
                }
            }
            if (CollUtil.isNotEmpty(buildinDsNames)) {
                parameters.put("buildinDsNames", buildinDsNames);
            }
        }
        // imes多租户业务表表名列表
        parameters.put("tenantTableNames", cfReportProperties.getTenantTableNames());
        // 用户组织id
        parameters.put("organId", userOrganId);
    }

}
