DtmsMessage.xml 13.2 KB
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.b2c.dtms.dao.DtmsMessageDao">

	<!-- dtms_message 所有查询列 -->
	<sql id="QUERY_COLUMN_LIST">
		<![CDATA[id,biz_id AS bizId,delay_seconds as delaySeconds,runtime,topic,content,memo,create_time AS createTime,wait_retry_num AS waitRetryNum,version,call_url AS callUrl,confirm_url AS confirmUrl,tag AS tag,lock_status AS lockStatus,lock_time AS lockTime,status,type,latest_consume_log AS latestConsumeLog]]>
	</sql>

	<!-- dtms_message 查询列来源表-->
	<sql id="QUERY_FROM_TABLE"><![CDATA[FROM dtms_message]]></sql>
	
	<!-- 全部条件(更多功能可以通过queryData扩展实现)  -->
	<sql id="QUERY_WHERE_CLAUSE">
		<where>
			<if test="id != null and id != ''"><![CDATA[AND id = #{id}]]></if>
			<if test="bizId != null and bizId != ''"><![CDATA[AND biz_id = #{bizId}]]></if>
			<if test="delaySeconds != null and delaySeconds != ''"><![CDATA[AND delay_seconds = #{delaySeconds}]]></if>
			<if test="runtime != null and runtime != ''"><![CDATA[AND runtime = #{runtime}]]></if>
			<if test="topic != null and topic != ''"><![CDATA[AND topic = #{topic}]]></if>
			<if test="content != null and content != ''"><![CDATA[AND content = #{content}]]></if>
			<if test="memo != null and memo != ''"><![CDATA[AND memo = #{memo}]]></if>
			<if test="createTime != null and createTime != ''"><![CDATA[AND create_time = #{createTime}]]></if>
			<if test="waitRetryNum != null and waitRetryNum != ''"><![CDATA[AND wait_retry_num = #{waitRetryNum}]]></if>
			<if test="callUrl != null and callUrl != ''"><![CDATA[AND call_url = #{callUrl}]]></if>
			<if test="confirmUrl != null and confirmUrl != ''"><![CDATA[AND confirm_url = #{confirmUrl}]]></if>
			<if test="version != null"><![CDATA[AND version = #{version}]]></if>
			<if test="lockStatus != null and lockStatus != ''"><![CDATA[AND lock_status = #{lockStatus}]]></if>
			<if test="lockTime != null and lockTime != ''"><![CDATA[AND lock_time = #{lockTime}]]></if>
			<if test="status != null and status != ''"><![CDATA[AND status = #{status}]]></if>
			<if test="type != null and type != ''"><![CDATA[AND type = #{type}]]></if>
            <choose>
                <when test="tag != null and tag != ''">
                  <![CDATA[AND tag = #{tag}]]>
                </when>
                <otherwise>
                    <if test="queryData != null and queryData != '' and queryData.tags!=null and queryData.tags.size>0">
	                  <![CDATA[ AND tag in ]]>
	                  <foreach item="item" index="index" collection="queryData.tags" open="(" separator="," close=")">  
	                       #{item}
	                  </foreach>
                    </if>
                </otherwise>
            </choose>
		</where>
	</sql>
	
	<!-- 智能排序与分页 -->
	<sql id="QUERY_ORDER_LIMIT_CONDTION">
		<if test="orderField != null and orderField != '' and orderFieldType != null and orderFieldType != ''"><![CDATA[ORDER BY ${orderField} ${orderFieldType}]]></if>
		<if test="startIndex != null and startIndex &gt;= 0 and pageSize != null and pageSize &gt; 0"><![CDATA[LIMIT #{startIndex},#{pageSize}]]></if>
	</sql>

	<!-- 更新列字段,只要不为NULL则更新,除开主键列 -->
	<sql id="UPDATE_COLUMN_SET">
		<set>
			<if test="bizId != null"><![CDATA[biz_id = #{bizId},]]></if>
			<if test="delaySeconds != null"><![CDATA[delay_seconds = #{delaySeconds},]]></if>
			<if test="runtime != null"><![CDATA[runtime = #{runtime},]]></if>
			<if test="topic != null"><![CDATA[topic = #{topic},]]></if>
			<if test="content != null"><![CDATA[content = #{content},]]></if>
			<if test="memo != null"><![CDATA[memo = #{memo},]]></if>
			<if test="createTime != null"><![CDATA[create_time = #{createTime},]]></if>
			<if test="waitRetryNum != null"><![CDATA[wait_retry_num = #{waitRetryNum},]]></if>
			<if test="version != null"><![CDATA[version = #{version},]]></if>
			<if test="callUrl != null"><![CDATA[call_url = #{callUrl},]]></if>
			<if test="confirmUrl != null"><![CDATA[confirm_url = #{confirmUrl},]]></if>
			<if test="tag != null"><![CDATA[tag = #{tag},]]></if>
			<if test="lockStatus != null"><![CDATA[lock_status = #{lockStatus},]]></if>
			<if test="type != null"><![CDATA[type = #{type},]]></if>
			<if test="latestConsumeLog != null"><![CDATA[latest_consume_log = #{latestConsumeLog},]]></if>
			<choose>
			    <when test="queryData != null and queryData.ignoreLockTimeNullCheck=='true'">
	               <![CDATA[lock_time = #{lockTime},]]>
	            </when>
	            <otherwise>
					<if test="lockTime != null"><![CDATA[lock_time = #{lockTime},]]></if>
	            </otherwise>
			</choose>
			<if test="status != null"><![CDATA[status = #{status},]]></if>
			<![CDATA[version = version + 1,]]>
		</set>
	</sql>

	<!-- 插入dtms_message记录 -->
	<insert id="insertEntry" parameterType="dtmsMessage" >
		<![CDATA[
			INSERT INTO dtms_message (id,biz_id,delay_seconds,runtime,topic,content,memo,create_time,wait_retry_num,version,call_url,confirm_url,tag,lock_status,lock_time,status,type,latest_consume_log)
			VALUES (#{id},#{bizId},#{delaySeconds},#{runtime},#{topic},#{content},#{memo},#{createTime},#{waitRetryNum},#{version},#{callUrl},#{confirmUrl},#{tag},0,#{lockTime},#{status},#{type},#{latestConsumeLog})
		]]>
	</insert>
	
	<!-- 返回插入的编号,在事务开启状态下有效 -->
	<select id="lastSequence" resultType="int"><![CDATA[SELECT LAST_INSERT_ID() AS id]]></select>

	<!-- 删除记录,主键IN(array) -->
	<delete id="deleteByArrayKey" parameterType="java.lang.reflect.Array" >
		<![CDATA[DELETE FROM dtms_message WHERE id IN]]>
		<foreach collection="array" item="id" open="(" separator="," close=")">
			<![CDATA[#{id}]]>
		</foreach>
	</delete>

	<!-- 删除,通过条件 -->
	<update id="deleteByCondtion" parameterType="dtmsMessage" >
		<![CDATA[DELETE FROM dtms_message]]>
		<include refid="QUERY_WHERE_CLAUSE"/>
	</update>

	<!-- 修改记录通过主键 -->
	<update id="updateByKey" parameterType="dtmsMessage" >
		<![CDATA[UPDATE dtms_message]]>
        <include refid="UPDATE_COLUMN_SET"/>
        <where>
            <if test="id != null"><![CDATA[AND id = #{id}]]></if>
	        <if test="queryData != null and  queryData.ignoreVersion!='true'">
	            AND version =#{version}
	        </if>
        </where>
	</update>
	
	<update id="updateByCondtion" parameterType="dtmsMessage" >
        <![CDATA[UPDATE dtms_message]]>
        <include refid="UPDATE_COLUMN_SET"/>
        <where>
            <if test="queryData != null and queryData != ''">
               <if test="queryData.id!=null"><![CDATA[AND id = #{queryData.id}]]></if>
               <if test="queryData.bizId != null"><![CDATA[AND biz_id = #{queryData.bizId}]]></if>
               <if test="queryData.delaySeconds != null"><![CDATA[AND delay_seconds = #{queryData.delaySeconds}]]></if>
               <if test="queryData.runtime != null"><![CDATA[AND runtime = #{queryData.runtime}]]></if>
               <if test="queryData.topic != null"><![CDATA[AND a.topic <= #{queryData.topic}]]></if>
               <if test="queryData.content != null"><![CDATA[AND content = #{queryData.content}]]></if>
               <if test="queryData.memo != null"><![CDATA[AND memo = #{queryData.memo}]]></if>
               <if test="queryData.createTime != null"><![CDATA[AND create_time = #{queryData.createTime}]]></if>
               <if test="queryData.waitRetryNum != null"><![CDATA[AND wait_retry_num = #{queryData.waitRetryNum}]]></if>
               <if test="queryData.callUrl != null"><![CDATA[AND call_url = #{queryData.callUrl}]]></if>
               <if test="queryData.confirmUrl != null"><![CDATA[AND confirm_url = #{queryData.confirmUrl}]]></if>
               <if test="queryData.version != null"><![CDATA[AND version = #{queryData.version}]]></if>
               <if test="queryData.lockStatus != null"><![CDATA[AND lock_status = #{queryData.lockStatus}]]></if>
               <if test="queryData.lockTime != null"><![CDATA[AND lock_time = #{queryData.lockTime}]]></if>
               <if test="queryData.status != null"><![CDATA[AND status = #{queryData.status}]]></if>
               <if test="queryData.tag != null"><![CDATA[AND tag = #{queryData.tag}]]></if>
               <if test="queryData.type != null"><![CDATA[AND type = #{queryData.type}]]></if>
               <if test="queryData.idList!=null and queryData.idList.size>0">
                 <![CDATA[ AND id in ]]>
                 <foreach item="item" index="index" collection="queryData.idList" open="(" separator="," close=")">  
                      #{item}
                 </foreach>
               </if>
            </if>
        </where>
    </update>

	<!-- 查询,通过主键IN(array) -->
	<select id="selectEntryArray" parameterType="java.lang.reflect.Array" resultType="dtmsMessage">
		<![CDATA[SELECT]]>
		<include refid="QUERY_COLUMN_LIST"/>
		<include refid="QUERY_FROM_TABLE"/>
		<![CDATA[WHERE id IN]]>
		<foreach collection="array" item="id" open="(" separator="," close=")">
			<![CDATA[#{id}]]>
		</foreach>
	</select>

	<!-- 查询,通过条件 -->
	<select id="selectEntryList" parameterType="dtmsMessage" resultType="dtmsMessage">
		<![CDATA[SELECT]]>
		<include refid="QUERY_COLUMN_LIST"/>
		<include refid="QUERY_FROM_TABLE"/>
		<include refid="QUERY_WHERE_CLAUSE"/>
		<include refid="QUERY_ORDER_LIMIT_CONDTION"/>
	</select>

	<!-- 总数查询,通过条件 -->
	<select id="selectEntryListCount" parameterType="dtmsMessage" resultType="int">
		<![CDATA[SELECT COUNT(id) AS dataCount]]>
		<include refid="QUERY_FROM_TABLE"/>
		<include refid="QUERY_WHERE_CLAUSE"/>
	</select>
	
	<!-- 其它SQL语句 -->
   <!--
                    查询场景:
       1 非定时任务未经过二次确认,且运行时间未到达时,不能被执行
       2 非定时任务经过二次确认,忽略运行时间,可以直接被执行
       3 定时任务,无论是否经过二次确认,都需要等待运行时间到达时才能被运行
   -->
	<select id="getListUnLockedBeforeSepcTime" parameterType="dtmsMessage" resultType="dtmsMessage">
        <![CDATA[SELECT distinct]]>
        <include refid="QUERY_COLUMN_LIST"/>
        <include refid="QUERY_FROM_TABLE"/>
        <where>
            <![CDATA[AND lock_status =0]]>
            <![CDATA[AND lock_time is null]]>
            <![CDATA[AND ((status=10 and runtime<= #{runtime} and type=1) or
                          (status=20 and type=1) or
                          (status in (10,20) and runtime<= #{runtime} and type in (2,3)))]]>
        </where>
        <![CDATA[LIMIT #{startIndex},#{endIndex}]]>
    </select>
    
    <select id="getLockTimeOutList" parameterType="dtmsMessage" resultType="dtmsMessage">
        SELECT a.id,
               a.biz_id AS bizId,
               a.topic,
               a.version,
               a.tag AS tag,
               a.lock_status AS lockStatus,
               a.lock_time AS lockTime
        from dtms_message a ,dtms_message_config b
        <where>
            a.topic=b.msg_topic
            <if test="lockStatus != null and lockStatus != ''"><![CDATA[AND a.lock_status=#{lockStatus}]]></if>
            <if test="queryData != null and queryData != ''">
                <if test="queryData.currentTime !=null">
                    <![CDATA[AND timediff(#{queryData.currentTime},a.lock_time) > b.lock_timeout]]>
               </if>
            </if>
        </where>
        <![CDATA[LIMIT #{startIndex},#{endIndex}]]>
    </select>
	
	
	<update id="tryAddLock"  parameterType="dtmsMessage" >
	    <![CDATA[
            update dtms_message
            set lock_status =1,
                lock_time=#{lockTime},
                version = version + 1
            where id=#{id} and version=#{version} and lock_status=0 and lock_time is null
        ]]>
    </update>
    
	<select id="selectLockedCount" parameterType="dtmsMessage" resultType="int">
	      <![CDATA[
            select count(id) from dtms_message where id=#{id} and version=#{version} and lock_status=#{lockStatus} and lock_time=#{lockTime} 
          ]]>  
    </select>
    
    <update id="tryReleaseLock" parameterType="dtmsMessage" >
         <![CDATA[
            update dtms_message
            set lock_status=0,
                lock_time=null,
                version = version + 1
            where id=#{id} and version=#{version} and lock_status=#{lockStatus} and lock_time=#{lockTime}
         ]]>
    </update>
    
    <delete id="deleteByKeyAndVersion" parameterType="dtmsMessage" >
            delete from dtms_message where id=#{id} and version=#{version}
    </delete>
    
    <update id="reduceRetryCount" parameterType="dtmsMessage" >
        <![CDATA[
            update dtms_message
            set wait_retry_num = #{waitRetryNum},
                latest_consume_log = #{latestConsumeLog},
                version = version + 1
            where id=#{id} and version=#{version}
        ]]>
    </update>
        
    <update id="reduceRetryCountAndSetNextRuntime" parameterType="dtmsMessage" >
        <![CDATA[
            update dtms_message
            set wait_retry_num = #{waitRetryNum},
                runtime = #{runtime},
                latest_consume_log = #{latestConsumeLog},
                version = version + 1
            where id=#{id} and version=#{version}
        ]]>
    </update>
    
</mapper>