<?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.ruoyi.ibs.dashboard.mapper.TabRankingMapper">
    
    <resultMap type="TabRanking" id="TabRankingResult">
        <result property="id"    column="id"    />
        <result property="tabId"    column="tab_id"    />
        <result property="tabName"    column="tab_name"    />
        <result property="level"    column="level"    />
        <result property="rankId"    column="rank_id"    />
        <result property="belongId"    column="belong_id"    />
        <result property="tabValue"    column="tab_value"    />
        <result property="dataDt"    column="data_dt"    />
    </resultMap>

    <sql id="selectTabRankingVo">
        select id, tab_id, tab_name, level, rank_id, belong_id, tab_value, data_dt from tab_ranking
    </sql>

    <select id="selectTabRankingList" parameterType="TabRanking" resultType="TabRanking">
        <include refid="selectTabRankingVo"/>
        <where>  
            <if test="tabId != null  and tabId != ''"> and tab_id = #{tabId}</if>
            <if test="tabName != null  and tabName != ''"> and tab_name like concat('%', #{tabName}, '%')</if>
            <if test="level != null  and level != ''"> and level = #{level}</if>
            <if test="rankId != null "> and rank_id = #{rankId}</if>
            <if test="belongId != null  and belongId != ''"> and belong_id like  concat('%', #{belongId}, '%')</if>
            <if test="tabValue != null  and tabValue != ''"> and tab_value = #{tabValue}</if>
            <if test="dataDt != null "> and left(data_dt,10) = left(#{dataDt},10)</if>
            <if test="custType != null  and custType != ''"> and cust_type = #{custType}</if>
        </where>
        order by rank_id asc
    </select>
    
    <select id="selectTabRankingById" parameterType="Long" resultMap="TabRankingResult">
        <include refid="selectTabRankingVo"/>
        where id = #{id}
    </select>

    <select id="selectConmmentById" parameterType="Long" resultType="TabCommentVO">
        select comment,belong_id,data_dt
        from tab_comment
        where ranking_id = #{id}
        order by data_dt desc
    </select>

    <insert id="insertTabRanking" parameterType="TabRanking" useGeneratedKeys="true" keyProperty="id">
        insert into tab_ranking
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tabId != null">tab_id,</if>
            <if test="tabName != null">tab_name,</if>
            <if test="level != null">level,</if>
            <if test="rankId != null">rank_id,</if>
            <if test="belongId != null">belong_id,</if>
            <if test="tabValue != null">tab_value,</if>
            <if test="dataDt != null">data_dt,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tabId != null">#{tabId},</if>
            <if test="tabName != null">#{tabName},</if>
            <if test="level != null">#{level},</if>
            <if test="rankId != null">#{rankId},</if>
            <if test="belongId != null">#{belongId},</if>
            <if test="tabValue != null">#{tabValue},</if>
            <if test="dataDt != null">#{dataDt},</if>
        </trim>
    </insert>


    <insert id="insertComment"  >
        insert into tab_comment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="rankingId != null">ranking_id,</if>
            <if test="comment != null">comment,</if>
            <if test="belongId != null">belong_id,</if>
           data_dt,
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="rankingId != null">#{rankingId},</if>
            <if test="comment != null">#{comment},</if>
            <if test="belongId != null">#{belongId},</if>
            sysdate(),
         </trim>
    </insert>

    <update id="updateTabRanking" parameterType="TabRanking">
        update tab_ranking
        <trim prefix="SET" suffixOverrides=",">
            <if test="tabId != null">tab_id = #{tabId},</if>
            <if test="tabName != null">tab_name = #{tabName},</if>
            <if test="level != null">level = #{level},</if>
            <if test="rankId != null">rank_id = #{rankId},</if>
            <if test="belongId != null">belong_id = #{belongId},</if>
            <if test="tabValue != null">tab_value = #{tabValue},</if>
            <if test="dataDt != null">data_dt = #{dataDt},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTabRankingById" parameterType="Long">
        delete from tab_ranking where id = #{id}
    </delete>

    <delete id="deleteTabRankingByIds" parameterType="String">
        delete from tab_ranking where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>