<?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.TagCustomMapper">
    <resultMap id="BaseTagDO" type="com.purui.prims.headless.server.persistence.dataobject.TagDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="item_id" jdbcType="BIGINT" property="itemId"/>
        <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"/>
    </resultMap>

    <resultMap id="TagResp" type="com.purui.prims.headless.api.pojo.response.TagResp">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="domain_id" jdbcType="BIGINT" property="domainId"/>
        <result column="domain_name" jdbcType="VARCHAR" property="domainName"/>
        <result column="model_id" jdbcType="BIGINT" property="modelId"/>
        <result column="model_name" jdbcType="VARCHAR" property="modelName"/>
        <result column="tag_object_id" jdbcType="BIGINT" property="tagObjectId"/>
        <result column="tag_object_name" jdbcType="VARCHAR" property="tagObjectName"/>
        <result column="type" jdbcType="VARCHAR" property="tagDefineType"/>
        <result column="item_id" jdbcType="VARCHAR" property="itemId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="biz_name" jdbcType="VARCHAR" property="bizName"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
    </resultMap>

    <select id="getTagDOList" resultMap="BaseTagDO">
        select * from pr_tag
        <where>
            <if test="itemIds != null and itemIds.size >0">
                and item_id in
                <foreach collection="itemIds" index="index" item="itemId" open="(" close=")"
                         separator=",">
                    #{itemId}
                </foreach>
            </if>
            <if test="ids != null and ids.size >0">
                and id in
                <foreach collection="ids" index="index" item="tagId" open="(" close=")"
                         separator=",">
                    #{tagId}
                </foreach>
            </if>
            <if test="tagDefineType != null">
                and type = #{tagDefineType}
            </if>
        </where>
    </select>

    <select id="queryTagRespList" resultMap="TagResp">
        select * from (
        select pr_tag.* , pr_dimension.model_id as model_id, pr_dimension.sensitive_level as sensitive_level,
        pr_dimension.name as name, pr_dimension.biz_name as biz_name, pr_dimension.description as description
        from pr_tag join pr_dimension
        on pr_tag.item_id = pr_dimension.id
        where pr_dimension.status=1 and pr_tag.type='DIMENSION'
        union
        select pr_tag.*, pr_metric.model_id as model_id, pr_metric.sensitive_level as sensitive_level,
        pr_metric.name as name, pr_metric.biz_name as biz_name, pr_metric.description as description
        from pr_tag join pr_metric
        on pr_tag.item_id = pr_metric.id
        where pr_metric.status=1 and pr_tag.type='METRIC'
        )t
        <where>
            <if test="tagDefineType != null">
                and type = #{tagDefineType}
            </if>
            <if test="itemIds != null and itemIds.size >0">
                and item_id in
                <foreach collection="itemIds" index="index" item="itemId" open="(" close=")"
                         separator=",">
                    #{itemId}
                </foreach>
            </if>
            <if test="ids != null and ids.size >0">
                and id in
                <foreach collection="ids" index="index" item="tagId" open="(" close=")"
                         separator=",">
                    #{tagId}
                </foreach>
            </if>
            <if test="name != null and name != ''">
                name = #{name}
            </if>
            <if test="bizName != null and bizName != ''">
                and biz_Name = #{bizName}
            </if>
            <if test="modelIds != null and modelIds.size >0">
                and model_id in
                <foreach collection="modelIds" index="index" item="modelId" open="(" close=")"
                         separator=",">
                    #{modelId}
                </foreach>
            </if>
            <if test="key != null and key != ''">
                and ( id like CONCAT('%',#{key , jdbcType=VARCHAR},'%') or
                name like CONCAT('%',#{key , jdbcType=VARCHAR},'%') or
                biz_name like CONCAT('%',#{key , jdbcType=VARCHAR},'%') or
                description like CONCAT('%',#{key , jdbcType=VARCHAR},'%'))
            </if>
            <if test="sensitiveLevel != null ">
                and sensitive_level = #{sensitiveLevel}
            </if>
            <if test="createdBy != null and createdBy != ''">
                and created_by = #{createdBy}
            </if>
        </where>
        order by updated_at desc
    </select>


    <delete id="deleteById">
        delete
        from pr_tag
        where id = #{id}
    </delete>

    <delete id="deleteBatchByIds">
        delete from pr_tag
        where id in
        <foreach collection="list" index="index" item="tagId" open="(" close=")"
                 separator=",">
            #{tagId}
        </foreach>
    </delete>

    <delete id="deleteBatchByType">
        delete from pr_tag
        where type = #{type}
        <if test="itemIds != null and itemIds.size >0">
            and item_id in
            <foreach collection="itemIds" index="index" item="itemId" open="(" close=")"
                     separator=",">
                #{itemId}
            </foreach>
        </if>
    </delete>

</mapper>
