<?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="dspBannerSqlMapper">

	<sql id="base_column">
		id,dsp_id as dspId,advertiser_id as advertiserId,path,height,width,rheight,rwidth,state,memo,updated
	</sql>

	<insert id="add" parameterType="java.util.List">
		insert into t_dsp_banner(dsp_id,advertiser_id,path,height,width,rheight,rwidth,state,memo,updated) values
		<foreach collection="dspBannerList" item="item" index="index" separator=",">
	    	( #{item.dspId}, #{item.advertiserId}, #{item.path}, #{item.height}, #{item.width}, 
	    		#{item.rheight}, #{item.rwidth}, 1, "", NOW() )
        </foreach>
	</insert>
	
	<update id="update" parameterType="java.util.List">
	 	<foreach collection="dspBannerList" item="item" index="index" open="" close="" separator=";">
	        update t_dsp_banner set path = #{item.path},height = #{item.height},width = #{item.width},
	        rheight = #{item.rheight}, rwidth = #{item.rwidth}, state = #{item.state},
	        <if test="item.memo != null and item.memo != '' ">
	        	memo = #{item.memo},
	       	</if>
	        updated = NOW()
           	where id=#{item.id} and dsp_id = #{item.dspId} and advertiser_id = #{item.advertiserId}
    	</foreach>
	</update>
	
	<select id="getAll" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.DspBanner">
		select <include refid="base_column"/>
		from t_dsp_banner where advertiser_id in
		<foreach item="advertiserId" collection="advertiserIdList" open="(" separator="," close=")">
			#{advertiserId}
		</foreach>
		and dsp_id = #{dspId}
	</select>
	
	<select id="get" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.DspBanner">
		select 
		<include refid="base_column"/>
		from t_dsp_banner where id in
		<foreach item="bannerId" collection="bannerIdList" open="(" separator="," close=")">
			#{bannerId}
		</foreach>
		<if test="dspId != null and dspId != '' ">
	    	and dsp_id = #{dspId}
	    </if>
	    <if test="advertiserId != null and advertiserId != '' ">
	    	and advertiser_id = #{advertiserId}
	    </if>
	</select>
	
	<select id="selectMaxId" parameterType="java.util.Map" resultType="java.lang.Long">
		select id from t_dsp_banner where dsp_id = #{dspId} and advertiser_id = #{advertiserId}
		<if test="bannerId != null and bannerId != '' ">
	    	and id != #{bannerId}
	    </if>
	</select>
	
	<select id="queryState" parameterType="java.util.Map" resultType="java.util.Map">
		select id as bannerId,dsp_id as dspId,advertiser_id as advertiserId,state,memo as refuseReason
		from t_dsp_banner where id in
		<foreach item="bannerId" collection="bannerIdList" open="(" separator="," close=")">
			#{bannerId}
		</foreach>
		<if test="dspId != null and dspId != '' ">
	    	and dsp_id = #{dspId}
	    </if>
	    <if test="advertiserId != null and advertiserId != '' ">
	    	and advertiser_id = #{advertiserId}
	    </if>
	</select>
	
</mapper>