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

	<sql id="base_column">
		id,
		name,
		contacts,
		tel,
		mobile,
		homepage as homePage,
		address,
		zip,
		email,
		title,
		operator_id as operatorId,
		admin_id as adminId,
		status
	</sql>
	
	<select id="findById" parameterType="int" resultType="com.cloudcross.ssp.model.AdvAgent">
		select 
		<include refid="base_column" />
		from t_adv_agent
		<where>
			id = #{id} and
			status != -1
		</where>
	</select>
	
	<select id="findAll" resultType="com.cloudcross.ssp.model.AdvAgent">
		select 
		<include refid="base_column" />
		from t_adv_agent
		<where>
			status != -1
		</where>
	</select>
	
	<select id="countByParams" parameterType="map" resultType="int">
		select count(id)
		from t_adv_agent where status != -1
		<if test="value!=null">
			and name like #{value}
		</if>
	</select>
	
	
	<select id="findByParams" parameterType="map" resultType="int">
		select 
		<include refid="base_column" />
		from t_adv_agent where status != -1
		<if test="value!=null">
			and name like #{value}
		</if>
		order by  id   desc
	limit #{pager.offset}, #{pager.limit}
	</select>
	<select id="getNew" parameterType="com.cloudcross.ssp.model.AdvAgent" resultType="com.cloudcross.ssp.model.AdvAgent">
		select 
		<include refid="base_column" />
		from t_adv_agent
		<where>
			status != -1 
			and admin_id = #{adminId} 
			and name = #{name}  
		</where>
	</select>
	
	<insert id="addAdvAgent" parameterType="com.cloudcross.ssp.model.AdvAgent">
		insert into t_adv_agent(name,contacts,tel,mobile,homepage,address,zip,email,title,admin_id,status)
		values(#{name},#{contacts},#{tel},#{mobile},#{homePage},#{address},#{zip},#{email},#{title},#{adminId},#{status})
	</insert>
	
	<update id="editAdvAgent" parameterType="com.cloudcross.ssp.model.AdvAgent">
		update t_adv_agent
		set name=#{name},
			contacts=#{contacts},
			tel=#{tel},
			mobile=#{mobile},
			homepage=#{homePage},
			address=#{address},
			zip=#{zip},
			email=#{email},
			title=#{title},
			admin_id=#{adminId},
			status=#{status}
		where id=#{id}
	</update>
	
	<update id="updateAdvAgentStatus" parameterType="HashMap">
		update t_adv_agent
			set status = #{status}
			where id in
			<foreach item="advAgentId" collection="advAgentIdList"
		      open="(" separator="," close=")">
		        #{advAgentId}
			</foreach>
	</update>
	
	<select id="getId" parameterType="long" resultType="long">
		select 
		id
		from t_adv_agent
		<where>
			operator_id = #{operatorId}
		</where>
	</select>
	
</mapper>