<?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.iformall.mapper.WxMsgRecordMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.msg.WxMsgRecord">
		<id column="id" property="id" />
		<result column="tenant_id" property="tenantId" />
		<result column="domain" property="domain" />
		<result column="msg_type" property="msgType" />
		<result column="msg" property="msg" />
		<result column="send_time" property="sendTime" />
		<result column="model_id" property="modelId" />
		<result column="sender" property="sender" />
		<result column="sender_user_id" property="senderUserId" />
		<result column="sender_user_name" property="senderUserName" />
		<result column="receiver" property="receiver" />
		<result column="receiver_user_id" property="receiverUserId" />
		<result column="receiver_user_name" property="receiverUserName" />
		<result column="msg_status" property="msgStatus" />
		<result column="status_message" property="statusMessage" />
		<result column="createtime" property="createtime" />
		<result column="updatetime" property="updatetime" />
		<result column="uuid" property="uuid" />
		<result column="signature" property="signature" />
		<result column="model_type" property="modelType" />
		<result column="msg_json" property="msgJson" />


	</resultMap>

	<sql id="allColumns">
		id,tenant_id,`domain`,msg_type,msg,send_time,model_id,sender,sender_user_id,sender_user_name,receiver,receiver_user_id,
		receiver_user_name,msg_status,status_message,createtime,updatetime,uuid,signature,model_type,msg_json
	</sql>


	<sql id="dynamicWhereConditions">
	        where 1 = 1
		<if test=" null != id ">
		and `id` = #{id} 
		</if>
        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>
		<if test=" null != receiverUserId ">
			and `receiver_user_id` = #{receiverUserId}
		</if>
		<if test=" null != receiver ">
			and `receiver` = #{receiver}
		</if>
		<if test=" null != msgStatus ">
			and `msg_status` = #{msgStatus}
		</if>
		<if test=" null != domain ">
			and `domain` = #{domain}
		</if>
        <if test=" null != msgType ">
            and `msg_type` = #{msgType}
        </if>
		order by createtime desc
	</sql>

	<update id="updateByBaseMsg" parameterType="com.iformall.domain.po.msg.BaseMsg">
		update wx_msg_record set updatetime = now()
		<if test=" null != msgStatus ">,msg_status = #{msgStatus}</if>
		<if test=" null != statusMessage ">,status_message = #{statusMessage}</if>
		where id in(
			select t.id from (
				select id from wx_msg_record where uuid = #{uuid}
			) t
		)
	</update>

	<select id="findList" parameterType="com.iformall.domain.po.msg.WxMsgRecord" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_msg_record
		<include refid="dynamicWhereConditions" />
	</select>

	<insert id="save" parameterType="com.iformall.domain.po.msg.WxMsgRecord" >
		INSERT INTO wx_msg_record( id,tenant_id,`uuid`, `msg_type`,
		`domain`, `msg`, `send_time`, `model_id`, `model_type`,
		`signature`, `sender`, `sender_user_id`, `sender_user_name`,
		`receiver`, `receiver_user_id`, `receiver_user_name`,
		`msg_status`, `status_message`, `msg_json`)
		VALUES(#{id},#{tenantId},#{uuid},#{msgType},
		#{domain},#{msg},#{sendTime},#{modelId},#{modelType},#{signature},#{sender},
		#{senderUserId},#{senderUserName},#{receiver},#{receiverUserId},#{receiverUserName},
		#{msgStatus},#{statusMessage},#{msgJson});
	</insert>


</mapper>
