<?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.iamberry.wechat.service.mapper.MQMapper">

	<sql id="allField">
		SERVICE_ID serviceId,
		SERVICE_TO_MESSAGE serviceToMessage,
		SERVICE_IS_SEND serviceIsSend,
		SERVICE_TYPE serviceType,
		SERVICE_STATUS serviceStatus,
		SERVICE_OTHER_MESSAGE serviceOtherMessage,
		SERVICE_CREATE_DATE serviceCreateDate,
		SERVICE_HANDLER_OBJECT_NAME serviceHandlerObjectName,
		SERVICE_HANDLER_METHOD_NAME serviceHandlerMethodName
	</sql>
	
	<!-- 获取10条待处理记录 -->
	<select id="selectWaitHandlerMessage" resultType="MQMessage">
		SELECT
			<include refid="allField"/>
		FROM
			TB_IAMBERRY_MQ_SERVICE
		WHERE
			SERVICE_IS_SEND = 2
		LIMIT
			0, 30
	</select>
	
	<!-- 判断是否已存在此任务 -->
	<select id="judgeMessageIsExist" resultType="Integer" parameterType="MQMessage">
		SELECT
			SERVICE_ID
		FROM
			TB_IAMBERRY_MQ_SERVICE
		WHERE
			SERVICE_TO_MESSAGE = #{serviceToMessage} AND SERVICE_IS_SEND = #{serviceIsSend} AND SERVICE_HANDLER_METHOD_NAME = #{serviceHandlerMethodName}
		LIMIT
			0, 1
	</select>
	
	<!-- 更新数据 -->
	<update id="updateBatchMessage" parameterType="java.util.List">
		<foreach collection="list" item="item" separator=";">
			UPDATE 
				TB_IAMBERRY_MQ_SERVICE 
			SET 
				SERVICE_IS_SEND = #{item.serviceIsSend},
				SERVICE_STATUS = #{item.serviceStatus},
				SERVICE_ERROR_MESSAGE = #{item.serviceErrorMessage}
			WHERE 
				SERVICE_ID = #{item.serviceId}
        </foreach>
	</update>
	
	<!-- 更新数据 -->
	<update id="updateMessageOne" parameterType="MQMessage">
		UPDATE 
			TB_IAMBERRY_MQ_SERVICE 
		SET 
			SERVICE_IS_SEND = #{serviceIsSend},
			SERVICE_STATUS = #{serviceStatus},
			SERVICE_ERROR_MESSAGE = #{serviceErrorMessage}
		WHERE 
			SERVICE_ID = #{serviceId}
	</update>
	
	<!-- 添加消息 -->
	<insert id="insertOneMQMessage" parameterType="MQMessage">
		INSERT INTO 
		 	TB_IAMBERRY_MQ_SERVICE
		 		(
			 		SERVICE_TO_MESSAGE, SERVICE_IS_SEND, SERVICE_TYPE, SERVICE_STATUS, SERVICE_OTHER_MESSAGE,
					SERVICE_CREATE_DATE, SERVICE_HANDLER_OBJECT_NAME, SERVICE_HANDLER_METHOD_NAME
				)
		 VALUES 
		  	( 
		  	  #{serviceToMessage},
			  #{serviceIsSend},  
			  #{serviceType}, 
			  #{serviceStatus},
			  #{serviceOtherMessage},
			  NOW(),
			  #{serviceHandlerObjectName},
			  #{serviceHandlerMethodName}
		  	)
	</insert>
	
	<!-- 运行后的添加程序 -->
	<insert id="insertOneMQMessageContainError" parameterType="MQMessage">
		INSERT INTO 
		 	TB_IAMBERRY_MQ_SERVICE
		 		(
			 		SERVICE_TO_MESSAGE, SERVICE_IS_SEND, SERVICE_TYPE, SERVICE_STATUS, SERVICE_OTHER_MESSAGE,
					SERVICE_CREATE_DATE, SERVICE_HANDLER_OBJECT_NAME, SERVICE_HANDLER_METHOD_NAME, SERVICE_ERROR_MESSAGE
				)
		 VALUES 
		  	( 
		  	  #{serviceToMessage},
			  #{serviceIsSend},  
			  #{serviceType}, 
			  #{serviceStatus},
			  #{serviceOtherMessage},
			  NOW(),
			  #{serviceHandlerObjectName},
			  #{serviceHandlerMethodName},
			  #{serviceErrorMessage}
		  	)
	</insert>
</mapper>