<?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.ruoyi.jarvis.mapper.WeComInboundTraceMapper">

    <resultMap id="WeComInboundTraceResult" type="WeComInboundTrace">
        <id property="id" column="id"/>
        <result property="msgId" column="msg_id"/>
        <result property="agentId" column="agent_id"/>
        <result property="corpId" column="corp_id"/>
        <result property="fromUserName" column="from_user_name"/>
        <result property="content" column="content"/>
        <result property="wxMsgTime" column="wx_msg_time"/>
        <result property="replyContent" column="reply_content"/>
        <result property="sessionActive" column="session_active"/>
        <result property="sessionScene" column="session_scene"/>
        <result property="sessionStep" column="session_step"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectVo">
        select id, msg_id, agent_id, corp_id, from_user_name, content, wx_msg_time,
               reply_content, session_active, session_scene, session_step, create_time
        from wecom_inbound_trace
    </sql>

    <insert id="insertWeComInboundTrace" parameterType="WeComInboundTrace" useGeneratedKeys="true" keyProperty="id">
        insert into wecom_inbound_trace (
            msg_id, agent_id, corp_id, from_user_name, content, wx_msg_time,
            reply_content, session_active, session_scene, session_step
        ) values (
            #{msgId}, #{agentId}, #{corpId}, #{fromUserName}, #{content}, #{wxMsgTime},
            #{replyContent}, #{sessionActive}, #{sessionScene}, #{sessionStep}
        )
    </insert>

    <select id="selectWeComInboundTraceById" resultMap="WeComInboundTraceResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectWeComInboundTraceList" parameterType="WeComInboundTrace" resultMap="WeComInboundTraceResult">
        <include refid="selectVo"/>
        <where>
            <if test="msgId != null and msgId != ''">and msg_id = #{msgId}</if>
            <if test="fromUserName != null and fromUserName != ''">and from_user_name like concat('%', #{fromUserName}, '%')</if>
            <if test="agentId != null and agentId != ''">and agent_id = #{agentId}</if>
            <if test="sessionActive != null">and session_active = #{sessionActive}</if>
            <if test="params.beginTime != null and params.beginTime != ''">
                and create_time &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and create_time &lt;= concat(#{params.endTime}, ' 23:59:59')
            </if>
        </where>
        order by id desc
    </select>

    <delete id="deleteWeComInboundTraceByIds" parameterType="Long">
        delete from wecom_inbound_trace where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteAllWeComInboundTrace">
        delete from wecom_inbound_trace
    </delete>

    <select id="selectTracesShareLinkRemarkDone" resultMap="WeComInboundTraceResult">
        <include refid="selectVo"/>
        where reply_content like concat('%', #{replyMark}, '%')
        order by id asc
    </select>

    <select id="selectLatestPriorTraceWith3cnLink" resultMap="WeComInboundTraceResult">
        <include refid="selectVo"/>
        where from_user_name = #{fromUserName}
          and id &lt; #{beforeId}
          and (content like '%https://3.cn/%' or content like '%http://3.cn/%')
        order by id desc
        limit 1
    </select>
</mapper>
