<?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="cn.com.countrygarden.bi.main.dao.BigholderRightManagerMapper">

	<resultMap id="BigholderRightManagerMap"
		type="cn.com.countrygarden.bi.main.model.BigholderRightManager">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="area" jdbcType="VARCHAR" property="area" />
		<result column="hand_user" jdbcType="VARCHAR" property="handUser" />
		<result column="area_id" jdbcType="VARCHAR" property="areaId" />
		<result column="user_id" jdbcType="VARCHAR" property="userId" />
	</resultMap>

	<select id="getRightManagerByHandmanId" parameterType="String"
		resultMap="BigholderRightManagerMap">
		SELECT id,area,hand_user,area_id,user_id FROM
		bigholder_rights_manage As b
		where b.user_id=#{HandmanId}
	</select>
	<select id="getRightManagerByArea" parameterType="String"
		resultMap="BigholderRightManagerMap">
		SELECT * FROM bigholder_rights_manage
		As b where 1=1
		<if test="area!=null">
			and b.area=#{area}
		</if>
	</select>

	<!-- 插入权限数据 -->
	<insert id="addRightList" parameterType="map">
		INSERT INTO
		bigholder_rights_manage (
		`area`,
		`hand_user`,
		`area_id`,
		`user_id`
		)
		VALUES(
		#{area},
		#{user},
		#{areaId},
		#{userId}
		)
	</insert>

	<!-- 删除权限数据 -->
	<delete id="deleteRightList" parameterType="map">
		DELETE rm
		FROM
		bigholder_rights_manage rm
		WHERE
		rm.area_id = #{areaId}
		AND
		rm.user_id =
		#{userId}
	</delete>

	<select id="findRightList" parameterType="string"
		resultMap="BigholderRightManagerMap">
		SELECT
		rm.id,
		rm.area_id,
		rm.area,
		rm.user_id,
		rm.hand_user
		FROM
		bigholder_rights_manage rm
		WHERE 1=1
		<!-- <if test="area != null and area != ''"> 
			AND rm.area = #{area} 
		</if> -->
		<if test="user != null and user != ''">
			AND rm.hand_user LIKE CONCAT('%',#{user},'%')
		</if>
	</select>
	
	<select id="findRightsByArea" parameterType="string"
		resultMap="BigholderRightManagerMap">
		SELECT
		rm.id,
		rm.area_id,
		rm.area,
		rm.user_id,
		rm.hand_user
		FROM
		bigholder_rights_manage rm
		WHERE 1=1
		<if test="area != null and area != ''"> 
			AND rm.area = #{area} 
		</if>
		<if test="areaId != null and areaId != ''"> 
			AND rm.area_id = #{areaId} 
		</if>
	</select>
	
	<select id="findRightsGroupByArea" resultMap="BigholderRightManagerMap">
		SELECT
			rm1.area,
			rm1.hand_user
		FROM
			bigholder_rights_manage rm1
		WHERE
			rm1.id = (
				SELECT
					rm2.id
				FROM
					bigholder_rights_manage rm2
				WHERE
					rm2.area = rm1.area
				LIMIT 1
			)
	</select>

	<select id="findAreaIds" parameterType="string" resultType="string">
		SELECT
		rm.area_id
		FROM
		bigholder_rights_manage rm
		WHERE 1=1
		<if test="user != null and user != ''">
			AND rm.hand_user LIKE CONCAT('%',#{user},'%')
		</if>
		GROUP BY rm.area_id
	</select>

	<select id="findAreaByRightManageTable" resultType="cn.com.countrygarden.bi.main.dto.AreaUser">
		SELECT
		rm.area,
		rm.area_id areaId
		FROM
		bigholder_rights_manage rm
		GROUP BY
		rm.area,rm.area_id
	</select>

	<select id="findAreasByUserCode" parameterType="string"
		resultType="string">
		SELECT
		rm.area
		FROM
		bigholder_rights_manage rm
		WHERE
		rm.user_id
		= #{userCode}
	</select>

</mapper>