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

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.List;
import java.util.Set;

/**
 * 报表数据源 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) {
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<ReportDatasourceDO>()
                .eq(ReportDatasourceDO::getId, id)
                .and(wr -> wr.or(wrapper -> wrapper
                                .eq(ReportDatasourceDO::getOrganId, organId)
                                .eq(ReportDatasourceDO::getBuildinType, ReportTemplateTypeEnum.CUSTOM))
                        .or(wrapper -> wrapper.eq(ReportDatasourceDO::getBuildinType, ReportTemplateTypeEnum.SYSTEM)));
        return selectOne(queryWrapper);
    }

    /**
     * 查询数据源列表
     * 普通用户用：organId+buildinType：1 or buildinType：0
     * @param
     * @return
     */
    @OrganIgnore
    default List<ReportDatasourceDO> selectNormalDatasourceList(Set<Long> datasourceIds, Long organId) {
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<ReportDatasourceDO>()
                .in(ReportDatasourceDO::getId, datasourceIds)
                .and(wr -> wr.or(wrapper -> wrapper
                                .eq(ReportDatasourceDO::getOrganId, organId)
                                .eq(ReportDatasourceDO::getBuildinType, ReportTemplateTypeEnum.CUSTOM))
                        .or(wrapper -> wrapper.eq(ReportDatasourceDO::getBuildinType, ReportTemplateTypeEnum.SYSTEM)));
        return selectList(queryWrapper);
    }
}
