<?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="zoneSqlMapper">
	
	<sql id="base_column">
		t_zone.id as id,
		operator_id ,
		t_zone.name as name,
		width,
		height,
		device,
		position,
		status,
		updated,
		supdated,
		default_path,
		default_url,
		recheck,
		default_path_small
	</sql>	
	<select id="findZone" parameterType="int" resultType="com.cloudcross.ssp.model.Zone">
		select * from t_zone where id=#{zoneId}
	</select>
	
	 <select id="selectByIdOperator"  parameterType="int" resultType="com.cloudcross.ssp.model.Zone">
		select *
		from t_zone 
		<where>
			t_zone.id = #{id}
		</where>
		order by t_zone.id  desc
	</select> 		
	<select id="findByName"  parameterType="string" resultType="long">
		select t_zone.id from t_zone 
	<where> name = #{name} </where>
	</select>
	<resultMap id="zoneResult" type="com.cloudcross.ssp.model.Zone">
		<id property="id" column="id" />
		<result property="operator_id" column="operator_id" />
		<result property="name" column="name" />
		<result property="width" column="width" />
		<result property="height" column="height" />
		<result property="device" column="device" />
		<result property="position" column="position" />
		<result property="status" column="status" />
		<result property="updated" column="updated" />
		<result property="supdated" column="supdated" />
	</resultMap> 
	
	<select id="selectByParams"  parameterType="map" resultMap="zoneResult">
		select 
		<include refid="base_column" />
		from t_zone 
		where t_zone.status != -1
		
		<if test="key!=null and value!=null">
			<choose>
				<when test="key=='name'">
					and t_zone.name like #{value}
				</when>
				<when test="key=='id'">
					and t_zone.id like #{value}
				</when>
			</choose>	
		</if>
		<if test="status != null">
				and status like #{status}
		</if>		
		<if test="device != null">
				and device like #{device}
		</if>
		<if test="position != null">
				and position like #{position}
		</if>	
		<if test="height != null">
				and height like #{height}
		</if>
		<if test="width != null">
				and width like #{width}
		</if>
		<if test="operatorId != null">
				and operator_id = #{operatorId}
		</if>	
		
		order by width,height
		limit #{pager.offset}, #{pager.limit}
	</select>		
	<select id="countByParams" parameterType="map" resultType="int">
		select count(t_zone.id)
		from t_zone 
		where t_zone.status != -1	
		
			 <if test="key!=null and value!=null">
			<choose>
				<when test="key=='id'">
					and t_zone.id like #{value}
				</when>
				<when test="key=='name'">
					and t_zone.name = #{value}
				</when>
			</choose>	
		</if>
		<if test="status != null">
				and status like #{status}
		</if>		
		<if test="device != null">
				and device like #{device}
		</if>	
		<if test="position != null">
				and position like #{position}
		</if>
		<if test="height != null">
				and height like #{height}
		</if>
		<if test="width != null">
				and width like #{width}
		</if>
		<if test="operatorId != null">
				and operator_id = #{operatorId}
		</if>	
	</select>
		
	<insert id="addZone" parameterType="com.cloudcross.ssp.model.Zone" useGeneratedKeys="true"
		keyProperty="id">
		insert into t_zone(operator_id,name,size_id,device,position,width,height,recheck,updated,supdated,default_path,default_url,default_path_small)
		values(#{operator_id},#{name},#{sizeId},#{device},#{position},#{width},#{height},#{recheck},#{updated},#{supdated},#{default_path},#{default_url},#{default_path_small})
		
	
	</insert>
	
	<update id="editZone" parameterType="com.cloudcross.ssp.model.Zone">
		update t_zone 
		set  name=#{name},device=#{device},position=#{position}, width=#{width}, height=#{height},default_path=#{default_path},default_path_small=#{default_path_small},default_url=#{default_url},recheck=#{recheck},updated=#{updated},supdated=#{supdated}
		where id = #{id}
			
	</update>
	
	<update id="updateZoneStatus" parameterType="HashMap">
		update t_zone
			set status = #{status},updated = #{updated}
			where id in
			<foreach item="id" collection="zoneIdList"
		      open="(" separator="," close=")">
		        #{id}
			</foreach>
	</update>

	<delete id="deleteById" parameterType="int">
		delete 
		from t_zone
		<where>
		id = #{id} 
		</where>
	</delete>
	
	
	
	
	<!-- 
	<select id="findByDeviceAndPlaceId" parameterType="map" resultType="zone">
		select t_zone.id,t_zone.name
		 from t_zone 
		 <where>
		 	<if test="zoneDevice != null">
		 		1=1 and t_zone.device = #{zoneDevice}
		 	</if>
		 	<if test="zonePlaceIdList != null">
		 		and t_zone.id in (
		 		select t_zone_place.zone_id from t_zone_place where place_id in
		 		<foreach item="placeId" collection="zonePlaceIdList"  open="(" separator="," close=")">
		 			#{placeId}
		 		</foreach>
		 		)
		 	</if>
		 </where>
	</select>
	 -->
	<!-- 广告位排期按在投广告位ID和日期查找 -->
	
     <select id="findAdgroupNameByZoneIdAndSchedule" resultType="string" parameterType="map">
       select t.name from t_adgroup t 
       where 
       t.id in (select az.adgroup_id from t_adgroup_zone az where az.zone_id = #{zoneId}) 
       and
       t.id in (select ac.adgroup_id from t_adgroup_schedule ac where ac.startdate &lt;= #{date}  and #{date} &lt;= ac.enddate)
     </select>
	
	<!-- 首页中查找设备，暂时只有PC和MoBile -->
     <select id="findByDevice" resultType="com.cloudcross.ssp.model.Zone" parameterType="int">
           select id,name from t_zone
         <where> device = #{device} </where>
           order by t_zone.id desc 
      </select>
      
      <!-- add by Wanfu for banner-audit -->
      <select id="findByOperatorIdOperator" resultType="com.cloudcross.ssp.model.Zone" parameterType="int">
           select * from t_zone where 
           operator_id = #{operatorId} and status != -1
      </select>
      
	
 <select id="selectByIdBack"  parameterType="long" resultType="com.cloudcross.ssp.model.Zone">
		select <include refid="base_column" />
		from t_zone 
		<where>
			t_zone.id = #{id}
		</where>
		order by t_zone.id  desc
	</select> 	
	
	<select id="findByOperatorIdBack" resultType="com.cloudcross.ssp.model.Zone" parameterType="long">
           select <include refid="base_column" /> 
           from t_zone where operator_id = #{operatorId} and status != -1
      </select>
      
      
     
     <!-- 根据广告位的位置查询广告位 -->
      <select id="queryZoneByCondition" resultType="com.cloudcross.ssp.model.Zone" parameterType="java.util.Map">
      	select id,name from t_zone where status = 0 and operator_id = #{operatorId} 
      	<if test="positionList != null">
      		and position in
      		<foreach item="position" collection="positionList"  open="(" separator="," close=")">
		 		#{position}
			</foreach>
      	</if>
     </select>
     
     <!-- 根据投放查询广告位 -->
     <select id="queryZoneByAdgroupId" parameterType="java.lang.Long" resultType="com.cloudcross.ssp.model.Zone" >
     	select DISTINCT(tz.id) id,tz.name as name from t_zone tz,t_adgroup_zone taz where tz.id=taz.zone_id and taz.status=0 and tz.status=0 and taz.adgroup_id = #{adgroupId}
     </select>
     
     <select id="queryZone" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.Zone">
     	select tz.id id,tz.name name from t_zone tz where tz.status != -1 
     	<if test="operatorId != null">
     	 	and tz.operator_id in (#{operatorId})
     	</if>
     	<if test="position != null">
     		 and tz.position in (#{position})
     	</if> 
     	limit #{pager.offset}, #{pager.limit}
     </select>
     
     <select id="queryZoneCount" parameterType="java.util.Map" resultType="java.lang.Integer">
     	select count(tz.id) from t_zone tz where tz.status != -1 
     	<if test="operatorId != null">
     	 	and tz.operator_id in (#{operatorId})
     	</if>
     	<if test="position != null">
     		 and tz.position in (#{position})
     	</if> 
     </select>
</mapper>