<?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.purui.prims.headless.server.persistence.mapper.DimensionDOCustomMapper">
    <resultMap id="BaseResultMap" type="com.purui.prims.headless.server.persistence.dataobject.DimensionDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="model_id" jdbcType="BIGINT" property="modelId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="biz_name" jdbcType="VARCHAR" property="bizName"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="sensitive_level" jdbcType="INTEGER" property="sensitiveLevel"/>
        <result column="type" jdbcType="VARCHAR" property="type"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
        <result column="semantic_type" jdbcType="VARCHAR" property="semanticType"/>
        <result column="alias" jdbcType="VARCHAR" property="alias"/>
        <result column="default_values" jdbcType="VARCHAR" property="defaultValues"/>
        <result column="dim_value_maps" jdbcType="VARCHAR" property="dimValueMaps"/>
    </resultMap>
    <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs"
               type="com.purui.prims.headless.server.persistence.dataobject.DimensionDO">
        <result column="type_params" jdbcType="LONGVARCHAR" property="typeParams"/>
        <result column="expr" jdbcType="LONGVARCHAR" property="expr"/>
    </resultMap>


    <sql id="Example_Where_Clause">
        <where>
            <foreach collection="oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" prefixOverrides="and" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and
                                    #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach close=")" collection="criterion.value" item="listItem"
                                             open="(" separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Base_Column_List">
        id
        , name, biz_name, description, status, model_id, type, type_params, expr, datasource_id,
    created_at, created_by, updated_by, updated_at, semantic_type
    </sql>

    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into pr_dimension (name, biz_name,
        description, status, model_id,
        type, type_params, expr,created_at, created_by,
        updated_by, updated_at, semantic_type,sensitive_level, is_tag)
        values
        <foreach collection="list" item="dimension" separator=",">
            (#{dimension.name,jdbcType=VARCHAR}, #{dimension.bizName,jdbcType=VARCHAR},
            #{dimension.description,jdbcType=VARCHAR}, #{dimension.status,jdbcType=INTEGER},
            #{dimension.modelId,jdbcType=BIGINT},
            #{dimension.type,jdbcType=VARCHAR}, #{dimension.typeParams,jdbcType=VARCHAR},
            #{dimension.expr,jdbcType=VARCHAR}, #{dimension.createdAt,jdbcType=TIMESTAMP},
            #{dimension.createdBy,jdbcType=VARCHAR},
            #{dimension.updatedBy,jdbcType=VARCHAR}, #{dimension.updatedAt,jdbcType=TIMESTAMP},
            #{dimension.semanticType,jdbcType=VARCHAR},
            #{dimension.sensitiveLevel,jdbcType=INTEGER}, #{dimension.isTag, jdbcType=INTEGER})
        </foreach>
    </insert>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="dimension" separator=";">
            update pr_dimension
            set name = #{dimension.name,jdbcType=VARCHAR},
            biz_name = #{dimension.bizName,jdbcType=VARCHAR},
            description = #{dimension.description,jdbcType=VARCHAR},
            status = #{dimension.status,jdbcType=INTEGER},
            model_id = #{dimension.modelId,jdbcType=BIGINT},
            type = #{dimension.type,jdbcType=VARCHAR},
            type_params = #{dimension.typeParams,jdbcType=VARCHAR},
            datasource_id = #{dimension.datasourceId,jdbcType=BIGINT},
            created_at = #{dimension.createdAt,jdbcType=TIMESTAMP},
            created_by = #{dimension.createdBy,jdbcType=VARCHAR},
            updated_by = #{dimension.updatedBy,jdbcType=VARCHAR},
            updated_at = #{dimension.updatedAt,jdbcType=TIMESTAMP},
            semantic_type = #{dimension.semanticType,jdbcType=VARCHAR},
            sensitive_level = #{dimension.sensitiveLevel,jdbcType=INTEGER},
            expr = #{dimension.expr,jdbcType=LONGVARCHAR}
            where id = #{dimension.id,jdbcType=BIGINT}
        </foreach>
    </update>

    <update id="batchUpdateStatus" parameterType="java.util.List">
        <foreach collection="list" item="dimension" separator=";">
            update pr_dimension
            set
            status = #{dimension.status,jdbcType=INTEGER},
            updated_by = #{dimension.updatedBy,jdbcType=VARCHAR},
            updated_at = #{dimension.updatedAt,jdbcType=TIMESTAMP}
            where id = #{dimension.id,jdbcType=BIGINT}
        </foreach>
    </update>

    <select id="query" resultMap="ResultMapWithBLOBs">
        select *
        from pr_dimension t
        left join (
        select *
        from pr_tag
        where type = 'DIMENSION'
        ) t1 on t.id = t1.item_id
        where status != 3
        <if test="key != null and key != ''">
            and ( t.id like CONCAT('%',#{key , jdbcType=VARCHAR},'%') or
            t.name like CONCAT('%',#{key , jdbcType=VARCHAR},'%') or
            t.biz_name like CONCAT('%',#{key , jdbcType=VARCHAR},'%') or
            t.alias like CONCAT('%',#{key , jdbcType=VARCHAR},'%') or
            t.description like CONCAT('%',#{key , jdbcType=VARCHAR},'%') )
        </if>
        <if test="id != null">
            and t.id like CONCAT('%',#{id , jdbcType=VARCHAR},'%')
        </if>
        <if test="name != null and name != '' ">
            and t.name like CONCAT('%',#{name , jdbcType=VARCHAR},'%')
        </if>
        <if test="bizName != null and bizName != ''">
            and t.biz_name like CONCAT('%',#{bizName , jdbcType=VARCHAR},'%')
        </if>
        <if test="sensitiveLevel != null">
            and t.sensitive_level = #{sensitiveLevel}
        </if>
        <if test="status != null">
            and t.status = #{status}
        </if>
        <if test="modelIds != null and modelIds.size >0">
            and t.model_id in
            <foreach collection="modelIds" index="index" item="model" open="(" close=")"
                     separator=",">
                #{model}
            </foreach>
        </if>
        <if test="ids != null and ids.size >0">
            and t.id in
            <foreach collection="ids" index="index" item="id" open="(" close=")"
                     separator=",">
                #{id}
            </foreach>
        </if>
        <if test="createdBy != null">
            and t.created_by = #{createdBy}
        </if>
        <if test="isTag != null and isTag == 1">
            and t1.id is not null
        </if>
        <if test="isTag != null and isTag == 0">
            and t1.id is null
        </if>
    </select>


    <select id="queryDimensions" resultMap="ResultMapWithBLOBs">
        select *
        from pr_dimension
        where status != 3
        <if test="modelIds != null and modelIds.size >0">
            and model_id in
            <foreach collection="modelIds" index="index" item="model" open="(" close=")"
                     separator=",">
                #{model}
            </foreach>
        </if>
        <if test="dimensionIds != null and dimensionIds.size >0">
            and id in
            <foreach collection="dimensionIds" index="index" item="dimensionId" open="(" close=")"
                     separator=",">
                #{dimensionId}
            </foreach>
        </if>
        <if test="dimensionNames != null and dimensionNames.size > 0">
            AND (
            (name IN
            <foreach collection="dimensionNames" index="index" item="dimensionName" open="(" close=")"
                     separator=",">
                #{dimensionName}
            </foreach>)
            OR
            (biz_name IN
            <foreach collection="dimensionNames" index="index" item="dimensionName" open="(" close=")"
                     separator=",">
                #{dimensionName}
            </foreach>)
            )
        </if>
    </select>


</mapper>
