<?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.ss.ad.mapper.AdMapper">

    <resultMap type="AdVO" id="AdResult" autoMapping="true"/>

    <sql id="selectAdVo">
        select
            sa.ad_id,
            sa.type,
            sa.picture,
            sa.url,
            sa.create_by,
            sa.create_time,
            sa.update_by,
            sa.update_time,
            sa.remark,
            sa.deleted,
            sa.url_type
        from ss_ad sa
    </sql>

    <sql id="searchCondition">
        <if test="adId != null "> and sa.ad_id = #{adId}</if>
        <if test="type != null  and type != ''"> and sa.type = #{type}</if>
        <if test="urlType != null  and urlType != ''"> and sa.url_type = #{urlType}</if>
        <if test="url != null  and url != ''"> and sa.url like concat('%', #{url}, '%')</if>
        <if test="deleted != null "> and sa.deleted = #{deleted}</if>
        <if test="deleted == null "> and sa.deleted = false</if>
    </sql>

    <select id="selectAdList" parameterType="AdQuery" resultMap="AdResult">
        <include refid="selectAdVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectAdByAdId" parameterType="Long" resultMap="AdResult">
        <include refid="selectAdVo"/>
        where sa.ad_id = #{adId} and sa.deleted = false
    </select>

    <select id="selectOne" resultMap="AdResult">
        <include refid="selectAdVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        limit 1
    </select>

    <insert id="insertAd" parameterType="Ad" useGeneratedKeys="true" keyProperty="adId">
        insert into ss_ad
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null">type,</if>
            <if test="picture != null">picture,</if>
            <if test="url != null">url,</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="remark != null">remark,</if>
            <if test="deleted != null">deleted,</if>
            <if test="urlType != null">url_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null">#{type},</if>
            <if test="picture != null">#{picture},</if>
            <if test="url != null">#{url},</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="remark != null">#{remark},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="urlType != null">#{urlType},</if>
         </trim>
    </insert>

    <update id="updateAd" parameterType="Ad">
        update ss_ad
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="picture != null">picture = #{picture},</if>
            <if test="url != null">url = #{url},</if>
            <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="remark != null">remark = #{remark},</if>
            <if test="urlType != null">url_type = #{urlType},</if>
        </trim>
        where ad_id = #{adId}
    </update>

    <update id="logicDel">
        update ss_ad
        set deleted = true
        where ad_id in
        <foreach item="adId" collection="ids" open="(" separator="," close=")">
            #{adId}
        </foreach>
    </update>

    <delete id="deleteAdByAdId" parameterType="Long">
        delete from ss_ad where ad_id = #{adId}
    </delete>

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