<?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.module.demo.dao.ibatis.DemoDaoImpl">
    
	<!-- 字段和属性映射 -->
	<resultMap type="DemoEntity" id="DemoEntity-Map">
		<result property="id" column="id" />
		<result property="fieldOne" column="fieldOne" />
		<result property="fieldTwo" column="fieldTwo" />
		<result property="updateTime" column="updateTime" />
		<result property="updateUser" column="updateUser" />
		<result property="createTime" column="createTime" />
		<result property="createUser" column="createUser" />
	</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('fieldOne') or colPickMode == 1 and data.containsKey('fieldOne')))">
				a.fieldOne as fieldOne,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('fieldTwo') or colPickMode == 1 and data.containsKey('fieldTwo')))">
				a.fieldTwo as fieldTwo,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('updateTime') or colPickMode == 1 and data.containsKey('updateTime')))">
				a.updateTime as updateTime,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('updateUser') or colPickMode == 1 and data.containsKey('updateUser')))">
				a.updateUser as updateUser,
			</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('createUser') or colPickMode == 1 and data.containsKey('createUser')))">
				a.createUser as createUser,
			</if>
		</trim>
	</sql>
	<!-- 新增 -->
	<insert id="insert" parameterType="DemoEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mortals_xhx_demo 
		(fieldOne,fieldTwo,updateTime,updateUser,createTime,
			createUser
		)VALUES(#{fieldOne},#{fieldTwo},#{updateTime},#{updateUser},#{createTime},
			#{createUser})
	</insert>
	<!-- 批量新增 -->
	<insert id="insertBatch" parameterType="paramDto">
		insert into mortals_xhx_demo 
		(fieldOne,fieldTwo,updateTime,updateUser,createTime,
			createUser
		)
		VALUES
		<foreach collection="data.dataList" item="item" index="index" separator="," >  
		(#{item.fieldOne},#{item.fieldTwo},#{item.updateTime},#{item.updateUser},#{item.createTime},
			#{item.createUser})
		</foreach>
	</insert>
	<!-- 根据ParamDto更新 -->
	<update id="update" parameterType="paramDto">
		update mortals_xhx_demo as a
		   set
		   <trim suffixOverrides="," suffix="">
				<if test="(colPickMode==0 and data.containsKey('fieldOne')) or (colPickMode==1 and !data.containsKey('fieldOne'))">
					a.fieldOne=#{data.fieldOne},
				</if>
				<if test="(colPickMode==0 and data.containsKey('fieldTwo')) or (colPickMode==1 and !data.containsKey('fieldTwo'))">
					a.fieldTwo=#{data.fieldTwo},
				</if>
				<if test="(colPickMode==0 and data.containsKey('fieldTwoIncrement')) or (colPickMode==1 and !data.containsKey('fieldTwoIncrement'))">
					a.fieldTwo=ifnull(a.fieldTwo,0) + #{data.fieldTwoIncrement},
				</if>
				<if test="(colPickMode==0 and data.containsKey('updateTime')) or (colPickMode==1 and !data.containsKey('updateTime'))">
					a.updateTime=#{data.updateTime},
				</if>
				<if test="(colPickMode==0 and data.containsKey('updateUser')) or (colPickMode==1 and !data.containsKey('updateUser'))">
					a.updateUser=#{data.updateUser},
				</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('createUser')) or (colPickMode==1 and !data.containsKey('createUser'))">
					a.createUser=#{data.createUser},
				</if>
		    </trim>
		   <trim suffixOverrides="where" suffix="">
			    where
			    <trim prefixOverrides="and" prefix="">
					<include refid="_condition_"/>
			   	</trim>
			</trim>
	</update>
	<!-- 批量更新 -->
	<update id="updateBatch" parameterType="paramDto">
		update mortals_xhx_demo as a
		<trim prefix="set" suffixOverrides=",">
			<trim prefix="fieldOne=(case" suffix="ELSE fieldOne end),">
				<foreach collection="data.dataList" item="item" index="index" separator="" >
					<if test="(colPickMode==0 and item.containsKey('fieldOne')) or (colPickMode==1 and !item.containsKey('fieldOne'))">
						when a.id=#{item.id} then #{item.fieldOne}
					</if>
				</foreach>
			</trim>
			<trim prefix="fieldTwo=(case" suffix="ELSE fieldTwo end),">
				<foreach collection="data.dataList" item="item" index="index" separator="" >
					<choose>
						<when test="(colPickMode==0 and item.containsKey('fieldTwo')) or (colPickMode==1 and !item.containsKey('fieldTwo'))">
							when a.id=#{item.id} then #{item.fieldTwo}
						</when>
						<when test="(colPickMode==0 and item.containsKey('fieldTwoIncrement')) or (colPickMode==1 and !item.containsKey('fieldTwoIncrement'))">
							when a.id=#{item.id} then ifnull(a.fieldTwo,0) + #{item.fieldTwoIncrement}
						</when>
					</choose>
				</foreach>
			</trim>
			<trim prefix="updateTime=(case" suffix="ELSE updateTime end),">
				<foreach collection="data.dataList" item="item" index="index" separator="" >
					<if test="(colPickMode==0 and item.containsKey('updateTime')) or (colPickMode==1 and !item.containsKey('updateTime'))">
						when a.id=#{item.id} then #{item.updateTime}
					</if>
				</foreach>
			</trim>
			<trim prefix="updateUser=(case" suffix="ELSE updateUser end),">
				<foreach collection="data.dataList" item="item" index="index" separator="" >
					<if test="(colPickMode==0 and item.containsKey('updateUser')) or (colPickMode==1 and !item.containsKey('updateUser'))">
						when a.id=#{item.id} then #{item.updateUser}
					</if>
				</foreach>
			</trim>
			<trim prefix="createTime=(case" suffix="ELSE createTime end),">
				<foreach collection="data.dataList" item="item" index="index" separator="" >
					<if test="(colPickMode==0 and item.containsKey('createTime')) or (colPickMode==1 and !item.containsKey('createTime'))">
						when a.id=#{item.id} then #{item.createTime}
					</if>
				</foreach>
			</trim>
			<trim prefix="createUser=(case" suffix="ELSE createUser end),">
				<foreach collection="data.dataList" item="item" index="index" separator="" >
					<if test="(colPickMode==0 and item.containsKey('createUser')) or (colPickMode==1 and !item.containsKey('createUser'))">
						when a.id=#{item.id} then #{item.createUser}
					</if>
				</foreach>
			</trim>
        </trim>
        where id in
		<foreach collection="data.dataList" item="item" index="index" open="(" separator="," close=")">
			#{item.id}
		</foreach>
	</update>
	<!-- 根据主健查询 -->
	<select id="getByKey" parameterType="paramDto" resultMap="DemoEntity-Map">
		select <include refid="_columns"/>
		from mortals_xhx_demo as a
	   where a.id=#{condition.id}
	</select>
	<!-- 根据主健删除 -->
	<delete id="deleteByKey" parameterType="paramDto">
		delete a.* from mortals_xhx_demo as a where a.id=#{condition.id}
	</delete>
	<!-- 根据主健删除一批，针对单一主健有效 -->
	<delete id="deleteByKeys">
		delete from mortals_xhx_demo 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_demo as a
		<trim suffixOverrides="where" suffix="">
			 where 
			<trim prefixOverrides="and" prefix="">
				<include refid="_condition_"/>
			</trim>
		</trim>
	</delete>
	<!-- 获取列表 -->
	<select id="getList" parameterType="paramDto" resultMap="DemoEntity-Map">
		select <include refid="_columns"/>
		  from mortals_xhx_demo 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_demo 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('fieldOne')">
					<if test="condition.fieldOne != null and condition.fieldOne != ''">
						and a.fieldOne like #{condition.fieldOne}
					</if>
					<if test="condition.fieldOne == null">
						and a.fieldOne is null
					</if>
				</if>
				<if test="condition.containsKey('fieldOneList')">
					 and a.fieldOne in 
					<foreach collection="condition.fieldOneList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('fieldTwo')">
					<if test="condition.fieldTwo != null ">
						and a.fieldTwo = #{condition.fieldTwo}
					</if>
					<if test="condition.fieldTwo == null">
						and a.fieldTwo is null
					</if>
				</if>
				<if test="condition.containsKey('fieldTwoList')">
					 and a.fieldTwo in 
					<foreach collection="condition.fieldTwoList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('fieldTwoStart') and condition.fieldTwoStart != null">
					<![CDATA[ and a.fieldTwo >= #{condition.fieldTwoStart} ]]>
				</if>
				<if test="condition.containsKey('fieldTwoEnd') and condition.fieldTwoEnd != null">
					<![CDATA[ and a.fieldTwo <= #{condition.fieldTwoEnd} ]]>
				</if>
				<if test="condition.containsKey('updateTime')">
					<if test="condition.updateTime != null ">
						and a.updateTime = #{condition.updateTime}
					</if>
					<if test="condition.updateTime == null">
						and a.updateTime is null
					</if>
				</if>
				<if test="condition.containsKey('updateTimeStart') and condition.updateTimeStart != null and condition.updateTimeStart!=''">
					<![CDATA[ and a.updateTime >= STR_TO_DATE(left(concat(#{condition.updateTimeStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s') ]]>
				</if>
				<if test="condition.containsKey('updateTimeEnd') and condition.updateTimeEnd != null and condition.updateTimeEnd!=''">
					<![CDATA[ and a.updateTime <= STR_TO_DATE(left(concat(#{condition.updateTimeEnd},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s') ]]>
				</if>
				<if test="condition.containsKey('updateUser')">
					<if test="condition.updateUser != null and condition.updateUser != ''">
						and a.updateUser like #{condition.updateUser}
					</if>
					<if test="condition.updateUser == null">
						and a.updateUser is null
					</if>
				</if>
				<if test="condition.containsKey('updateUserList')">
					 and a.updateUser in 
					<foreach collection="condition.updateUserList" 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('createUser')">
					<if test="condition.createUser != null and condition.createUser != ''">
						and a.createUser like #{condition.createUser}
					</if>
					<if test="condition.createUser == null">
						and a.createUser is null
					</if>
				</if>
				<if test="condition.containsKey('createUserList')">
					 and a.createUser in 
					<foreach collection="condition.createUserList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</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('fieldOne')">
					a.fieldOne
					<if test="orderCol.fieldOne!= null">
						${orderCol.fieldOne}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('fieldTwo')">
					a.fieldTwo
					<if test="orderCol.fieldTwo!= null">
						${orderCol.fieldTwo}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('updateTime')">
					a.updateTime
					<if test="orderCol.updateTime!= null">
						${orderCol.updateTime}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('updateUser')">
					a.updateUser
					<if test="orderCol.updateUser!= null">
						${orderCol.updateUser}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('createTime')">
					a.createTime
					<if test="orderCol.createTime!= null">
						${orderCol.createTime}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('createUser')">
					a.createUser
					<if test="orderCol.createUser!= null">
						${orderCol.createUser}
					</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>