<?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.valid.dao.ibatis.ValidCodeDaoImpl">
    
	<!-- 字段和属性映射 -->
	<resultMap type="ValidCodeEntity" id="ValidCodeEntity-Map">
		<result property="id" column="id" />
		<result property="code" column="code" />
		<result property="url" column="url" />
		<result property="mobile" column="mobile" />
		<result property="email" column="email" />
		<result property="type" column="type" />
		<result property="sessionId" column="sessionId" />
		<result property="ip" column="ip" />
		<result property="createTime" column="createTime" />
		<result property="lapseTime" column="lapseTime" />
	</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('code') or colPickMode == 1 and data.containsKey('code')))">
				a.code as code,
			</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('mobile') or colPickMode == 1 and data.containsKey('mobile')))">
				a.mobile as mobile,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('email') or colPickMode == 1 and data.containsKey('email')))">
				a.email as email,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('type') or colPickMode == 1 and data.containsKey('type')))">
				a.type as type,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('sessionId') or colPickMode == 1 and data.containsKey('sessionId')))">
				a.sessionId as sessionId,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('ip') or colPickMode == 1 and data.containsKey('ip')))">
				a.ip as ip,
			</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('lapseTime') or colPickMode == 1 and data.containsKey('lapseTime')))">
				a.lapseTime as lapseTime,
			</if>
		</trim>
	</sql>
	<!-- 新增 -->
	<insert id="insert" parameterType="ValidCodeEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mortals_xhx_valid_code 
		(code,url,mobile,email,type,
			sessionId,ip,createTime,lapseTime
		)VALUES(#{code},#{url},#{mobile},#{email},#{type},
			#{sessionId},#{ip},#{createTime},#{lapseTime})
	</insert>
	<!-- 批量新增 -->
	<insert id="insertBatch" parameterType="paramDto">
		insert into mortals_xhx_valid_code 
		(code,url,mobile,email,type,
			sessionId,ip,createTime,lapseTime
		)
		VALUES
		<foreach collection="data.dataList" item="item" index="index" separator="," >  
		(#{item.code},#{item.url},#{item.mobile},#{item.email},#{item.type},
			#{item.sessionId},#{item.ip},#{item.createTime},#{item.lapseTime})
		</foreach>
	</insert>
	<!-- 根据ParamDto更新 -->
	<update id="update" parameterType="paramDto">
		update mortals_xhx_valid_code as a
		   set
		   <trim suffixOverrides="," suffix="">
				<if test="(colPickMode==0 and data.containsKey('code')) or (colPickMode==1 and !data.containsKey('code'))">
					a.code=#{data.code},
				</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('mobile')) or (colPickMode==1 and !data.containsKey('mobile'))">
					a.mobile=#{data.mobile},
				</if>
				<if test="(colPickMode==0 and data.containsKey('email')) or (colPickMode==1 and !data.containsKey('email'))">
					a.email=#{data.email},
				</if>
				<if test="(colPickMode==0 and data.containsKey('type')) or (colPickMode==1 and !data.containsKey('type'))">
					a.type=#{data.type},
				</if>
				<if test="(colPickMode==0 and data.containsKey('typeIncrement')) or (colPickMode==1 and !data.containsKey('typeIncrement'))">
					a.type=ifnull(a.type,0) + #{data.typeIncrement},
				</if>
				<if test="(colPickMode==0 and data.containsKey('sessionId')) or (colPickMode==1 and !data.containsKey('sessionId'))">
					a.sessionId=#{data.sessionId},
				</if>
				<if test="(colPickMode==0 and data.containsKey('ip')) or (colPickMode==1 and !data.containsKey('ip'))">
					a.ip=#{data.ip},
				</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('lapseTime')) or (colPickMode==1 and !data.containsKey('lapseTime'))">
					a.lapseTime=#{data.lapseTime},
				</if>
		    </trim>
		   <trim suffixOverrides="where" suffix="">
			    where
			    <trim prefixOverrides="and" prefix="">
					<include refid="_condition_"/>
			   	</trim>
			</trim>
	</update>

	<!-- 根据主健查询 -->
	<select id="getByKey" parameterType="paramDto" resultMap="ValidCodeEntity-Map">
		select <include refid="_columns"/>
		from mortals_xhx_valid_code as a
	   where a.id=#{condition.id}
	</select>
	<!-- 根据主健删除 -->
	<delete id="deleteByKey" parameterType="paramDto">
		delete a.* from mortals_xhx_valid_code as a where a.id=#{condition.id}
	</delete>
	<!-- 根据主健删除一批，针对单一主健有效 -->
	<delete id="deleteByKeys">
		delete from mortals_xhx_valid_code 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_valid_code as a
		<trim suffixOverrides="where" suffix="">
			 where 
			<trim prefixOverrides="and" prefix="">
				<include refid="_condition_"/>
			</trim>
		</trim>
	</delete>
	<!-- 获取列表 -->
	<select id="getList" parameterType="paramDto" resultMap="ValidCodeEntity-Map">
		select <include refid="_columns"/>
		  from mortals_xhx_valid_code 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_valid_code 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('code')">
					<if test="condition.code != null and condition.code != ''">
						and a.code like #{condition.code}
					</if>
					<if test="condition.code == null">
						and a.code is null
					</if>
				</if>
				<if test="condition.containsKey('codeList')">
					 and a.code in 
					<foreach collection="condition.codeList" 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('mobile')">
					<if test="condition.mobile != null and condition.mobile != ''">
						and a.mobile like #{condition.mobile}
					</if>
					<if test="condition.mobile == null">
						and a.mobile is null
					</if>
				</if>
				<if test="condition.containsKey('mobileList')">
					 and a.mobile in 
					<foreach collection="condition.mobileList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('email')">
					<if test="condition.email != null and condition.email != ''">
						and a.email like #{condition.email}
					</if>
					<if test="condition.email == null">
						and a.email is null
					</if>
				</if>
				<if test="condition.containsKey('emailList')">
					 and a.email in 
					<foreach collection="condition.emailList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('type')">
					<if test="condition.type != null ">
						and a.type = #{condition.type}
					</if>
					<if test="condition.type == null">
						and a.type is null
					</if>
				</if>
				<if test="condition.containsKey('typeList')">
					 and a.type in 
					<foreach collection="condition.typeList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('typeStart') and condition.typeStart != null">
					<![CDATA[ and a.type >= #{condition.typeStart} ]]>
				</if>
				<if test="condition.containsKey('typeEnd') and condition.typeEnd != null">
					<![CDATA[ and a.type <= #{condition.typeEnd} ]]>
				</if>
				<if test="condition.containsKey('sessionId')">
					<if test="condition.sessionId != null and condition.sessionId != ''">
						and a.sessionId like #{condition.sessionId}
					</if>
					<if test="condition.sessionId == null">
						and a.sessionId is null
					</if>
				</if>
				<if test="condition.containsKey('sessionIdList')">
					 and a.sessionId in 
					<foreach collection="condition.sessionIdList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('ip')">
					<if test="condition.ip != null and condition.ip != ''">
						and a.ip like #{condition.ip}
					</if>
					<if test="condition.ip == null">
						and a.ip is null
					</if>
				</if>
				<if test="condition.containsKey('ipList')">
					 and a.ip in 
					<foreach collection="condition.ipList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</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('lapseTime')">
					<if test="condition.lapseTime != null ">
						and a.lapseTime = #{condition.lapseTime}
					</if>
					<if test="condition.lapseTime == null">
						and a.lapseTime is null
					</if>
				</if>
				<if test="condition.containsKey('lapseTimeStart') and condition.lapseTimeStart != null and condition.lapseTimeStart!=''">
					<![CDATA[ and a.lapseTime >= STR_TO_DATE(left(concat(#{condition.lapseTimeStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s') ]]>
				</if>
				<if test="condition.containsKey('lapseTimeEnd') and condition.lapseTimeEnd != null and condition.lapseTimeEnd!=''">
					<![CDATA[ and a.lapseTime <= STR_TO_DATE(left(concat(#{condition.lapseTimeEnd},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s') ]]>
				</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('code')">
					a.code
					<if test="orderCol.code!= null">
						${orderCol.code}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('url')">
					a.url
					<if test="orderCol.url!= null">
						${orderCol.url}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('mobile')">
					a.mobile
					<if test="orderCol.mobile!= null">
						${orderCol.mobile}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('email')">
					a.email
					<if test="orderCol.email!= null">
						${orderCol.email}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('type')">
					a.type
					<if test="orderCol.type!= null">
						${orderCol.type}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('sessionId')">
					a.sessionId
					<if test="orderCol.sessionId!= null">
						${orderCol.sessionId}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('ip')">
					a.ip
					<if test="orderCol.ip!= null">
						${orderCol.ip}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('createTime')">
					a.createTime
					<if test="orderCol.createTime!= null">
						${orderCol.createTime}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('lapseTime')">
					a.lapseTime
					<if test="orderCol.lapseTime!= null">
						${orderCol.lapseTime}
					</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>