<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
 <mapper namespace="com.iamberry.wechat.service.mapper.DrpRoleInfoMapper">
 
	 <sql id="drpRoleAllField">
 		ROLE_ID roleId,
		ROLE_NAME roleName,
		ROLE_TYPE roleType,
		ROLE_CREATE_DATE roleCreateDate,
		ROLE_UPDATE_DATE roleUpdateDate,
		ROLE_STATE roleStatus 
	 </sql>
 	
 	<!-- 分页查询角色信息表数据 -->
 	<select id="getDrpRoleList" parameterType="PlaceRoleInfo" resultType="PlaceRoleInfo">
 		SELECT 
 			<include refid="drpRoleAllField"/>
 		FROM 
 			TB_IAMBERRY_PLACE_ROLE 
 		<where>
 			<if test="roleName != null and roleName != ''">
 				ROLE_NAME like CONCAT('%',#{roleName},'%')
 			</if>
 		</where>
 		LIMIT ${page.recordBegin},${page.pageSize}
 	</select>
 	
 	<select id="getDrpRoleList2"  resultType="PlaceRoleInfo">
 	    select 
 	    ROLE_ID roleId,
		ROLE_NAME roleName
		from TB_IAMBERRY_PLACE_ROLE 
		where role_state=1 
 	</select>
 	
 	<!-- 查询所有在用状态的角色信息 -->
 	<select id="getDrpRoleListByState"  resultType="PlaceRoleInfo">
 	    select 
 	    <include refid="drpRoleAllField"/>
		from TB_IAMBERRY_PLACE_ROLE 
		where role_state=1 
 	</select>

 	<!-- 根据条件查询角色信息表数据条目数 -->
 	<select id="selectDrpRoleCount" resultType="int" parameterType="PlaceRoleInfo">
 		SELECT 
 			COUNT(ROLE_ID) roleNum 
 		FROM 
 			TB_IAMBERRY_PLACE_ROLE 
 		<where>
 			<if test="roleName != null and roleName != ''">
 				ROLE_NAME like CONCAT('%',#{roleName},'%')
 			</if>
 		</where>
 	</select>
 	
 	<!-- 根据条件查询角色信息 -->
 	<select id="selectDrpRoleInfo" resultType="PlaceRoleInfo" parameterType="PlaceRoleInfo">
 		SELECT 
 			<include refid="drpRoleAllField"/>
 		FROM 
 			TB_IAMBERRY_PLACE_ROLE 
 		<where>
 			<if test="roleId != null and roleId != ''">
 				ROLE_ID = #{roleId}
 			</if>
 		</where>
 	</select>
 	
 	<!-- 添加角色信息 -->
 	<insert id="addDrpRoleInfo" parameterType="PlaceRoleInfo" useGeneratedKeys="true" keyProperty="roleId">
 		INSERT INTO TB_IAMBERRY_PLACE_ROLE 
 		(
 			ROLE_NAME,
 			ROLE_CREATE_DATE,
 			ROLE_TYPE,
 			ROLE_STATE 
 		)
 		VALUES
 		(
 			#{roleName},
 			NOW(),
 			#{roleType},
 			#{roleStatus}
 		)
 	</insert>
 	
 	<!-- 修改分销角色数据 -->
 	<update id="updateDrpRoleInfo" parameterType="PlaceRoleInfo">
 		UPDATE 
 			TB_IAMBERRY_PLACE_ROLE 
 		SET 
 			ROLE_NAME = #{roleName},
 			ROLE_UPDATE_DATE = NOW(),
 			ROLE_TYPE = #{roleType},
 			ROLE_STATE = #{roleStatus} 
 		WHERE 
 			ROLE_ID = #{roleId}
 	</update>
 	
 	<!-- 修改分销角色状态 -->
 	<update id="updateDrpRoleStatus" parameterType="PlaceRoleInfo">
 		UPDATE 
 			TB_IAMBERRY_PLACE_ROLE 
 		SET 
 			ROLE_STATE = #{roleStatus} 
 		WHERE 
 			ROLE_ID = #{roleId}
 	</update>
 	
 	<!-- 根据placeId查询角色信息 -->
 	<select id="selectRoleName" resultType="PlaceRoleInfo" parameterType="PlaceRoleInfo">
 		SELECT
		 ROLE_ID roleId 
		FROM
		 TB_IAMBERRY_PLACE_ROLE 
		<where>
			<if test="roleName != null and roleName == '门店'.toString()">
				ROLE_NAME = '门店' OR ROLE_NAME = '母婴店'
			</if>
		</where>
 	</select>

 </mapper>