<?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 advertiser_id = #{item.advertiserId}, path = #{item.path}, height = #{item.height}, 
	        width = #{item.width},rheight = #{item.rheight}, rwidth = #{item.rwidth},
	        <if test="item.state != null and item.state != '' ">
	        	state = #{item.state},
	       	</if>
	        <if test="item.memo != null and item.memo != '' ">
	        	memo = #{item.memo},
	       	</if>
	        updated = NOW()
           	where id=#{item.id} and dsp_id = #{item.dspId}
    	</foreach>
	</update>
	
	<select id="getAll" parameterType="java.util.Map" resultType="java.util.Map">
		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="java.util.Map">
		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>
	</select>
	
	<select id="selectMaxId" parameterType="java.util.Map" resultType="java.lang.Long">
		select max(id) as 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>
	</select>
	
	<select id="countByParams" parameterType="java.util.Map" resultType="int">
		select count(*) 
		from t_dsp_banner a ,t_dsp_agent b
		<where>
				a.dsp_id = b.id
			<if test="searchValue !=null">
				and b.name like #{searchValue}
			</if>
			<if test="backState !=null">
				and a.state = #{backState}
			</if>
		</where>
	</select>
	
	<select id="findByParams" parameterType="java.util.Map" resultType="java.util.Map">
		select a.id,a.dsp_id,a.path,a.width,a.height,a.rwidth,a.rheight,a.state,a.memo,a.updated,b.name
		from t_dsp_banner a,t_dsp_agent b
		<where>
		a.dsp_id = b.id
		<if test="searchValue !=null">
			and b.name like #{searchValue}
		</if>
		<if test="backState !=null">
			and a.state = #{backState}
		</if>
		</where>
		order by a.id desc
		limit #{pager.offset}, #{pager.limit} 
	</select>
	             
	<update id="updateBackPass" parameterType="HashMap">
		update t_dsp_banner a ,t_dsp_banner_operator b 
		set a.state=#{state},a.updated=#{updated},
		b.checked=0,b.supdated=#{supdated} 
		where a.id = b.banner_id and a.id in
		<foreach item="id" collection="auditIdList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
	</update>
	
	<update id="updateBackNoPass" parameterType="HashMap">
		update t_dsp_banner a ,t_dsp_banner_operator b 
		set a.state=#{state},a.updated=#{updated},a.memo=#{memo},
		b.checked=3,b.supdated=#{supdated} 
		where a.id = b.banner_id and a.id in
		<foreach item="id" collection="auditIdList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
	</update>
	
	
	
	<select id="findByParamMap" parameterType="java.util.Map" resultType="java.util.Map">
		select a.id,a.dsp_id,a.path,a.width,a.height,a.rwidth,a.rheight,a.state,a.memo,a.updated,b.name
		from t_dsp_banner a,t_dsp_agent b
		<where>
		a.dsp_id = b.id
		<if test="searchValue !=null">
			and b.name like #{searchValue}
		</if>
		<if test="backState !=null">
			and a.state = #{backState}
		</if>
		</where>
	</select>
	<!-- ssssss后台审核通过后续判断是否t_dsp_operator有没有则添加 -->
	<select id="countByParamsOperator" parameterType="java.util.Map" resultType="int">
		select count(*) 
		from t_dsp_banner a 
		INNER JOIN t_dsp_agent b on a.dsp_id = b.id
		LEFT JOIN t_dsp_banner_operator c on a.id = c.banner_id and a.dsp_id = c.dsp_id
		<where>
			a.state =0 
			<if test="zoneList.size()>0">
				and
			   	<foreach item="zone" collection="zoneList"
			   		 open="(" separator="or" close=")" >
				      a.rwidth &lt;= #{zone.rwidth} and a.rheight &lt;= #{zone.rheight}
				</foreach>
			</if>
			<if test="searchValue !=null">
				and b.name like #{searchValue}
			</if>
			<if test="operatorChecked !=null">
				and c.checked = #{operatorChecked}
			</if>
		</where> 
	</select>
	
	<select id="findByParamsOperator" parameterType="java.util.Map" resultType="java.util.Map">
		select a.id,a.dsp_id,a.path,a.width,a.height,a.rwidth,a.rheight,a.state,a.memo,c.checked,c.supdated,b.name
		from t_dsp_banner a 
		INNER JOIN t_dsp_agent b on a.dsp_id = b.id
		LEFT JOIN t_dsp_banner_operator c on a.id = c.banner_id and a.dsp_id = c.dsp_id
		<where>
			a.state =0
			<if test="zoneList.size()>0">
				and
			   	<foreach item="zone" collection="zoneList"
			   		 open="(" separator="or" close=")" >
				      a.rwidth &lt;= #{zone.rwidth} and a.rheight &lt;= #{zone.rheight}
				</foreach>
			</if>
			<if test="searchValue !=null">
				and b.name like #{searchValue}
			</if>
			<if test="operatorChecked !=null">
				and c.checked = #{operatorChecked}
			</if>
		</where>
		order by a.id desc
		limit #{pager.offset}, #{pager.limit} 
	</select>
	
	
	<update id="updateOperatorPass" parameterType="HashMap">
		update t_dsp_banner a ,t_dsp_banner_operator b 
		set a.updated=#{updated},
		b.checked=#{checked},b.supdated=#{supdated} 
		where a.id = b.banner_id and a.id =#{bannerId}
	</update>
	
	<update id="updateOperatorNoPass" parameterType="HashMap">
		update t_dsp_banner a ,t_dsp_banner_operator b 
		set a.updated=#{updated},a.memo=#{memo},
		b.checked=#{checked},b.supdated=#{supdated} 
		where a.id = b.banner_id and a.id =#{bannerId}
	</update>
	
	
	<select id="getState" parameterType="HashMap" resultType="int">
		select IFNULL(checked,-1) as checked from t_dsp_banner_operator
		where operator_id=#{operatorId} and banner_id = #{bannerId}
	</select>
	
	<select id="findById" parameterType="int" resultType="DspBanner">
		select <include refid="base_column"/> from t_dsp_banner where id = #{id}
	</select>
	
	<insert id="addBannerOperator" parameterType="HashMap">
		insert t_dsp_banner_operator set 
		operator_id = #{operatorId},
		dsp_id = #{dspId},
		banner_id = #{bannerId},
		checked = 0,
		supdated = #{supdated}
		
	</insert>
	
</mapper>