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

    <resultMap type="SopUserLogs" id="SopUserLogsResult">
        <result property="id"    column="id"    />
        <result property="sopId"    column="sop_id"    />
        <result property="sopTempId"    column="sop_temp_id"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="corpId"    column="corp_id"    />
        <result property="startTime"    column="start_time"  jdbcType="TIMESTAMP"   />
        <result property="status"    column="status"    />
        <result property="userId"    column="user_id"    />
        <result property="countDays" column="count_days" />
    </resultMap>

    <sql id="selectSopUserLogsVo">
        SELECT
            id,
            sop_id,
            sop_temp_id,
            qw_user_id,
            corp_id,
            start_time,
            status,
            user_id,
            DATEDIFF(CURRENT_DATE, start_time) + 1 AS count_days
        FROM sop_user_logs
    </sql>

    <select id="selectSopUserLogsList" parameterType="com.fs.sop.domain.SopUserLogs" resultMap="SopUserLogsResult">
        <include refid="selectSopUserLogsVo"/>
        <where>
            <if test="id != null "> and id = #{id}</if>
            <if test="sopId != null "> and sop_id = #{sopId}</if>
            <if test="sopTempId != null "> and sop_temp_id = #{sopTempId}</if>
            <if test="qwUserId != null "> and qw_user_id = #{qwUserId}</if>
            <if test="corpId != null "> and corp_id = #{corpId}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
        order by start_time desc
    </select>

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

    <insert id="insertSopUserLogsByDate" parameterType="com.fs.sop.params.SopUserLogsParamByDate" useGeneratedKeys="false" >
        insert into sop_user_logs
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="data.id != null  ">id,</if>
            <if test="data.sopId != null  ">sop_id,</if>
            <if test="data.sopTempId != null  ">sop_temp_id,</if>
            <if test="data.qwUserId != null  ">qw_user_id,</if>
            <if test="data.corpId != null  ">corp_id,</if>
            <if test="data.startTime != null  ">start_time,</if>
            <if test="data.status != null  ">status,</if>
            <if test="data.userId != null  ">user_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="data.id != null  ">#{data.id},</if>
            <if test="data.sopId != null  ">#{data.sopId},</if>
            <if test="data.sopTempId != null  ">#{data.sopTempId},</if>
            <if test="data.qwUserId != null  ">#{data.qwUserId},</if>
            <if test="data.corpId != null  ">#{data.corpId},</if>
            <if test="data.startTime != null  ">#{data.startTime},</if>
            <if test="data.status != null  ">#{data.status},</if>
            <if test="data.userId != null  ">#{data.userId},</if>
        </trim>
    </insert>

    <select id="selectSopUserLogsById" parameterType="String" resultMap="SopUserLogsResult">
        <include refid="selectSopUserLogsVo"/>
        where id = #{id}
    </select>

    <select id="selectSopUserLogsBySopId" parameterType="String" resultMap="SopUserLogsResult">
        <include refid="selectSopUserLogsVo"/>
        where sop_id = #{sopId}
    </select>

    <insert id="insertSopUserLogsByList" parameterType="com.fs.sop.params.SopUserLogsList" useGeneratedKeys="false" >
        insert into sop_user_logs
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="data.id != null  ">id,</if>
            <if test="data.sopId != null  ">sop_id,</if>
            <if test="data.sopTempId != null  ">sop_temp_id,</if>
            <if test="data.qwUserId != null  ">qw_user_id,</if>
            <if test="data.corpId != null  ">corp_id,</if>
            <if test="data.startTime != null  ">start_time,</if>
            <if test="data.status != null  ">status,</if>
            <if test="data.userId != null  ">user_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="data.id != null  ">#{data.id},</if>
            <if test="data.sopId != null  ">#{data.sopId},</if>
            <if test="data.sopTempId != null  ">#{data.sopTempId},</if>
            <if test="data.qwUserId != null  ">#{data.qwUserId},</if>
            <if test="data.corpId != null  ">#{data.corpId},</if>
            <if test="data.startTime != null  ">#{data.startTime},</if>
            <if test="data.status != null  ">#{data.status},</if>
            <if test="data.userId != null  ">#{data.userId},</if>
        </trim>
    </insert>
    <insert id="batchInsertSopUserLogs">
        INSERT INTO sop_user_logs
        (
        sop_id, sop_temp_id, qw_user_id,chat_id,corp_id, start_time,
        status, user_id
        )
        VALUES
        <foreach collection="list" item="log" separator=",">
            (
            #{log.sopId},
            #{log.sopTempId},
            #{log.qwUserId},
            #{log.chatId},
            #{log.corpId},
            #{log.startTime},
            #{log.status},
            #{log.userId}
            )
        </foreach>
    </insert>

    <update id="updateSopUserLogsByTempId" parameterType="String">
        update  sop_user_logs
        SET sop_temp_id = #{sopTempId} where sop_id = #{sopId}
    </update>


    <update id="deleteSopUserLogsBySopId" parameterType="String">
        update  sop_user_logs
        SET status = 0 where sop_id = #{sopId}
    </update>

    <update id="deleteSopUserLogsByRemoveSop" parameterType="String">
        update  sop_user_logs
        SET status = 0
        where sop_id = #{sopId} and qw_user_id=#{qwUserId} and corp_id=#{corpId}
    </update>

    <update id="deleteSopUserLogsBySopIds" parameterType="String">
        update  sop_user_logs
        SET status = 0 where sop_id in
        <foreach item="id" collection="sopIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <update id="updateSopUserLogsByStatus" parameterType="SopUserLogs" useGeneratedKeys="false" keyProperty="id" >
        UPDATE  sop_user_logs
        SET  status= #{data.status}
        WHERE sop_id = #{data.sopId}
    </update>

    <update id="updateSopUserLogsByStartTime" parameterType="SopUserLogs" useGeneratedKeys="false" keyProperty="id" >
        UPDATE  sop_user_logs
        SET  status= #{data.status},start_time = #{data.startTime}
        where id = #{data.id}
    </update>

    <update id="updateSopUserLogsByCompanyInfo" parameterType="SopUserLogs" useGeneratedKeys="false" keyProperty="id" >
        UPDATE  sop_user_logs
        SET  user_id= #{data.userId}
        where id = #{data.id}
    </update>

    <select id="repairSopUserLogsList" resultType="com.fs.sop.domain.SopUserLogs">
        select id,qw_user_id,corp_id,user_id from sop_user_logs  where status=1 and user_id like '%null%'
    </select>


    <select id="selectSopUserLogsListByTime" resultType="com.fs.sop.vo.SopUserLogsVo">
        select a.*,b.min_conversion_day,b.max_conversion_day,b.min_send,b.max_send,b.is_fixed,b.is_register,b.chat_id,b.filter_mode from sop_user_logs a
        inner join qw_sop b on a.sop_id = b.id
        where a.start_time &lt;= Now() and a.status = 1 and b.send_type != 4 and b.status in (2,3)
    </select>
<!--    <select id="selectSopUserLogsListByTime" resultType="com.fs.sop.vo.SopUserLogsVo">-->
<!--        select a.*,b.min_conversion_day,b.max_conversion_day,b.min_send,b.max_send from sop_user_logs a-->
<!--        inner join qw_sop b on a.sop_id = b.id-->
<!--        where b.id="23d77d16-2967-4760-940c-bdce4ca78b59"-->
<!--    </select>-->
<!--    <select id="selectSopUserLogsListByTime" resultType="com.fs.sop.vo.SopUserLogsVo">-->
<!--        select a.*,b.min_conversion_day,b.max_conversion_day,b.min_send,b.max_send from sop_user_logs a-->
<!--        inner join qw_sop b on a.sop_id = b.id-->
<!--        where a.id="2263eef1-f037-4db8-875a-b29d717929ef"-->
<!--    </select>-->


    <select id="selectSopUserLogsListByTest" resultType="com.fs.sop.vo.SopUserLogsVo">
        select a.*,b.min_conversion_day,b.max_conversion_day,b.min_send,b.max_send from sop_user_logs a
         inner join qw_sop b on a.sop_id = b.id
        where a.sop_id = '1d3aedf1-91f0-4cd2-85ef-47be5e8bcacb' and  a.start_time &lt;= Now() and a.status = 1 and b.send_type != 4
    </select>

    <select id="selectSopUserLogsByUnionSopId" resultType="String" parameterType="SopUserLogs">
        SELECT id
        FROM sop_user_logs
        WHERE sop_id = #{data.sopId}
          AND sop_temp_id = #{data.sopTempId}
          AND qw_user_id = #{data.qwUserId}
          AND corp_id = #{data.corpId}
          AND start_time = STR_TO_DATE(#{data.startTime}, '%Y-%m-%d')
          AND status = #{data.status}
            AND chat_id is null
            limit 1
    </select>



    <select id="selectSopUserLogsByUpdate" resultType="String" parameterType="SopUserLogs">
        SELECT id
        FROM sop_user_logs
        WHERE sop_id = #{data.sopId}
          AND qw_user_id = #{data.qwUserId}
          AND corp_id = #{data.corpId}
          AND start_time = STR_TO_DATE(#{data.startTime}, '%Y-%m-%d')
          AND status = #{data.status}
          AND chat_id is null limit 1
    </select>

    <select id="selectSopUserLogsInfoListByTime" resultType="com.fs.sop.vo.SopUserLogsInfoVo">
        select
        c.id sopId,
        b.id sopLogsId,
        a.id sopLogsInfoId,
        a.external_id externalId,
        a.create_time createTime,a.qw_user_id qwUserId,a.corp_id corpId,a.fs_user_id fsUserId,a.external_user_name externalUserName,
        c.min_conversion_day minConversionDay,c.max_conversion_day maxConversionDay,c.min_send minSend,c.max_send maxSend
        from sop_user_logs_info a
        inner join sop_user_logs b on a.user_logs_id = b.id
        inner join qw_sop c on b.sop_id = c.id
        where a.create_time &lt; CURDATE() and b.status = 1 and c.min_conversion_day is not null and c.max_conversion_day is not null and (c.min_send = 0 or c.max_send = 0)
    </select>


    <select id="selectSopUserLogsListBySopId" resultMap="SopUserLogsResult">
        select
            id,
            sop_id,
            sop_temp_id,
            qw_user_id,
            corp_id,
            start_time,
            status,
            user_id
            from sop_user_logs where sop_id = #{data.sopId} order by start_time desc
    </select>

    <select id="selectSopUserInfoLogsListByTime" resultType="com.fs.sop.vo.SopUserLogsInfoVo">
        select a.*,b.min_conversion_day,b.max_conversion_day,b.min_send,b.max_send from sop_user_logs_info_chat a
        inner join qw_sop b on a.sop_id = b.id
        where a.start_time &lt;= Now() and a.status = 1
    </select>

    <select id="selectSopUserLogsByDate" resultType="String">
        SELECT id
        FROM sop_user_logs
        WHERE start_time BETWEEN STR_TO_DATE(#{scheduleStartTime}, '%Y-%m-%d') AND STR_TO_DATE(#{scheduleEndTime}, '%Y-%m-%d')
    </select>

    <select id="selectQwSopLogsMessSendList" resultType="com.fs.qw.param.SopUserLogsVO">
        <include refid="selectSopUserLogsVo"/>
        <where>
             count_days = 1
            <if test="sopIds != null and sopIds.size() > 0">
            and sop_id in
            <foreach item="sopId" index="index" collection="sopIds" open="(" separator="," close=")">
                #{sopId}
            </foreach>
            </if>
        </where>
        order by send_time desc
    </select>
    <select id="selectSopUserLogsByDateAndIds" resultType="com.fs.sop.domain.SopUserLogs" >
        select * from sop_user_logs where sop_id=#{sopId} and sop_temp_id=#{sopTempId}  and qw_user_id=#{qwUserId}  and corp_id=#{corpId}  and start_time=#{startTime} and chat_id is null
    </select>
    <select id="selectSopUserLogByChatIds" resultType="com.fs.sop.domain.SopUserLogs">
        select * from sop_user_logs where chat_id in <foreach collection="ids" open="(" separator="," close=")" item="item">#{item}</foreach>
    </select>

</mapper>
