<?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="cn.roadm.admin.mapper.ChannelChatbotMapper">

    <select id="listPageCsp" resultType="cn.roadm.admin.dto.ChannelChatbotListDTO">
        select id,
               channelid,
               sign,
               chatbot,
               csptoken,
               null        as appId,
               null        as appKey,
               DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createtime,
               status
        from sms_channel_sign_chatbot
        <where>
            <if test="reqVO.channelid != null">
                and channelid = #{reqVO.channelid}
            </if>
            <if test="reqVO.sign != null and reqVO.sign != ''">
                and sign like concat('%', #{reqVO.sign}, '%')
            </if>
            <if test="reqVO.chatbot != null and reqVO.chatbot != ''">
                and chatbot like concat('%', #{reqVO.chatbot}, '%')
            </if>
        </where>
        order by id desc
    </select>

    <select id="listCsp" resultType="cn.roadm.admin.dto.ChannelChatbotListDTO">
        select id,
               channelid,
               sign,
               chatbot,
               csptoken,
               null        as appId,
               null        as appKey,
               DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createtime,
               status
        from sms_channel_sign_chatbot
        <where>
            <if test="reqVO.channelid != null">
                and channelid = #{reqVO.channelid}
            </if>
            <if test="reqVO.sign != null and reqVO.sign != ''">
                and sign like concat('%', #{reqVO.sign}, '%')
            </if>
            <if test="reqVO.chatbot != null and reqVO.chatbot != ''">
                and chatbot like concat('%', #{reqVO.chatbot}, '%')
            </if>
        </where>
        order by id desc
    </select>

    <select id="listPageTelecom" resultType="cn.roadm.admin.dto.ChannelChatbotListDTO">
        select id,
               channelid,
               sign,
               chatbot,
               null        as csptoken,
               appId,
               appKey,
               DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createtime,
               status
        from sms_channel_sign_telecom_unicom_chatbot
        <where>
            <if test="reqVO.channelid != null">
                and channelid = #{reqVO.channelid}
            </if>
            <if test="reqVO.sign != null and reqVO.sign != ''">
                and sign like concat('%', #{reqVO.sign}, '%')
            </if>
            <if test="reqVO.chatbot != null and reqVO.chatbot != ''">
                and chatbot like concat('%', #{reqVO.chatbot}, '%')
            </if>
        </where>
        order by id desc
    </select>

    <select id="listTelecom" resultType="cn.roadm.admin.dto.ChannelChatbotListDTO">
        select id,
               channelid,
               sign,
               chatbot,
               null        as csptoken,
               appId,
               appKey,
               DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createtime,
               status
        from sms_channel_sign_telecom_unicom_chatbot
        <where>
            <if test="reqVO.channelid != null">
                and channelid = #{reqVO.channelid}
            </if>
            <if test="reqVO.sign != null and reqVO.sign != ''">
                and sign like concat('%', #{reqVO.sign}, '%')
            </if>
            <if test="reqVO.chatbot != null and reqVO.chatbot != ''">
                and chatbot like concat('%', #{reqVO.chatbot}, '%')
            </if>
        </where>
        order by id desc
    </select>

    <select id="getCspById" resultType="cn.roadm.admin.dto.ChannelChatbotListDTO">
        select id,
               channelid,
               sign,
               chatbot,
               csptoken,
               null as appId,
               null as appKey,
               DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createtime,
               status
        from sms_channel_sign_chatbot
        where id = #{id}
        limit 1
    </select>

    <select id="getTelecomById" resultType="cn.roadm.admin.dto.ChannelChatbotListDTO">
        select id,
               channelid,
               sign,
               chatbot,
               null as csptoken,
               appId,
               appKey,
               DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createtime,
               status
        from sms_channel_sign_telecom_unicom_chatbot
        where id = #{id}
        limit 1
    </select>

    <insert id="insertCsp">
        insert into sms_channel_sign_chatbot (chatbot, channelid, sign, csptoken, createtime, status)
        values (#{reqVO.chatbot}, #{reqVO.channelid}, #{reqVO.sign}, #{reqVO.csptoken}, now(), 0)
    </insert>

    <update id="updateCsp">
        update sms_channel_sign_chatbot
        set chatbot   = #{reqVO.chatbot},
            channelid = #{reqVO.channelid},
            sign      = #{reqVO.sign},
            csptoken  = #{reqVO.csptoken}
        where id = #{reqVO.id}
    </update>

    <insert id="insertTelecom">
        insert into sms_channel_sign_telecom_unicom_chatbot (chatbot, channelid, sign, appId, appKey, createtime, status)
        values (#{reqVO.chatbot}, #{reqVO.channelid}, #{reqVO.sign}, #{reqVO.appId}, #{reqVO.appKey}, now(), 0)
    </insert>

    <update id="updateTelecom">
        update sms_channel_sign_telecom_unicom_chatbot
        set chatbot   = #{reqVO.chatbot},
            channelid = #{reqVO.channelid},
            sign      = #{reqVO.sign},
            appId     = #{reqVO.appId},
            appKey    = #{reqVO.appKey}
        where id = #{reqVO.id}
    </update>

    <delete id="deleteCspById">
        delete
        from sms_channel_sign_chatbot
        where id = #{id}
    </delete>

    <delete id="deleteTelecomById">
        delete
        from sms_channel_sign_telecom_unicom_chatbot
        where id = #{id}
    </delete>

    <update id="updateCspStatus">
        update sms_channel_sign_chatbot
        set status = #{status}
        where chatbot = #{chatbot}
          and channelid = #{channelid}
    </update>

    <update id="updateTelecomStatus">
        update sms_channel_sign_telecom_unicom_chatbot
        set status = #{status}
        where chatbot = #{chatbot}
          and channelid = #{channelid}
    </update>

    <select id="selectAgentIdFromSms" resultType="string">
        select a.id as agentIds
        from csp_agent a,
             (select JSON_UNQUOTE(channelTemplateVar->'$.cpsId') as cspId
              from http_channel_template_config
              where channelid = #{channelId}) b,
             (select mobile_prefix from sms_send_channel_info where id = #{channelId}) c
        where a.csp_code = b.cspId
          and a.service_code like concat('%', c.mobile_prefix, '%')
    </select>

    <select id="selectChatbotConfig" resultType="map">
        select chatbot, `sign`, csptoken
        from (select a.chatbotId as chatbot, a.`sign` as `sign`, FROM_BASE64(b.csp_token) as csptoken
              from (select *
                    from csp_chatbot
                    where status = 30
                      and agent_id in (${agentIds})) a
                       left join csp_chatbot_send_info b on a.chatbotid = b.chatbot_id) c
        where c.chatbot not in (select chatbot from sms_channel_sign_chatbot where channelid = #{channelId} and chatbot is not null)
    </select>

    <select id="selectGkChatbotConfig" resultType="map">
        select chatbot, `sign`, csptoken
        from (select a.chatbotId as chatbot, a.`sign` as `sign`, FROM_BASE64(b.devToken) as csptoken
              from (select *
                    from csp_chatbot
                    where status = 30
                      and agent_id in (${agentIds})) a
                       left join csp_chatbot_info b on a.chatbotid = b.chatbotId) c
        where c.csptoken is not null
          and c.chatbot not in (select chatbot from sms_channel_sign_chatbot where channelid = #{channelId} and chatbot is not null)
    </select>

    <select id="selectTelecomUnicomChatbotConfig" resultType="map">
        select chatbotId as chatbot, `sign`, appId, appKey
        from csp_chatbot
        where status = 30
          and agent_id in (${agentIds})
          and chatbotId not in (select chatbot
                                from sms_channel_sign_telecom_unicom_chatbot
                                where channelid = #{channelId}
                                  and chatbot is not null)
    </select>

    <insert id="batchUpdateChatbotConfig">
        insert into sms_channel_sign_chatbot(channelid, sign, chatbot, csptoken, createtime)
        values
        <foreach collection="param" item="item" separator=",">
            (#{channelId}, #{item.sign}, #{item.chatbot}, #{item.csptoken}, now())
        </foreach>
    </insert>

    <insert id="batchUpdateTelecomUnicomChatbotConfig">
        insert into sms_channel_sign_telecom_unicom_chatbot(channelid, sign, chatbot, appId, appKey, createtime)
        values
        <foreach collection="param" item="item" separator=",">
            (#{channelId}, #{item.sign}, #{item.chatbot}, #{item.appId}, #{item.appKey}, now())
        </foreach>
    </insert>
</mapper>
