<?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="bannerTemplateSqlMapper">
	<sql id="base_columnadvertiser">
		t_banner_template.id,
		agent_id as agentId,
		advertiser_id as advertiserId,
		operator_id as operatorId,
		name,
		path,
		path_small as pathSmall,
		type,
		width,
		height,
		t_banner_template.status,
		t_banner_template.updated
	</sql>
	
	<sql id="base_columnoperator">
		id,
		advertiser_id as advertiserId,
		name,
		path,
		type,
		width,
		height,
		status,
		updated
	</sql>
	<select id="countByParams" parameterType="map" resultType="int">
		select count(id) 
		from t_banner_template
		<where>
			id in (select banner_template_id from t_banner where adgroup_id=#{adGroupId})
			and status!=-1
			<if test="key!=null and value!=null">
				<choose>
					<when test="key=='name'">
					and name like #{value}
					</when>
				</choose>
			</if>
		</where>
	</select>
	
	<select id="countByParams3operator" parameterType="map" resultType="int">
		select count(id)
		from t_banner_template
		<where>
		id not in (select banner_template_id from t_banner where adgroup_id=#{adGroupId})
			and status != -1
			<if test="advertiserId != null">
				and advertiser_id=#{advertiserId}
			</if>
		</where>
	</select>
	
	<select id="countByParams3advertiser" parameterType="map" resultType="int">
		select count(id)
		from t_banner_template
		<where>
		id not in (select banner_template_id from t_banner where adgroup_id=#{adGroupId})
			and t_banner_template.status != -1
			<if test="advertiserId != null">
				and advertiser_id=#{advertiserId}
			</if>
			<if test="type != null">
				and type=#{type}
			</if>
			<if test="height != null">
				and height=#{height}
			</if>
			<if test="width != null">
				and width=#{width}
			</if>
			<if test="value != null">
				and name like #{value}
			</if>
			
		</where>
	</select>
	<select id="findByParams" parameterType="map" resultType="com.cloudcross.ssp.model.BannerTemplate">
		select t_banner_template.id as id, t_banner_template.advertiser_id as advertiser_id,t_banner_template.name as name,
        t_banner_template.path as path,t_banner_template.type as type,t_banner_template.width as width,t_banner_template.height as height,
        t_banner_template.status as status,t_banner_template.updated as updated,t_banner.id as TBannerId from t_banner_template left join t_banner on t_banner_template.id = t_banner.banner_template_id
        <where> 
        t_banner.adgroup_id = #{adGroupId} 
			and status!=-1
			<if test="key!=null and value!=null">
				<choose>
					<when test="key=='name'">
					and name like #{value}
					</when>
				</choose>
			</if>
		</where>
		order by id desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	<select id="findById" parameterType="long" resultType="com.cloudcross.ssp.model.BannerTemplate">
		select
		<include refid="base_columnoperator"/>
		from t_banner_template
		<where>
		id = #{id}
		</where>
	</select>
	
	<update id="updateBannerTemplateStatus" parameterType="HashMap">
		update t_banner_template
		set status=#{status},updated = #{updated}
		where id in
		<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
	</update>
	
	<update id="updateBannerTime" parameterType="HashMap">
		update t_banner 
		set supdated=#{updataTime}
		where banner_template_id in 
		<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
	</update>
	
	<!-- 模板状态改变，更形t_banner_operator时间 -->
	<update id="updateBannerOperatorTime" parameterType="HashMap">
		update t_banner_operator 
		set supdated=#{updataTime}
		where banner_id in (select id from t_banner 
		where t_banner.banner_template_id in
		<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>)
	</update>
	
<update id="updataDeleteBannerTemplateStatus" parameterType="HashMap">
update t_banner_template  
set status=#{status},
updated = #{updated}
where id in 
	<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>

</update>

<update id="updataDeleteBannerStatus" parameterType="HashMap">
update t_banner 
set status=#{status},
 updated = #{updated},supdated = #{supdated} 
where banner_template_id in 
	<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>

</update>



<update id="updataDeleteBannerTemplateOperatorStatus" parameterType="HashMap">
		update t_banner_operator 
		set checked =3,
		supdated=#{supdated}
		where banner_id in (select id from t_banner 
		where t_banner.banner_template_id in
		<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>)
	</update>
	
	
	<select id="findByCampaignId" parameterType="long" resultType="com.cloudcross.ssp.model.AdGroup">
	    select
		<include refid="base_columnadvertiser"/>
		from t_adgroup 
		where campaign_id=#{campaignId} and t_adgroup.status!=-1
	</select>
	
		
	<update id="alterName" parameterType="map">
		update t_banner_template set name = #{name} , updated = now() where id = #{id}
	</update>
	
	<update id="editadvertiser" parameterType="com.cloudcross.ssp.model.BannerTemplate">
		update t_banner_template 
		set name = #{name},
		path = #{path},
		path_small = #{pathSmall},
		width = #{width},
		height = #{height},
		type = ${type},
		updated = #{updated}
		WHERE id = #{id}
	</update>
	
	<!-- 替换素材对应创意审核状态更新 -->
	<update id="updataBannerReplace" parameterType="map">
	update t_banner 
	set checked = 0,
		updated = #{updated},
		supdated = #{supdated}	
	where banner_template_id = #{id}
	</update>
	<update id="updataBannerOperatorReplace" parameterType="map">
	update t_banner_operator 
	set checked = 0,
		supdated = #{supdated}	
	where banner_id in (select id from t_banner where banner_template_id =#{id})
	</update>
	<update id="updataIds" parameterType="com.cloudcross.ssp.model.BannerTemplate">
		update t_banner_template a
		set a.advertiser_id = #{advertiserId},
		a.agent_id = #{agentId}	
		WHERE a.id = #{id}
	</update>
		<update id="editoperator" parameterType="com.cloudcross.ssp.model.BannerTemplate">
		update t_banner_template a,t_banner b
		set a.name = #{name},
		a.updated = #{updated},
		b.visit_address = #{banner.visitAddress},
		b.click_address = #{banner.clickAddress},
		b.impression_address = #{banner.impressionAddress}
		WHERE a.id = b.banner_template_id and a.id = #{id}
	</update>
	<select id="countByParams2advertiser" parameterType="map" resultType="int">
		select count(t_banner_template.id)
		from t_banner_template
		<where>
			<if test="status != null">
			t_banner_template.status=#{status}
			</if>
			<if test="status == null">
				t_banner_template.status in (0,1)
			</if>
			<if test="agentId != null">
				and agent_id = #{agentId} 
			</if>
			<if test="type != null">
				and type=#{type}
			</if>
			<if test="height != null">
				and height=#{height}
			</if>
			<if test="width != null">
				and width=#{width}
			</if>
			<if test="advertiserId != null">
				and advertiser_id = #{advertiserId}
			</if>
			<if test="value != null">
				and name like #{value}
			</if>
		</where>

	</select>
	
	<select id="countByParams2operator" parameterType="map" resultType="int">
		select count(id)
		from t_banner_template
		<where>
			<if test="status != null">
				status=#{status}
			</if>
			<if test="status == null">
				status in (0,1)
			</if>
			<!-- <if test="advertiserId != null"> and advertiser_id=#{advertiserId} 
				</if> -->
			<if test="type != null">
				and type=#{type}
			</if>
			<if test="height != null">
				and height=#{height}
			</if>
			<if test="width != null">
				and width=#{width}
			</if>
			<if test="value != null">
				and name like #{value}
			</if>
		</where>

	</select>

	<select id="findByParams2advertiser" parameterType="map" resultType="com.cloudcross.ssp.model.BannerTemplate">
		select <include refid="base_columnadvertiser"/>
		from t_banner_template
		<where>
			<if test="status != null">
				t_banner_template.status=#{status}
			</if>
			<if test="status == null">
				t_banner_template.status in (0,1)
			</if>
			<if test="agentId != null">
				and agent_id = #{agentId} 
			</if>
			<if test="type != null">
				and type=#{type}
			</if>
			<if test="height != null">
				and height=#{height}
			</if>
			<if test="width != null">
				and width=#{width}
			</if>
			<if test="advertiserId != null">
				and advertiser_id = #{advertiserId}
			</if>
			<if test="value != null">
				and name like #{value}
			</if>
		</where>
		order by t_banner_template.id desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	<select id="findByParams2Operator" parameterType="map" resultType="com.cloudcross.ssp.model.BannerTemplate">
		select
		<include refid="base_columnoperator" />
		from t_banner_template
		<where>
			<if test="status != null">
				status=#{status}
			</if>
			<if test="status == null">
				status in (0,1)
			</if>
			<!-- <if test="advertiserId != null"> and advertiser_id=#{advertiserId} 
				</if> -->
			<if test="type != null">
				and type=#{type}
			</if>
			<if test="height != null">
				and height=#{height}
			</if>
			<if test="width != null">
				and width=#{width}
			</if>
			<if test="value != null">
				and name like #{value}
			</if>
		</where>
		order by id desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	
		
	<select id="findByParams4advertiser" parameterType="map" resultType="com.cloudcross.ssp.model.BannerTemplate">
		select
		<include refid="base_columnadvertiser"/>
		from t_banner_template
		<where>
		id not in (select banner_template_id from t_banner where adgroup_id=#{adGroupId})
		and t_banner_template.status != -1
			<if test="advertiserId != null">
				and advertiser_id=#{advertiserId}
			</if>
			<if test="type != null">
				and type = #{type}
			</if>
			<if test="height != null">
				and height = #{height}
			</if>
			<if test="width != null">
				and width = #{width}
			</if>
			<if test="value != null">
				and name like #{value}
			</if>
		</where>
		order by  id  desc
		limit #{pager.offset}, #{pager.limit}
	</select>

<select id="findByParams4operator" parameterType="map" resultType="com.cloudcross.ssp.model.BannerTemplate">
		select
		<include refid="base_columnoperator"/>
		from t_banner_template
		<where>
		id not in (select banner_template_id from t_banner where adgroup_id=#{adGroupId})
		and	status != -1
			<if test="advertiserId != null">
				and advertiser_id=#{advertiserId}
			</if>
		</where>
		order by  id  desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	<insert id="addBannerTemplate" parameterType="com.cloudcross.ssp.model.BannerTemplate" useGeneratedKeys="true"
        keyProperty="id">
insert into t_banner_template (agent_id,advertiser_id,operator_id,name,path,path_small,type,width,height,status,updated) 
values(#{agentId},#{advertiserId},#{operatorId},#{name},#{path},#{pathSmall},#{type},#{width},#{height},#{status},#{updated})	

</insert>
	
</mapper>