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

	<sql id="base_column">
		id,token,name,company_name as companyName,homepage,contacts,tel,email,memo,ip,port,updated
	</sql>

	<insert id="add" parameterType="com.cloudcross.ssp.model.DspAgent">
		insert into t_dsp_agent(token,name,company_name,homepage,contacts,tel,email,memo,ip,port,updated) values
	    ( #{token}, #{name}, #{companyName}, #{homepage}, #{contacts}, #{tel}, #{email}, #{memo}, #{ip}, #{port}, NOW() )
	</insert>
	
	<update id="edit" parameterType="com.cloudcross.ssp.model.DspAgent">
		update t_dsp_agent
		<set>
			<if test="name != null and name != ''">
				name = #{name},
			</if>
			<if test="companyName != null and companyName != ''">
				company_name = #{companyName},
			</if>
			<if test="homepage != null and homepage != ''">
				homepage = #{homepage},
			</if>
			<if test="contacts != null and contacts != ''">
				contacts = #{contacts},
			</if>
			<if test="tel != null and tel != ''">
				tel = #{tel},
			</if>
			<if test="email != null and email != ''">
				email = #{email},
			</if>
			<if test="memo != null and memo !=''">
				memo = #{memo},
			</if>
			<if test="ip != null and ip !=''">
				ip = #{ip},
			</if>
			<if test="port != null and port !=''">
				port = #{port},
			</if>
			updated = NOW()
		</set>
		where id=#{id}
	</update>
	
	<select id="selectAllDspName" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.DspAgent">
		select id,name from t_dsp_agent 
		<if test="value != null and value != '' ">
	    	where name like #{value}
	    </if>
	</select>
	
	<select id="selectDspAgentById" resultType="com.cloudcross.ssp.model.DspAgent">
		select 
		<include refid="base_column"/>
		from t_dsp_agent where id = #{id}
	</select>
	
	<select id="isCheck" parameterType="com.cloudcross.ssp.model.DspAgent" resultType="java.lang.Integer">
		select count(*) from t_dsp_agent where name = #{name} and token = #{token}
	</select>
	
	<select id="selectMaxId" parameterType="java.util.Map" resultType="java.lang.Long">
		select id from t_dsp_agent where name = #{name}
		<if test="dspId != null and dspId != '' ">
	    	and id != #{dspId}
	    </if>
	</select>
	
		<select id="findAllAgent" resultType="com.cloudcross.ssp.model.DspAgent">
		select <include refid="base_column"/> from t_dsp_agent
	</select>
	
	<select id="checkIdentity" parameterType="java.util.Map" resultType="java.lang.Long">
		select count(id) from t_dsp_agent where token = #{token} and id = #{dspId}
	</select>
</mapper>