<?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.RoleDaoImpl">
    
	<!-- 字段和属性映射 -->
	<resultMap type="RoleEntity" id="RoleEntity-Map">
		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="remark" column="remark" />
		<result property="roleType" column="roleType" />
		<result property="customerId" column="customerId" />
		<result property="createTime" column="createTime" />
		<result property="createUserId" column="createUserId" />
		<result property="createUserName" column="createUserName" />
		<result property="userType" column="userType" />
	</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('name') or colPickMode == 1 and data.containsKey('name')))">
				a.name as name,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('remark') or colPickMode == 1 and data.containsKey('remark')))">
				a.remark as remark,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('roleType') or colPickMode == 1 and data.containsKey('roleType')))">
				a.roleType as roleType,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('customerId') or colPickMode == 1 and data.containsKey('customerId')))">
				a.customerId as customerId,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('createTime') or colPickMode == 1 and data.containsKey('createTime')))">
				a.createTime as createTime,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('createUserId') or colPickMode == 1 and data.containsKey('createUserId')))">
				a.createUserId as createUserId,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('createUserName') or colPickMode == 1 and data.containsKey('createUserName')))">
				a.createUserName as createUserName,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('userType') or colPickMode == 1 and data.containsKey('userType')))">
				a.userType as userType,
			</if>
		</trim>
	</sql>
	<!-- 新增 -->
	<insert id="insert" parameterType="RoleEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mortals_xhx_role 
		(name,remark,roleType,customerId,createTime,
			createUserId,createUserName,userType
		)VALUES(#{name},#{remark},#{roleType},#{customerId},#{createTime},
			#{createUserId},#{createUserName},#{userType})
	</insert>
	<!-- 批量新增 -->
	<insert id="insertBatch" parameterType="paramDto">
		insert into mortals_xhx_role 
		(name,remark,roleType,customerId,createTime,
			createUserId,createUserName,userType
		)
		VALUES
		<foreach collection="data.dataList" item="item" index="index" separator="," >  
		(#{item.name},#{item.remark},#{item.roleType},#{item.customerId},#{item.createTime},
			#{item.createUserId},#{item.createUserName},#{item.userType})
		</foreach>
	</insert>
	<!-- 根据ParamDto更新 -->
	<update id="update" parameterType="paramDto">
		update mortals_xhx_role as a
		   set
		   <trim suffixOverrides="," suffix="">
				<if test="(colPickMode==0 and data.containsKey('name')) or (colPickMode==1 and !data.containsKey('name'))">
					a.name=#{data.name},
				</if>
				<if test="(colPickMode==0 and data.containsKey('remark')) or (colPickMode==1 and !data.containsKey('remark'))">
					a.remark=#{data.remark},
				</if>
				<if test="(colPickMode==0 and data.containsKey('roleType')) or (colPickMode==1 and !data.containsKey('roleType'))">
					a.roleType=#{data.roleType},
				</if>
				<if test="(colPickMode==0 and data.containsKey('roleTypeIncrement')) or (colPickMode==1 and !data.containsKey('roleTypeIncrement'))">
					a.roleType=ifnull(a.roleType,0) + #{data.roleTypeIncrement},
				</if>
				<if test="(colPickMode==0 and data.containsKey('customerId')) or (colPickMode==1 and !data.containsKey('customerId'))">
					a.customerId=#{data.customerId},
				</if>
				<if test="(colPickMode==0 and data.containsKey('customerIdIncrement')) or (colPickMode==1 and !data.containsKey('customerIdIncrement'))">
					a.customerId=ifnull(a.customerId,0) + #{data.customerIdIncrement},
				</if>
				<if test="(colPickMode==0 and data.containsKey('createTime')) or (colPickMode==1 and !data.containsKey('createTime'))">
					a.createTime=#{data.createTime},
				</if>
				<if test="(colPickMode==0 and data.containsKey('createUserId')) or (colPickMode==1 and !data.containsKey('createUserId'))">
					a.createUserId=#{data.createUserId},
				</if>
				<if test="(colPickMode==0 and data.containsKey('createUserIdIncrement')) or (colPickMode==1 and !data.containsKey('createUserIdIncrement'))">
					a.createUserId=ifnull(a.createUserId,0) + #{data.createUserIdIncrement},
				</if>
				<if test="(colPickMode==0 and data.containsKey('createUserName')) or (colPickMode==1 and !data.containsKey('createUserName'))">
					a.createUserName=#{data.createUserName},
				</if>
				<if test="(colPickMode==0 and data.containsKey('userType')) or (colPickMode==1 and !data.containsKey('userType'))">
					a.userType=#{data.userType},
				</if>
				<if test="(colPickMode==0 and data.containsKey('userTypeIncrement')) or (colPickMode==1 and !data.containsKey('userTypeIncrement'))">
					a.userType=ifnull(a.userType,0) + #{data.userTypeIncrement},
				</if>
		    </trim>
		   <trim suffixOverrides="where" suffix="">
			    where
			    <trim prefixOverrides="and" prefix="">
					<include refid="_condition_"/>
			   	</trim>
			</trim>
	</update>

	<!-- 根据主健查询 -->
	<select id="getByKey" parameterType="paramDto" resultMap="RoleEntity-Map">
		select <include refid="_columns"/>
		from mortals_xhx_role as a
	   where a.id=#{condition.id}
	</select>
	<!-- 根据主健删除 -->
	<delete id="deleteByKey" parameterType="paramDto">
		delete a.* from mortals_xhx_role as a where a.id=#{condition.id}
	</delete>
	<!-- 根据主健删除一批，针对单一主健有效 -->
	<delete id="deleteByKeys">
		delete from mortals_xhx_role 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 as a
		<trim suffixOverrides="where" suffix="">
			 where 
			<trim prefixOverrides="and" prefix="">
				<include refid="_condition_"/>
			</trim>
		</trim>
	</delete>
	<!-- 获取列表 -->
	<select id="getList" parameterType="paramDto" resultMap="RoleEntity-Map">
		select <include refid="_columns"/>
		  from mortals_xhx_role 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 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('name')">
					<if test="condition.name != null and condition.name != ''">
						and a.name like #{condition.name}
					</if>
					<if test="condition.name == null">
						and a.name is null
					</if>
				</if>
				<if test="condition.containsKey('nameList')">
					 and a.name in 
					<foreach collection="condition.nameList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('remark')">
					<if test="condition.remark != null and condition.remark != ''">
						and a.remark like #{condition.remark}
					</if>
					<if test="condition.remark == null">
						and a.remark is null
					</if>
				</if>
				<if test="condition.containsKey('remarkList')">
					 and a.remark in 
					<foreach collection="condition.remarkList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('roleType')">
					<if test="condition.roleType != null ">
						and a.roleType = #{condition.roleType}
					</if>
					<if test="condition.roleType == null">
						and a.roleType is null
					</if>
				</if>
				<if test="condition.containsKey('roleTypeList')">
					 and a.roleType in 
					<foreach collection="condition.roleTypeList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('roleTypeStart') and condition.roleTypeStart != null">
					<![CDATA[ and a.roleType >= #{condition.roleTypeStart} ]]>
				</if>
				<if test="condition.containsKey('roleTypeEnd') and condition.roleTypeEnd != null">
					<![CDATA[ and a.roleType <= #{condition.roleTypeEnd} ]]>
				</if>
				<if test="condition.containsKey('customerId')">
					<if test="condition.customerId != null ">
						and a.customerId = #{condition.customerId}
					</if>
					<if test="condition.customerId == null">
						and a.customerId is null
					</if>
				</if>
				<if test="condition.containsKey('customerIdList')">
					 and a.customerId in 
					<foreach collection="condition.customerIdList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('customerIdStart') and condition.customerIdStart != null">
					<![CDATA[ and a.customerId >= #{condition.customerIdStart} ]]>
				</if>
				<if test="condition.containsKey('customerIdEnd') and condition.customerIdEnd != null">
					<![CDATA[ and a.customerId <= #{condition.customerIdEnd} ]]>
				</if>
				<if test="condition.containsKey('createTime')">
					<if test="condition.createTime != null ">
						and a.createTime = #{condition.createTime}
					</if>
					<if test="condition.createTime == null">
						and a.createTime is null
					</if>
				</if>
				<if test="condition.containsKey('createTimeStart') and condition.createTimeStart != null and condition.createTimeStart!=''">
					<![CDATA[ and a.createTime >= STR_TO_DATE(left(concat(#{condition.createTimeStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s') ]]>
				</if>
				<if test="condition.containsKey('createTimeEnd') and condition.createTimeEnd != null and condition.createTimeEnd!=''">
					<![CDATA[ and a.createTime <= STR_TO_DATE(left(concat(#{condition.createTimeEnd},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s') ]]>
				</if>
				<if test="condition.containsKey('createUserId')">
					<if test="condition.createUserId != null ">
						and a.createUserId = #{condition.createUserId}
					</if>
					<if test="condition.createUserId == null">
						and a.createUserId is null
					</if>
				</if>
				<if test="condition.containsKey('createUserIdList')">
					 and a.createUserId in 
					<foreach collection="condition.createUserIdList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('createUserIdStart') and condition.createUserIdStart != null">
					<![CDATA[ and a.createUserId >= #{condition.createUserIdStart} ]]>
				</if>
				<if test="condition.containsKey('createUserIdEnd') and condition.createUserIdEnd != null">
					<![CDATA[ and a.createUserId <= #{condition.createUserIdEnd} ]]>
				</if>
				<if test="condition.containsKey('createUserName')">
					<if test="condition.createUserName != null and condition.createUserName != ''">
						and a.createUserName like #{condition.createUserName}
					</if>
					<if test="condition.createUserName == null">
						and a.createUserName is null
					</if>
				</if>
				<if test="condition.containsKey('createUserNameList')">
					 and a.createUserName in 
					<foreach collection="condition.createUserNameList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('userType')">
					<if test="condition.userType != null ">
						and a.userType = #{condition.userType}
					</if>
					<if test="condition.userType == null">
						and a.userType is null
					</if>
				</if>
				<if test="condition.containsKey('userTypeList')">
					 and a.userType in 
					<foreach collection="condition.userTypeList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('userTypeStart') and condition.userTypeStart != null">
					<![CDATA[ and a.userType >= #{condition.userTypeStart} ]]>
				</if>
				<if test="condition.containsKey('userTypeEnd') and condition.userTypeEnd != null">
					<![CDATA[ and a.userType <= #{condition.userTypeEnd} ]]>
				</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('name')">
					a.name
					<if test="orderCol.name!= null">
						${orderCol.name}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('remark')">
					a.remark
					<if test="orderCol.remark!= null">
						${orderCol.remark}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('roleType')">
					a.roleType
					<if test="orderCol.roleType!= null">
						${orderCol.roleType}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('customerId')">
					a.customerId
					<if test="orderCol.customerId!= null">
						${orderCol.customerId}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('createTime')">
					a.createTime
					<if test="orderCol.createTime!= null">
						${orderCol.createTime}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('createUserId')">
					a.createUserId
					<if test="orderCol.createUserId!= null">
						${orderCol.createUserId}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('createUserName')">
					a.createUserName
					<if test="orderCol.createUserName!= null">
						${orderCol.createUserName}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('userType')">
					a.userType
					<if test="orderCol.userType!= null">
						${orderCol.userType}
					</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>