<?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="adGroupSqlMapper">
	<sql id="base_column">
		t_adgroup.id,
		t_adgroup.agent_id as agentId,
		t_adgroup.advertiser_id as advertiserId,
		t_adgroup.order_id as orderId,
		t_adgroup.campaign_id as campaignId,
		t_adgroup.operator_id as operatorId,
		name as adGroupName,
		active_time as startDate,
		expire_time as endDate,
		totalbudget as totalBudget,
		mode,
		price,
		dailybudget as dailyBudget,
		impression as maxImpression,
		click as maxClick,
		conversion as maxConversion,
		daily_impression as dailyMaxImpression,
		daily_click as dailyMaxClick,
		daily_conversion as dailyMaxConversion,
		per_impression_type as perImpressionType,
		per_impression as perImpression,
		per_click_type as perClickType,
		per_click as perClick,
		position,
		site,
		retarget,
		period,
		t_adgroup.status,
		t_adgroup.updated as updated,
		t_adgroup.os os
	</sql>
	
	
	<select id="countByParams" parameterType="map" resultType="int">
		select count(id) 
		from t_adgroup
		where status != -1
		<if test="agentId!=null">
			 and agent_id = #{agentId}
		</if>
		<if test="agentId == null">
			and advertiser_id = #{advertiserId}
		</if>
		<if test="orderMark == null">
			and order_id = #{orderId}
		</if>
		<if test="campaignMark == null">
			and campaign_id = #{campaignId}
		</if>
		<if test="adGroupName != null">
			and name like #{adGroupName}
		</if>
			<if test="selectedStatus1!=null">
			and now()>active_time and expire_time>now() and status=0
			</if>
			<if test="selectedStatus2!=null">
			and active_time>now()
			</if>
			<if test="selectedStatus3!=null">
			and now()>expire_time and status=1
			</if>
			<if test="selectedStatus4!=null">
			and status=1
			</if>
	</select>
	
	
	<select id="countByParamsBack" parameterType="map" resultType="int">
		select count(id) 
		from t_adgroup
		where status != -1
		<if test="agentId!=null">
			 and agent_id = #{agentId}
		</if>
		<if test="agentId == null">
			and advertiser_id = #{advertiserId}
		</if>
		<if test="orderMark != null">
			and order_id = #{orderId}
		</if>
		<if test="campaignMark != null">
			and campaign_id = #{campaignId}
		</if>
		<if test="adGroupName != null">
			and name like #{adGroupName}
			</if>
			<if test="selectedStatus1!=null">
			and now()>active_time and expire_time>now() and status=0
			</if>
			<if test="selectedStatus2!=null">
			and active_time>now()
			</if>
			<if test="selectedStatus3!=null">
			and now()>expire_time and status=1
			</if>
			<if test="selectedStatus4!=null">
			and status=1
			</if>
	</select>
	
	
	
	<select id="findByParams" parameterType="map" resultType="com.cloudcross.ssp.model.AdGroup">
		select
		<include refid="base_column"/>, (select count(t_banner.id) from t_banner where t_banner.adgroup_id = t_adgroup.id and t_banner.status !=-1
		) as bannerNumber
		from t_adgroup 
		<where>
			 t_adgroup.status!=-1
			<if test="agentId!=null">
			 and t_adgroup.agent_id = #{agentId}
		</if>
		<if test="agentId == null">
			and t_adgroup.advertiser_id = #{advertiserId}
		</if>
		<if test="orderMark == null">
			and t_adgroup.order_id = #{orderId}
		</if>
		<if test="campaignMark == null">
			and t_adgroup.campaign_id = #{campaignId}
		</if>
		<if test="adGroupName != null">
			and name like #{adGroupName}
			</if>
			<if test="selectedStatus1!=null">
			and now()>active_time and expire_time>now() and t_adgroup.status=0
			</if>
			<if test="selectedStatus2!=null">
			and active_time>now()
			</if>
			<if test="selectedStatus3!=null">
			and now()>expire_time and t_adgroup.status=1
			</if>
			<if test="selectedStatus4!=null">
			and t_adgroup.status=1
			</if>
		</where>
		group by t_adgroup.id
		order by t_adgroup.id desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	
		<select id="findByParamsBack" parameterType="map" resultType="com.cloudcross.ssp.model.AdGroup">
		select
		<include refid="base_column"/>, count(banner_template_id) as bannerNumber
		from t_adgroup left join t_banner
		on t_adgroup.id=t_banner.adgroup_id
		<where>
			t_adgroup.campaign_id=#{campaignId} and status!=-1
			<if test="agentId!=null">
			 and agent_id = #{agentId}
		</if>
		<if test="agentId == null">
			and advertiser_id = #{advertiserId}
		</if>
		<if test="orderMark != null">
			and order_id = #{orderId}
		</if>
		<if test="campaignMark != null">
			and campaign_id = #{campaignId}
		</if>
		<if test="adGroupName != null">
			and name like #{adGroupName}
			</if>
			<if test="selectedStatus1!=null">
			and now()>active_time and expire_time>now() and status=0
			</if>
			<if test="selectedStatus2!=null">
			and active_time>now()
			</if>
			<if test="selectedStatus3!=null">
			and now()>expire_time and status=1
			</if>
			<if test="selectedStatus4!=null">
			and status=1
			</if>
		</where>
		group by t_adgroup.id
		order by t_adgroup.id desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	
	<select id="findByParams1" parameterType="map" resultType="com.cloudcross.ssp.model.AdGroup">
		select
		<include refid="base_column"/>, count(banner_template_id) as bannerNumber
		from t_adgroup left join t_banner
		on t_adgroup.id=t_banner.adgroup_id
		<where>
			 t_adgroup.status!=-1
			<if test="agentId!=null">
			 and t_adgroup.agent_id = #{agentId}
		</if>
		<if test="agentId == null">
			and t_adgroup.advertiser_id = #{advertiserId}
		</if>
		<if test="orderMark == null">
			and t_adgroup.order_id = #{orderId}
		</if>
		<if test="campaignMark == null">
			and t_adgroup.campaign_id = #{campaignId}
		</if>
		<if test="adGroupName != null">
			and name like #{adGroupName}
			</if>
			<if test="selectedStatus1!=null">
			and now()>active_time and expire_time>now() and t_adgroup.status=0
			</if>
			<if test="selectedStatus2!=null">
			and active_time>now()
			</if>
			<if test="selectedStatus3!=null">
			and now()>expire_time and t_adgroup.status=1
			</if>
			<if test="selectedStatus4!=null">
			and t_adgroup.status=1
			</if>
		</where>
		group by t_adgroup.id
		order by t_adgroup.id desc
	</select>
	
	
	
	<update id="updateAdGroupStatus" parameterType="HashMap">
		update t_adgroup 
		set status=#{status},updated = #{updated}
		where 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_column"/>
		from t_adgroup 
		where campaign_id=#{campaignId} and status!=-1
	</select>
	
	<insert id="addAdGroup" parameterType="com.cloudcross.ssp.model.AdGroup"  useGeneratedKeys="true"
		keyProperty="id">
		insert into t_adgroup
			(name,campaign_id,order_id,agent_id,advertiser_id,active_time,expire_time,totalbudget,mode,
			price,
			<if test="operatorId !=null">operator_id,</if>
			<if test="dailyBudget !=null">dailybudget,</if>
			<if test="maxImpression !=null">impression,</if>
			<if test="maxClick !=null">	click,</if>
			<if test="maxConversion !=null"> conversion,</if>
			<if test="dailyMaxImpression !=null">daily_impression,</if>
			<if test="dailyMaxClick !=null">daily_click,</if>
			<if test="dailyMaxConversion !=null">daily_conversion,</if>
			<if test="perImpressionType !=null">per_impression_type,</if>
			<if test="perImpression !=null">per_impression,</if>
			<if test="perClickType !=null">per_click_type,</if>
			<if test="perClick !=null">per_click,</if>			
			<if test="retarget !=null">retarget,</if>
			<if test="period !=null">period,</if>
			<if test="site !=null">site,</if>
			<if test="position !=null">position,</if>
			<if test="os !=null">os,</if>
			status,updated)
		values
			(#{adGroupName},#{campaignId},#{orderId},#{agentId},#{advertiserId},#{startDate},#{endDate},#{totalBudget},#{mode},
			#{price},
			<if test="operatorId !=null">#{operatorId},</if>
			<if test="dailyBudget !=null">#{dailyBudget},</if>
			<if test="maxImpression !=null">#{maxImpression},</if>
			<if test="maxClick !=null">#{maxClick},</if>
			<if test="maxConversion !=null">#{maxConversion},</if>
			<if test="dailyMaxImpression !=null">#{dailyMaxImpression},</if>
			<if test="dailyMaxClick !=null">#{dailyMaxClick},</if>
			<if test="dailyMaxConversion !=null">#{dailyMaxConversion},</if>
			<if test="perImpressionType !=null">#{perImpressionType},</if>
			<if test="perImpression !=null">#{perImpression},</if>
			<if test="perClickType !=null">#{perClickType},</if>
			<if test="perClick !=null">#{perClick},</if>			
			<if test="retarget !=null">#{retarget},</if>
			<if test="period !=null">#{period},</if>
			<if test="site !=null">#{site},</if>
			<if test="position !=null">#{position},</if>
			<if test="os !=null">#{os},</if>
			#{status},NOW())
	</insert>
	
	<insert id="addAdGroupBack" parameterType="com.cloudcross.ssp.model.AdGroup"  useGeneratedKeys="true"
		keyProperty="id">
		insert into t_adgroup
			(name,campaign_id,order_id,activate_time,expire_time,totalbudget,
			<if test="price !=null">price,</if>
			<if test="dailyBudget !=null">dailybudget,</if>
			<if test="maxImpression !=null">impression,</if>
			<if test="maxClick !=null">	click,</if>
			<if test="maxConversion !=null"> conversion,</if>
			<if test="dailyMaxImpression !=null">daily_impression,</if>
			<if test="dailyMaxClick !=null">daily_click,</if>
			<if test="dailyMaxConversion !=null">daily_conversion,</if>
			<if test="perImpressionType !=null">per_impression_type,</if>
			<if test="perImpression !=null">per_impression,</if>
			<if test="perClickType !=null">per_click_type,</if>
			<if test="perClick !=null">per_click,</if>			
			<if test="retarget !=null">retarget,</if>
			<if test="period !=null">period,</if>
			<if test="site !=null">site,</if>
			<if test="position !=null">position,</if>
			status,updated)
		values
			(#{adGroupName},#{campaignId},#{orderId},#{startDate},#{endDate},#{totalBudget},
			<if test="price !=null">#{price},</if>
			<if test="dailyBudget !=null">#{dailyBudget},</if>
			<if test="maxImpression !=null">#{maxImpression},</if>
			<if test="maxClick !=null">#{maxClick},</if>
			<if test="maxConversion !=null">#{maxConversion},</if>
			<if test="dailyMaxImpression !=null">#{dailyMaxImpression},</if>
			<if test="dailyMaxClick !=null">#{dailyMaxClick},</if>
			<if test="dailyMaxConversion !=null">#{dailyMaxConversion},</if>
			<if test="perImpressionType !=null">#{perImpressionType},</if>
			<if test="perImpression !=null">#{perImpression},</if>
			<if test="perClickType !=null">#{perClickType},</if>
			<if test="perClick !=null">#{perClick},</if>			
			<if test="retarget !=null">#{retarget},</if>
			<if test="period !=null">#{period},</if>
			<if test="site !=null">#{site},</if>
			<if test="position !=null">#{position},</if>
			#{status},NOW())
	</insert>
	
	
	
	<select id="findById" parameterType="long" resultType="com.cloudcross.ssp.model.AdGroup">
		select
		<include refid="base_column"/>
		from t_adgroup
		<where>
		id = #{id}
		</where>
	</select>
	
	<update id="editAdGroup" parameterType="com.cloudcross.ssp.model.AdGroup">
		update t_adgroup
		set name = #{adGroupName},
		active_time = #{startDate},
		expire_time = #{endDate},		
		totalbudget = #{totalBudget},
		price = #{price},		
		dailybudget = IFNULL(#{dailyBudget},default(dailybudget)),
		impression = IFNULL(#{maxImpression},default(impression)),
		click =IFNULL( #{maxClick},default(click)),
		conversion=IFNULL( #{maxConversion},default(conversion)),
		daily_impression = IFNULL(#{dailyMaxImpression},default(daily_impression)),
		daily_click =IFNULL( #{dailyMaxClick},default(daily_click)),
		daily_conversion = IFNULL(#{dailyMaxConversion},default(daily_conversion)),
		per_impression_type = IFNULL(#{perImpressionType},default(per_impression_type)),
		per_impression = IFNULL( #{perImpression},default(per_impression)),
		per_click_type =IFNULL( #{perClickType},default(per_click_type)),
		per_click = IFNULL( #{perClick},default(per_click)),
		retarget = IFNULL( #{retarget},default(retarget)),
		period = IFNULL( #{period},default(period)),
		site = IFNULL( #{site},default(site)),		
		position =IFNULL( #{position},default(position)),		
		status = #{status},
		os = #{os},
		updated = NOW()
		<where>
			id = #{id}
		</where>
	</update>
	
		<update id="editAdGroupBack" parameterType="com.cloudcross.ssp.model.AdGroup">
		update t_adgroup
		set name = #{adGroupName},
		activate_time = #{startDate},
		expire_time = #{endDate},		
		totalbudget = #{totalBudget},
		price = #{price},		
		dailybudget = IFNULL(#{dailyBudget},default(dailybudget)),
		impression = IFNULL(#{maxImpression},default(impression)),
		click =IFNULL( #{maxClick},default(click)),
		conversion=IFNULL( #{maxConversion},default(conversion)),
		daily_impression = IFNULL(#{dailyMaxImpression},default(daily_impression)),
		daily_click =IFNULL( #{dailyMaxClick},default(daily_click)),
		daily_conversion = IFNULL(#{dailyMaxConversion},default(daily_conversion)),
		per_impression_type = IFNULL(#{perImpressionType},default(per_impression_type)),
		per_impression = IFNULL( #{perImpression},default(per_impression)),
		per_click_type =IFNULL( #{perClickType},default(per_click_type)),
		per_click = IFNULL( #{perClick},default(per_click)),
		retarget = IFNULL( #{retarget},default(retarget)),
		period = IFNULL( #{period},default(period)),
		site = IFNULL( #{site},default(site)),		
		position =IFNULL( #{position},default(position)),		
		status = #{status},
		updated = NOW()
		<where>
			id = #{id}
		</where>
	</update>
	
	
	<select id="findAdgroupByAdvertiserId_Dashboard" parameterType="long" resultType="map">  
    select g.id as adGroupId, g.name as adGroupName
    from t_order o, t_adgroup g
    where o.id = g.order_id and o.advertiser_id = #{advertiserId} and  date(g.active_time) &lt;=date(date_add(now(), interval -1 day)) and date(g.expire_time)>=date(date_add(now(), interval -1 day))
    </select>
	
	<!-- ************************************************** -->
	<!-- 根据广告主ID查询投放ID -->
	<select id="findAllAdGroudByAdvertiserId" parameterType = "long" resultType = "long">
		select id from t_adgroup where t_adgroup.advertiser_id = #{advertiserId}
	</select>
	
	
	
	
	
	<update id="updateStatusByAdvertiserId" parameterType="HashMap">
		update t_adgroup
			set status = #{status},updated = NOW()
			where t_adgroup.advertiser_id in
			<foreach item="advertiserId" collection="advertiserIdList"
		      open="(" separator="," close=")">
		        #{advertiserId}
			</foreach>
	</update>
	
	<update id="updateStatusByOrderId" parameterType="HashMap">
		update t_adgroup 
		set status=#{status},updated = #{modifyDate}
		where t_adgroup.order_id in
		<foreach item="orderId" collection="orderIdList" open="(" separator="," close=")">
			#{orderId}
		</foreach>
	</update>
	
	<update id="updateStatusByCampaignId" parameterType="HashMap">
		update t_adgroup 
		set status=#{status},updated = #{modifyDate}
		where t_adgroup.campaign_id in
		<foreach item="campaignId" collection="campaignIdList" open="(" separator="," close=")">
			#{campaignId}
		</foreach>
	</update>
	
	<select id="queryCampaignByCondition" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.AdGroup">
		SELECT
			ta.id id,
			ta.name adGroupName,
			(select count(tb.id)from t_banner tb where tb.adgroup_id = ta.id) as bannerNumber,
			ta.active_time startDate,
			ta.expire_time endDate,
			ta.updated updated,
			ta.status status 
		FROM
			t_adgroup ta
		WHERE ta.status!=-1
			<if test="agentId != null">
				and ta.agent_id = #{agentId}
			</if>
			<if test="advertiserId != null">
				and ta.advertiser_id = #{advertiserId}
			</if>
			<if test="orderId != null">
				and ta.order_id = #{orderId}
			</if>
			<if test="campaignId != null">
				and ta.campaign_id = #{campaignId}
			</if>
			<if test="adGroupName != null">
				and ta.name like #{adGroupName}
			</if>
			<choose>
				<when test="selectedStatus == 1">
					and now()>ta.active_time and ta.expire_time>now() and ta.status=0
				</when>
				<when test="selectedStatus == 2">
					and ta.active_time>now()
				</when>
				<when test="selectedStatus == 3">
					and now()>ta.expire_time
				</when>
				<when test="selectedStatus == 4">
					and ta.status=1
				</when>
			</choose>
		group by ta.id order by ta.id desc
		limit #{pager.offset}, #{pager.limit}	
	</select>
	
	<select id="queryCampaignCountByCondition" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT count(ta.id) from t_adgroup ta WHERE ta.status!=-1
			<if test="agentId != null">
				and ta.agent_id = #{agentId}
			</if>
			<if test="advertiserId != null">
				and ta.advertiser_id = #{advertiserId}
			</if>
			<if test="orderId != null">
				and ta.order_id = #{orderId}
			</if>
			<if test="campaignId != null">
				and ta.campaign_id = #{campaignId}
			</if>
			<if test="adGroupName != null">
				and ta.name like #{adGroupName}
			</if>
			<choose>
				<when test="selectedStatus == 1">
					and now()>ta.active_time and ta.expire_time>now() and ta.status=0
				</when>
				<when test="selectedStatus == 2">
					and ta.active_time>now()
				</when>
				<when test="selectedStatus == 3">
					and now()>ta.expire_time
				</when>
				<when test="selectedStatus == 4">
					and ta.status=1
				</when>
			</choose>
	</select>
</mapper>