<?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="dspAdvertiserSqlMapper">

	<sql id="base_column">
		dsp_id as dspId,advertiser_id as advertiserId,industry_id as industryId,name,reg_name as regName,
		homepage,tel,email,licence_path as licencePath,id_path as idPath,org_path as orgPath,cpi_path as cpiPath,
		state,memo,updated
	</sql>

	<insert id="add" parameterType="java.util.List">
		insert into t_dsp_advertiser(dsp_id,advertiser_id,industry_id,name,reg_name,homepage,tel,email,licence_path,
		id_path,org_path,cpi_path,state,memo,updated) values
		<foreach collection="dspAdvertiserList" item="item" index="index" separator=",">
	    	( #{item.dspId},#{item.advertiserId}, #{item.industryId},#{item.name},#{item.regName},#{item.homepage},#{item.tel},
	    	 #{item.email}, #{item.licencePath},#{item.idPath}, #{item.orgPath},#{item.cpiPath}, 1,
	    	 "", NOW() )
        </foreach>
	</insert>
	
	<update id="update" parameterType="java.util.List">
	 	<foreach collection="dspAdvertiserList" item="item" index="index" open="" close="" separator=";">
	        update t_dsp_advertiser set 
            <trim suffix="" suffixOverrides=",">
	            <if test="item.industryId != null and item.industryId != '' ">
	                industry_id = #{item.industryId},
	            </if>
	            <if test="item.name != null and item.name != '' ">
	                name = #{item.name},
	            </if>
	           	<if test="item.regName != null and item.regName != '' ">
	                reg_name = #{item.regName},
	            </if>
	            <if test="item.homepage != null and item.homepage != '' ">
	                homepage = #{item.homepage},
	            </if>
	            <if test="item.tel != null and item.tel != '' ">
	                tel = #{item.tel},
	            </if>
	            <if test="item.email != null and item.email != '' ">
	                email = #{item.email},
	            </if>
	            <if test="item.licencePath != null and item.licencePath != '' ">
	                licence_path = #{item.licencePath},
	            </if>
	            <if test="item.idPath != null and item.idPath != '' ">
	                id_path = #{item.idPath},
	            </if>
	            <if test="item.orgPath != null and item.orgPath != '' ">
	                org_path = #{item.orgPath},
	            </if>
	            <if test="item.cpiPath != null and item.cpiPath != '' ">
	                cpi_path = #{item.cpiPath},
	            </if>
	            <if test="item.state != null and item.state != '' ">
	                state = #{item.state},
	            </if>
	            <if test="item.memo != null and item.memo != '' ">
	                memo = #{item.memo},
	            </if>
	            updated = NOW()
            	where dsp_id = #{item.dspId} and advertiser_id = #{item.advertiserId}
        	</trim>
    	</foreach>
	</update>
	
	<select id="getAll" parameterType="java.util.Map" resultType="java.util.Map">
		select <include refid="base_column"/> from t_dsp_advertiser
		where dsp_id = #{dspId}
	</select>
	
	<select id="get" parameterType="java.util.Map" resultType="java.util.Map">
		select 
		<include refid="base_column"/>
		from t_dsp_advertiser where advertiser_id in
		<foreach item="advertiserId" collection="advertiserIdList" open="(" separator="," close=")">
			#{advertiserId}
		</foreach>
		and dsp_id = #{dspId}
	</select>
	
	<select id="queryState" parameterType="java.util.Map" resultType="java.util.Map">
		select dsp_id as dspId,advertiser_id as advertiserId,state,memo as refuseReason
		from t_dsp_advertiser where advertiser_id in
		<foreach item="advertiserId" collection="advertiserIdList" open="(" separator="," close=")">
			#{advertiserId}
		</foreach>
		and dsp_id = #{dspId}
	</select>
	
	<select id="isRepeat" parameterType="java.util.Map" resultType="java.lang.Long">
		select count(*) from t_dsp_advertiser where dsp_id = #{dspId} and advertiser_id = #{advertiserId}
	</select>
</mapper>