<?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.SopUserLogsInfoMapper">

    <!-- 返回结果映射 -->
    <resultMap id="SopUserLogsInfoResult" type="com.fs.sop.domain.SopUserLogsInfo">
        <id property="id" column="id"/>
        <result property="sopId" column="sop_id"/>
        <result property="userLogsId" column="user_logs_id"/>
        <result property="externalContactId" column="external_contact_id"  jdbcType="VARCHAR"/>
        <result property="qwUserId" column="qw_user_id"  jdbcType="VARCHAR"/>
        <result property="corpId" column="corp_id"  jdbcType="VARCHAR"/>
        <result property="externalId" column="external_id" />
        <result property="fsUserId" column="fs_user_id"/>
        <result property="externalUserName" column="external_user_name"  jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="VARCHAR" />
        <result property="crtTime" column="crt_Time"  />
        <result property="updateTime" column="update_time" jdbcType="VARCHAR" />
    </resultMap>

    <sql id="selectSopUserLogsInfoVo">
        select id, sop_id, user_logs_id, external_contact_id,qw_user_id,corp_id,external_id, fs_user_id, external_user_name,create_time,crt_Time,update_time
        from sop_user_logs_info
    </sql>

    <select id="selectSopUserLogsInfoById" parameterType="String" resultMap="SopUserLogsInfoResult">
        <include refid="selectSopUserLogsInfoVo"/>
        where id = #{id}
    </select>

    <select id="selectSopUserLogsInfoByIdsHasUserId" parameterType="com.fs.sop.params.SopUserLogsInfoByIdsHasUserIdParam" resultMap="SopUserLogsInfoResult">
        <include refid="selectSopUserLogsInfoVo"/>
        <where>
            id IN
            <foreach collection="ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
            <if test="isRegister == 1">and fs_user_id is not null and fs_user_id != 0 </if>
        </where>
        order by crt_Time desc
    </select>


    <select id="selectSopUserLogsInfoByIds" parameterType="java.util.List" resultMap="SopUserLogsInfoResult">
        <include refid="selectSopUserLogsInfoVo"/>
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <!-- 根据ID更新记录 -->
    <update id="updateSopUserLogsInfoToTime" parameterType="com.fs.sop.domain.SopUserLogsInfo">
        UPDATE  sop_user_logs_info
        SET user_logs_id=#{data.userLogsId},create_time= Date(#{data.createTime}),update_time=#{data.updateTime}
        WHERE id = #{data.id}
    </update>
    <update id="updateSopUserLogsInfoFsUserIdById" parameterType="java.util.List" useGeneratedKeys="false">
        UPDATE  sop_user_logs_info
        SET
        fs_user_id = #{userId}
        WHERE id IN
        <foreach collection="data" item="log" open="(" separator="," close=")">
            #{log.id}
        </foreach>
    </update>

    <update id="insertSopUserLogsInfo" parameterType="com.fs.sop.domain.SopUserLogsInfo" useGeneratedKeys="false">
        INSERT INTO sop_user_logs_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sopId != null">sop_id,</if>
            <if test="userLogsId != null">user_logs_id,</if>
            <if test="externalContactId != null">external_contact_id,</if>
            <if test="externalId != null">external_id,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="fsUserId != null">fs_user_id,</if>
            <if test="externalUserName != null">external_user_name,</if>
            <if test="crtTime != null">crt_Time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="sopId != null">#{sopId},</if>
            <if test="userLogsId != null">#{userLogsId},</if>
            <if test="externalContactId != null">#{externalContactId},</if>
            <if test="externalId != null">#{externalId},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="fsUserId != null">#{fsUserId},</if>
            <if test="externalUserName != null">#{externalUserName},</if>
            <if test="crtTime != null">#{crtTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </update>


    <!-- 根据ID更新记录 -->
    <update id="updateById" parameterType="com.fs.sop.domain.SopUserLogsInfo">
        UPDATE  sop_user_logs_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="sopId != null"> sop_id = #{sopId},</if>
            <if test="userLogsId != null"> user_logs_id = #{userLogsId},</if>
            <if test="externalContactId != null"> external_contact_id = #{externalContactId},</if>
            <if test="qwUserId != null"> qw_user_id = #{qwUserId},</if>
            <if test="corpId != null"> corp_id = #{corpId},</if>
            <if test="externalId != null"> external_id = #{externalId},</if>
            <if test="fsUserId != null"> fs_user_id = #{fsUserId},</if>
            <if test="externalUserName != null"> external_user_name = #{externalUserName},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <!-- 根据ID删除记录 -->
    <delete id="deleteById" parameterType="String">
        DELETE FROM sop_user_logs_info  WHERE id = #{id}
    </delete>

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

    <delete id="deleteByQwUserIdAndCorpIdToContactId" parameterType="String">
        DELETE FROM sop_user_logs_info  WHERE qw_user_id = #{qwUserId} and corp_id = #{corpId} and external_contact_id = #{externalContactId}
    </delete>

    <delete id="deleteByQwUserIdAndCorpIdToContactIdByChat" parameterType="String">
        DELETE FROM sop_user_logs_info_chat  WHERE qw_user_id = #{qwUserId} and corp_id = #{corpId} and external_contact_id = #{externalContactId}
    </delete>


    <delete id="deleteByQwUserIdAndCorpIdToContactIdList" parameterType="com.fs.sop.params.DeleteQwSopParam">
        DELETE FROM sop_user_logs_info
            WHERE qw_user_id = #{data.qwUserId}
            and corp_id = #{data.corpId}
            and external_contact_id IN
        <foreach collection="data.externalUserIds" item="extId" open="(" separator="," close=")">
            #{extId}
        </foreach>
    </delete>

    <delete id="deleteByQwUserIdAndCorpIdToContactIdByChatList" parameterType="com.fs.sop.params.DeleteQwSopParam">
        DELETE FROM sop_user_logs_info_chat
            WHERE qw_user_id = #{data.qwUserId}
            and corp_id = #{data.corpId}
            and external_contact_id IN
        <foreach collection="data.externalUserIds" item="extId" open="(" separator="," close=")">
            #{extId}
        </foreach>
    </delete>



    <delete id="deleteSopUserLogsInfoBySopIds" parameterType="map">
        DELETE FROM sop_user_logs_info  WHERE sop_id IN
        <foreach item="sopId" index="index" collection="sopIds"
                 open="(" separator="," close=")">
            #{sopId}
        </foreach>
    </delete>

    <delete id="deleteBySopIdToContactId" parameterType="com.fs.sop.params.SopUserLogsInfoDelParam">
        DELETE FROM sop_user_logs_info
            WHERE sop_id = #{data.sopId}
            and qw_user_id = #{data.qwUserId}
            and corp_id = #{data.corpId}
            and external_id = #{data.externalId}
    </delete>

    <delete id="deleteBySopIdToContactIdChat" parameterType="com.fs.sop.params.SopUserLogsInfoDelParam">
        DELETE FROM sop_user_logs_info_chat
            WHERE sop_id = #{data.sopId}
            and qw_user_id = #{data.qwUserId}
            and corp_id = #{data.corpId}
            and external_id = #{data.externalId}
    </delete>


    <!-- 根据ID查询单条记录 -->
    <select id="selectById" parameterType="String" resultMap="SopUserLogsInfoResult">
        SELECT id, sop_id, user_logs_id, external_contact_id,qw_user_id,corp_id,external_id, fs_user_id, external_user_name,create_time,crt_Time,update_time
        FROM sop_user_logs_info
        WHERE id = #{id}
    </select>


    <select id="selectSopUserLogsInfoList" parameterType="com.fs.sop.domain.SopUserLogsInfo" resultMap="SopUserLogsInfoResult">
        <include refid="selectSopUserLogsInfoVo"/>
        <where>
            <if test="sopId != null">and sop_id = #{sopId}</if>
            <if test="userLogsId != null">and user_logs_id = #{userLogsId}</if>
            <if test="externalContactId != null">and external_contact_id = #{externalContactId}</if>
            <if test="qwUserId != null">and qw_user_id = #{qwUserId}</if>
            <if test="corpId != null">and corp_id = #{corpId}</if>
            <if test="externalId != null">and external_id = #{externalId}</if>
            <if test="fsUserId != null">and fs_user_id = #{fsUserId}</if>
            <if test="externalUserName != null">and external_user_name like CONCAT('%', #{externalUserName}, '%')</if>
            <if test="createTime != null">and create_time = #{createTime}</if>
            <if test="crtTime != null">and crt_Time = #{crtTime}</if>
            <if test="updateTime != null">and updateTime = #{updateTime}</if>
        </where>
        order by crt_Time desc
    </select>


    <select id="selectSopUserLogsInfoListByIsRegister" parameterType="com.fs.sop.params.SopUserLogsInfoParam" resultMap="SopUserLogsInfoResult">
        <include refid="selectSopUserLogsInfoVo"/>
        <where>
            <if test="data.sopId != null">and sop_id = #{data.sopId}</if>
            <if test="data.userLogsId != null">and user_logs_id = #{data.userLogsId}</if>
            <if test="data.isRegister == 1">and fs_user_id is not null and fs_user_id != 0 </if>
        </where>
        order by crt_Time desc
    </select>

    <select id="selectSopUserLogsInfo" parameterType="com.fs.sop.domain.SopUserLogsInfo" resultMap="SopUserLogsInfoResult">
        <include refid="selectSopUserLogsInfoVo"/>
        <where>
            <if test="sopId != null">and sop_id = #{sopId}</if>
            <if test="externalContactId != null">and external_contact_id = #{externalContactId}</if>
            <if test="qwUserId != null">and qw_user_id = #{qwUserId}</if>
            <if test="corpId != null">and corp_id = #{corpId}</if>
            <if test="externalId != null">and external_id = #{externalId}</if>
        </where>
        limit 1
    </select>

    <select id="selectSopUserLogsInfoByCheckJobTransfer" parameterType="com.fs.sop.domain.SopUserLogsInfo" resultMap="SopUserLogsInfoResult">
        select id,user_logs_id
        from sop_user_logs_info
        where sop_id = #{sopId} and qw_user_id=#{qwUserId} and corp_id=#{corpId} and external_contact_id=#{externalContactId} limit 1
    </select>

    <select id="selectSopUserLogsInfoListBySopId" parameterType="String" resultMap="SopUserLogsInfoResult">
        select
            id, sop_id, user_logs_id, external_contact_id,qw_user_id,corp_id,external_id,
            fs_user_id, external_user_name,create_time,crt_Time,update_time
        from sop_user_logs_info where sop_id = #{sopId} and user_logs_id=#{userLogsId}
    </select>

    <!-- 查询所有记录 -->
    <select id="selectAll" resultMap="SopUserLogsInfoResult">
        SELECT id, sop_id, user_logs_id, external_contact_id,qw_user_id,corp_id,external_id,
               fs_user_id, external_user_name,create_time,crt_Time,update_time
        FROM sop_user_logs_info
    </select>
    <select id="selectChatSopUserLogsInfo" resultType="com.fs.sop.domain.SopUserLogsInfo">
        select id, sop_id, user_logs_id, external_contact_id,qw_user_id,corp_id,external_id,
               fs_user_id, external_user_name,create_time,crt_Time,update_time
        from sop_user_logs_info_chat
        <where>
            <if test="sopId != null">and sop_id = #{sopId}</if>
            <if test="externalContactId != null">and external_contact_id = #{externalContactId}</if>
            <if test="qwUserId != null">and qw_user_id = #{qwUserId}</if>
            <if test="corpId != null">and corp_id = #{corpId}</if>
            <if test="externalId != null">and external_id = #{externalId}</if>
        </where>
        limit 1
    </select>

    <select id="selectChatSopUserLogsByQwUserId" resultMap="SopUserLogsInfoResult">
        SELECT
            id, sop_id, user_logs_id, external_contact_id,qw_user_id,corp_id,external_id,
            fs_user_id, external_user_name,create_time,crt_Time,update_time
            FROM sop_user_logs_info_chat
            where qw_user_id=#{qwUserId} and corp_id=#{corpId} AND crt_Time > NOW() - INTERVAL 24 HOUR;
    </select>

    <!-- 批量插入 SopUserLogsInfo -->
    <insert id="batchInsertSopUserLogsInfo" parameterType="java.util.List" useGeneratedKeys="false">
        INSERT INTO sop_user_logs_info
        (
        sop_id, user_logs_id, external_contact_id,qw_user_id,corp_id, external_id,
        fs_user_id, external_user_name
        )
        VALUES
        <foreach collection="SopUserLogsInfo" item="log" separator=",">
            (
            #{log.sopId},
            #{log.userLogsId},
            #{log.externalContactId},
            #{log.qwUserId},
            #{log.corpId},
            #{log.externalId},
            #{log.fsUserId},
            #{log.externalUserName}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
        update_time = Now()
    </insert>

    <update id="insertChatSopUserLogsInfo" parameterType="com.fs.sop.domain.SopUserLogsInfo" useGeneratedKeys="false">
        INSERT INTO sop_user_logs_info_chat
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sopId != null">sop_id,</if>
            <if test="userLogsId != null">user_logs_id,</if>
            <if test="externalContactId != null">external_contact_id,</if>
            <if test="externalId != null">external_id,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="fsUserId != null">fs_user_id,</if>
            <if test="externalUserName != null">external_user_name,</if>
            <if test="crtTime != null">crt_Time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="sopId != null">#{sopId},</if>
            <if test="userLogsId != null">#{userLogsId},</if>
            <if test="externalContactId != null">#{externalContactId},</if>
            <if test="externalId != null">#{externalId},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="fsUserId != null">#{fsUserId},</if>
            <if test="externalUserName != null">#{externalUserName},</if>
            <if test="crtTime != null">#{crtTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </update>


    <update id="batchUpdateSopUserLogsInfoToTime" parameterType="com.fs.sop.params.BatchSopUserLogsInfoParamU">
        UPDATE sop_user_logs_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="userLogsId != null">user_logs_id = #{userLogsId},</if>
            update_time = NOW()
        </trim>
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <select id="selectEerGroup"  resultMap="SopUserLogsInfoResult">
        select s.user_logs_id,ANY_VALUE(s.sop_id) sop_id,ANY_VALUE(s.qw_user_id) qw_user_id,ANY_VALUE(s.corp_id) corp_id  from sop_user_logs_info s LEFT JOIN sop_user_logs ss ON s.user_logs_id=ss.id where create_time='2025-02-11' and ss.id is null GROUP BY s.user_logs_id
    </select>

    <select id="selectSopUserLogsInfoListByUserLogsId"  resultMap="SopUserLogsInfoResult">
        select * from sop_user_logs_info where user_logs_id=#{id}
    </select>
    <select id="repeatProject" resultType="com.fs.sop.domain.SopUserLogsInfo">
        select a.*,b.project from sop_user_logs_info a
        inner join qw_sop b on a.sop_id = b.id
        where b.project in <foreach collection="projects" open="(" separator="," close=")" item="item">#{item}</foreach>
        and a.external_contact_id = #{externalUserID}
    </select>


</mapper>
