<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
<mapper namespace="com.iamberry.wechat.service.mapper.AdminBannerMapper">
	
	<!-- banner活动所有字段映射 -->
	<sql id="bannerAllField">
		B_ID bannerId, B_START_DATE bannerStartDate, B_END_DATE bannerEndDate,
		B_STATUS bannerStatus, B_DATE bannerCreateDate, B_NAME bannerName,
		B_DESC bannerDesc, B_TYPE bannerType
	</sql>
	
	<!-- banner图片所有字段映射 -->
	<sql id="imgAllField">
		B_I_ID bannerImageId, B_IMG_URL bannerImageUrl, B_I_CHECKOUT_URL bannerImageCheckOutUrl,
		B_I_DATE bannerCreateDate, B_BANNER_ID bannerId
	</sql>
	
	<!-- 查询一个当前在使用Banner或者一组广告位 -->
	<select id="selectNowBannerByType" parameterType="int" resultType="ActionBannerImage">
		(
			SELECT 
				B_IMG_URL bannerImageUrl, B_I_CHECKOUT_URL bannerImageCheckOutUrl, 1 as bannerId
			FROM 
				TB_ACTION_BANNER_IMG 
			WHERE 
				B_BANNER_ID = (
					SELECT
						B_ID
					FROM
						TB_ACTION_BANNER
					WHERE
						B_TYPE = 1 AND
						B_STATUS = 1 AND
						<![CDATA[ B_START_DATE <= NOW() AND B_END_DATE >= NOW()]]>
					ORDER BY B_DATE DESC
					LIMIT 0,1
				)
			ORDER BY B_I_DATE DESC
			LIMIT 10
		)
		UNION ALL
		(
			SELECT 
				B_IMG_URL bannerImageUrl, B_I_CHECKOUT_URL bannerImageCheckOutUrl, 2 as bannerId
			FROM 
				TB_ACTION_BANNER_IMG 
			WHERE 
				B_BANNER_ID = (
					SELECT
						B_ID
					FROM
						TB_ACTION_BANNER
					WHERE
						B_TYPE = 2 AND
						B_STATUS = 1 AND
						<![CDATA[ B_START_DATE <= NOW() AND B_END_DATE >= NOW()]]>
					ORDER BY B_DATE DESC
					LIMIT 0,1
				)
			ORDER BY B_I_DATE DESC
			LIMIT 10
		)
	</select>
	
	<!-- 查询banner列表 -->
	<select id="selectBannerListByPaper" parameterType="PaperBean" resultType="ActionBanner">
		SELECT
			<include refid="bannerAllField"/>
		FROM
			TB_ACTION_BANNER
		<!-- 基本where语句 -->
		<if test="str != null">
			WHERE B_NAME LIKE #{str} OR B_DESC LIKE #{str}
		</if>
		<!-- 排序 -->
		ORDER BY B_DATE DESC
		<!-- 分页基本语句 -->
		LIMIT #{minNum}, #{maxNum}
	</select>
	
	<!-- 查询banner列表 -->
	<select id="selectBannerCountByPaper" parameterType="PaperBean" resultType="int">
		SELECT
			COUNT(B_ID)
		FROM
			TB_ACTION_BANNER
		<!-- 基本where语句 -->
		<if test="str != null">
			WHERE B_NAME LIKE #{str} OR B_DESC LIKE #{str}
		</if>
	</select>

	<!-- 添加一个banner -->
	<insert id="insertBanner" parameterType="ActionBanner">
		 INSERT INTO 
		 	TB_ACTION_BANNER
		 		(
			 		B_START_DATE, B_END_DATE, B_STATUS, B_DATE,
					B_NAME, B_DESC, B_TYPE
				)
		 VALUES 
		  	( 
			  #{bannerStartDate},  
			  #{bannerEndDate}, 
			  #{bannerStatus},
			  NOW(),
			  #{bannerName},  
			  #{bannerDesc},
			  #{bannerType}
		  	)
	</insert>
	
	<!-- 添加banner的img -->
	<insert id="insertBannerImg" useGeneratedKeys="true" keyProperty="bannerImageId" parameterType="ActionBannerImage">
		INSERT INTO 
		 	TB_ACTION_BANNER_IMG
		 		(
			 		B_IMG_URL, B_I_CHECKOUT_URL, B_I_DATE, B_BANNER_ID
				)
		 VALUES 
		  	( 
			  #{bannerImageUrl},  
			  #{bannerImageCheckOutUrl}, 
			  NOW(),
			  #{bannerId}
		  	)
	</insert>
	
	<!-- 修改一个banner -->
	<update id="updateBannerById" parameterType="ActionBanner">
		update 
			TB_ACTION_BANNER 
		SET 
			B_START_DATE = #{bannerStartDate},  
			B_END_DATE = #{bannerEndDate}, 
			B_STATUS = #{bannerStatus},
			B_NAME = #{bannerName},  
			B_DESC = #{bannerDesc},
			B_TYPE = #{bannerType}
		WHERE
			B_ID = #{bannerId}
	</update>
	
	<!-- 停止某一个banner -->
	<update id="stopBannerById" parameterType="int">
		update TB_ACTION_BANNER SET 
		B_STATUS = 2
		WHERE B_ID = #{bannerId} AND B_STATUS = 1
	</update>
	
	<!-- 获取一个banner -->
	<select id="selectBannerById" parameterType="int" resultType="ActionBanner">
		SELECT
			<include refid="bannerAllField"/>
		FROM
			TB_ACTION_BANNER
		WHERE
			B_ID = #{bannerId}
	</select>
	
	<!-- 根据ID，获取一个banner的图片 -->
	<select id="selectBannerImgByBannerId" parameterType="int" resultType="ActionBannerImage">
		SELECT
			<include refid="imgAllField"/>
		FROM
			TB_ACTION_BANNER_IMG
		WHERE
			B_BANNER_ID = #{bannerId} 
		ORDER BY B_I_DATE DESC
	</select>
	
	<!-- 根据banner图片ID，删除图片 -->
	<delete id="deleteBannerImgById" parameterType="int">
		DELETE FROM 
			TB_ACTION_BANNER_IMG 
		WHERE B_I_ID = #{imgId }
	</delete>
	
	<!-- 批量修改时间 -->
	<update id="sort" parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" separator=";">
				UPDATE 
					TB_ACTION_BANNER_IMG 
				<set>
					B_I_DATE = #{item.bannerCreateDate}
				</set>
				WHERE 
					B_I_ID = #{item.bannerImageId}
	   </foreach>
	</update>
</mapper>
