<?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.admin.dao.AdDao">

    <resultMap type="com.kmall.admin.entity.AdEntity" id="adMap">
        <result property="id" column="id"/>
        <result property="adPositionId" column="ad_position_id"/>
        <result property="mediaType" column="media_type"/>
        <result property="name" column="name"/>
        <result property="storeId" column="store_id"/>
        <result property="merchSn" column="merch_sn"/>
        <result property="link" column="link"/>
        <result property="imageUrl" column="image_url"/>
        <result property="content" column="content"/>
        <result property="endTime" column="end_time"/>
        <result property="enabled" column="enabled"/>
        <result property="sortOrder" column="sort_order"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.AdEntity">
		select
			id,
			ad_position_id,
			media_type,
			name,
			store_id,
			merch_sn,
			link,
			image_url,
			sort_order,
			content,
			end_time,
			enabled
		from mall_ad
		where id = #{id}
	</select>

    <select id="queryList" resultType="com.kmall.admin.entity.AdEntity">
        select
        mall_ad.id,
        mall_ad.ad_position_id,
        mall_ad.media_type,
        mall_ad.name,
        mall_ad.store_id,
        mall_ad.merch_sn,
        mall_ad.link,
        mall_ad.image_url,
        mall_ad.sort_order,
        mall_ad.content,
        mall_ad.end_time,
        mall_ad.enabled,
        mall_ad_position.name ad_Position_Name
        from mall_ad LEFT JOIN mall_ad_position on mall_ad.ad_position_id = mall_ad_position.id
        where 1=1
        <if test="storeId != null and storeId != ''">
            AND store_id = #{storeId}
        </if>
        <if test="merchSn != null and merchSn.trim() != ''">
            AND merch_sn = #{merchSn}
        </if>
        <if test="name != null and name.trim() != ''">
            AND mall_ad.name LIKE concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by mall_ad.id desc
            </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
        <if test="storeId != null and storeId != ''">
            AND store_id = #{storeId}
        </if>
        <if test="merchSn != null and merchSn.trim() != ''">
            AND merch_sn = #{merchSn}
        </if>
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
	</select>

    <insert id="save" parameterType="com.kmall.admin.entity.AdEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_ad(
			`ad_position_id`,
			`media_type`,
			`name`,
			`store_id`,
			`merch_sn`,
			`link`,
			`image_url`,
			`sort_order`,
			`content`,
			`end_time`,
			`enabled`)
		values(
			#{adPositionId},
			#{mediaType},
			#{name},
			#{storeId},
			#{merchSn},
			#{link},
			#{imageUrl},
			#{sortOrder},
			#{content},
			#{endTime},
			#{enabled})
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.AdEntity">
        update mall_ad
        <set>
            <if test="adPositionId != null">`ad_position_id` = #{adPositionId},</if>
            <if test="mediaType != null">`media_type` = #{mediaType},</if>
            <if test="name != null">`name` = #{name},</if>
            <if test="storeId != null">`store_id` = #{storeId}, </if>
            <if test="merchSn != null">`merch_sn` = #{merchSn}, </if>
            <if test="link != null">`link` = #{link},</if>
            <if test="imageUrl != null">`image_url` = #{imageUrl},</if>
            <if test="sortOrder != null">`sort_order` = #{sortOrder},</if>
            <if test="content != null">`content` = #{content},</if>
            <if test="endTime != 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>