<?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.fs.sop.mapper.QwSopLogsMapper">

    <resultMap type="QwSopLogs" id="QwSopLogsResult">
        <result property="id"    column="id"    />
        <result property="qwUserid"    column="qw_userid"    />
        <result property="externalUserId"    column="external_user_id"    />
        <result property="externalUserName"    column="external_user_name"    />
        <result property="logType"    column="log_type"    />
        <result property="contentJson"    column="content_json"    />
        <result property="sendStatus"    column="send_status"    />
        <result property="sendTime"    column="send_time"   jdbcType="VARCHAR"   />
        <result property="realSendTime"    column="real_send_time" jdbcType="VARCHAR"    />
        <result property="companyId"    column="company_id"    />
        <result property="receivingStatus"    column="receiving_status"    />
        <result property="msgId"    column="msg_id"    />
        <result property="sendType"    column="send_type"    />
        <result property="sopId"    column="sop_id"    />
        <result property="remark"    column="remark"    />
        <result property="corpId"    column="corp_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="fsUserId"    column="fs_user_id"    />
        <result property="takeRecords"    column="take_records"    />
        <result property="sort"    column="sort"    />
    </resultMap>

    <sql id="selectQwSopLogsVo">
        select id,sop_id,customer_id, qw_userid,corp_id,external_user_id,external_user_name, log_type, content_json, send_status,receiving_status,msg_id, send_time, real_send_time, company_id, send_type,remark,fs_user_id,take_records from qw_sop_logs
    </sql>

    <select id="selectQwSopLogsListVO" parameterType="QwSopLogs" resultMap="QwSopLogsResult">
        <include refid="selectQwSopLogsVo"/>
        <where>
            <if test="qwUserid != null "> and qw_userid = #{qwUserid}</if>
            <if test="externalUserId != null  and externalUserId != ''"> and external_user_id = #{externalUserId}</if>
            <if test="sendStatus != null "> and send_status = #{sendStatus}</if>
            <if test="corpId != null "> and corp_id = #{corpId}</if>
        </where>
        order by send_time desc
    </select>

    <select id="selectQwSopLogsList" parameterType="QwSopLogs" resultMap="QwSopLogsResult">
        <include refid="selectQwSopLogsVo"/>
        <where>
            <if test="qwUserid != null "> and qw_userid = #{qwUserid}</if>
            <if test="externalUserId != null  and externalUserId != ''"> and external_user_id = #{externalUserId}</if>
            <if test="externalUserName != null  and externalUserName != ''"> and external_user_name = #{externalUserName}</if>
            <if test="logType != null  and logType != ''"> and log_type = #{logType}</if>
            <if test="contentJson != null  and contentJson != ''"> and content_json = #{contentJson}</if>
            <if test="sendStatus != null "> and send_status = #{sendStatus}</if>
            <if test="sendTime != null "> and send_time = #{sendTime}</if>
            <if test="realSendTime != null "> and real_send_time = #{realSendTime}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="receivingStatus != null "> and receiving_status = #{receivingStatus}</if>
            <if test="msgId != null "> and msg_id = #{msgId}</if>
            <if test="corpId != null "> and corp_id = #{corpId}</if>
            <if test="sendType != null "> and send_type = #{sendType}</if>
            <if test="sopId != null "> and sop_id = #{sopId}</if>
            <if test="remark != null "> and remark = #{remark}</if>
            <if test="customerId != null "> and customer_id = #{customerId}</if>
            <if test="fsUserId != null "> and fs_user_id = #{fsUserId}</if>
            <if test="takeRecords != null "> and take_records = #{takeRecords}</if>
        </where>
    </select>

    <select id="selectQwSopLogsById" parameterType="String" resultMap="QwSopLogsResult">
        <include refid="selectQwSopLogsVo"/>
        where id = #{id} order by send_time
    </select>

    <insert id="insertQwSopLogs" parameterType="QwSopLogs" useGeneratedKeys="false">
        insert into qw_sop_logs
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="data.qwUserid != null">qw_userid,</if>
            <if test="data.externalUserId != null">external_user_id,</if>
            <if test="data.externalUserName != null">external_user_name,</if>
            <if test="data.logType != null">log_type,</if>
            <if test="data.contentJson != null">content_json,</if>
            <if test="data.sendStatus != null">send_status,</if>
            <if test="data.sendTime != null">send_time,</if>
            <if test="data.realSendTime != null">real_send_time,</if>
            <if test="data.sendType != null ">send_type,</if>
            <if test="data.companyId != null">company_id,</if>
            <if test="data.receivingStatus != null ">receiving_status,</if>
            <if test="data.msgId != null ">msg_id,</if>
            <if test="data.sopId != null ">sop_id,</if>
            <if test="data.remark != null ">remark,</if>
            <if test="data.corpId != null">corp_id,</if>
            <if test="data.customerId != null">customer_id,</if>
            <if test="data.fsUserId != null">fs_user_id,</if>
            <if test="data.takeRecords != null">take_records,</if>
            <if test="data.sort != null">sort,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="data.qwUserid != null">#{data.qwUserid},</if>
            <if test="data.externalUserId != null">#{data.externalUserId},</if>
            <if test="data.externalUserName != null">#{data.externalUserName},</if>
            <if test="data.logType != null">#{data.logType},</if>
            <if test="data.contentJson != null">#{data.contentJson},</if>
            <if test="data.sendStatus != null">#{data.sendStatus},</if>
            <if test="data.sendTime != null">#{data.sendTime},</if>
            <if test="data.realSendTime != null">#{data.realSendTime},</if>
            <if test="data.sendType != null ">#{data.sendType},</if>
            <if test="data.companyId != null">#{data.companyId},</if>
            <if test="data.receivingStatus != null "> #{data.receivingStatus},</if>
            <if test="data.msgId != null ">#{data.msgId},</if>
            <if test="data.sopId != null ">#{data.sopId},</if>
            <if test="data.remark != null ">#{data.remark},</if>
            <if test="data.corpId != null">#{data.corpId},</if>
            <if test="data.customerId != null">#{data.customerId},</if>
            <if test="data.fsUserId != null">#{data.fsUserId},</if>
            <if test="data.takeRecords != null">#{data.takeRecords},</if>
            <if test="data.sort != null">#{data.sort},</if>
        </trim>
    </insert>

    <update id="updateQwSopLogs" parameterType="QwSopLogs" >
        update qw_sop_logs
        <trim prefix="SET" suffixOverrides=",">
            <if test="data.externalUserName != null">external_user_name = #{data.externalUserName},</if>
            <if test="data.logType != null">log_type = #{data.logType},</if>
            <if test="data.contentJson != null">content_json = #{data.contentJson},</if>
            <if test="data.sendStatus != null">send_status = #{data.sendStatus},</if>
            <if test="data.sendTime != null">send_time = #{data.sendTime},</if>
            <if test="data.realSendTime != null">real_send_time = #{data.realSendTime},</if>
            <if test="data.companyId != null">company_id = #{data.companyId},</if>
            <if test="data.receivingStatus != null ">receiving_status = #{data.receivingStatus},</if>
            <if test="data.msgId != null ">msg_id = #{data.msgId},</if>
            <if test="data.sendType != null ">send_type = #{data.sendType},</if>
            <if test="data.sopId != null ">sop_id = #{data.sopId},</if>
            <if test="data.customerId != null ">customer_id = #{data.customerId},</if>
            <if test="data.remark != null ">remark = #{data.remark},</if>
            <if test="data.fsUserId != null ">fs_user_id = #{data.fsUserId},</if>
            <if test="data.sort != null ">sort = #{data.sort},</if>
        </trim>
        where id = #{data.id}
    </update>

    <select id="selectQwSopLogsDoSendList" resultType="com.fs.sop.vo.QwSopLogsDoSendListTVO">
   <![CDATA[
        select ql.id as id,
               ql.qw_userid,
               ql.external_user_id,
               ql.external_user_name,
               qs.name,
               ql.content_json,
               ql.send_time,
               ql.fs_user_id,
               qs.expiry_time as expiryTime
        from qw_sop_logs ql
                 INNER join qw_sop qs on qs.id = ql.sop_id
        where ql.qw_userid=#{qwUserId} and ql.corp_id=#{corpId}
          and ql.log_type = 2
          and ql.send_type = 2
          and ql.send_status = 3
          and ql.send_time <= now()
        order by ql.send_time desc
            limit 5
        ]]>
    </select>

    <select id="selectQwSopLogsListByQwSopId"  parameterType="com.fs.sop.params.QwSopLogsParam" resultMap="QwSopLogsResult" >
        SELECT * FROM qw_sop_logs
        <where>
            <if test="map.sopId != null">sop_id = #{map.sopId}</if>
            <if test="map.corpId != null">AND corp_id = #{map.corpId}</if>
            <if test="map.sendType != null">AND send_type = #{map.sendType}</if>
            <if test="map.sendStatus != null">AND send_status = #{map.sendStatus}</if>
            <if test="map.receivingStatus != null">AND receiving_status = #{map.receivingStatus}</if>
            <if test="map.qwUserid != null and map.qwUserid !='' ">AND qw_userid = #{map.qwUserid}</if>
            <if test="map.externalUserName != null and map.externalUserName!= '' ">AND external_user_name = #{map.externalUserName}</if>
            <if test="map.scheduleStartTime != null">AND send_time &gt;= #{map.scheduleStartTime}</if>
            <if test="map.scheduleEndTime != null">AND send_time &lt;=  #{map.scheduleEndTime}</if>
        </where>
        ORDER BY send_time desc

    </select>

    <update id="updateQwSopLogsByWatchLogType" >
        UPDATE qw_sop_logs
        SET
            send_status = 5,
            receiving_status = 4,
            remark = #{remark},
            real_send_time = NOW()
        WHERE
            id = #{id}
    </update>

    <update id="updateQwSopLogsBycollectIds" useGeneratedKeys="false" keyProperty="id" >
        UPDATE qw_sop_logs SET
        send_status = #{sendStatus},
        msg_id = #{msgId},
        real_send_time = NOW()
        WHERE id IN
        <foreach collection="collectIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <delete id="deleteQwSopLogsById" parameterType="String">
        delete from qw_sop_logs  where id = #{id}
    </delete>

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

    <update id="deleteQwSopLogsBySopID" parameterType="String">
        update qw_sop_logs
        SET send_status = 5,  receiving_status = 2 , remark='已删除' where sop_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateQwSopLogsBySendStatus">
        <![CDATA[
        update qw_sop_logs
        set
            send_status = 5,
            receiving_status = 4,
            remark = '已过期，不发送',
            real_send_time = NOW()
        WHERE
            send_status = '3'
          AND send_time <= NOW() - INTERVAL (
            SELECT expiry_time
            FROM qw_sop
            WHERE id IN (SELECT sop_id FROM qw_sop_logs WHERE send_status = '3')
            LIMIT 1
            ) HOUR

        ]]>
    </update>

    <select id="checkQwSopLogs" resultType="QwSopLogs" >
        <![CDATA[
        select * from qw_sop_logs where log_type=2 and send_status=3 and send_type=1 and send_time <= NOW()
        ]]>
    </select>


    <select id="selectSopLogsByCreateCorpMassSending" parameterType="String" resultType="QwSopLogs" >
        <![CDATA[
        select * from qw_sop_logs where log_type=2 and send_status=3 and send_type=1 and send_time >= #{date}
        ]]>
    </select>


    <select id="selectSopLogsByCreateCorpMassSendResult" resultType="QwSopLogs" >
        <![CDATA[
        SELECT
            *
        FROM
            qw_sop_logs
        WHERE
            log_type = 2
          AND send_type = 1
          AND send_status = 1
          AND receiving_status = 0
          AND msg_id IS NOT NULL
          AND DATE(send_time) = CURDATE();
        ]]>
    </select>

    <select id="qwSopLogsResult" resultType="QwSopLogs">
        <![CDATA[
         select * from qw_sop_logs
                  where  log_type=2
                    and send_type=1
                    and  send_status=1
                    and receiving_status=0
                    AND send_time BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW()
        ]]>
    </select>

    <select id="getQwSopLogsByJsApiAll" parameterType="com.fs.sop.params.GetQwSopLogsByJsApiParam" resultType="com.fs.sop.vo.QwSopLogsDoSendListTVO">
        <![CDATA[
        select ql.id as id,
               ql.qw_userid,
               ql.external_user_id,
               ql.external_user_name,
               qs.name,
               ql.content_json,
               ql.send_time,
               ql.fs_user_id,
               ql.send_type,
               qs.expiry_time as expiryTime
        from qw_sop_logs ql
                 left join qw_sop qs on qs.id = ql.sop_id
        where ql.qw_userid=#{data.qwUserId}
          and ql.corp_id=#{data.corpId}
          and ql.external_user_id =#{data.externalUserId}
          and ql.log_type = 2
          and ql.send_type != 1
          and ql.send_status = 3
          and ql.receiving_status = 0
          and ql.send_time <= now()
        order by ql.sort DESC ,ql.send_time asc
            LIMIT 3
        ]]>
    </select>

    <select id="getQwSopLogsByJsApi" parameterType="com.fs.sop.params.GetQwSopLogsByJsApiParam" resultType="com.fs.sop.vo.QwSopLogsDoSendListTVO">
        <![CDATA[
        select ql.id as id,
               ql.qw_userid,
               ql.external_user_id,
               ql.external_user_name,
               qs.name,
               ql.content_json,
               ql.send_time,
               ql.fs_user_id,
               qs.expiry_time as expiryTime
        from qw_sop_logs ql
                 left join qw_sop qs on qs.id = ql.sop_id
        where ql.qw_userid=#{data.qwUserId}
          and ql.corp_id=#{data.corpId}
          and ql.external_user_id =#{data.externalUserId}
          and ql.log_type = 2
          and ql.send_type in (2,5)
          and ql.send_status = 3
          and ql.receiving_status = 0
          and ql.send_time <= now()
        order by ql.sort DESC ,ql.send_time asc
        ]]>
    </select>

    <select id="getQwSopLogsByJsApiOver" parameterType="com.fs.sop.params.GetQwSopLogsByJsApiParam" resultType="com.fs.sop.vo.QwSopLogsDoSendListTVO">
        <![CDATA[
        select ql.id as id,
               ql.qw_userid,
               ql.external_user_id,
               ql.external_user_name,
               ql.content_json,
               ql.send_time,
               ql.fs_user_id
        from qw_sop_logs ql
        where ql.qw_userid=#{data.qwUserId}
          and ql.corp_id=#{data.corpId}
          and ql.external_user_id =#{data.externalUserId}
          and ql.log_type = 2
          and ql.send_type = 3
          and ql.send_status = 3
          and ql.receiving_status = 0
          and ql.send_time <= now()
        order by ql.sort DESC ,ql.send_time asc
        ]]>
    </select>

    <select id="getExpiredMessagesByQwSopLogs" resultType="com.fs.sop.vo.QwSopLogsDoSendListTVO">
        <![CDATA[
        SELECT
            ql.id
        FROM
            qw_sop_logs ql
                LEFT JOIN qw_sop qs ON qs.id = ql.sop_id
        WHERE
            ql.log_type = 2
          AND ql.send_type IN (2, 5)
          AND ql.send_status = 3
          AND ql.receiving_status = 0
          AND ql.send_time <= now()
          AND (qs.expiry_time IS NOT NULL AND ql.send_time <= now() - interval qs.expiry_time hour)
        ORDER BY
            ql.sort DESC,
            ql.send_time asc;
        ]]>
    </select>

    <select id="getQwSopLogsByJsApiNext" parameterType="com.fs.sop.params.GetQwSopLogsByJsApiParam" resultType="QwSopLogs">
        <![CDATA[
        SELECT  external_user_id,send_type
        FROM qw_sop_logs
        WHERE qw_userid = #{data.qwUserId}
          AND corp_id = #{data.corpId}
          AND external_user_id != #{data.externalUserId}
           AND log_type = 2
           AND send_status = 3
           AND send_type !=1
           AND receiving_status = 0
           AND send_time <= now()
        ORDER BY sort DESC,send_time ASC, rand()
            LIMIT 1
        ]]>
    </select>


    <delete id="deleteQwSopLogsByJsApi" parameterType="com.fs.sop.params.GetQwSopLogsByJsApiParam" >
        delete FROM qw_sop_logs
        where qw_userid = #{data.qwUserId}
          AND corp_id = #{data.corpId}
          AND external_user_id = #{data.externalUserId}
    </delete>

    <delete id="deleteQwSopLogsByRemoveSop" parameterType="String" >
        delete FROM qw_sop_logs
        where qw_userid = #{qwUserId}
          AND corp_id = #{corpId}
          AND sop_id = #{sopId}
    </delete>

    <delete id="deleteQwSopLogsByExternalUserIdList" parameterType="com.fs.sop.params.DeleteQwSopParam" >
        delete FROM qw_sop_logs
        where qw_userid = #{data.qwUserId}
          AND corp_id = #{data.corpId}
         AND external_user_id IN
        <foreach collection="data.externalUserIds" item="extId" open="(" separator="," close=")">
            #{extId}
        </foreach>
    </delete>


    <insert id="batchInsertQwSopLogsOneTouch" parameterType="java.util.List" useGeneratedKeys="false">
        INSERT INTO qw_sop_logs
        (
        qw_userid, external_user_id, external_user_name, log_type,
        content_json, send_status, send_time, real_send_time, send_type,
        company_id, receiving_status, msg_id, sop_id, remark,
        corp_id, customer_id, fs_user_id,sort
        )
        VALUES
        <foreach collection="qwSopLogs" item="log" separator=",">
            (
            #{log.qwUserid},
            #{log.externalUserId},
            #{log.externalUserName},
            #{log.logType},
            #{log.contentJson},
            #{log.sendStatus},
            #{log.sendTime},
            #{log.realSendTime},
            #{log.sendType},
            #{log.companyId},
            #{log.receivingStatus},
            #{log.msgId},
            #{log.sopId},
            #{log.remark},
            #{log.corpId},
            #{log.customerId},
            #{log.fsUserId},
            #{log.sort}
            )
        </foreach>
    </insert>
    <!-- 批量插入 QwSopLogs -->
    <insert id="batchInsertQwSopLogs" parameterType="java.util.List" useGeneratedKeys="false">
        INSERT INTO qw_sop_logs
        (
        qw_userid, external_user_id, external_user_name, log_type,
        content_json, send_status, send_time, real_send_time, send_type,
        company_id, receiving_status, msg_id, sop_id, remark,
        corp_id, customer_id, fs_user_id
        )
        VALUES
        <foreach collection="qwSopLogs" item="log" separator=",">
            (
            #{log.qwUserid},
            #{log.externalUserId},
            #{log.externalUserName},
            #{log.logType},
            #{log.contentJson},
            #{log.sendStatus},
            #{log.sendTime},
            #{log.realSendTime},
            #{log.sendType},
            #{log.companyId},
            #{log.receivingStatus},
            #{log.msgId},
            #{log.sopId},
            #{log.remark},
            #{log.corpId},
            #{log.customerId},
            #{log.fsUserId}
            )
        </foreach>
    </insert>

    <!-- 批量更新 QwSopLogs -->
    <update id="batchUpdateQwSopLogsByTakeRecordsList" parameterType="java.util.List" useGeneratedKeys="false">
        UPDATE qw_sop_logs
        SET take_records = 1
        WHERE id IN
        <foreach collection="data" item="log" open="(" separator="," close=")">
            #{log.id}
        </foreach>
    </update>



    <update id="updateQwSopLogsByDeleted"  parameterType="com.fs.sop.params.GetQwSopLogsByJsApiParam"  useGeneratedKeys="false">
        <![CDATA[
        UPDATE qw_sop_logs
        SET
        send_status = 5,
        receiving_status = 4,
        remark = '非外部联系人',
        real_send_time = NOW()
        WHERE qw_userid = #{data.qwUserId}
        AND corp_id = #{data.corpId}
        AND external_user_id = #{data.externalUserId}
        AND send_time <= now()
        ]]>
    </update>

    <!-- 批量更新 QwSopLogs -->
    <update id="batchUpdateQwSopLogsBySendTime" parameterType="java.util.List" useGeneratedKeys="false">
        UPDATE qw_sop_logs
        SET
        send_status = 5,
        receiving_status = 4,
        remark = '已过期,不发送',
        real_send_time = NOW()
        WHERE id IN
        <foreach collection="data" item="log" open="(" separator="," close=")">
            #{log.id}
        </foreach>
    </update>


    <select id="selectQwSopLogsByCancel" resultMap="QwSopLogsResult">
        <include refid="selectQwSopLogsVo"/>
        <![CDATA[
        WHERE send_type = 3
        AND send_status = 5
        AND receiving_status = 4
        AND remark = '已过期,不发送'
        AND real_send_time >= now() - INTERVAL 1 HOUR
        AND real_send_time < now()
        ]]>
    </select>
    <select id="selectQwSopLogsListByChatSopId" resultType="com.fs.sop.vo.QwSopLogsListCVO">
        SELECT * FROM qw_sop_logs
        <where>
            <if test="map.sopId != null">sop_id = #{map.sopId}</if>
            <if test="map.corpId != null">AND corp_id = #{map.corpId}</if>
            <if test="map.sendType != null">AND send_type = #{map.sendType}</if>
            <if test="map.sendStatus != null">AND send_status = #{map.sendStatus}</if>
            <if test="map.receivingStatus != null">AND receiving_status = #{map.receivingStatus}</if>
            <if test="map.qwUserid != null and map.qwUserid !='' ">AND qw_userid = #{map.qwUserid}</if>
            <if test="map.externalUserName != null and map.externalUserName!= '' ">AND external_user_name = #{map.externalUserName}</if>
            <if test="map.scheduleStartTime != null">AND send_time &gt;= #{map.scheduleStartTime}</if>
            <if test="map.scheduleEndTime != null">AND send_time &lt;=  #{map.scheduleEndTime}</if>
        </where>
        ORDER BY send_time desc
    </select>


    <!-- 批量更新 QwSopLogs -->
    <update id="batchUpdateQwSopLogsByCancel" parameterType="java.util.List" useGeneratedKeys="false">
        UPDATE  qw_sop_logs
        SET
        send_status = 3,
        receiving_status = 0,
        remark = null,
        real_send_time = null,
        send_time = DATE_ADD(now(), INTERVAL 30 MINUTE)
        WHERE id IN
        <foreach collection="data" item="log" open="(" separator="," close=")">
            #{log.id}
        </foreach>
    </update>


    <delete id="deleteQwSopLogsByDate" >
        <![CDATA[
            DELETE FROM qw_sop_logs WHERE DATE(send_time) < CURDATE() - INTERVAL 2 DAY;
        ]]>
    </delete>

</mapper>
