<?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="wifiSqlMapper">
	<sql id="base_column">
		t_wifi.id as id,
		t_wifi.name as name,
		t_wifi.SSID as SSID,
		t_wifi.location as location,
		t_wifi.apmac as apmac,
		t_wifi.address as address,
		t_wifi.longitude as longitude,
		t_wifi.latitude as latitude,
		t_wifi.place as place,
		t_wifi.place_id as placeId,
		t_wifi.operator_id as operatorId,
		t_wifi.status as status,
		t_wifi.updated as updated
	</sql>
	
	<sql id="base_column2">
		id,
		location,
		address,
		longitude,
		latitude
	</sql>
	
	<select id="countByParamsBack" parameterType="map" resultType="int">
		SELECT
		count(tw.id)
		FROM
			t_wifi tw LEFT JOIN t_location tl on tl.location = tw.location
			LEFT JOIN t_place tp on  tp.id = tw.place_id
			LEFT JOIN t_place_class tpc on tp.id = tpc.place_id
			LEFT JOIN t_place_class_info tpci on tpci.id = tpc.class_id
		where tw.status != -1 
		<if test="cn != null">
			and tl.cn = #{cn}
		</if>
		<if test="cnCity != null">
			and tl.cn_city = #{cnCity}
		</if>
		<if test="place != null">
			and tw.place = #{place}
		</if>
		<if test="operatorId != null">
			and tw.operator_id = #{operatorId}
		</if>
		<if test="placeClassId != null">
			and tpci.id = #{placeClassId}
		</if>
		<if test="mac != null">
			and tw.apmac like #{mac}
		</if>
	</select>
	
	<select id="findByParamsBack" parameterType="map" resultType="Wifi">
		SELECT
			tw.id id,
			tw.NAME name,
			tw.SSID SSID,
			tl.cn cn,
			tl.cn_city cnCity,
			tw.address address,
			tw.longitude longitude,
			tw.latitude latitude,
			tw.apmac apmac,
			tw.place place,
			tp.name placeName,
			tpci.name as placeClassName,
			tw.updated updated,
			tw.status status,
			tw.operator_id operatorId
		FROM
			t_wifi tw LEFT JOIN t_location tl on tl.location = tw.location
			LEFT JOIN t_place tp on  tp.id = tw.place_id
			LEFT JOIN t_place_class tpc on tp.id = tpc.place_id
			LEFT JOIN t_place_class_info tpci on tpci.id = tpc.class_id
		where tw.status != -1 
		<if test="cn != null">
			and tl.cn = #{cn}
		</if>
		<if test="cnCity != null">
			and tl.cn_city = #{cnCity}
		</if>
		<if test="place != null">
			and tw.place = #{place}
		</if>
		<if test="operatorId != null">
			and tw.operator_id = #{operatorId}
		</if>
		<if test="placeClassId != null">
			and tpci.id = #{placeClassId}
		</if>
		<if test="mac != null">
			and tw.apmac like #{mac}
		</if>
		 order by tw.id asc
		 limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="findByParams1Back" parameterType="map" resultType="Wifi">
			SELECT
			tw.id id,
			tw.NAME name,
			tw.SSID SSID,
			tl.cn cn,
			tl.cn_city cnCity,
			tw.address address,
			tw.longitude longitude,
			tw.latitude latitude,
			tw.apmac apmac,
			tw.place place,
			tp.name placeName,
			tpci.name as placeClassName,
			tw.updated updated,
			tw.status status,
			tw.operator_id operatorId
		FROM
			t_wifi tw LEFT JOIN t_location tl on tl.location = tw.location
			LEFT JOIN t_place tp on  tp.id = tw.place_id
			LEFT JOIN t_place_class tpc on tp.id = tpc.place_id
			LEFT JOIN t_place_class_info tpci on tpci.id = tpc.class_id
		where tw.status != -1 
		<if test="cn != null">
			and tl.cn = #{cn}
		</if>
		<if test="cnCity != null">
			and tl.cn_city = #{cnCity}
		</if>
		<if test="place != null">
			and tw.place = #{place}
		</if>
		<if test="operatorId != null">
			and tw.operator_id = #{operatorId}
		</if>
		<if test="placeClassId != null">
			and tpci.id = #{placeClassId}
		</if>
		<if test="mac != null">
			and tw.apmac like #{mac}
		</if>
		 order by tw.id asc
	</select>
	
	<select id="countByParamsOperator" parameterType="map" resultType="int">
		select count(w.id) from t_wifi w, t_location l
		<where>
			w.location = l.location and w.status != -1 and w.operator_id = #{operatorId}
			<if test="cn != null">
				and l.cn = #{cn}
			</if> 
			<if test="cnCity != null">
				and l.cn_city = #{cnCity}
			</if>
			<if test="placeId != null">
				and w.place_id = #{placeId}
			</if>
			<if test="place != null">
				and w.place = #{place}
			</if>
			<if test="searchValue != null">
				and (w.address like #{searchValue} or w.name like #{searchValue})
			</if>
		</where> 
	</select>
	
	<select id="findByParams" parameterType="map" resultType="com.cloudcross.ssp.model.Wifi">
		select 
		<include refid="base_column" />, t_location.cn as cn, t_location.cn_city as cnCity, t_place.name as placeName
		from t_wifi, t_location, t_place
		<where>
			t_wifi.location = t_location.location and t_wifi.place_id = t_place.id
			and t_wifi.status != -1 and t_wifi.operator_id = #{operatorId}
			<if test="cn != null">
				and t_location.cn = #{cn}
			</if> 
			<if test="cnCity != null">
				and t_location.cn_city = #{cnCity}
			</if>
			<if test="placeId != null">
				and t_wifi.place_id = #{placeId}
			</if>
			<if test="place != null">
				and t_wifi.place = #{place}
			</if>
			<if test="searchValue != null">
				and (t_wifi.address like #{searchValue} or t_wifi.name like #{searchValue})
			</if>
		</where> 
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="findByParams1" parameterType="map" resultType="com.cloudcross.ssp.model.Wifi">
		select 
		<include refid="base_column" />, t_location.cn as cn, t_location.cn_city as cnCity, t_place.name as placeName
		from t_wifi, t_location, t_place
		<where>
			t_wifi.location = t_location.location and t_wifi.place_id = t_place.id
			and t_wifi.status != -1 and t_wifi.operator_id = #{operatorId}
			<if test="cn != null">
				and t_location.cn = #{cn}
			</if> 
			<if test="cnCity != null">
				and t_location.cn_city = #{cnCity}
			</if>
			<if test="placeId != null">
				and t_wifi.place_id = #{placeId}
			</if>
			<if test="place != null">
				and t_wifi.place = #{place}
			</if>
			<if test="searchValue != null">
				and (t_wifi.address like #{searchValue} or t_wifi.name like #{searchValue})
			</if>
		</where> 
	</select>
	
	
	<select id="findById" parameterType="long" resultType="com.cloudcross.ssp.model.Wifi">
		select 
		<include refid="base_column" />, t_location.cn as cn, t_location.cn_city as cnCity, t_place.name as placeName
		from t_wifi, t_location, t_place
		<where>
			t_wifi.location = t_location.location and t_wifi.place_id = t_place.id
			and t_wifi.status != -1 and t_wifi.id = #{id}
		</where>
	</select>
	
	<update id="updateWifiStatus" parameterType="HashMap">
		update t_wifi
		set status=#{status},updated = #{updated}
		where id in
		<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
	</update>
	
	<update id="editWifi" parameterType="com.cloudcross.ssp.model.Wifi">
		update t_wifi set name = #{name},
		location = #{location},
		address = #{address},
		longitude = #{longitude},
		latitude = #{latitude},
		apmac = #{apmac},
		place = #{place},
		place_id = #{placeId}
		where id = #{id}
	</update>
	
	<insert id="add" parameterType="com.cloudcross.ssp.model.Wifi" useGeneratedKeys="true" keyProperty="id">
		insert into t_wifi(name, SSID, location, apmac, address, longitude, latitude, place, place_id, operator_id, updated)
		values(#{name}, #{SSID}, #{location}, #{apmac}, #{address}, #{longitude}, #{latitude}, #{place}, #{placeId}, #{operatorId}, #{updated})
	</insert>
	

	<select id="findAddress" parameterType="map" resultType="com.cloudcross.ssp.model.Wifi">
       select 
		<include refid="base_column2" />
		from t_wifi where 1 = 1
		<if test="key!=null and value!=null">
			<choose>
				<when test="key=='address'">
					and address like #{value}
				</when>
			</choose>	
		</if>
		order by  id				
	</select>
	
  	<select id="findWifi" parameterType="map" resultType="com.cloudcross.ssp.model.Wifi">
       select 
       <include refid="base_column2" />
       from t_wifi where location in (select location from t_wifi where location = #{location})
       and latitude &lt;= #{maxlat} and latitude &gt;= #{minlat} 
       and longitude &lt;= #{maxlng}	and longitude &gt;= #{minlng}			
	</select>
	
	<select id="findLocation" parameterType="String" resultType="com.cloudcross.ssp.model.Wifi">
       select 
       <include refid="base_column2" />
       from t_wifi where location = #{location}			
	</select>
	
	<select id="findAllAdgroupWifi" parameterType="Long" resultType="com.cloudcross.ssp.model.Wifi">
       select 
       <include refid="base_column2" />
       from t_wifi where id in (select wifi_id from t_adgroup_wifi where adgroup_id = #{adGroupId})
       order by id		
	</select>
	
	
	<insert id="addAdgroupWifi" parameterType="map">
		insert into t_adgroup_wifi (adgroup_id,wifi_id,status,supdated)
		values (#{adGroupId},#{wifiId},#{status},#{supdated})
	</insert>
	
	<update id="deleteAdgroupWifi" parameterType="long"> 
	update t_adgroup_wifi set status = -1 where adgroup_id = #{adGroupId} 
	</update>
	
	<select id="selectByParams"  parameterType="map" resultType="com.cloudcross.ssp.model.Wifi">
		select 
		<include refid="base_column2" />
		from t_wifi where 1 = 1
		<if test="key!=null and value!=null">
			<choose>
				<when test="key=='address'">
					and address like #{value}
				</when>
			</choose>	
		</if>
		order by  id
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="countByParamsAdvertiser" parameterType="map" resultType="int">
		select count(id)
		from t_wifi where 1 = 1
		<if test="key!=null and value!=null">
			<choose>
				<when test="key=='address'">
					and address like #{value}
				</when>
			</choose>	
		</if>
	</select>
	
	<!-- 热点分组add-count -->
	<select id="queryWifiCountByCondition" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT 
			count(DISTINCT(wifi.id ))
		FROM
			t_wifi wifi,
			t_location loc,
			t_wifi_group twg,
			t_wifi_group_info twgi
		WHERE
			wifi.status != -1 and twgi.status = 0 and twg.status = 0
			and wifi.place_id = loc.id
			and wifi.id = twg.wifi_id
			and twg.group_id = twgi.id
		and wifi.operator_id = #{operatorId}
		<if test="name != null">
		and twgi.name = #{name}
		</if>
		<if test="apmac != null">
		and wifi.apmac = #{apmac}
		</if>
	</select>
	
	<!-- 热点分组add -->
	<select id="queryWifiByCondition" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.Wifi">
		SELECT 
			DISTINCT(wifi.id ) id,
			wifi.NAME name,
			wifi.SSID SSID,
			wifi.location location,
			wifi.apmac apmac,
			wifi.address address,
			wifi.longitude longitude,
			wifi.latitude latitude,
			loc.cn cn,
			loc.cn_city cnCity
		FROM
			t_wifi wifi,
			t_location loc,
			t_wifi_group twg,
			t_wifi_group_info twgi
		WHERE
			wifi.status != -1 and twgi.status = 0 and twg.status = 0
			and wifi.place_id = loc.id
			and wifi.id = twg.wifi_id
			and twg.group_id = twgi.id
		and wifi.operator_id = #{operatorId}
		<if test="name != null">
		and twgi.name = #{name}
		</if>
		<if test="apmac != null">
		and wifi.apmac = #{apmac}
		</if>
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<!-- 热点分组edit -->
	<select id="findWifiByCondition" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.Wifi">
		SELECT
			wifi.id id,
			wifi.NAME name,
			wifi.SSID SSID,
			wifi.location location,
			wifi.apmac apmac,
			wifi.address address,
			wifi.longitude longitude,
			wifi.latitude latitude,
			loc.cn cn,
			loc.cn_city cnCity
		FROM
			t_wifi wifi,
			t_location loc
		WHERE
			wifi.place_id = loc.id
			and wifi.status != -1
			<if test="operatorId != null">
				and wifi.operator_id = #{operatorId}
			</if>
			<if test="apmac != null">
				and wifi.apmac = #{apmac}
			</if>
			<if test="cn != null">
				and loc.cn = #{cn}
			</if> 
			<if test="cnCity != null">
				and loc.cn_city = #{cnCity}
			</if>
			limit #{pager.offset}, #{pager.limit}
	</select>
	
	<!-- 热点分组edit-count -->
	<select id="findWifiCountByCondition" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT count(wifi.id) FROM t_wifi wifi,t_location loc WHERE
			wifi.place_id = loc.id
			and wifi.status != -1
			<if test="operatorId != null">
				and wifi.operator_id = #{operatorId}
			</if>
			<if test="apmac != null">
				and wifi.apmac = #{apmac}
			</if>
			<if test="cn != null">
				and loc.cn = #{cn}
			</if> 
			<if test="cnCity != null">
				and loc.cn_city = #{cnCity}
			</if>
	</select>
	
	<select id="queryWifiNameById" resultType="java.lang.String" parameterType="java.lang.Long">
		select name from t_wifi where id = #{id}
	</select>
	
	<select id="queryWifi" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.Wifi">
		SELECT
			tw.id id,
			tw. NAME NAME,
			tw.SSID SSID,
			tl.cn cn,
			tl.cn_city CnCity,
			tw.longitude longitude,
			tw.latitude latitude,
			tw.address address,
			tw.apmac apmac
		FROM
			t_wifi tw,
			t_location tl
		WHERE
			tw.location = tl.location
			and tw.status != -1
			<if test="cn != null and cn != ''">
				and cn = #{cn}
			</if>
			<if test="cnCity != null and cnCity != ''">
				and cn_city = #{cnCity}
			</if>
	</select>
	
	<select id="queryWifiCount" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT count(tw.id)
		FROM
			t_wifi tw,
			t_location tl
		WHERE
			tw.location = tl.location
			and tw.status != -1
			<if test="cn != null and cn != ''">
				and cn = #{cn}
			</if>
			<if test="cnCity != null and cnCity != ''">
				and cn_city = #{cnCity}
			</if>
	</select>
</mapper>