<?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.idgenerator.dao.ibatis.IdgeneratorDaoImpl">
    
	<!-- 字段和属性映射 -->
	<resultMap type="IdgeneratorEntity" id="IdgeneratorEntity-Map">
		<result property="id" column="id" />
		<result property="idType" column="idType" />
		<result property="idMaxValue" column="idMaxValue" />
		<result property="remark" column="remark" />
		<result property="versionNum" column="versionNum" />
		<result property="gmtModify" column="gmtModify" />
	</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('idType') or colPickMode == 1 and data.containsKey('idType')))">
				a.idType as idType,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('idMaxValue') or colPickMode == 1 and data.containsKey('idMaxValue')))">
				a.idMaxValue as idMaxValue,
			</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('versionNum') or colPickMode == 1 and data.containsKey('versionNum')))">
				a.versionNum as versionNum,
			</if>
			<if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('gmtModify') or colPickMode == 1 and data.containsKey('gmtModify')))">
				a.gmtModify as gmtModify,
			</if>
		</trim>
	</sql>
	<!-- 新增 -->
	<insert id="insert" parameterType="IdgeneratorEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mortals_xhx_idgenerator 
		(idType,idMaxValue,remark,versionNum,gmtModify
		)VALUES(#{idType},#{idMaxValue},#{remark},#{versionNum},#{gmtModify})
	</insert>
	<!-- 批量新增 -->
	<insert id="insertBatch" parameterType="paramDto">
		insert into mortals_xhx_idgenerator 
		(idType,idMaxValue,remark,versionNum,gmtModify
		)
		VALUES
		<foreach collection="data.dataList" item="item" index="index" separator="," >  
		(#{item.idType},#{item.idMaxValue},#{item.remark},#{item.versionNum},#{item.gmtModify})
		</foreach>
	</insert>
	<!-- 根据ParamDto更新 -->
	<update id="update" parameterType="paramDto">
		update mortals_xhx_idgenerator as a
		   set
		   <trim suffixOverrides="," suffix="">
				<if test="(colPickMode==0 and data.containsKey('idType')) or (colPickMode==1 and !data.containsKey('idType'))">
					a.idType=#{data.idType},
				</if>
				<if test="(colPickMode==0 and data.containsKey('idMaxValue')) or (colPickMode==1 and !data.containsKey('idMaxValue'))">
					a.idMaxValue=#{data.idMaxValue},
				</if>
				<if test="(colPickMode==0 and data.containsKey('idMaxValueIncrement')) or (colPickMode==1 and !data.containsKey('idMaxValueIncrement'))">
					a.idMaxValue=ifnull(a.idMaxValue,0) + #{data.idMaxValueIncrement},
				</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('versionNum')) or (colPickMode==1 and !data.containsKey('versionNum'))">
					a.versionNum=#{data.versionNum},
				</if>
				<if test="(colPickMode==0 and data.containsKey('versionNumIncrement')) or (colPickMode==1 and !data.containsKey('versionNumIncrement'))">
					a.versionNum=ifnull(a.versionNum,0) + #{data.versionNumIncrement},
				</if>
				<if test="(colPickMode==0 and data.containsKey('gmtModify')) or (colPickMode==1 and !data.containsKey('gmtModify'))">
					a.gmtModify=#{data.gmtModify},
				</if>
		    </trim>
		   <trim suffixOverrides="where" suffix="">
			    where
			    <trim prefixOverrides="and" prefix="">
					<include refid="_condition_"/>
			   	</trim>
			</trim>
	</update>

	<!-- 根据主健查询 -->
	<select id="getByKey" parameterType="paramDto" resultMap="IdgeneratorEntity-Map">
		select <include refid="_columns"/>
		from mortals_xhx_idgenerator as a
	   where a.id=#{condition.id}
	</select>
	<!-- 根据主健删除 -->
	<delete id="deleteByKey" parameterType="paramDto">
		delete a.* from mortals_xhx_idgenerator as a where a.id=#{condition.id}
	</delete>
	<!-- 根据主健删除一批，针对单一主健有效 -->
	<delete id="deleteByKeys">
		delete from mortals_xhx_idgenerator 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_idgenerator as a
		<trim suffixOverrides="where" suffix="">
			 where 
			<trim prefixOverrides="and" prefix="">
				<include refid="_condition_"/>
			</trim>
		</trim>
	</delete>
	<!-- 获取列表 -->
	<select id="getList" parameterType="paramDto" resultMap="IdgeneratorEntity-Map">
		select <include refid="_columns"/>
		  from mortals_xhx_idgenerator 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_idgenerator 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('idType')">
					<if test="condition.idType != null and condition.idType != ''">
						and a.idType like #{condition.idType}
					</if>
					<if test="condition.idType == null">
						and a.idType is null
					</if>
				</if>
				<if test="condition.containsKey('idTypeList')">
					 and a.idType in 
					<foreach collection="condition.idTypeList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('idMaxValue')">
					<if test="condition.idMaxValue != null ">
						and a.idMaxValue = #{condition.idMaxValue}
					</if>
					<if test="condition.idMaxValue == null">
						and a.idMaxValue is null
					</if>
				</if>
				<if test="condition.containsKey('idMaxValueList')">
					 and a.idMaxValue in 
					<foreach collection="condition.idMaxValueList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('idMaxValueStart') and condition.idMaxValueStart != null">
					<![CDATA[ and a.idMaxValue >= #{condition.idMaxValueStart} ]]>
				</if>
				<if test="condition.containsKey('idMaxValueEnd') and condition.idMaxValueEnd != null">
					<![CDATA[ and a.idMaxValue <= #{condition.idMaxValueEnd} ]]>
				</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('versionNum')">
					<if test="condition.versionNum != null ">
						and a.versionNum = #{condition.versionNum}
					</if>
					<if test="condition.versionNum == null">
						and a.versionNum is null
					</if>
				</if>
				<if test="condition.containsKey('versionNumList')">
					 and a.versionNum in 
					<foreach collection="condition.versionNumList" open="(" close=")" index="index" item="item" separator=",">
						#{item}
					</foreach>
				</if>
				<if test="condition.containsKey('versionNumStart') and condition.versionNumStart != null">
					<![CDATA[ and a.versionNum >= #{condition.versionNumStart} ]]>
				</if>
				<if test="condition.containsKey('versionNumEnd') and condition.versionNumEnd != null">
					<![CDATA[ and a.versionNum <= #{condition.versionNumEnd} ]]>
				</if>
				<if test="condition.containsKey('gmtModify')">
					<if test="condition.gmtModify != null ">
						and a.gmtModify = #{condition.gmtModify}
					</if>
					<if test="condition.gmtModify == null">
						and a.gmtModify is null
					</if>
				</if>
				<if test="condition.containsKey('gmtModifyStart') and condition.gmtModifyStart != null and condition.gmtModifyStart!=''">
					<![CDATA[ and a.gmtModify >= STR_TO_DATE(left(concat(#{condition.gmtModifyStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s') ]]>
				</if>
				<if test="condition.containsKey('gmtModifyEnd') and condition.gmtModifyEnd != null and condition.gmtModifyEnd!=''">
					<![CDATA[ and a.gmtModify <= STR_TO_DATE(left(concat(#{condition.gmtModifyEnd},' 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('idType')">
					a.idType
					<if test="orderCol.idType!= null">
						${orderCol.idType}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('idMaxValue')">
					a.idMaxValue
					<if test="orderCol.idMaxValue!= null">
						${orderCol.idMaxValue}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('remark')">
					a.remark
					<if test="orderCol.remark!= null">
						${orderCol.remark}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('versionNum')">
					a.versionNum
					<if test="orderCol.versionNum!= null">
						${orderCol.versionNum}
					</if>
					,
				</if>
				<if test="orderCol.containsKey('gmtModify')">
					a.gmtModify
					<if test="orderCol.gmtModify!= null">
						${orderCol.gmtModify}
					</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>