<?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.kmall.api.dao.ApiAdMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.api.entity.AdVo" id="adMap">
        <result property="id" column="id"/>
        <result property="ad_position_id" column="adPositionId"/>
        <result property="media_type" column="mediaType"/>
        <result property="name" column="name"/>
        <result property="link" column="link"/>
        <result property="image_url" column="imageUrl"/>
        <result property="content" column="content"/>
        <result property="end_time" column="endTime"/>
        <result property="enabled" column="enabled"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="merchSn" column="mersh_sn"/>
        <result property="storeId" column="store_id"/>
    </resultMap>

    <select id="queryObject" resultMap="adMap">
		select * from mall_ad where id = #{value}
	</select>

    <select id="queryList" resultMap="adMap">
        select * from mall_ad
        where 1 = 1 and enabled = 1
        <if test="ad_position_id != null and ad_position_id != null">
            and ad_position_id = #{ad_position_id}
        </if>
        <if test="storeId != null and storeId != null">
            and store_id = #{storeId}
        </if>
        <if test="merchSn != null and merchSn != null">
            and mersh_sn = #{merchSn}
        </if>
        <if test="ad_position_ids != null and ad_position_ids.length > 0">
            and ad_position_id in
            <foreach item="item" collection="ad_position_ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="media_types != null">
            and media_type in
            <foreach item="item" collection="media_types" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by sort_order
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
		select count(*) from mall_ad where 1 = 1 and enabled = 1
        <if test="ad_position_id != null and ad_position_id != null">
            and ad_position_id = #{ad_position_id}
        </if>
        <if test="storeId != null and storeId != null">
            and store_id = #{storeId}
        </if>
        <if test="merchSn != null and merchSn != null">
            and mersh_sn = #{merchSn}
        </if>
        <if test="ad_position_ids != null and ad_position_ids.length > 0">
            and ad_position_id in
            <foreach item="item" collection="ad_position_ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="media_types != null">
            and media_type in
            <foreach item="item" collection="media_types" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
	</select>

    <insert id="save" parameterType="com.kmall.api.entity.AdVo" useGeneratedKeys="true" keyProperty="id">
		insert into mall_ad
		(
			`ad_position_id`, 
			`media_type`, 
			`name`, 
			`link`, 
			`image_url`, 
			`content`, 
			`end_time`, 
			`enabled`
		)
		values
		(
			#{ad_position_id},
			#{media_type},
			#{name}, 
			#{link}, 
			#{image_url},
			#{content}, 
			#{end_time},
			#{enabled}
		)
	</insert>

    <update id="update" parameterType="com.kmall.api.entity.AdVo">
        update mall_ad
        <set>
            <if test="ad_position_id != null">`ad_position_id` = #{adPositionId},</if>
            <if test="media_type != null">`media_type` = #{mediaType},</if>
            <if test="name != null">`name` = #{name},</if>
            <if test="link != null">`link` = #{link},</if>
            <if test="image_url != null">`image_url` = #{imageUrl},</if>
            <if test="content != null">`content` = #{content},</if>
            <if test="end_time != null">`end_time` = #{endTime},</if>
            <if test="enabled != null">`enabled` = #{enabled}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from mall_ad where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from mall_ad where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>