<?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="auditBannerSqlMapper">
	<sql id="base_column_banner">
		 a.id as id,
		 a.banner_template_id as bannerTemplateId,
		 a.order_id as orderId,
		 a.campaign_id as campaignId,
		 a.adgroup_id as adGroupId,
		 a.visit_address as visitAddress,
		a.click_address as clickAddress,
		a.impression_address as impressionAddress,
		a.status as status1,
		a.checked as checked,
		a.updated as updated,
		a.supdated as supdated,
		a.memo as memo
	</sql>
	<sql id="base_column_bannertemplate">
		b.id as bannerTemplateId,
		b.agent_id as agentId,
		b.advertiser_id as advertiserId,
		b.agent_id as agentId,
		b.name as name,
		b.path as path,
		b.path_small as pathSmall,
		b.type as type,
		b.width as width,
		b.height as height,
		b.status as status,
		b.updated as updated	
	</sql>
	
	<select id="countByParams" parameterType="map" resultType="int">
	select count(a.id) from t_banner a ,t_banner_template b 
		<where>
		b.status != -1
				and a.banner_template_id = b.id 
				<if test="key!=null and value!=null">
					<choose>
						<when test="key=='name'">
							and b.name like #{value}
						</when>
					</choose>
				</if>
				<if test="checked !=null">
					and a.checked like #{checked}
				</if>
		</where>
	</select>
	
	<select id="findByParams" parameterType="map" resultMap="AuditBanner">  
	
	select <include refid="base_column_banner"/> ,
		<include refid="base_column_bannertemplate"/>,
(select	name from t_advertiser where id=b.advertiser_id) as advertiserName,(select name from t_industry where id = (select industry_id from t_advertiser where id=b.advertiser_id)) as industryName from t_banner a
    left join t_banner_template b on a.banner_template_id=b.id
	left join t_order c on a.order_id= c.id
	left JOIN t_advertiser d on c.advertiser_id = d.id
	left join t_industry e on d.industry_id = e.id
	<where>
	b.status != -1
			<if test="key!=null and value!=null">
					<choose>
						<when test="key=='name'">
							and b.name like #{value}
						</when>
					</choose>
			</if>
			<if test="checked !=null">
				and a.checked like #{checked}
			</if>
	</where>
	order by a.id   desc
	limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="findAudit" parameterType="map" resultMap="AuditBanner">  
	
	select <include refid="base_column_banner"/> ,
		<include refid="base_column_bannertemplate"/>,
	(select	name from t_advertiser where id=b.advertiser_id) as advertiserName,(select name from t_industry where id = (select industry_id from t_advertiser where id=b.advertiser_id)) as industryName from t_banner a
    left join t_banner_template b on a.banner_template_id=b.id
	left join t_order c on a.order_id= c.id
	left JOIN t_advertiser d on c.advertiser_id = d.id
	left join t_industry e on d.industry_id = e.id
	<where>
	b.status != -1
			<if test="key!=null and value!=null">
					<choose>
						<when test="key=='name'">
							and b.name like #{value}
						</when>
					</choose>
			</if>
			<if test="checked !=null">
				and a.checked like #{checked}
			</if>
	</where>
	order by a.id   desc
	</select>
	
	<resultMap type="com.cloudcross.ssp.model.AuditBanner" id="AuditBanner">
	<id property="id" column="id"/>
	  <result property="bannerTemplateId" column="bannerTemplateId"/> 
	<result property="orderId" column="orderId"/>
	<result property="campaignId" column="campaignId"/> 
  	<result property="adGroupId" column="adGroupId"/>   
    <result property="visitAddress" column="visitAddress"/> 
    <result property="clickAddress" column="clickAddress"/> 
    <result property="impressionAddress" column="impressionAddress"/>
    <result property="checked" column="checked"/>
    <result property="updated" column="updated"/> 
     <result property="memo" column="memo"/>
     <result property="operatorChecked" column="operatorChecked"/>
     <result property="industryName" column="industryName"/>
     <result property="advertiserName" column="advertiserName"/>
    <association property="bannerTemplate" javaType="com.cloudcross.ssp.model.BannerTemplate" resultMap="bannerTemplate"/>
</resultMap>  
<resultMap type="com.cloudcross.ssp.model.BannerTemplate" id="bannerTemplate">  
   <id property="id" column="bannerTemplateId"/>  
   <result property="advertiserId" column="advertiserId"/> 
   <result property="name" column="name"/>
    <result property="path" column="path"/>
    <result property="pathSmall" column="pathSmall"/>  
   <result property="type" column="type"/>
   <result property="width" column="width"/> 
   <result property="height" column="height"/> 
   <result property="status" column="status"/>   
   <result property="updated" column="updated"/>     
</resultMap>  
	
	<update id="updateBannerStatus" parameterType="HashMap">
		update t_banner a,t_banner_template b
		set a.checked=#{checked},b.updated = #{updated},a.updated = #{updated}
		<if test="memo != null">
		,a.memo = #{memo}
		</if>
		where
		a.banner_template_id = b.id and
		a.id in
		<foreach item="id" collection="auditBannerIdList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
		
	</update>
	
	<update id="updateBannerOperatorStatus" parameterType="HashMap">
		update t_banner_operator set supdated = #{supdated}
		<if test="checke==2">
			,checked = 2
		</if>
		<if test="checke==1">
		,checked = 0
		</if>
		where banner_id in 
		<foreach item="id" collection="auditBannerIdList" open="(" separator=","
			close=")">
			#{id}
		</foreach> 
	</update>
	
	<update id="updateOperatorBannerTime" parameterType="HashMap">
		update t_banner_operator
		set supdated = #{supdated}
		where banner_id in
		<foreach item="id" collection="auditBannerIdList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
	</update>
	
	
		<select id="countByZoneSizeOperator" parameterType="HashMap" resultType="int">
		select count(*) 
		from t_banner a 
		inner join t_banner_template b
		on a.banner_template_id = b.id 
		where b.status != -1 
		and checked != 2 
		and checked !=0
	   	<if test="zoneList.size()>0">
			and
		   	<foreach item="zone" collection="zoneList"
		   		 open="(" separator="or" close=")" >
			      b.width &lt;= #{zone.width} and b.height &lt;= #{zone.height}
			</foreach>
		</if>
		<if test="searchKey!=null and searchValue!=null">
			and b.name like #{searchValue}
		</if>
		<if test="checked!=null">
			and a.checked like #{checked}
		</if>
	</select>
	
	<select id="findByZoneSizeOperator" parameterType="HashMap" resultMap="AuditBanner">  
		select <include refid="base_column_banner"/> ,
		<include refid="base_column_bannertemplate"/>,
		IFNULL(bo.checked,-1) as operatorChecked
		from t_banner a 
		inner join t_banner_template b
		on a.banner_template_id = b.id
		where b.status != -1 
		and checked != 2 
		and checked !=0
		<if test="zoneList.size()>0">
			and
		   	<foreach item="zone" collection="zoneList"
		   		 open="(" separator="or" close=")" >
			      b.width &lt;= #{zone.width} and b.height &lt;= #{zone.height}
			</foreach>
		</if>
	   	<if test="searchKey!=null and searchValue!=null">
			and b.name like #{searchValue}
		</if>
		<if test="checked!=null">
			and a.checked like #{checked}
		</if>
	    order by  a.id   desc
	    <if test="pager!=null">
		limit #{pager.offset}, #{pager.limit}
		</if>
	</select>
	
	<select id="findByIdOperator" parameterType="int" resultMap="AuditBanner">  
		select <include refid="base_column_banner"/> ,
		<include refid="base_column_bannertemplate"/>,
		IFNULL(bo.checked,-1) as operatorChecked
		from t_banner a 
		inner join t_banner_template b 
		on a.banner_template_id = b.id 
		left join t_banner_operator bo
		on a.id=bo.banner_id where a.id=#{id}
	</select>

<select id="countByOperator" parameterType="HashMap" resultType="int">
		select count(*) 
		from t_banner a 
		inner join t_banner_template b 
		on a.banner_template_id = b.id 
		left join t_banner_operator bo
		on a.id=bo.banner_id 
		and bo.operator_id=#{operatorId}  
		where a.status = 0 
		and b.status = 0 
		and a.checked = 1 
	   	<if test="zoneList.size()>0">
			and
		   	<foreach item="zone" collection="zoneList"
		   		 open="(" separator="or" close=")" >
			      b.width &lt;= #{zone.width} and b.height &lt;= #{zone.height}
			</foreach>
		</if>
		<if test="value!=null">
			and b.name like #{value}
		</if>
		<if test="operatorChecked!=null">
		<choose>
			<when test="operatorChecked == 0">
				and ISNULL(bo.checked) or bo.checked =0
			</when>
			<otherwise>
				and bo.checked=#{operatorChecked}
			</otherwise>	
		</choose>
		</if>
		<if test="agentId!=null">
			and b.agent_id = #{agentId}
		</if>
	</select>
	
	<select id="findByOpertor" parameterType="HashMap" resultMap="AuditBanner">  
		select <include refid="base_column_banner"/> ,
		<include refid="base_column_bannertemplate"/>,
		IFNULL(bo.checked,-1) as operatorChecked
		from t_banner a 
		inner join t_banner_template b
		on a.banner_template_id = b.id 
		left join t_banner_operator bo
		on a.id=bo.banner_id 
		and bo.operator_id=#{operatorId}  
		where a.status = 0 
		and b.status = 0 
		and a.checked = 1
		<if test="zoneList.size()>0">
			and
		   	<foreach item="zone" collection="zoneList"
		   		 open="(" separator="or" close=")" >
			      b.width &lt;= #{zone.width} and b.height &lt;= #{zone.height}
			</foreach>
		</if>
	   	<if test="value!=null">
			and b.name like #{value}
		</if>
		<if test="operatorChecked!=null">
		<choose>
			<when test="operatorChecked == 0">
				and ISNULL(bo.checked) or bo.checked =0
			</when>
			<otherwise>
				and bo.checked=#{operatorChecked}
			</otherwise>	
		</choose>
		</if>
		<if test="agentId!=null">
			and b.agent_id = #{agentId}
		</if>
	    order by  a.id   desc
	    <if test="pager!=null">
		limit #{pager.offset}, #{pager.limit}
		</if>
	</select>


	<update id="updateBannerStatusOperator" parameterType="HashMap">
		update t_banner 
			set checked = #{checked},
			updated = #{updated}
			where id in
			<foreach item="bannerId" collection="bannerIdList"
		      open="(" separator="," close=")">
		        #{bannerId}
			</foreach>
	</update>
	
	<insert id="addOperatorBanner" parameterType="HashMap">
	insert into t_banner_operator 
			(operator_id,banner_id,checked,supdated)
			values(#{operatorId},#{bannerId},#{operatorChecked},#{supdated})
		</insert>
	
	
	<update id="updateOperatorBanner" parameterType="HashMap">
		update t_banner_operator 
			set operator_id=#{operatorId},checked = #{operatorChecked},supdated = #{supdated}
			where banner_id = #{bannerId}
	</update>
	
</mapper>