<?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.resource.dao.ibatis.ResourceDaoImpl">
    
	<!-- 字段和属性映射 -->
	<resultMap type="ResourceEntity" id="ResourceEntity-Map">
		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="url" column="url" />
		<result property="authType" column="authType" />
		<result property="sourceType" column="sourceType" />
		<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('url') or colPickMode == 1 and data.containsKey('url')))">
				a.url as url,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('authType') or colPickMode == 1 and data.containsKey('authType')))">
				a.authType as authType,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('sourceType') or colPickMode == 1 and data.containsKey('sourceType')))">
				a.sourceType as sourceType,
			</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="ResourceEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mortals_xhx_resource 
		(name,url,authType,sourceType,createTime,
			createUserId,createUserName,userType
		)VALUES(#{name},#{url},#{authType},#{sourceType},#{createTime},
			#{createUserId},#{createUserName},#{userType})
	</insert>
	<!-- 批量新增 -->
	<insert id="insertBatch" parameterType="paramDto">
		insert into mortals_xhx_resource 
		(name,url,authType,sourceType,createTime,
			createUserId,createUserName,userType
		)
		VALUES
		<foreach collection="data.dataList" item="item" index="index" separator="," >  
		(#{item.name},#{item.url},#{item.authType},#{item.sourceType},#{item.createTime},
			#{item.createUserId},#{item.createUserName},#{item.userType})
		</foreach>
	</insert>
	<!-- 根据ParamDto更新 -->
	<update id="update" parameterType="paramDto">
		update mortals_xhx_resource 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('url')) or (colPickMode==1 and !data.containsKey('url'))">
					a.url=#{data.url},
				</if>
				<if test="(colPickMode==0 and data.containsKey('authType')) or (colPickMode==1 and !data.containsKey('authType'))">
					a.authType=#{data.authType},
				</if>
				<if test="(colPickMode==0 and data.containsKey('authTypeIncrement')) or (colPickMode==1 and !data.containsKey('authTypeIncrement'))">
					a.authType=ifnull(a.authType,0) + #{data.authTypeIncrement},
				</if>
				<if test="(colPickMode==0 and data.containsKey('sourceType')) or (colPickMode==1 and !data.containsKey('sourceType'))">
					a.sourceType=#{data.sourceType},
				</if>
				<if test="(colPickMode==0 and data.containsKey('sourceTypeIncrement')) or (colPickMode==1 and !data.containsKey('sourceTypeIncrement'))">
					a.sourceType=ifnull(a.sourceType,0) + #{data.sourceTypeIncrement},
				</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="ResourceEntity-Map">
		select <include refid="_columns"/>
		from mortals_xhx_resource as a
	   where a.id=#{condition.id}
	</select>
	<!-- 根据主健删除 -->
	<delete id="deleteByKey" parameterType="paramDto">
		delete a.* from mortals_xhx_resource as a where a.id=#{condition.id}
	</delete>
	<!-- 根据主健删除一批，针对单一主健有效 -->
	<delete id="deleteByKeys">
		delete from mortals_xhx_resource 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_resource as a
		<trim suffixOverrides="where" suffix="">
			 where 
			<trim prefixOverrides="and" prefix="">
				<include refid="_condition_"/>
			</trim>
		</trim>
	</delete>
	<!-- 获取列表 -->
	<select id="getList" parameterType="paramDto" resultMap="ResourceEntity-Map">
		select <include refid="_columns"/>
		  from mortals_xhx_resource 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_resource 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('url')">
					<if test="condition.url != null and condition.url != ''">
						and a.url like #{condition.url}
					</if>
					<if test="condition.url == null">
						and a.url is null
					</if>
				</if>
				<if test="condition.containsKey('urlList')">
					 and a.url in 
					<foreach collection="condition.urlList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('authType')">
					<if test="condition.authType != null ">
						and a.authType = #{condition.authType}
					</if>
					<if test="condition.authType == null">
						and a.authType is null
					</if>
				</if>
				<if test="condition.containsKey('authTypeList')">
					 and a.authType in 
					<foreach collection="condition.authTypeList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('authTypeStart') and condition.authTypeStart != null">
					<![CDATA[ and a.authType >= #{condition.authTypeStart} ]]>
				</if>
				<if test="condition.containsKey('authTypeEnd') and condition.authTypeEnd != null">
					<![CDATA[ and a.authType <= #{condition.authTypeEnd} ]]>
				</if>
				<if test="condition.containsKey('sourceType')">
					<if test="condition.sourceType != null ">
						and a.sourceType = #{condition.sourceType}
					</if>
					<if test="condition.sourceType == null">
						and a.sourceType is null
					</if>
				</if>
				<if test="condition.containsKey('sourceTypeList')">
					 and a.sourceType in 
					<foreach collection="condition.sourceTypeList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('sourceTypeStart') and condition.sourceTypeStart != null">
					<![CDATA[ and a.sourceType >= #{condition.sourceTypeStart} ]]>
				</if>
				<if test="condition.containsKey('sourceTypeEnd') and condition.sourceTypeEnd != null">
					<![CDATA[ and a.sourceType <= #{condition.sourceTypeEnd} ]]>
				</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('url')">
					a.url
					<if test="orderCol.url!= null">
						${orderCol.url}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('authType')">
					a.authType
					<if test="orderCol.authType!= null">
						${orderCol.authType}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('sourceType')">
					a.sourceType
					<if test="orderCol.sourceType!= null">
						${orderCol.sourceType}
					</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>