<?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="placeOperatorSqlMapper">
	<sql id="base_column">
		id,operator_id as operatorId,name,place_id as placeId
	</sql>
	
	<select id="findByNameAndOperatorId" parameterType="String" resultType="long">
		select place_id from t_place_operator
		where name = #{name} and operator_id= #{operatorId}
	</select>
	
	<select id="findPlaceByOperator" parameterType="long" resultType="String">
		select name from t_place_operator where place_id != 0
		<if test="_parameter!=null">
		and operator_id = #{_parameter}
		</if>
		  
	</select>
	
	<select id="findPlace" parameterType="long" resultType="map">
		select o.place_id,p.name from t_place_operator o, t_place p where o.place_id = p.id and o.operator_id = #{operatorId}
	</select>
	
	<select id="countByParams" parameterType="map" resultType="int">
		select count(o.id)
		from t_place_operator o, t_place p
		where o.place_id = p.id and o.operator_id = #{operatorId}
		<if test="searchValue != null">
		and (o.name like #{searchValue} or p.name like #{searchValue})
		</if>
	</select>
	
	<select id="findByParams" parameterType="map" resultType="com.cloudcross.ssp.model.PlaceOperator">
		select o.id as id, o.operator_id as operatorId, o.name as name, p.name as placeName, p.id as placeId
		from t_place_operator o, t_place p
		where o.place_id = p.id and o.operator_id = #{operatorId}
		<if test="searchValue != null">
		and (o.name like #{searchValue} or p.name like #{searchValue})
		</if>
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="findByParams1" parameterType="map" resultType="com.cloudcross.ssp.model.PlaceOperator">
			select o.id as id, o.operator_id as operatorId, o.name as name, p.name as placeName, p.id as placeId
		from t_place_operator o, t_place p
		where o.place_id = p.id and o.operator_id = #{operatorId}
		<if test="searchValue != null">
		and (o.name like #{searchValue} or p.name like #{searchValue})
		</if>
	</select>
	
	
	<select id="countByParamsBack" parameterType="map" resultType="int">
		select count(o.id)
		from t_place_operator o , t_operator p
		where o.operator_id = p.id and o.place_id != 0
		<if test="operatorName != null">
		and p.company_name like #{operatorName}
		</if>
	</select>
	
	<select id="findByParamsBack" parameterType="map" resultType="PlaceOperator">
		select t_operator.company_name as operatorName,o.id as id, o.operator_id as operatorId, o.name as name, p.name as placeName, p.id as placeId
		from t_place_operator o, t_place p, t_operator
		where o.place_id = p.id and o.operator_id = t_operator.id
		<if test="operatorName != null">
		and t_operator.company_name like #{operatorName}
		</if>
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="findByParams1Back" parameterType="map" resultType="PlaceOperator">
		select t_operator.company_name as operatorName,o.id as id, o.operator_id as operatorId, o.name as name, p.name as placeName, p.id as placeId
		from t_place_operator o, t_place p, t_operator
		where o.place_id = p.id and o.operator_id = t_operator.id
		<if test="operatorName != null">
		and t_operator.company_name like #{operatorName}
		</if>
	</select>
	
	<update id="deleteRelationShip" parameterType="long">
		update t_place_operator set place_id = 0 where id = #{id}
	</update>
	
	
	<select id="countUnclassfiedPlace" parameterType="map" resultType="int">
		select count(id) from t_place_operator where
		place_id = 0 
		<if test="place != null">
			and name like #{place}
		</if>
	</select>
	
	<select id="findUnclassfiedPlace" parameterType="map" resultType="PlaceOperator">
	select <include refid="base_column" /> from t_place_operator where
		place_id = 0 
		<if test="place != null">
			and name like #{place}
		</if>
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<update id="update" parameterType="map">
		update t_place_operator set place_id = #{placeId}
		where id in <foreach item="id" collection="idList"
		      open="(" separator="," close=")">
		        #{id}
			</foreach>
	</update>
	
	<select id="queryPlaceNameCountByCondition" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT
			count(1)
		FROM
			t_operator tor,
			t_place_operator tpor,
			t_place tp,
			t_place_class_info tpci,
			t_place_class tpc
		WHERE
			tor.id = tpor.operator_id
		AND tp.id = tpor.place_id
		AND tpc.place_id = tp.id
		AND tpc.class_id = tpci.id
		and tpor.operator_id = #{operatorId}
		<if test="searchValue != null">
		and (tpor.name like #{searchValue} or tp.name like #{searchValue} or tpci.name like #{searchValue})
		</if>
	</select>
	
	<select id="queryPlaceNameByCondition" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.pojo.PlaceNamePojo">
		SELECT 
			tor.company_name operatorName,
			tpor. NAME opeatorPlaceName,
			tp. NAME placeName,
			tpci. NAME placeClassName
		FROM
			t_operator tor,
			t_place_operator tpor,
			t_place tp,
			t_place_class_info tpci,
			t_place_class tpc
		WHERE
			tor.id = tpor.operator_id
		AND tp.id = tpor.place_id
		AND tpc.place_id = tp.id
		AND tpc.class_id = tpci.id
		and tpor.operator_id = #{operatorId}
		<if test="searchValue != null">
		and (tpor.name like #{searchValue} or tp.name like #{searchValue} or tpci.name like #{searchValue})
		</if>
	</select>
</mapper>