<?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.house365.ws.dao.mapper.YxMessageMapper">

    <resultMap id="infoMap" type="com.house365.beans.entity.YxMessageEntity">
        <result property="id" column="id"/>
        <result property="eventType" column="eventType"/>
        <result property="convType" column="convType"/>
        <result property="info" column="info"/>
        <result property="fromAccount" column="fromAccount"/>
        <result property="fromClientType" column="fromClientType"/>
        <result property="fromDeviceId" column="fromDeviceId"/>
        <result property="fromNick" column="fromNick"/>
        <result property="msgTimestamp" column="msgTimestamp"/>
        <result property="msgType" column="msgType"/>
        <result property="body" column="body"/>
        <result property="attach" column="attach"/>
        <result property="msgidClient" column="msgidClient"/>
        <result property="msgidServer" column="msgidServer"/>

        <result property="resendFlag" column="resendFlag"/>
        <result property="customSafeFlag" column="customSafeFlag"/>
        <result property="customApnsText" column="customApnsText"/>
        <result property="tMembers" column="tMembers"/>
        <result property="ext" column="ext"/>
        <result property="antispam" column="antispam"/>
        <result property="yidunRes" column="yidunRes"/>
        <result property="relateId" column="relate_id"/>
        <result property="createTime" column="create_time"/>
    </resultMap>


    <insert id="save" parameterType="com.house365.beans.entity.YxMessageEntity">
        INSERT INTO yx_message (
            eventType,
            convType,
            info,
            fromAccount,
            fromClientType,
            fromDeviceId,
            fromNick,
            msgTimestamp,
            msgType,
            body,
            attach,
            msgidClient,
            msgidServer,
            resendFlag,
            customSafeFlag,
            customApnsText,
            tMembers,
            ext,
            antispam,
            yidunRes,
            relate_id,
            create_time
        )
        VALUES (
            #{eventType},
            #{convType},
            #{info},
            #{fromAccount},
            #{fromClientType},
            #{fromDeviceId},
            #{fromNick},
            #{msgTimestamp},
            #{msgType},
            #{body},
            #{attach},
            #{msgidClient},
            #{msgidServer},
            #{resendFlag},
            #{customSafeFlag},
            #{customApnsText},
            #{tMembers},
            #{ext},
            #{antispam},
            #{yidunRes},
            #{relateId},
            #{createTime}
        )
    </insert>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT *
        FROM yx_message
        where convType = 'PERSON'
        AND create_time BETWEEN #{time} AND (SELECT SUBDATE(#{time},INTERVAL -2 MINUTE))
        <if test="fromAccount != null and fromAccount != ''">
            AND fromAccount = #{fromAccount}
        </if>
        <if test="info != null and info != ''">
            AND info = #{info}
        </if>
    </select>

    <select id="queryPersonList" resultMap="infoMap" parameterType="map">
        SELECT * FROM yx_message
        WHERE convType = 'PERSON'
        AND fromAccount LIKE 'tf_%'
        AND create_time BETWEEN (SELECT SUBDATE(NOW(),INTERVAL 4 MINUTE)) AND (SELECT SUBDATE(NOW(),INTERVAL 2 MINUTE))
    </select>

    <delete id="deleteMessage" parameterType="map">
        delete FROM yx_message
        where #{time} > create_time
    </delete>

    <select id="queryWxList" resultMap="infoMap" parameterType="map">
        SELECT * FROM yx_message
        WHERE convType = 'PERSON'
        AND fromAccount LIKE 'tf_%'
        AND create_time BETWEEN (SELECT SUBDATE(NOW(),INTERVAL 30 SECOND)) AND (SELECT SUBDATE(NOW(),INTERVAL 15 SECOND))
    </select>

    <select id="queryByWxConditions" resultMap="infoMap" parameterType="map">
        SELECT *
        FROM yx_messages
        where convType = 'PERSON'
        AND create_time BETWEEN #{time} AND (SELECT SUBDATE(#{time},INTERVAL -30 SECOND))
        <if test="fromAccount != null and fromAccount != ''">
            AND fromAccount = #{fromAccount}
        </if>
        <if test="info != null and info != ''">
            AND info = #{info}
        </if>
    </select>

</mapper>