package com.cf.imes.module.report.dal.mysql.datasource;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.organ.core.aop.OrganIgnore;
import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import org.apache.ibatis.annotations.Mapper;

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

import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.CF_ORGANID;

/**
 * 报表数据源 Mapper
 *
 * @author Gqr
 * @since 2024/7/8 11:01
 */
@Mapper
public interface ReportDatasourceMapper extends BaseMapperX<ReportDatasourceDO> {

    /**
     * 根据数据源id查询数据源
     * 普通用户用：organId+buildType：1 or buildType：0
     *
     * @param id 数据源id
     * @return
     */
    @OrganIgnore
    default ReportDatasourceDO selectNormalDatasourceById(Long id, Long organId) {
        ReportDatasourceDO result;

        // 1. 先查 CUSTOM（当前组织）
        LambdaQueryWrapper<ReportDatasourceDO> customWrapper = new LambdaQueryWrapper<ReportDatasourceDO>()
                .eq(ReportDatasourceDO::getId, id)
                .eq(ReportDatasourceDO::getOrganId, organId)
                .eq(ReportDatasourceDO::getBuildinType, ReportTemplateTypeEnum.CUSTOM);

        result = selectOne(customWrapper);

        // 2. 如果 CUSTOM 没查到，再查 SYSTEM（固定组织）
        if (result == null) {
            LambdaQueryWrapper<ReportDatasourceDO> systemWrapper = new LambdaQueryWrapper<ReportDatasourceDO>()
                    .eq(ReportDatasourceDO::getId, id)
                    .eq(ReportDatasourceDO::getOrganId, CF_ORGANID)
                    .eq(ReportDatasourceDO::getBuildinType, ReportTemplateTypeEnum.SYSTEM);

            result = selectOne(systemWrapper);
        }

        return result;
    }

    /**
     * 查询数据源列表
     * 普通用户用：organId+buildinType：1 or buildinType：0
     * @param
     * @return
     */
    @OrganIgnore
    default List<ReportDatasourceDO> selectNormalDatasourceList(Set<Long> datasourceIds, Long organId) {
        List<ReportDatasourceDO> result = new ArrayList<>();

        // 1. CUSTOM（当前组织）
        List<ReportDatasourceDO> customList = selectList(
                new LambdaQueryWrapper<ReportDatasourceDO>()
                        .in(ReportDatasourceDO::getId, datasourceIds)
                        .eq(ReportDatasourceDO::getOrganId, organId)
                        .eq(ReportDatasourceDO::getBuildinType, ReportTemplateTypeEnum.CUSTOM)
        );

        if (CollUtil.isNotEmpty(customList)) {
            result.addAll(customList);
        }

        // 2. SYSTEM（固定组织）
        List<ReportDatasourceDO> systemList = selectList(
                new LambdaQueryWrapper<ReportDatasourceDO>()
                        .in(ReportDatasourceDO::getId, datasourceIds)
                        .eq(ReportDatasourceDO::getOrganId, CF_ORGANID)
                        .eq(ReportDatasourceDO::getBuildinType, ReportTemplateTypeEnum.SYSTEM)
        );

        if (CollUtil.isNotEmpty(systemList)) {
            result.addAll(systemList);
        }

        return result;
    }
}
