<?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.frequency.shield.mapper.ShieldMapper">
    
    <resultMap type="com.ruoyi.frequency.shield.entity.Shield" id="ShieldResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="userId"    column="user_id"    />
        <result property="shieldUserId"    column="shield_user_id"    />
        <result property="userType"    column="user_type"    />
        <result property="shieldUserType"    column="shield_user_type"    />
    </resultMap>

    <sql id="selectShieldVo">
        select id, create_by, create_time, update_by, update_time, del_flag, user_id, shield_user_id, user_type, shield_user_type from t_shield
    </sql>

    <select id="selectShieldList" parameterType="com.ruoyi.frequency.shield.entity.Shield" resultMap="ShieldResult">
        <include refid="selectShieldVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="shieldUserId != null "> and shield_user_id = #{shieldUserId}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
            <if test="shieldUserType != null "> and shield_user_type = #{shieldUserType}</if>
        </where>
    </select>
    
    <select id="selectShieldById" parameterType="Long" resultMap="ShieldResult">
        <include refid="selectShieldVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertShield" parameterType="com.ruoyi.frequency.shield.entity.Shield">
        insert into t_shield
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="userId != null">user_id,</if>
            <if test="shieldUserId != null">shield_user_id,</if>
            <if test="userType != null">user_type,</if>
            <if test="shieldUserType != null">shield_user_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="userId != null">#{userId},</if>
            <if test="shieldUserId != null">#{shieldUserId},</if>
            <if test="userType != null">#{userType},</if>
            <if test="shieldUserType != null">#{shieldUserType},</if>
         </trim>
    </insert>

    <update id="updateShield" parameterType="com.ruoyi.frequency.shield.entity.Shield">
        update t_shield
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="shieldUserId != null">shield_user_id = #{shieldUserId},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="shieldUserType != null">shield_user_type = #{shieldUserType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteShieldById" parameterType="Long">
        delete from t_shield where id = #{id}
    </delete>

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

    <select id="shieldPage" resultType="com.ruoyi.controller.resp.ShieldPageResp">
        SELECT
            t.id,
            t.shield_user_id,
            t.shield_user_type,
            CASE

                WHEN t.shield_user_type = 1 THEN
                    t2.nickname
                WHEN t.shield_user_type = 2 THEN
                    t3.nickname ELSE ''
                END nickname,
            CASE

                WHEN t.shield_user_type = 1 THEN
                    t2.avatar
                WHEN t.shield_user_type = 2 THEN
                    t3.avatar ELSE ''
                END avatar,
            t.create_time
        FROM
            t_shield AS t
                LEFT JOIN t_customer AS t2 ON t.shield_user_id = t2.id
                AND t.shield_user_type = 1
                LEFT JOIN t_store AS t3 ON t.shield_user_id = t3.id
                AND t.shield_user_type = 2
                LEFT JOIN t_enterprise AS t4 ON t.shield_user_id = t4.id
                AND t.shield_user_type = 4
        WHERE
            t.del_flag = 1
          AND t.user_type = #{userVo.userType}
          AND t.user_id = #{userVo.userId}
        <if test="req.keyword!=null and req.keyword!=''">
            AND (
            t2.nickname LIKE CONCAT('%',#{req.keyword},'%')
            or
            t3.nickname LIKE CONCAT('%',#{req.keyword},'%')
            or
            t4.name LIKE CONCAT('%',#{req.keyword},'%')
            )
        </if>
        ORDER BY
            t.create_time DESC
    </select>
</mapper>