<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yice.webadmin.app.dao.DatasetManageMapper">
    <resultMap id="BaseResultMap" type="com.yice.webadmin.app.model.DatasetManage">
        <id column="dataset_id" jdbcType="BIGINT" property="datasetId"/>
        <result column="dataset_name" jdbcType="VARCHAR" property="datasetName"/>
        <result column="dimension_type" jdbcType="TINYINT" property="dimensionType"/>
        <result column="template" jdbcType="TINYINT" property="template"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="data_type" jdbcType="TINYINT" property="dataType"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO lmp_dataset_manage
        (dataset_id,
        dataset_name,
        dimension_type,
        template,
        create_user_id,
        create_time,
        update_user_id,
        update_time,
        data_type)
        VALUES
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.datasetId},
            #{item.datasetName},
            #{item.dimensionType},
            #{item.template},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.dataType})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.yice.webadmin.app.dao.DatasetManageMapper.inputFilterRef"/>
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="datasetManageFilter != null">
            <if test="datasetManageFilter.datasetId != null">
                AND lmp_dataset_manage.dataset_id = #{datasetManageFilter.datasetId}
            </if>
            <if test="datasetManageFilter.datasetName != null and datasetManageFilter.datasetName != ''">
                AND lmp_dataset_manage.dataset_name = #{datasetManageFilter.datasetName}
            </if>
            <if test="datasetManageFilter.dimensionType != null">
                AND lmp_dataset_manage.dimension_type = #{datasetManageFilter.dimensionType}
            </if>
            <if test="datasetManageFilter.searchString != null and datasetManageFilter.searchString != ''">
                <bind name="safeDatasetManageSearchString" value="'%' + datasetManageFilter.searchString + '%'"/>
                AND IFNULL(lmp_dataset_manage.dataset_name,'') LIKE #{safeDatasetManageSearchString}
            </if>
        </if>
    </sql>

    <select id="getDatasetManageList" resultMap="BaseResultMap"
            parameterType="com.yice.webadmin.app.model.DatasetManage">
        SELECT * FROM lmp_dataset_manage
        <where>
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <!-- 支持基于一对一或者一对多从表字段过滤的SQL_ID -->
    <select id="getDatasetManageListEx" resultMap="BaseResultMap">
        SELECT
        lmp_dataset_manage.*
        FROM
        lmp_dataset_manage
        <where>
            <include refid="filterRef"/>
            <if test="datasetVersionFilter != null">
                AND EXISTS (SELECT * FROM lmp_dataset_version
                <where>
                    lmp_dataset_manage.dataset_id = lmp_dataset_version.dataset_id
                    <include refid="com.yice.webadmin.app.dao.DatasetVersionMapper.filterRef"/>
                </where>
                )
            </if>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
