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

    <resultMap type="com.fs.sop.domain.QwSop" id="QwSopResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="status"    column="status"    />
        <result property="type"    column="type"    />
        <result property="qwUserIds"    column="qw_user_ids"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="companyId"    column="company_id"    />
        <result property="sendType"    column="send_type"    />
        <result property="tags"    column="tags"    />
        <result property="excludeTags"    column="exclude_tags"    />
        <result property="startTime"    column="start_time"  />
        <result property="filterType"    column="filter_type"    />
        <result property="tempId"    column="temp_id"    />
        <result property="corpId"    column="corp_id"    />
        <result property="expiryTime"    column="expiry_time"    />
        <result property="isAutoSop"    column="is_auto_sop"    />
        <result property="autoSopTime"    column="auto_sop_time"    />
        <result property="minConversionDay"    column="min_conversion_day"    />
        <result property="maxConversionDay"    column="max_conversion_day"    />
        <result property="minSend"    column="min_send"    />
        <result property="maxSend"    column="max_send"    />
        <result property="stopTime"    column="stop_time"    />
    </resultMap>

    <sql id="selectQwSopVo">
        select *  from qw_sop
    </sql>

    <select id="selectQwSopAutoByTagsByForeach"  resultType="com.fs.qw.vo.QwSopRuleTimeVO">
        SELECT
            qs.*,
            qst.name AS temp_name,
            qst.setting AS temp_setting,
            qst.status AS temp_status,
            qst.gap AS temp_gap,
            qst.sort AS temp_sort,
            qst.create_time AS temp_create_time,
            qst.create_by AS temp_create_by,
            qst.corp_id AS temp_company_id
        FROM
            qw_sop qs
                LEFT JOIN
            qw_sop_temp qst ON qs.temp_id = qst.id
        WHERE
            FIND_IN_SET(#{map.qwUserId}, COALESCE(qs.qw_user_ids, '')) > 0
          AND qs.corp_id = #{map.corpId}
          AND qst.status = '1'
          AND qs.status IN (2, 3, 4)
          AND qs.is_auto_sop = 1
          AND qs.send_type = #{map.sendType}
          AND (
           <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' or '>
                     find_in_set( #{item}, TRIM(REGEXP_REPLACE(qs.tags, '[\"\\\\[\\\\]]', '')))
           </foreach>
            )
    </select>


    <select id="selectQwSopAutoByTagsByForeachNotAuto"  resultType="com.fs.qw.vo.QwSopRuleTimeVO">
        SELECT
        qs.*,
        qst.name AS temp_name,
        qst.setting AS temp_setting,
        qst.status AS temp_status,
        qst.gap AS temp_gap,
        qst.sort AS temp_sort,
        qst.create_time AS temp_create_time,
        qst.create_by AS temp_create_by,
        qst.corp_id AS temp_company_id
        FROM
        qw_sop qs
        LEFT JOIN
        qw_sop_temp qst ON qs.temp_id = qst.id
        WHERE
            FIND_IN_SET(#{map.qwUserId}, COALESCE(qs.qw_user_ids, '')) > 0
        AND qs.corp_id = #{map.corpId}
        AND qst.status = '1'
        AND qs.status IN (2, 3, 4)
        AND qs.send_type = #{map.sendType}
        AND (
            <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' or '>
                find_in_set( #{item}, TRIM(REGEXP_REPLACE(qs.tags, '[\"\\\\[\\\\]]', '')) )
            </foreach>
        )
    </select>

    <select id="selectQwAiSopAutoByTagsByForeach"  resultType="com.fs.qw.vo.QwSopRuleTimeVO">
        SELECT
        qs.*,
        qst.name AS temp_name,
        qst.setting AS temp_setting,
        qst.status AS temp_status,
        qst.gap AS temp_gap,
        qst.sort AS temp_sort,
        qst.create_time AS temp_create_time,
        qst.create_by AS temp_create_by,
        qst.corp_id AS temp_company_id
        FROM
        qw_sop qs
        LEFT JOIN
        qw_sop_temp qst ON qs.temp_id = qst.id
        WHERE
             FIND_IN_SET(#{map.qwUserId}, COALESCE(qs.qw_user_ids, '')) > 0
        AND qs.corp_id = #{map.corpId}
        AND qst.status = '1'
        AND qs.status IN (2, 3, 4)
        AND qs.send_type = 4
        AND (
        <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' or '>
            find_in_set( #{item}, TRIM(REGEXP_REPLACE(qs.tags, '[\"\\\\[\\\\]]', '')) )
        </foreach>
        )
    </select>

    <select id="selectQwSopByQwUserId"  resultType="com.fs.qw.vo.QwSopRuleTimeVO">
        SELECT
            qs.*
        FROM
            qw_sop qs
                LEFT JOIN
            qw_sop_temp qst ON qs.temp_id = qst.id
        WHERE
            FIND_IN_SET(#{map.qwUserId}, COALESCE(qs.qw_user_ids, '')) > 0
          AND qs.corp_id = #{map.corpId}
          AND qst.status = '1'
          AND qs.status IN (2, 3, 4)
          AND qs.send_type = #{map.sendType}
    </select>


    <select id="checkSopRuleTime" resultType="com.fs.qw.vo.QwSopRuleTimeVO">
      <![CDATA[
            SELECT
            qs.*,
            qst.name AS temp_name,
            qst.setting AS temp_setting,
            qst.status AS temp_status,
            qst.gap AS temp_gap,
            qst.sort AS temp_sort,
            qst.create_time AS temp_create_time,
            qst.create_by AS temp_create_by,
            qst.corp_id AS temp_company_id
            FROM
            qw_sop qs
            LEFT JOIN qw_sop_temp qst ON qs.temp_id = qst.id
            WHERE
            qs.status = 1
            AND qs.start_time <= NOW()
            ]]>
    </select>

    <select id="selectQwSopByClickHouseId" parameterType="String" resultType="com.fs.qw.vo.QwSopRuleTimeVO">
        <![CDATA[
            SELECT
                qs.*,
                qst.name AS temp_name,
                qst.setting AS temp_setting,
                qst.status AS temp_status,
                qst.gap AS temp_gap,
                qst.sort AS temp_sort,
                qst.create_time AS temp_create_time,
                qst.create_by AS temp_create_by,
                qst.corp_id AS temp_company_id
            FROM
                qw_sop qs
                    LEFT JOIN
                qw_sop_temp qst
                ON
                    qs.temp_id = qst.id
            WHERE
                qs.id = #{id}
            ]]>
    </select>

    <select id="selectQwSopList" parameterType="QwSop" resultMap="QwSopResult">
        <include refid="selectQwSopVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="id != null "> and id = #{id}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="qwUserIds != null  and qwUserIds != ''"> and FIND_IN_SET(#{qwUserIds}, qw_user_ids) > 0</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</if>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
            <if test="createTime != null "> and DATE(create_time) = #{createTime}</if>
            <if test="sendType != null "> and send_type = #{sendType}</if>
            <if test="expiryTime != null "> and expiry_time = #{expiryTime}</if>
            <if test="isAutoSop != null "> and is_auto_sop = #{isAutoSop}</if>
            <if test="autoSopTime != null "> and auto_sop_time = #{autoSopTime}</if>
            <if test="minSend != null "> and min_send = #{minSend}</if>
            <if test="maxSend != null "> and max_send = #{maxSend}</if>
            <if test="stopTime != null "> and stop_time = #{stopTime}</if>
            <!-- 加入固定条件 -->
            and status != 6
        </where>
        order by create_time desc
    </select>

    <select id="selectChatQwSopList" parameterType="QwSop" resultMap="QwSopResult">
        <include refid="selectQwSopVo"/>
        where send_type = 4
            and status != 6
         order by create_time desc
    </select>

    <select id="selectQwSopById" parameterType="String" resultMap="QwSopResult">
        <include refid="selectQwSopVo"/>
        where id = #{id}
    </select>

    <insert id="insertQwSop" parameterType="com.fs.sop.domain.QwSop" useGeneratedKeys="false" keyProperty="id">
        insert into qw_sop
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="data.name != null">name,</if>
            <if test="data.status != null">status,</if>
            <if test="data.type != null">type,</if>
            <if test="data.qwUserIds != null">qw_user_ids,</if>
            <if test="data.createBy != null">create_by,</if>
            <if test="data.createTime != null">create_time,</if>
            <if test="data.companyId != null">company_id,</if>
            <if test="data.sendType != null">send_type,</if>
            <if test="data.tags != null">tags,</if>
            <if test="data.excludeTags != null">exclude_tags,</if>
            <if test="data.startTime != null">start_time,</if>
            <if test="data.filterType != null">filter_type,</if>
            <if test="data.tempId != null">temp_id,</if>
            <if test="data.corpId != null">corp_id,</if>
            <if test="data.expiryTime != null">expiry_time,</if>
            <if test="data.isAutoSop != null">is_auto_sop,</if>
            <if test="data.autoSopTime != null">auto_sop_time,</if>
            <if test="data.minConversionDay != null">min_conversion_day,</if>
            <if test="data.maxConversionDay != null">max_conversion_day,</if>
            <if test="data.minSend != null">min_send,</if>
            <if test="data.maxSend != null">max_send,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="data.name != null">#{data.name},</if>
            <if test="data.status != null">#{data.status},</if>
            <if test="data.type != null">#{data.type},</if>
            <if test="data.qwUserIds != null">#{data.qwUserIds},</if>
            <if test="data.createBy != null">#{data.createBy},</if>
            <if test="data.createTime != null">#{data.createTime},</if>
            <if test="data.companyId != null">#{data.companyId},</if>
            <if test="data.sendType != null">#{data.sendType},</if>
            <if test="data.tags != null">#{data.tags},</if>
            <if test="data.excludeTags != null">#{data.excludeTags},</if>
            <if test="data.startTime != null">#{data.startTime},</if>
            <if test="data.filterType != null">#{data.filterType},</if>
            <if test="data.tempId != null">#{data.tempId},</if>
            <if test="data.corpId != null">#{data.corpId},</if>
            <if test="data.expiryTime != null">#{data.expiryTime},</if>
            <if test="data.isAutoSop != null">#{data.isAutoSop},</if>
            <if test="data.autoSopTime != null">#{data.autoSopTime},</if>
            <if test="data.minConversionDay != null">#{data.minConversionDay},</if>
            <if test="data.maxConversionDay != null">#{data.maxConversionDay},</if>
            <if test="data.minSend != null">#{data.minSend},</if>
            <if test="data.maxSend != null">#{data.maxSend},</if>
        </trim>
    </insert>

    <update id="updateAutoSopTime" useGeneratedKeys="false" keyProperty="id" >
        UPDATE  qw_sop
        SET
        is_auto_sop = #{map.isAutoSop}
        <if test="map.autoSopTime != null and map.autoSopTime != ''">
            ,auto_sop_time = #{map.autoSopTime}
        </if>
        WHERE id = #{map.id}
    </update>

    <update id="updateSopStatus" useGeneratedKeys="false" keyProperty="id" >
        UPDATE  qw_sop
        SET status = #{map.status},stop_time = NOW()
        WHERE id = #{map.id}
    </update>


    <select id="selectStatusQwSopById" resultType="QwSop">
        select * from qw_sop where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="executeSopByIds"  resultType="com.fs.qw.vo.QwSopRuleTimeVO">
        SELECT qs.*,
        qst.name AS temp_name,
        qst.setting AS temp_setting,
        qst.status AS temp_status,
        qst.gap AS temp_gap,
        qst.sort AS temp_sort,
        qst.create_time AS temp_create_time,
        qst.create_by AS temp_create_by,
        qst.corp_id AS temp_company_id
        FROM qw_sop qs
        LEFT JOIN qw_sop_temp qst ON qs.temp_id = qst.id
        WHERE qs.id IN
        <foreach item="id" collection="ids"  open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectAiChatQwSopList" resultType="com.fs.sop.domain.QwSop">
        <include refid="selectQwSopVo"/>
        <where>
            send_type = 4
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="qwUserIds != null  and qwUserIds != ''"> and qw_user_ids = #{qwUserIds}</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</if>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
            <if test="createTime != null "> and DATE(create_time) = #{createTime}</if>
            <if test="expiryTime != null "> and expiry_time = #{expiryTime}</if>
            <if test="isAutoSop != null "> and is_auto_sop = #{isAutoSop}</if>
            <if test="autoSopTime != null "> and auto_sop_time = #{autoSopTime}</if>
            <if test="minSend != null "> and min_send = #{minSend}</if>
            <if test="maxSend != null "> and max_send = #{maxSend}</if>
            <if test="stopTime != null "> and stop_time = #{stopTime}</if>
            <!-- 加入固定条件 -->
            and status != 6
        </where>
        order by create_time desc
    </select>
    <select id="selectByTemplateId" resultType="com.fs.sop.domain.QwSop">
        select * from qw_sop where temp_id = #{tempId}
    </select>
    <update id="updateQwSop" parameterType="QwSop" useGeneratedKeys="false" keyProperty="id" >
        UPDATE  qw_sop
        <trim prefix="SET" suffixOverrides=",">
            <if test="data.name != null">name = #{data.name},</if>
            <if test="data.status != null">status = #{data.status},</if>
            <if test="data.type != null">type = #{data.type},</if>
            <if test="data.qwUserIds != null">qw_user_ids = #{data.qwUserIds},</if>
            <if test="data.createBy != null">create_by = #{data.createBy},</if>
            <if test="data.createTime != null">create_time = #{data.createTime},</if>
            <if test="data.companyId != null">company_id = #{data.companyId},</if>
            <if test="data.sendType != null">send_type = #{data.sendType},</if>
            <if test="data.tags != null">tags = #{data.tags},</if>
            <if test="data.excludeTags != null">exclude_tags = #{data.excludeTags},</if>
            <if test="data.startTime != null">start_time = #{data.startTime},</if>
            <if test="data.filterType != null">filter_type = #{data.filterType},</if>
            <if test="data.tempId != null">temp_id = #{data.tempId},</if>
            <if test="data.corpId != null">corp_id = #{data.corpId},</if>
            <if test="data.expiryTime != null">expiry_time = #{data.expiryTime},</if>
            <if test="data.isAutoSop != null">is_auto_sop = #{data.isAutoSop},</if>
            <if test="data.autoSopTime != null">auto_sop_time = #{data.autoSopTime},</if>
            <if test="data.minConversionDay != null">min_conversion_day = #{data.minConversionDay},</if>
            <if test="data.maxConversionDay != null">max_conversion_day = #{data.maxConversionDay},</if>
            <if test="data.minSend != null">min_send = #{data.minSend},</if>
            <if test="data.maxSend != null">max_send = #{data.maxSend},</if>
            <if test="data.voice != null">voice = #{data.voice},</if>
        </trim>
        where id = #{data.id}
    </update>

    <delete id="deleteQwSopById" parameterType="String">
        DELETE FROM qw_sop
          where id = #{id}
    </delete>

    <delete id="deleteQwSopByIds" parameterType="String">
        DELETE FROM qw_sop
         where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="deleteQwSopByIdsUpdate" useGeneratedKeys="false" keyProperty="id">
        UPDATE qw_sop
        SET status = 6
        WHERE id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateStatusQwSopById" useGeneratedKeys="false" keyProperty="id" >
        UPDATE qw_sop
        SET status = 4
        WHERE id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateMinSendStatus">
        UPDATE qw_sop
        SET min_send = 1
        WHERE id = #{id}
    </update>

    <update id="updateMaxSendStatus">
        UPDATE qw_sop
        SET max_send = 1
        WHERE id = #{id}
    </update>
</mapper>
