<?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="campaignSqlMapper">
	<sql id="base_column">
	    t_campaign.id as id,
	    t_campaign.order_id as orderId,
	    t_campaign.name as name,
	    t_campaign.activate_time as activeTime,
	    t_campaign.expire_time as expireTime,
	    t_campaign.totalbudget as totalBudget,
	    t_campaign.dailybudget as dailyBudget,
	    t_campaign.impression as impression,
	    t_campaign.click as click,
	    t_campaign.operator_id as operatorId,
	    t_campaign.conversion as conversion,
	    t_campaign.daily_impression as dailyImpression,
	    t_campaign.daily_click as dailyClick,
	    t_campaign.daily_conversion as dailyConversion,
	    t_campaign.memo as memo,
	    t_campaign.status as status,
	    t_campaign.updated as upDated
	</sql>
	
	<insert id="addCampaignadvertiser" parameterType="com.cloudcross.ssp.model.Campaign">
		insert into t_campaign
			(name,agent_id,advertiser_id,operator_id,
			order_id,activate_time,expire_time,totalbudget,
			<if test="dailyBudget !=null">dailybudget,</if>
			<if test="impression!=null">impression,</if>
			<if test="click !=null">click,</if>
			<if test="conversion !=null">conversion,</if>
			<if test="dailyImpression !=null">daily_impression,</if>
			<if test="dailyClick !=null">daily_click,</if>
			<if test="dailyConversion !=null">daily_conversion,</if>
			<if test="memo !=null">memo,</if>
			 updated)
		values
			(#{name},#{agentId},#{advertiserId},#{operatorId},
			#{orderId},#{activeTime},#{expireTime},#{totalBudget},
			<if test="dailyBudget !=null">#{dailyBudget},</if>
			<if test="impression!=null">#{impression},</if>
			<if test="click !=null">#{click},</if>
			<if test="conversion !=null">#{conversion},</if>
			<if test="dailyImpression !=null">#{dailyImpression},</if>
			<if test="dailyClick !=null">#{dailyClick},</if>
			<if test="dailyConversion !=null">#{dailyConversion},</if>
			<if test="memo !=null">#{memo},</if>
			NOW())
	</insert>
	
	<insert id="addCampaignbackoroperator" parameterType="com.cloudcross.ssp.model.Campaign">
		insert into t_campaign
			(name,order_id,activate_time,expire_time,
			totalbudget,dailybudget,impression,click,conversion,daily_impression,
			daily_click,daily_conversion,memo,updated)
		values
			(#{name},#{orderId},#{activeTime},#{expireTime},#{totalBudget},
			#{dailyBudget},#{impression},#{click},#{conversion},#{dailyImpression},
			#{dailyClick},#{dailyConversion},#{memo},NOW())
	</insert>
	<update id="editCampaignadvertiser" parameterType="com.cloudcross.ssp.model.Campaign">
		update t_campaign set
		name = #{name},
		activate_time = #{activeTime},
		expire_time = #{expireTime},
		totalbudget = #{totalBudget},
		dailybudget = IFNULL(#{dailyBudget},default(dailybudget)),
		conversion =IFNULL(#{conversion},default(conversion)),
		click = IFNULL(#{click},default(click)),
		impression =IFNULL(#{impression},default(impression)),
		daily_click = IFNULL(#{dailyClick},default(daily_click)),
		daily_conversion = #{dailyConversion},
		daily_impression = IFNULL(#{dailyImpression},default(daily_impression)),
		memo = IFNULL(#{memo},default(memo)),
		updated = NOW()
		where t_campaign.id = #{id}
	</update>
	<update id="editCampaignbackoroperator" parameterType="com.cloudcross.ssp.model.Campaign">
		update t_campaign set
		name = #{name},
		activate_time = #{activeTime},
		expire_time = #{expireTime},
		totalbudget = #{totalBudget},
		dailybudget = IFNULL(#{dailyBudget},default(dailybudget)),
		conversion =IFNULL(#{conversion},default(conversion)),
		click = IFNULL(#{click},default(click)),
		impression =IFNULL(#{impression},default(impression)),
		daily_click = IFNULL(#{dailyClick},default(daily_click)),
		daily_conversion = 0,
		daily_impression = IFNULL(#{dailyImpression},default(daily_impression)),
		memo = IFNULL(#{memo},default(memo)),
		updated = NOW()
		where t_campaign.id = #{id}
	</update>	
	
	<select id="findByParamsadvertiser" parameterType="map" resultType="com.cloudcross.ssp.model.Campaign">
		select
		<include refid="base_column" />
		,count(t_adgroup.campaign_id) as groupNumber from t_campaign left join t_adgroup
		    on t_campaign.id = t_adgroup.campaign_id and t_adgroup.status != -1
		    where 1=1
		<if test="advMark!=null">
			and t_campaign.agent_id = #{agentId}
		</if> 
		<if test="advMark==null">
			and t_campaign.advertiser_id = #{advertiserId}
		</if> 
		<if test="orderMark==null">
			and t_campaign.order_id = #{orderId}
		</if> 
	 
		 and t_campaign.status != -1
		 	<if test="value!=null">
				and t_campaign.name like #{value}
			</if>			
		<if test="status!=null">	
			and t_campaign.status = #{status}
		</if>						
			  group by t_campaign.id
		order by t_campaign.id desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	<select id="findByParamsback" parameterType="map" resultType="com.cloudcross.ssp.model.Campaign">
		select
		<include refid="base_column" />
		,count(t_adgroup.campaign_id) as groupNumber from t_campaign left join t_adgroup
		    on t_campaign.id = t_adgroup.campaign_id 
		    where t_campaign.order_id = #{orderId} and t_campaign.status != -1
		 	<if test="value!=null">
				and t_campaign.name like #{value}
			</if>			
		<if test="status!=null">	
			and t_campaign.status = #{status}
		</if>						
			  group by t_campaign.id
		order by t_campaign.id desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="findByParams" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.Campaign">
		select
		<include refid="base_column" />
		,count(t_adgroup.campaign_id) as groupNumber from t_campaign left join t_adgroup
		    on t_campaign.id = t_adgroup.campaign_id 
		    where t_campaign.order_id = #{orderId} and t_campaign.status != -1
		 	<if test="key!=null and value!=null">
	            <choose>
					<when test="key=='name'">
						and t_campaign.name like #{value}
					</when>				
				</choose>
			</if>			
		<if test="status!=null">	
			and t_campaign.status = #{status}
		</if>						
			  group by t_campaign.id
		order by t_campaign.id desc
		limit #{pager.offset}, #{pager.limit}
	</select>

	<select id="countByParamsadvertiser" parameterType="map" resultType="int">
		select count(id) from t_campaign
		where 1=1
		<if test="advMark!=null">
			and t_campaign.agent_id = #{agentId}
		</if> 
		<if test="advMark==null">
			and t_campaign.advertiser_id = #{advertiserId}
		</if> 
		<if test="orderMark==null">
			and t_campaign.order_id = #{orderId}
		</if> 
		and status != -1
			<if test="value!=null">
				and name like #{value}
			</if>
						
		<if test="status!=null">	
			and t_campaign.status = #{status}
		</if>
	</select>
<select id="countByParamsback" parameterType="map" resultType="int">
		select count(id) from t_campaign
		<where>
			order_id = #{orderId} and status != -1
			<if test="value!=null">
				and name like #{value}
			</if>
						
		<if test="status!=null">	
			and t_campaign.status = #{status}
		</if>
		</where>
	</select>
	<select id="countByParamsoperator" parameterType="map" resultType="int">
		select count(id) from t_campaign
		<where>
			order_id = #{orderId} and status != -1
			<if test="key!=null and value!=null">
				<choose>
					<when test="key=='name'">
						and name like #{value}
					</when>
				</choose>
			</if>
						
		<if test="status!=null">	
			and t_campaign.status = #{status}
		</if>
		</where>
	</select>
	<update id="updateCampaignStatus" parameterType="HashMap">
		update t_campaign 
		set status=#{status},
			updated = #{upDated}
		where
		id in
		<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
	</update>
	
	
	
	<select id="findById" parameterType="long" resultType="com.cloudcross.ssp.model.Campaign">
		select
		<include refid="base_column" />
		from t_campaign 
		where id = #{id}
	</select>
	
	<select id="findByName" parameterType="String" resultType="long">
		select
		max(id)
		from t_campaign 
		where name = #{name}
	</select>
	

	<delete id="deleteByID" parameterType="int">
	delete from t_campaign where id=#{id}
	</delete>
	
	
	<select id="findByOrderId" parameterType="long" resultType="com.cloudcross.ssp.model.Campaign">
		select
		<include refid="base_column" />
		from t_campaign
		<where>
			order_id = #{orderId} and status != -1
		</where>
	</select>

	
	<select id="selectByStatus"  parameterType="map" resultType="com.cloudcross.ssp.model.Campaign">
		select 
		<include refid="base_column" />
		,count(t_adgroup.campaign_id) as groupNumber from t_campaign left join t_adgroup
		 on t_campaign.id = t_adgroup.campaign_id 
		 where 1 = 1 
		 and t_campaign.status = #{status}
		 and t_campaign.order_id = #{orderId}  
		group by t_campaign.id
		order by t_campaign.id   desc
		limit #{pager.offset}, #{pager.limit}
	</select>	
	
	<!-- 导出Excel用 -->
	<select id="findDeliverEffectAnalysis" parameterType="map" resultType="com.cloudcross.ssp.model.Campaign">
		select
		<include refid="base_column" />
		,count(t_adgroup.campaign_id) as groupNumber from t_campaign left join t_adgroup
		    on t_campaign.id = t_adgroup.campaign_id 
		    where t_campaign.order_id = #{orderId} and t_campaign.status != -1
		 	<if test="key!=null and value!=null">
	            <choose>
					<when test="key=='name'">
						and t_campaign.name like #{value}
					</when>				
				</choose>
			</if>			
		<if test="status!=null">	
			and t_campaign.status = #{status}
		</if>						
			  group by t_campaign.id
		order by t_campaign.id desc
	</select>
	
	<update id="updateStatusByAdvertiserId" parameterType="HashMap">
		update t_campaign
			set status = #{status},updated = NOW()
			where t_campaign.advertiser_id in
			<foreach item="advertiserId" collection="advertiserIdList"
		      open="(" separator="," close=")">
		        #{advertiserId}
			</foreach>
	</update>
	
	<update id="updateStatusByOrderId" parameterType="HashMap">
		update t_campaign 
			set status = #{status},updated = #{modifyDate}
			where t_campaign.order_id in
			<foreach item="orderId" collection="orderIdList"
		      open="(" separator="," close=")">
		        #{orderId}
			</foreach>
	</update>
	
	<select id="queryCampaignById" parameterType="java.lang.Integer" resultType="com.cloudcross.ssp.model.pojo.CampaignPojo">
		SELECT
			tcn.id id,
			tcn.agent_id agentId,
			tcn.advertiser_id advertiserId,
			tcn.operator_id operatorId,
			tcn.order_id orderId,
			tor.budget orderbudget
		FROM
			t_campaign tcn,
			t_order tor
		WHERE
			tcn.order_id = tor.id
			and tcn.id = #{campaignId}
	</select>
</mapper>