<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"mybatis-3-mapper.dtd">
<mapper namespace="com.mortals.xhx.base.system.role.dao.ibatis.RoleAuthDaoImpl">
    
	<!-- 字段和属性映射 -->
	<resultMap type="RoleAuthEntity" id="RoleAuthEntity-Map">
		<result property="id" column="id" />
		<result property="roleId" column="roleId" />
		<result property="resourceId" column="resourceId" />
	</resultMap>
	
	
	
	<!-- 表所有列 -->
	<sql id="_columns">
		<trim suffixOverrides="," suffix="">
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('id') or colPickMode == 1 and data.containsKey('id')))">
				a.id as id,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('roleId') or colPickMode == 1 and data.containsKey('roleId')))">
				a.roleId as roleId,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('resourceId') or colPickMode == 1 and data.containsKey('resourceId')))">
				a.resourceId as resourceId,
			</if>
		</trim>
	</sql>
	<!-- 新增 -->
	<insert id="insert" parameterType="RoleAuthEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mortals_xhx_role_auth 
		(roleId,resourceId
		)VALUES(#{roleId},#{resourceId})
	</insert>
	<!-- 批量新增 -->
	<insert id="insertBatch" parameterType="paramDto">
		insert into mortals_xhx_role_auth 
		(roleId,resourceId
		)
		VALUES
		<foreach collection="data.dataList" item="item" index="index" separator="," >  
		(#{item.roleId},#{item.resourceId})
		</foreach>
	</insert>
	<!-- 根据ParamDto更新 -->
	<update id="update" parameterType="paramDto">
		update mortals_xhx_role_auth as a
		   set
		   <trim suffixOverrides="," suffix="">
				<if test="(colPickMode==0 and data.containsKey('roleId')) or (colPickMode==1 and !data.containsKey('roleId'))">
					a.roleId=#{data.roleId},
				</if>
				<if test="(colPickMode==0 and data.containsKey('roleIdIncrement')) or (colPickMode==1 and !data.containsKey('roleIdIncrement'))">
					a.roleId=ifnull(a.roleId,0) + #{data.roleIdIncrement},
				</if>
				<if test="(colPickMode==0 and data.containsKey('resourceId')) or (colPickMode==1 and !data.containsKey('resourceId'))">
					a.resourceId=#{data.resourceId},
				</if>
				<if test="(colPickMode==0 and data.containsKey('resourceIdIncrement')) or (colPickMode==1 and !data.containsKey('resourceIdIncrement'))">
					a.resourceId=ifnull(a.resourceId,0) + #{data.resourceIdIncrement},
				</if>
		    </trim>
		   <trim suffixOverrides="where" suffix="">
			    where
			    <trim prefixOverrides="and" prefix="">
					<include refid="_condition_"/>
			   	</trim>
			</trim>
	</update>

	<!-- 根据主健查询 -->
	<select id="getByKey" parameterType="paramDto" resultMap="RoleAuthEntity-Map">
		select <include refid="_columns"/>
		from mortals_xhx_role_auth as a
	   where a.id=#{condition.id}
	</select>
	<!-- 根据主健删除 -->
	<delete id="deleteByKey" parameterType="paramDto">
		delete a.* from mortals_xhx_role_auth as a where a.id=#{condition.id}
	</delete>
	<!-- 根据主健删除一批，针对单一主健有效 -->
	<delete id="deleteByKeys">
		delete from mortals_xhx_role_auth where id in
		<foreach collection="array" item="item" index="index" open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>
	<!-- 根据paramDto删除一批 -->
	<delete id="deleteByMap" parameterType="paramDto">
		delete a.* from mortals_xhx_role_auth as a
		<trim suffixOverrides="where" suffix="">
			 where 
			<trim prefixOverrides="and" prefix="">
				<include refid="_condition_"/>
			</trim>
		</trim>
	</delete>
	<!-- 获取列表 -->
	<select id="getList" parameterType="paramDto" resultMap="RoleAuthEntity-Map">
		select <include refid="_columns"/>
		  from mortals_xhx_role_auth as a
		 <trim suffixOverrides="where" suffix="">
			 where 
			 <trim prefixOverrides="and" prefix="">
			 	<include refid="_condition_"/>
			 </trim>
		 </trim>
		 <include refid="_orderCols_"/>
	</select>
	<!-- 获取 -->
	<select id="getListCount" parameterType="paramDto" resultType="int">
		select count(1)
		  from mortals_xhx_role_auth as a
		 <trim suffixOverrides="where" suffix="">
			 where 
			 <trim prefixOverrides="and" prefix="">
				<include refid="_condition_"/>
			</trim>
		</trim>
	</select>
	
	<!-- 条件映射 -->
	<sql id="_condition_">
		<if test="condition != null and !condition.isEmpty()">
				<if test="condition.containsKey('id')">
					<if test="condition.id != null">
						and a.id=#{condition.id}
					</if>
				</if>
				<if test="condition.containsKey('id')">
					<if test="condition.id != null ">
						and a.id = #{condition.id}
					</if>
					<if test="condition.id == null">
						and a.id is null
					</if>
				</if>
				<if test="condition.containsKey('idList')">
					 and a.id in 
					<foreach collection="condition.idList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('idStart') and condition.idStart != null">
					<![CDATA[ and a.id >= #{condition.idStart} ]]>
				</if>
				<if test="condition.containsKey('idEnd') and condition.idEnd != null">
					<![CDATA[ and a.id <= #{condition.idEnd} ]]>
				</if>
				<if test="condition.containsKey('roleId')">
					<if test="condition.roleId != null ">
						and a.roleId = #{condition.roleId}
					</if>
					<if test="condition.roleId == null">
						and a.roleId is null
					</if>
				</if>
				<if test="condition.containsKey('roleIdList')">
					 and a.roleId in 
					<foreach collection="condition.roleIdList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('roleIdStart') and condition.roleIdStart != null">
					<![CDATA[ and a.roleId >= #{condition.roleIdStart} ]]>
				</if>
				<if test="condition.containsKey('roleIdEnd') and condition.roleIdEnd != null">
					<![CDATA[ and a.roleId <= #{condition.roleIdEnd} ]]>
				</if>
				<if test="condition.containsKey('resourceId')">
					<if test="condition.resourceId != null ">
						and a.resourceId = #{condition.resourceId}
					</if>
					<if test="condition.resourceId == null">
						and a.resourceId is null
					</if>
				</if>
				<if test="condition.containsKey('resourceIdList')">
					 and a.resourceId in 
					<foreach collection="condition.resourceIdList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('resourceIdStart') and condition.resourceIdStart != null">
					<![CDATA[ and a.resourceId >= #{condition.resourceIdStart} ]]>
				</if>
				<if test="condition.containsKey('resourceIdEnd') and condition.resourceIdEnd != null">
					<![CDATA[ and a.resourceId <= #{condition.resourceIdEnd} ]]>
				</if>
		</if>
	</sql>
	<sql id="_orderCols_">
		<if test="orderColList != null and !orderColList.isEmpty()">
	        order by
	        <trim suffixOverrides=","  suffix="">
				<foreach collection="orderColList" open="" close="" index="index" item="item" separator=",">
						${item.colName} ${item.sortKind}
				</foreach>
			</trim>
		</if>
		<if test="(orderColList == null or orderColList.isEmpty()) and orderCol != null and !orderCol.isEmpty()">
			order by
			<trim suffixOverrides=","  suffix="">
				<if test="orderCol.containsKey('id')">
					a.id
					<if test="orderCol.id!= null">
						${orderCol.id}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('roleId')">
					a.roleId
					<if test="orderCol.roleId!= null">
						${orderCol.roleId}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('resourceId')">
					a.resourceId
					<if test="orderCol.resourceId!= null">
						${orderCol.resourceId}
					</if>
					,
				</if>
			</trim>
		</if>
	</sql>
	<sql id="_group_by_">
		<if test="groupList != null and !groupList.isEmpty()">
			GROUP BY
			<trim suffixOverrides=","  suffix="">
				<foreach collection="groupList" open="" close="" index="index" item="item" separator=",">
						${item}
				</foreach>
			</trim>
		</if>
	</sql>
</mapper>