<?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="advertiserSqlMapper">
	
	<!--广告主端 广告主list页面数据总条数 -->
	<select id="countByParams" parameterType="java.util.Map" resultType="java.lang.Integer">
		select count(t.advertiserId) from (
			SELECT
				tar.id advertiserId
			FROM
				t_advertiser tar
			LEFT JOIN t_order tor ON tar.id = tor.advertiser_id
			LEFT JOIN t_industry tiy ON tar.industry_id = tiy.id
			WHERE tar. STATUS != - 1
				<if test="value!=null">
					and tar.name like #{value}
				</if>
				<if test="status!=null">	
					and tar.status = #{status}
				</if>
				<if test="industryId!=null">	
					and tar.industry_id = #{industryId}
				</if>
				<if test="agentId!=null">	
					and tar.agent_id = #{agentId}
				</if>
			GROUP BY tar.id
		)t
	</select>
	
	<!--广告主端 广告主list页面数据加载 -->
	<select id="selectByParams"  parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.Advertiser">
		select 
			tar.id id,
			tar.name name,
			tiy.name industryName,
			count(tor.id) as orderNum,
			tar.updated updated,
			tar.status status
		from t_advertiser tar 
		LEFT JOIN t_order tor on tar.id = tor.advertiser_id and tor.status != -1
		LEFT JOIN t_industry tiy ON tar.industry_id = tiy.id 
		where tar.status != -1
		 <if test="value!=null">
			and tar.name like #{value}
		</if>
		 <if test="status!=null">	
			and tar.status = #{status}
		</if>
		 <if test="industryId!=null">	
			and tar.industry_id = #{industryId}
		</if>
		 <if test="agentId!=null">	
			and tar.agent_id = #{agentId}
		</if>
		group by tar.id order by tar.id desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<!-- 根据广告主ID来查询一条信息 -->
	<select id="selectById" parameterType="java.lang.Integer" resultType="com.cloudcross.ssp.model.Advertiser">
		SELECT
			tar.id id,
			tar.industry_id industryId,
			tar.agent_id agentId,
			tar.operator_id operatorId,
			tar.name name,
			tar.reg_name regName,
			tar.homepage homePage,
			tar.contacts contacts,
			tar.tel tel,
			tar.email email,
			tar.memo memo,
			tar.balance balance,
			tar.cpi_path cpiPath,
			tar.licence_path licencePath,
			tar.id_path idPath,
			tar.org_path orgPath,
			tar.check_account checkAccount,
			tar.check_status checkStatus,
			tar.check_memo checkMemo,
			tar.admin_id adminId,
			tar.status status,
			tar.updated updated
		FROM
			t_advertiser tar where id = #{id}
	</select>
	
	<!-- 添加 -->
	<insert id="addAdvertiser" parameterType="com.cloudcross.ssp.model.Advertiser">
        insert into t_advertiser 
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="industryId != null">
                industry_id,
            </if>
            <if test="agentId != null">
                agent_id,
            </if>
            <if test="operatorId != null">
                operator_id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="regName != null">
                reg_name,
            </if>
            <if test="homePage != null">
                homePage,
            </if>
            <if test="contacts != null">
                contacts,
            </if>
            <if test="tel != null">
                tel,
            </if>
            <if test="email != null">
                email,
            </if>
            <if test="memo != null">
                memo,
            </if>
            <if test="balance != null">
                balance,
            </if>
            <if test="cpiPath != null">
                cpi_path,
            </if>
            <if test="licencePath != null">
                licence_path,
            </if>
            <if test="idPath != null">
                id_path,
            </if>
            <if test="orgPath != null">
                org_path,
            </if>
            <if test="checkAccount != null">
                check_account,
            </if>
            <if test="checkStatus != null">
                check_status,
            </if>
            <if test="checkMemo != null">
                check_memo,
            </if>
            <if test="adminId != null">
                admin_id,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="updated != null">
                updated
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="industryId != null">
                #{industryId},
            </if>
            <if test="agentId != null">
               #{agentId},
            </if>
            <if test="operatorId != null">
                #{operatorId},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="regName != null">
                #{regName},
            </if>
            <if test="homePage != null">
                #{homePage},
            </if>
            <if test="contacts != null">
                #{contacts},
            </if>
            <if test="tel != null">
                #{tel},
            </if>
            <if test="email != null">
               	#{email},
            </if>
            <if test="memo != null">
                #{memo},
            </if>
            <if test="balance != null">
                #{balance},
            </if>
            <if test="cpiPath != null">
                #{cpiPath},
            </if>
            <if test="licencePath != null">
                #{licencePath},
            </if>
            <if test="idPath != null">
                #{idPath},
            </if>
            <if test="orgPath != null">
                #{orgPath,jdbcType=VARCHAR},
            </if>
            <if test="checkAccount != null">
               	#{checkAccount},
            </if>
            <if test="checkStatus != null">
                #{checkStatus},
            </if>
            <if test="checkMemo != null">
                #{checkMemo},
            </if>
            <if test="adminId != null">
                #{adminId},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="updated != null">
                #{updated},
            </if>
        </trim>
    </insert>
	
	<!-- 修改 -->
	 <update id="editAdvertiser" parameterType="com.cloudcross.ssp.model.Advertiser">
        update t_advertiser set
        <trim suffix="" suffixOverrides=",">
            <if test="industryId != null">
                industry_id = #{industryId},
            </if>
            <if test="agentId != null">
                agent_id = #{agentId},
            </if>
            <if test="operatorId != null">
                operator_id = #{operatorId},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="regName != null">
                reg_name = #{regName},
            </if>
            <if test="homePage != null">
                homePage = #{homePage},
            </if>
            <if test="contacts != null">
                contacts = #{contacts},
            </if>
            <if test="tel != null">
                tel = #{tel},
            </if>
            <if test="email != null">
                email = #{email},
            </if>
            <if test="memo != null">
                memo = #{memo},
            </if>
            <if test="balance != null">
                balance = #{balance},
            </if>
            <if test="cpiPath != null">
                cpi_path = #{cpiPath},
            </if>
            <if test="licencePath != null">
                licence_path = #{licencePath},
            </if>
            <if test="idPath != null">
                id_path = #{idPath},
            </if>
            <if test="orgPath != null">
                org_path = #{orgPath},
            </if>
            <if test="checkAccount != null">
                check_account = #{checkAccount},
            </if>
            <if test="checkStatus != null">
                check_status = #{checkStatus},
            </if>
            <if test="checkMemo != null">
                check_memo = #{checkMemo},
            </if>
            <if test="adminId != null">
                admin_id = #{adminId},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="updated != null">
                updated = #{updated,jdbcType=TIMESTAMP}
            </if>
            where id = #{id}
        </trim>
    </update>
    
	<sql id="base_column">
		t_advertiser.id as id,
		t_advertiser.agent_id as agentId,
		industry_id as industryId,
		t_advertiser.name as name,
		reg_name as regName,
		homepage as homePage,
		t_advertiser.contacts,
		t_advertiser.tel as tel,
		t_advertiser.email as email,
		t_advertiser.balance as balance,
		t_advertiser.status as status,
		t_advertiser.memo as memo,
		t_advertiser.cpi_path as cpiPath,
		t_advertiser.licence_path as licencePath,
		t_advertiser.id_path as idPath,
		t_advertiser.org_path as orgPath,
		t_advertiser.check_account as checkAccount,
		t_advertiser.check_status as checkStatus,
		t_advertiser.check_memo as checkMemo,
		t_advertiser.admin_id as adminId,
		t_advertiser.updated as updated
	</sql>
	
	<select id="selectByAgentId" parameterType="int" resultType="com.cloudcross.ssp.model.Advertiser">
		select 
		<include refid="base_column" />
		from t_advertiser
		<where>
			agent_id = #{id} and
			status != -1
		</where>
	</select>
	
	<select id="selectByName" parameterType="String" resultType="long">
		select 
		max(id)
		from t_advertiser
		<where>
		name = #{name} and
			status != -1
		</where>
	</select>
		
	<select id="selectByOperatoradvertiser" parameterType="int" resultType="com.cloudcross.ssp.model.Advertiser">
		select 
		<include refid="base_column" />
		from t_advertiser
		<where>
			agent_id = #{operatorId} and
			status != -1.
		</where>
	</select>
	
	<select id="selectByOperatoroperator" parameterType="int" resultType="com.cloudcross.ssp.model.Advertiser">
		select 
		<include refid="base_column" />
		from t_advertiser
		<where>
			operator_id = #{operatorId} and
			status != -1.
		</where>
	</select>
		
	
	<select id="selectByParamsback"  parameterType="map" resultType="com.cloudcross.ssp.model.Advertiser">
	select 
	<include refid="base_column" />
	,b.name as industryName, count(c.advertiser_id) as orderNum from t_industry b,t_advertiser left join t_order c
	 on t_advertiser.id = c.advertiser_id 
	 where 1 = 1 
	 <if test="value!=null">
		and t_advertiser.name like #{value}
	</if>
	 <if test="status!=null">	
		and t_advertiser.status = #{status}
	</if>
	 <if test="industryId!=null">	
		and t_advertiser.industry_id = #{industryId}
	</if>
	 <if test="agentId!=null">	
		and t_advertiser.agent_id = #{agentId}
	</if>
	 and t_advertiser.status != -1
	 and t_advertiser.industry_id = b.id 
	group by t_advertiser.id
	order by t_advertiser.id   desc
	limit #{pager.offset}, #{pager.limit}
</select>

	<select id="selectByParamsoperator"  parameterType="map" resultType="com.cloudcross.ssp.model.Advertiser">
		select 
		<include refid="base_column" />
		,b.name as industryName, count(c.advertiser_id) as orderNum from t_industry b,t_advertiser left join t_order c
		 on t_advertiser.id = c.advertiser_id 
		 where 1 = 1 
		 <if test="key!=null and value!=null">
			<choose>
				<when test="key=='name'">
					and t_advertiser.name like #{value}
				</when>
			</choose>	
		</if>
		 <if test="status!=null">	
			and t_advertiser.status = #{status}
		</if>
		 <if test="industryId!=null">	
			and t_advertiser.industry_id = #{industryId}
		</if>
		 and t_advertiser.status != -1
		 and t_advertiser.industry_id = b.id 
		group by t_advertiser.id
		order by t_advertiser.id   desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="selectByStatus"  parameterType="map" resultType="com.cloudcross.ssp.model.Advertiser">
		select 
		<include refid="base_column" />
		,b.name as industryName, count(c.advertiser_id) as orderNum from t_industry b,t_advertiser left join t_order c
		 on t_advertiser.id = c.advertiser_id 
		 where 1 = 1 
		 and t_advertiser.status = #{status}
		 and t_advertiser.industry_id = b.id 
		group by t_advertiser.id
		order by t_advertiser.id   desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="countByStatus" parameterType="map" resultType="int">
		select count(a.id)
		from t_advertiser a where status = #{status}
	</select>
	
	
	<select id="countByParamsoperator" parameterType="map" resultType="int">
		select count(a.id)
		from t_advertiser a where status != -1
		<if test="key!=null and value!=null">
			<choose>
				<when test="key=='name'">
					and a.name like #{value}
				</when>
			</choose>	
		</if>
		 <if test="status!=null">	
			and a.status = #{status}
		</if>
		 <if test="industryId!=null">	
			and a.industry_id = #{industryId}
		</if>
	</select>
	
	
	
	<insert id="addAdvertiserback" parameterType="com.cloudcross.ssp.model.Advertiser">
		insert into t_advertiser(industry_id,name,reg_name,homepage,contacts,tel,email,memo
		,cpi_path,licence_path,id_path,org_path,check_memo,updated)
		values(#{industryId},#{name},#{regName},#{homePage},#{contacts},#{tel}, #{email},#{memo}
		,#{cpiPath},#{licencePath},#{idPath}, #{orgPath},#{checkMemo},NOW())
	</insert>
	
	<insert id="addAdvertiseroperator" parameterType="com.cloudcross.ssp.model.Advertiser">
		insert into t_advertiser(industry_id,name,reg_name,homepage,contacts,tel,email,updated,memo)
		values(#{industryId},#{name},#{regName},#{homePage},#{contacts},#{tel}, #{email},#{updated},#{memo})
	</insert>
	

	
	<update id="editAdvertiserback" parameterType="com.cloudcross.ssp.model.Advertiser">
		update t_advertiser 
		set industry_id = #{industryId},name = #{name},reg_name = #{regName},homepage = #{homePage},contacts = #{contacts},tel=#{tel}
		,email = #{email},memo = #{memo},updated = NOW()
		,cpi_path = #{cpiPath},licence_path = #{licencePath},id_path = #{idPath},org_path = #{orgPath},check_memo = #{checkMemo}
		where t_advertiser.id=#{id}
	</update>
	
	<update id="editAdvertiseroperator" parameterType="com.cloudcross.ssp.model.Advertiser">
	update t_advertiser 
	set industry_id = #{industryId},name = #{name},reg_name = #{regName},homepage = #{homePage},contacts = #{contacts},tel=#{tel}
	,email = #{email},updated = #{updated},memo = #{memo}
	where t_advertiser.id=#{id}
	</update>
	
	<update id="updateAdvertiserStatus" parameterType="HashMap">
		update t_advertiser 
			set status = #{status},updated = #{modifyDate}
			where t_advertiser.id in
			<foreach item="advertiserId" collection="advertiserIdList"
		      open="(" separator="," close=")">
		        #{advertiserId}
			</foreach>
	</update>
	
	
	<!-- 导出Excel用 -->
	<select id="findDeliverEffectAnalysis" parameterType="map" resultType="com.cloudcross.ssp.model.Advertiser">
		select 
		<include refid="base_column" />
		,b.name as industryName, count(c.advertiser_id) as orderNum from t_industry b,t_advertiser left join t_order c
		 on t_advertiser.id = c.advertiser_id 
		 where 1 = 1 
		 <if test="key!=null and value!=null">
			<choose>
				<when test="key=='name'">
					and t_advertiser.name like #{value}
				</when>
			</choose>	
		</if>
		 <if test="status!=null">	
			and t_advertiser.status = #{status}
		</if>
		 <if test="industryId!=null">	
			and t_advertiser.industry_id = #{industryId}
		</if>
		 and t_advertiser.status != -1
		 and t_advertiser.industry_id = b.id 
		group by t_advertiser.id
		order by t_advertiser.id   desc
	</select>
	
	<select id="selectAdvertiserNameById" parameterType="int" resultType="String">
		select name from t_advertiser where id = #{id}
	</select>
	
	<update id="updateBalance" parameterType="map">
		update t_advertiser set balance = #{advertiserBalance}
		where id = #{advertiserId}
	</update>
</mapper>