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

    <resultMap id="channelListPage" type="cn.roadm.admin.dto.SmsSendChannelDO">
        <id property="id" column="id"/>
        <result column="is_support_international" property="isSupportInternational"/>
        <result column="is_support_unicom" property="isSupportUnicom"/>
        <result column="is_support_telecom" property="isSupportUnicom"/>
        <result column="is_support_mobile" property="isSupportMobile"/>
        <result column="carrier" property="carrier"/>
        <result column="channel_name" property="channelName"/>
        <result column="unicom_prefix" property="unicomPrefix"/>
        <result column="mobile_prefix" property="mobilePrefix"/>
        <result column="telecom_prefix" property="telecomPrefix"/>
        <result column="loginname" property="loginName"/>
        <result column="accessnumber" property="accessNumber"/>
        <result column="ip" property="ip"/>
        <result column="port" property="port"/>
        <result column="service_id" property="serviceId"/>
        <result column="carrier_sp_id" property="carrierSpId"/>
        <result column="fee_type" property="feeType"/>
        <result column="fee_code" property="feeCode"/>
        <result column="price" property="price"/>
        <result column="version" property="version"/>
        <result column="carrier_speed" property="carrierSpeed"/>
        <result column="socket_conn" property="socketConn"/>
        <result column="conn_speed" property="connSpeed"/>
        <result column="src_note_id" property="srcNoteId"/>
        <result column="local_listen_port" property="localListenPort"/>
        <result column="carrier_sp_sign" property="carrierSpSign"/>
        <result column="pa_url" property="paUrl"/>
        <result column="status" property="status"/>
        <result column="desc" property="desc"/>
        <result column="protocol_type" property="protocolType"/>
        <result column="batchsize" property="batchsize"/>
        <result column="template_id" property="templateId"/>
        <result column="template_parameter" property="templateParameter"/>
        <result column="connect_status" property="connectStatus"/>
        <result column="inter_price_group" property="interPriceGroup"/>
        <result column="business_manager" property="businessManager"/>
    </resultMap>

    <resultMap id="singleChannelDetail" type="cn.roadm.admin.dto.SmsSendChannelDO" extends="channelListPage">
        <result column="password" property="password"/>
        <result column="accessnumber" property="accessNumber"/>
        <result column="signflag" property="signFlag"/>
        <result column="access_key" property="accessKey"/>
        <result column="http_response" property="httpResponse"/>
        <result column="max_expand" property="maxExpand"/>
        <result column="spid" property="spid"/>
        <result column="is_only_expand" property="isOnlyExpand"/>
        <result column="fee_terminal_id" property="feeTerminalId"/>
        <result column="fee_usertype" property="feeUsertype"/>
        <result column="overspeed_resend" property="overSpeedResend"/>
        <result column="province" property="province"/>
        <result column="provinceNetPrice" property="provinceNetPrice"/>
        <result column="autoExtend" property="autoExtend"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="pro_group_price_id" property="proGroupPriceId"/>
        <result column="mmsContentFormat" property="mmsContentFormat"/>
        <result column="access_province" property="accessProvince"/>
        <result column="access_city" property="accessCity"/>
        <result column="access_company" property="accessCompany"/>
        <result column="windowSize" property="windowSize"/>
        <result column="windowSizeFlag" property="windowSizeFlag"/>
        <result column="sign_port" property="signPort"/>
        <result column="server_id" property="serverId"/>
    </resultMap>

    <resultMap id="spChannelListPage" type="cn.roadm.admin.dto.SpChannelConfigDO">
        <id column="id" property="id"/>
        <result column="sp_name" property="spName"/>
        <result column="international_prefix" property="internationalPrefix"/>
        <result column="international_num" property="internationalNum"/>
        <result column="international_suffix" property="internationalSuffix"/>
        <result column="mobile_prefix" property="mobilePrefix"/>
        <result column="mobile_suffix" property="mobileSuffix"/>
        <result column="mobile_num" property="mobileNum"/>
        <result column="unicom_prefix" property="unicomPrefix"/>
        <result column="unicom_suffix" property="unicomSuffix"/>
        <result column="unicom_num" property="unicomNum"/>
        <result column="telecom_prefix" property="telecomPrefix"/>
        <result column="telecom_suffix" property="telecomSuffix"/>
        <result column="mms_mobile_prefix" property="mmsMobilePrefix"/>
        <result column="mms_mobile_suffix" property="mmsMobileSuffix"/>
        <result column="mms_unicom_prefix" property="mmsUnicomPrefix"/>
        <result column="mms_unicom_suffix" property="mmsUnicomSuffix"/>
        <result column="mms_telecom_prefix" property="mmsTelecomPrefix"/>
        <result column="mms_telecom_suffix" property="mmsTelecomSuffix"/>
        <result column="mms_mobile_num" property="mmsMobileNum"/>
        <result column="mms_unicom_num" property="mmsUnicomNum"/>
        <result column="mms_telecom_num" property="mmsTelecomNum"/>
        <result column="platform" property="platform"/>
        <result column="is_open_province" property="isOpenProvince"/>
        <result column="is_open_province_group" property="isOpenProvinceGroup"/>
        <result column="is_open_country" property="isOpenCountry"/>
        <result column="content_type" property="contentType"/>
        <result column="pro_group_id" property="proGroupId"/>
        <result column="mobile" property="mobile"/>
        <result column="unicom" property="unicom"/>
        <result column="telecom" property="telecom"/>
        <result column="mobileGroup" property="mobileGroup"/>
        <result column="unicomGroup" property="unicomGroup"/>
        <result column="telecomGroup" property="telecomGroup"/>
        <result column="countryGroupId" property="countryGroupId"/>
        <result column="inter_sms_flag" property="interSmsFlag"/>
        <result column="type" property="isOpenChannelGroup"/>
    </resultMap>

    <resultMap id="channelPriceMap" type="cn.roadm.admin.dto.ChannelPriceDTO">
        <id column="id" property="id"/>
        <result column="channelid" property="channelId"/>
        <result column="channel_name" property="channelName"/>
        <result column="channel_price" property="channelPrice"/>
        <result column="otype" property="otype"/>
        <result column="province" property="province"/>
        <result column="createtime" property="createTime"/>
        <result column="effecttime" property="effectTime"/>
        <result column="user" property="user"/>
        <result column="status" property="status"/>
        <result column="provinceNetPrice" property="provinceNetPrice"/>
    </resultMap>

    <select id="getChannelGroupList" resultType="cn.roadm.admin.table.SmsChannelGroupTable">
        select *
        from sms_channel_group g
        <where>
            g.status = '1'
            <if test="network != null and network != ''">
                and g.network = #{network}
            </if>
            <if test="name != null and name != ''">
                and g.name like CONCAT('%', #{name}, '%')
            </if>
            <if test="channelId != null and channelId != ''">
                and CONCAT(',', channels, ',') like CONCAT('%,', #{channelId}, ',%')
            </if>
        </where>
        order by g.id desc
    </select>

    <select id="getChannelConfig" resultType="cn.roadm.admin.table.SmsChannelConfigTable">
        select cg.id,
               cg.opertor_type,
               cg.channelid,
               cg.sp_id,
               cg.createtime
        from sms_chennel_config cg
        <where>
            <if test="spId != null and spId > 0">
                and cg.sp_id = #{spId}
            </if>
            <if test="opertorType != null and opertorType > 0">
                and cg.opertor_type = #{opertorType}
            </if>
        </where>
    </select>

    <select id="listPageSpChannelConfig" parameterType="cn.roadm.admin.controller.vo.SpChannelListQueryVO"
            resultMap="spChannelListPage">
        select sp.international_prefix,
               sp.international_suffix,
               sp.international_num,
               sp.id,
               sp.sp_name,
               sp.mobile_prefix,
               sp.mobile_suffix,
               sp.mobile_num,
               sp.unicom_prefix,
               sp.unicom_suffix,
               sp.unicom_num,
               sp.telecom_prefix,
               sp.telecom_suffix,
               sp.mms_mobile_prefix,
               sp.mms_mobile_suffix,
               sp.mms_mobile_num,
               sp.mms_unicom_prefix,
               sp.mms_unicom_suffix,
               sp.mms_unicom_num,
               sp.mms_telecom_prefix,
               sp.mms_telecom_suffix,
               sp.mms_telecom_num,
               sp.platform,
               sa.content_type,
               sp.is_open_province,
               sp.pro_group_id,
               sp.is_open_province_group,
               sscc.mobile,
               sscc.unicom,
               sscc.telecom,
               sscc.mobileGroup,
               sscc.unicomGroup,
               sscc.telecomGroup,
               sp.countryGroupId,
               sp.inter_sms_flag,
               sp.is_open_country,
               sscc.type
        from sms_sp_info sp
                 left join sms_api_setting sa on sp.id = sa.sp_id
                 left join sms_sp_channel_config sscc on sscc.spid = sp.id
        where sp.status = 1 and sp.is_lock = 1
        <if test="queryVO.spName != null and queryVO.spName != ''">
            and sp.sp_name like CONCAT('%', #{queryVO.spName}, '%')
        </if>
        <if test="queryVO.mobileChannelId != null and queryVO.mobileChannelId != '' and queryVO.mobileChannelId.indexOf('g') &gt;= 0">
            and sp.id in (select spId
                          from sms_sp_channel_config
                          where type = 2
                            and mobileGroup like CONCAT(#{queryVO.mobileGroupId}, ',%'))
        </if>
        <if test="queryVO.mobileChannelId != null and queryVO.mobileChannelId != '' and queryVO.mobileChannelId.indexOf('g') &lt; 0">
            and sp.id in
                (select sp_id from sms_chennel_config where OPERTOR_TYPE = 2 and CHANNELID = #{queryVO.mobileChannelId})
        </if>
        <if test="queryVO.unicomChannelId != null and queryVO.unicomChannelId != '' and queryVO.unicomChannelId.indexOf('g') &lt; 0">
            and sp.id in
                (select sp_id from sms_chennel_config where OPERTOR_TYPE = 1 and CHANNELID = #{queryVO.unicomChannelId})
        </if>
        <if test="queryVO.unicomChannelId != null and queryVO.unicomChannelId != '' and queryVO.unicomChannelId.indexOf('g') &gt;= 0">
            and sp.id in (select spId
                          from sms_sp_channel_config
                          where type = 1
                            and unicomGroup like CONCAT(#{queryVO.unicomGroupId}, ',%'))
        </if>
        <if test="queryVO.telecomChannelId != null and queryVO.telecomChannelId != '' and queryVO.telecomChannelId.indexOf('g') &lt; 0">
            and sp.id in (select sp_id
                          from sms_chennel_config
                          where OPERTOR_TYPE = 3
                            and CHANNELID = #{queryVO.telecomChannelId})
        </if>
        <if test="queryVO.telecomChannelId != null and queryVO.telecomChannelId != '' and queryVO.telecomChannelId.indexOf('g') &gt;= 0">
            and sp.id in (select spId
                          from sms_sp_channel_config
                          where type = 1
                            and telecomGroup like CONCAT(#{queryVO.telecomGroupId}, ',%'))
        </if>
        <if test="queryVO.mobileSuffix != null and queryVO.mobileSuffix != ''">
            and sp.mobile_suffix = #{queryVO.mobileSuffix}
        </if>
        <if test="queryVO.unicomSuffix != null and queryVO.unicomSuffix != ''">
            and sp.unicom_suffix = #{queryVO.unicomSuffix}
        </if>
        <if test="queryVO.telecomSuffix != null and queryVO.telecomSuffix != ''">
            and sp.telecom_suffix = #{queryVO.telecomSuffix}
        </if>
        <if test="queryVO.spId != null and queryVO.spId != 0">
            and sp.id = #{queryVO.spId}
        </if>
        order by sp.createtime desc
    </select>

    <select id="getMoreChanelConfig" resultType="cn.roadm.admin.dto.SpChannelSimpleConfigDO">
        select cg.SP_ID as spId, cg.OPERTOR_TYPE as otype, cg.CHANNELID as channelId, sc.CHANNEL_NAME as channelName
        from sms_chennel_config cg
                 left join sms_send_channel_info sc on cg.CHANNELID = sc.ID
        where cg.sp_id in
        <foreach collection="spIds" item="spId" open="(" separator="," close=")">
            #{spId}
        </foreach>
    </select>

    <select id="listPageQueryCmppChannelList" resultMap="channelListPage">
        select distinct a.id,
                        a.is_support_international,
                        a.is_support_unicom,
                        a.is_support_telecom,
                        a.is_support_mobile,
                        p.carrier,
                        a.channel_name,
                        a.unicom_prefix,
                        a.mobile_prefix,
                        a.telecom_prefix,
                        p.loginname,
                        a.mobile_prefix as accessnumber,
                        p.ip,
                        p.port,
                        p.service_id,
                        p.carrier_sp_id,
                        p.fee_type,
                        p.fee_code,
                        a.price,
                        p.version,
                        p.carrier_speed,
                        p.socket_conn,
                        p.conn_speed,
                        p.src_note_id,
                        p.local_listen_port,
                        p.carrier_sp_sign,
                        a.pa_url,
                        a.status,
                        a.desc,
                        a.protocol_type,
                        a.batchsize,
                        a.template_id,
                        a.template_parameter,
                        a.connect_status,
                        a.inter_price_group,
                        a.business_manager
        from sms_send_channel_info a
                 left JOIN sms_carrier_channel_info p ON a.id = p.id
        <where>
            and a.protocol_type != 4
            <if test="channelParams.name != null">
                and a.channel_name like CONCAT('%', #{channelParams.name}, '%')
            </if>
            <if test="channelParams.id != null">
                and a.id = #{channelParams.id}
            </if>
            <if test="channelParams.status != null and channelParams.status != -1">
                and a.status = #{channelParams.status}
            </if>
            <if test="channelParams.ip != null">
                and p.ip like CONCAT('%', #{channelParams.ip}, '%')
            </if>
            <if test="channelParams.loginName != null">
                and p.loginname like CONCAT('%', #{channelParams.loginName}, '%')
            </if>
            <if test="channelParams.businessManager != null and channelParams.businessManager != ''">
                and a.business_manager = #{channelParams.businessManager}
            </if>
        </where>
        order by a.id desc
    </select>

    <select id="getChannelDetailById" resultMap="singleChannelDetail">
        select a.id,
               a.is_support_international,
               a.is_support_unicom,
               a.is_support_telecom,
               a.is_support_mobile,
               p.carrier,
               a.channel_name,
               p.loginname,
               p.password,
               a.mobile_prefix as accessnumber,
               p.ip,
               p.port,
               p.service_id,
               p.carrier_sp_id,
               p.fee_type,
               p.fee_code,
               a.price,
               p.version,
               p.carrier_speed,
               p.socket_conn,
               p.src_note_id,
               p.local_listen_port,
               p.carrier_sp_sign,
               p.signflag,
               a.status,
               a.pa_url,
               a.protocol_type,
               a.batchsize,
               a.template_id,
               a.template_parameter,
               a.access_key,
               a.http_response,
               a.spid,
               a.max_expand,
               a.is_only_expand,
               p.fee_terminal_id,
               p.fee_usertype,
               p.conn_speed,
               p.overspeed_resend,
               a.server_id,
               a.province,
               a.provinceNetPrice,
               a.`desc`,
               a.autoExtend,
               a.supplier_id,
               a.pro_group_price_id,
               a.mmsContentFormat,
               a.access_province,
               a.access_city,
               a.access_company,
               p.windowSize,
               p.windowSizeFlag,
               a.sign_port
        from sms_send_channel_info a
                 left JOIN sms_carrier_channel_info p ON a.id = p.id
        where a.id = #{id}
    </select>

    <select id="getChannelSupplierListOptions" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as value, supplier_name as label
        from channel_supplier
    </select>

    <select id="getBusinessManagerOptions" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsStringVO">
        select NAME as label, NAME as value
        from sms_user_info
        where ROLE_ID in (42, 520)
          and STATUS = 1
        order by NAME asc
    </select>

    <select id="getChannelNetPriceGroupOptions" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        SELECT id as value, name as label
        FROM sms_province_price_group
    </select>

    <select id="selectSocketChannelList" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as value, channel_name as label
        from sms_send_channel_info
        <where>
            <if test="status != null">
                STATUS = #{status}
            </if>
            and protocol_type in (1, 2, 3)
        </where>
    </select>

    <select id="httpChannelList" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as value, channel_name as label
        from sms_send_channel_info
        <where>
            <if test="status != null">
                STATUS = #{status}
            </if>
            and protocol_type = 4
        </where>
    </select>

    <select id="allChannelOptionList" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as value, channel_name as label
        from sms_send_channel_info
        <where>
            <if test="status != null">
                STATUS = #{status}
            </if>
        </where>
    </select>

    <select id="listPageChannelExtendInterceptorList" resultType="cn.roadm.admin.dto.SmsChannelExtendLimitDO">
        select a.id, a.channelid, ch.channel_name as channelName, a.callmdn, a.createtime, a.uid, sp.username
        from sms_channel_extend_limit a
                 left join sms_sp_user_info sp on a.uid = sp.id
                 left join sms_send_channel_info ch on a.channelid = ch.ID
        <where>
            <if test="reqVO.channelId != null and reqVO.channelId != ''">
                and channelid = #{reqVO.channelId}
            </if>
            <if test="reqVO.extendNo != null and reqVO.extendNo != ''">
                and callmdn like CONCAT('%', #{reqVO.extendNo}, '%')
            </if>
        </where>
    </select>

    <select id="listPageQueryHttpChannelList" resultMap="channelListPage">
        select distinct a.id,
                        a.is_support_international,
                        a.is_support_unicom,
                        a.is_support_telecom,
                        a.is_support_mobile,
                        p.carrier,
                        a.channel_name,
                        a.unicom_prefix,
                        a.mobile_prefix,
                        a.telecom_prefix,
                        p.loginname,
                        a.mobile_prefix as accessnumber,
                        p.ip,
                        p.port,
                        p.service_id,
                        p.carrier_sp_id,
                        p.fee_type,
                        p.fee_code,
                        a.price,
                        p.version,
                        p.carrier_speed,
                        p.socket_conn,
                        p.conn_speed,
                        p.src_note_id,
                        p.local_listen_port,
                        p.carrier_sp_sign,
                        a.pa_url,
                        a.status,
                        a.desc,
                        a.protocol_type,
                        a.batchsize,
                        a.template_id,
                        a.template_parameter,
                        a.connect_status,
                        a.inter_price_group,
                        a.business_manager
        from sms_send_channel_info a
                 left JOIN sms_carrier_channel_info p ON a.id = p.id
        <where>
            and a.protocol_type = 4
            <if test="channelParams.name != null">
                and a.channel_name like CONCAT('%', #{channelParams.name}, '%')
            </if>
            <if test="channelParams.id != null">
                and a.id = #{channelParams.id}
            </if>
            <if test="channelParams.status != null and channelParams.status != -1">
                and a.status = #{channelParams.status}
            </if>
            <if test="channelParams.ip != null">
                and p.ip like CONCAT('%', #{channelParams.ip}, '%')
            </if>
            <if test="channelParams.loginName != null">
                and p.loginname like CONCAT('%', #{channelParams.loginName}, '%')
            </if>
            <if test="channelParams.businessManager != null and channelParams.businessManager != ''">
                and a.business_manager = #{channelParams.businessManager}
            </if>
        </where>
        order by a.id desc
    </select>

    <select id="getHttpChannelTemplateByChannelId" resultType="cn.roadm.admin.table.HttpChannelTemplateTable">
        select t.id,
               t.templateCode,
               t.templateName,
               t.templateDefaultVar,
               t.templateVar,
               t.supplier,
               t.version
        from http_channel_template t
                 inner join http_channel_template_config c on c.templateCode = t.templateCode
        where c.channelid = #{channelId}
        limit 1
    </select>

    <select id="getMmsPaHttpConfigById" resultMap="spChannelListPage">
        select a.id,
               a.is_support_unicom,
               a.is_support_telecom,
               a.is_support_mobile,
               a.is_support_international,
               a.channel_name  name,
               a.mobile_prefix accessnumber,
               a.status,
               a.pa_url,
               a.protocol_type,
               a.batchsize,
               a.access_key,
               a.http_response,
               p.templateCode,
               p.channelTemplatevar,
               b.templateDefaultVar,
               a.`desc`,
               a.price
        from mms_send_channel_info a
                 left join mms_http_channel_template_config p on a.id = p.channelid
                 left join mms_http_channel_template b on p.templateCode = b.templateCode
        where a.id = #{id}
        limit 1
    </select>

    <select id="httpChannelListOptions" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select a.templateCode as value,a.templateName as label from http_channel_template a where a.status = 0
    </select>

    <select id="getInterPriceGroupOptions" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as `value`, name as label from inter_sms_price_group
    </select>

    <select id="getChannelHttpConfig" resultType="cn.roadm.admin.dto.SendChannelHttpConfigDTO">
        select a.id,
               a.is_support_international,
               a.is_support_unicom,
               a.is_support_telecom,
               a.is_support_mobile,
               a.channel_name      as name,
               a.mobile_prefix     accessNumber,
               a.status,
               a.query_time  as    queryTime,
               a.ping_time   as    pingTime,
               a.auto_change as    autoChange,
               a.pa_url,
               a.protocol_type,
               a.batchsize,
               a.template_id,
               a.template_parameter,
               a.access_key,
               a.http_response,
               a.spid,
               p.templateCode,
               p.channelTemplateVar,
               h.templateName,
               h.templateDefaultVar,
               h.templateVar,
               h.supplier,
               h.version,
               a.max_expand,
               a.is_only_expand,
               a.server_id,
               a.business_manager  businessManager,
               a.price,
               a.desc,
               a.cooperate_company as cooperateCompany,
               a.attract_limit as attractLimit,
               a.link_properties as linkProperties,
               a.sign_template_limit as signTemplateLimit,
               a.sign_channel_enable as signChannelEnable,
               a.inter_mdn_0 as interMdn0,
               a.open_acct_type as openAcctType,
               a.inter_price_group interPriceGroup,
               a.province,
               a.provinceNetPrice,
               a.supplier_id,
               cs.supplier_name as supplierName,
               a.pro_group_price_id as pro_group_price_id
        from sms_send_channel_info a
                 left join http_channel_template_config p on a.id = p.channelid
                 left join http_channel_template h on p.templateCode = h.templateCode
                 left join channel_supplier cs on cs.id = a.supplier_id
        where a.id = #{id}
        limit 1
    </select>

    <select id="getHttpChannelTemplateCodeInfo" resultType="cn.roadm.admin.table.HttpChannelTemplateTable">
        select a.id, a.templateCode, a.templateName, a.templateDefaultVar, a.templateVar, a.supplier, a.version
        from http_channel_template a
        where a.templateCode = #{templateCode}
    </select>

    <delete id="deleteHttpTemplateConfigByChannelId">
        delete from http_channel_template_config where channelid = #{channelId}
    </delete>

    <insert id="insertHttpTemplateConfig">
        insert into http_channel_template_config (channelid, templateCode, channelTemplatevar, createtime)
        values (#{channelId}, #{templateCode}, #{channelTemplatevar}, now())
    </insert>

    <update id="updateHttpTemplateConfig">
        update http_channel_template_config
        set templateCode = #{templateCode},
            channelTemplatevar = #{channelTemplatevar}
        where channelid = #{channelId}
    </update>

    <update id="updateSpInfoPrefixByChannelId">
        update sms_sp_info
        set mobile_prefix = #{prefix},
            mobile_num = concat(#{prefix}, ifnull(mobile_suffix, '')),
            unicom_prefix = #{prefix},
            unicom_num = concat(#{prefix}, ifnull(unicom_suffix, '')),
            telecom_prefix = #{prefix},
            telecom_num = concat(#{prefix}, ifnull(telecom_suffix, ''))
        where id in (
            select t.spId from (
                select spId from sms_sp_channel_config
                where substring_index(mobile, ',', 1) = #{channelId}
                   or substring_index(unicom, ',', 1) = #{channelId}
                   or substring_index(telecom, ',', 1) = #{channelId}
            ) t
        )
    </update>

    <update id="updateSpChannelConfigPrefixByChannelId">
        update sms_sp_channel_config
        set mobile = if(substring_index(mobile, ',', 1) = #{channelId},
                        concat(substring_index(mobile, ',', 1), ',', #{prefix}, ',', substring_index(mobile, ',', -1)),
                        mobile),
            unicom = if(substring_index(unicom, ',', 1) = #{channelId},
                        concat(substring_index(unicom, ',', 1), ',', #{prefix}, ',', substring_index(unicom, ',', -1)),
                        unicom),
            telecom = if(substring_index(telecom, ',', 1) = #{channelId},
                         concat(substring_index(telecom, ',', 1), ',', #{prefix}, ',', substring_index(telecom, ',', -1)),
                         telecom)
        where substring_index(mobile, ',', 1) = #{channelId}
           or substring_index(unicom, ',', 1) = #{channelId}
           or substring_index(telecom, ',', 1) = #{channelId}
    </update>

    <select id="listPageHttpChannelTemplateList" resultType="cn.roadm.admin.table.ChannelTemplateTable">
        select id,
               template_id,
               channelid,
               content,
               DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createTime,
               `name`,
               status,
               reason,
               local_template
        from channel_template
        <where>
            <if test="channelHttpTemplateVO.templateCode != null and channelHttpTemplateVO.templateCode != ''">
                and template_id = #{channelHttpTemplateVO.templateCode}
            </if>
            <if test="channelHttpTemplateVO.channelId != null and channelHttpTemplateVO.channelId != ''">
                and channelid = #{channelHttpTemplateVO.channelId}
            </if>
            <if test="channelHttpTemplateVO.templateContent != null and channelHttpTemplateVO.templateContent != ''">
                and content like concat('%', #{channelHttpTemplateVO.templateContent}, '%')
            </if>
            <if test="channelHttpTemplateVO.templateName != null and channelHttpTemplateVO.templateName != ''">
                and name like concat('%', #{channelHttpTemplateVO.templateName}, '%')
            </if>
            <if test="channelHttpTemplateVO.status != null and channelHttpTemplateVO.status != ''">
                and status = #{channelHttpTemplateVO.status}
            </if>
        </where>
    </select>

    <select id="listPageQueryChannelPriceList" resultMap="channelPriceMap">
        select hp.id,hp.channelid,hp.channel_price,hp.otype,hp.province, DATE_FORMAT(hp.createtime, '%Y-%m-%d %H:%i:%s') as createtime,
    	DATE_FORMAT(hp.effecttime, '%Y-%m-%d') as effecttime,hp.user,hp.status,
    	coalesce(s.channel_name, m.channel_name) as channel_name
    	from channel_history_price hp
    	left join sms_send_channel_info s on hp.channelid = s.id
    	left join mms_send_channel_info m on hp.channelid = m.id
    	<where>
    		(s.id is not null or m.id is not null)
    		<if test="channelPriceListReqVO.isMmsPrice != null and channelPriceListReqVO.isMmsPrice == 1">
    			and m.id is not null
    		</if>
    		<if test="channelPriceListReqVO.isMmsPrice != null and channelPriceListReqVO.isMmsPrice == 0">
    			and s.id is not null
    		</if>
    		<if test="channelPriceListReqVO.channelId != null">
    			and hp.channelid=#{channelPriceListReqVO.channelId}
    		</if>
    		<if test="channelPriceListReqVO.otype != null and channelPriceListReqVO.otype > 0">
    			and hp.otype=#{channelPriceListReqVO.otype}
    		</if>
    		<if test="channelPriceListReqVO.otype !=null and channelPriceListReqVO.otype == 0">
    			and (hp.otype =0 or hp.otype is null)
    		</if>
    		<if test="channelPriceListReqVO.status!=null and channelPriceListReqVO.status>=0">
    			and hp.status=#{channelPriceListReqVO.status}
    		</if>
    		<if test="channelPriceListReqVO.channelName!=null and channelPriceListReqVO.channelName!=''">
    			and coalesce(s.channel_name, m.channel_name) like concat('%',#{channelPriceListReqVO.channelName},'%')
    		</if>
    		<if test="channelPriceListReqVO.startTime!=null and channelPriceListReqVO.startTime!=''">
    			and hp.createtime &gt;= #{channelPriceListReqVO.startTime}
    		</if>
    		<if test="channelPriceListReqVO.endTime!=null and channelPriceListReqVO.endTime!=''">
    			and hp.createtime &lt;= #{channelPriceListReqVO.endTime}
    		</if>
    	</where>
    </select>

    <select id="getChannelPriceInfo" resultMap="channelPriceMap">
        select id as channelid, price as channel_price, province, provinceNetPrice
        from sms_send_channel_info
        where id = #{channelId}
    </select>

    <select id="getMmsChannelPriceByChannelId" resultMap="channelPriceMap">
        select id channelid, price channel_price
        from mms_send_channel_info
        where id = #{channelId}
    </select>

    <select id="getChannelOtypePrice" resultType="cn.roadm.admin.table.ChannelPriceTable">
        select id, channelid, unicom_price, mobile_price, telecom_price from channel_price where channelid = #{channelid}
    </select>

    <insert id="insertChannelHistoryPrice">
        insert into channel_history_price(channelid, channel_price, otype, province, createtime, effecttime, user, status)
        values (#{channelId}, #{channelPrice,jdbcType=DECIMAL} * 10000, #{isOtype}, #{province}, now(), #{effectTime}, #{user}, #{status})
    </insert>

    <update id="updatePriceHistory">
        update channel_history_price
        set effecttime = #{effectTime},
            channel_price = #{channelPrice,jdbcType=DECIMAL} * 10000,
            province = #{province},
            otype = #{isOtype},
            user = #{user}
        where id = #{id}
    </update>

    <update id="updatePriceHistoryStatus">
        update channel_history_price
        set status = #{status},
            effecttime = ifnull(effecttime, date_format(now(), '%Y-%m-%d')),
            user = #{user}
        where id = #{id}
    </update>

    <select id="getChannelHistoryPriceById" resultMap="channelPriceMap">
        select hp.id,
               hp.channelid,
               hp.channel_price,
               hp.otype,
               hp.province,
               DATE_FORMAT(hp.createtime, '%Y-%m-%d %H:%i:%s') as createtime,
               DATE_FORMAT(hp.effecttime, '%Y-%m-%d') as effecttime,
               hp.user,
               hp.status,
               coalesce(s.channel_name, m.channel_name) as channel_name,
               s.provinceNetPrice
        from channel_history_price hp
                 left join sms_send_channel_info s on hp.channelid = s.id
                 left join mms_send_channel_info m on hp.channelid = m.id
        where hp.id = #{id}
          and (s.id is not null or m.id is not null)
        limit 1
    </select>

    <update id="updateChannelPriceByChannelid">
        update sms_send_channel_info
        set price = #{channelPrice},
            provinceNetPrice = #{provinceNetPrice},
            province = #{province}
        where id = #{channelid}
    </update>

    <update id="updateMmsChannelPriceByChannelid">
        update mms_send_channel_info
        set price = #{channelPrice}
        where id = #{channelid}
    </update>

    <select id="countSmsSendChannelById" resultType="int">
        select count(1) from sms_send_channel_info where id = #{id}
    </select>

    <select id="countMmsSendChannelById" resultType="int">
        select count(1) from mms_send_channel_info where id = #{id}
    </select>

    <insert id="upsertChannelOtypePrice">
        insert into channel_price(channelid, unicom_price, mobile_price, telecom_price)
        values (#{channelid},
                #{unicomPrice,jdbcType=DECIMAL} * 10000,
                #{mobilePrice,jdbcType=DECIMAL} * 10000,
                #{telecomPrice,jdbcType=DECIMAL} * 10000)
        on duplicate key update unicom_price = values(unicom_price),
                                mobile_price = values(mobile_price),
                                telecom_price = values(telecom_price)
    </insert>

    <select id="exportChannelHistoryPrice" resultMap="channelPriceMap">
        select hp.id,
               hp.channelid,
               hp.channel_price,
               hp.otype,
               hp.province,
               DATE_FORMAT(hp.createtime, '%Y-%m-%d %H:%i:%s') as createtime,
               DATE_FORMAT(IFNULL(hp.effecttime, hp.createtime), '%Y-%m-%d') as effecttime,
               hp.user,
               hp.status,
               coalesce(s.channel_name, m.channel_name) as channel_name
        from channel_history_price hp
                 left join sms_send_channel_info s on hp.channelid = s.id
                 left join mms_send_channel_info m on hp.channelid = m.id
        <where>
            (s.id is not null or m.id is not null)
            <if test="channelPriceListReqVO.isMmsPrice != null and channelPriceListReqVO.isMmsPrice == 1">
                and m.id is not null
            </if>
            <if test="channelPriceListReqVO.isMmsPrice != null and channelPriceListReqVO.isMmsPrice == 0">
                and s.id is not null
            </if>
            <if test="channelPriceListReqVO.channelId != null">
                and hp.channelid = #{channelPriceListReqVO.channelId}
            </if>
            <if test="channelPriceListReqVO.otype != null and channelPriceListReqVO.otype > 0">
                and hp.otype = #{channelPriceListReqVO.otype}
            </if>
            <if test="channelPriceListReqVO.otype != null and channelPriceListReqVO.otype == 0">
                and (hp.otype = 0 or hp.otype is null)
            </if>
            <if test="channelPriceListReqVO.status != null and channelPriceListReqVO.status &gt;= 0">
                and hp.status = #{channelPriceListReqVO.status}
            </if>
            <if test="channelPriceListReqVO.channelName != null and channelPriceListReqVO.channelName != ''">
                and coalesce(s.channel_name, m.channel_name) like concat('%', #{channelPriceListReqVO.channelName}, '%')
            </if>
            <if test="channelPriceListReqVO.startTime != null and channelPriceListReqVO.startTime != ''">
                and hp.createtime &gt;= #{channelPriceListReqVO.startTime}
            </if>
            <if test="channelPriceListReqVO.endTime != null and channelPriceListReqVO.endTime != ''">
                and hp.createtime &lt;= #{channelPriceListReqVO.endTime}
            </if>
        </where>
        order by hp.createtime desc
    </select>

    <select id="listPageChannelGroupList" resultType="cn.roadm.admin.table.SmsChannelGroupTable">
        select id, name, network, platform, biz, strategy, channels, status, weight, strategytype, modifytime, overstocks
        from sms_channel_group g
        <where>
            g.status = '1'
            <if test="queryVO.network != null and queryVO.network != ''">
                and g.network = #{queryVO.network}
            </if>
            <if test="queryVO.name != null and queryVO.name != ''">
                and g.name like CONCAT('%', #{queryVO.name}, '%')
            </if>
            <if test="queryVO.channelId != null and queryVO.channelId != ''">
                and CONCAT(',', channels, ',') like CONCAT('%,', #{queryVO.channelId}, ',%')
            </if>
        </where>
        order by g.id desc
    </select>

    <select id="getChannelGroupDetail" resultType="cn.roadm.admin.table.SmsChannelGroupTable">
         select id, name, network, platform, biz, strategy, channels, status, weight, strategytype, modifytime, overstocks
        from sms_channel_group g where id = #{channelGroupId}
    </select>

    <select id="getAllChannelOptionOtypeList" resultType="cn.roadm.admin.dto.ChannelSimpleListDO">
        select id as channelId, CHANNEL_NAME as channelName, IS_SUPPORT_MOBILE as isMobile, IS_SUPPORT_UNICOM as isUnicom, IS_SUPPORT_TELECOM as isTelecom from sms_send_channel_info where status = 1
    </select>

    <insert id="addChannelGroup">
         insert into sms_channel_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test = "id != null">
                id,
            </if>
            <if test = "network != '' and network!=null">
                network,
            </if>
            <if test = "name != null and name!= ''">
                name,
            </if>
            <if test = "biz != null and biz != ''">
                biz,
            </if>
            <if test = "channels != null and channels != ''">
                channels,
            </if>
             <if test = "weights != null and weights != ''">
                weight,
            </if>
            <if test = "overstocks != null and overstocks != ''">
                overstocks,
            </if>
             modifyTime,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test = "id != null">
                #{id, jdbcType=INTEGER},
            </if>
            <if test = "network != null and network != ''">
                #{network, jdbcType=VARCHAR},
            </if>
            <if test = "name != null and name != ''">
                #{name, jdbcType=VARCHAR},
            </if>
            <if test = "biz != null and biz != ''">
                #{biz, jdbcType=VARCHAR},
            </if>
            <if test = "channels != null and channels != ''">
                #{channels, jdbcType=VARCHAR},
            </if>
             <if test = "weights != null and weights != ''">
                #{weights, jdbcType=VARCHAR},
            </if>
            <if test = "overstocks != null and overstocks != ''">
                #{overstocks, jdbcType=VARCHAR},
            </if>
            sysdate(),
        </trim>
    </insert>

    <update id="updateChannelGroup">
        update sms_channel_group
        <set>
            <if test = "name != null and name != ''">
                name = #{name, jdbcType=VARCHAR},
            </if>
            <if test = "network != null and network != ''">
                network = #{network, jdbcType=VARCHAR},
            </if>
            <if test = "biz != null and biz != ''">
                biz = #{biz, jdbcType=VARCHAR},
            </if>
            <if test = "channels != null and channels != ''">
                channels = #{channels, jdbcType=VARCHAR},
            </if>
             <if test = "weights != null and weights != ''">
                weight = #{weights, jdbcType=VARCHAR},
            </if>
            <if test = "overstocks != null and overstocks != ''">
                overstocks = #{overstocks, jdbcType=VARCHAR},
            </if>
            modifyTime = sysdate(),
        </set>
        where id = #{id, jdbcType=INTEGER}
    </update>

    <update id="updateChannelGroupStatus">
        update sms_channel_group
        set status = '0'
        where id = #{id, jdbcType=INTEGER}
    </update>

    <select id="getChannelGroupOptionsByOtype" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as value, name as label
        from sms_channel_group g
        <where>
            g.status = '1'
            <if test="otype != null and otype > 0">
                and g.network = #{otype}
            </if>
        </where>
    </select>

    <select id="listPageQueryChannelGroupProvinceSpList" resultType="cn.roadm.admin.dto.ChannelGroupProvinceSpListDTO">
        select sp.id                       as spId,
               sp.sp_name                  as spName,
               sp.is_open_province            isOpenProvince,
               sp.pro_group_id             as proGroupId,
               sp.is_open_province_group      isOpenProvinceGroup,
                spg.name                   as proGroupName,
               GROUP_CONCAT(spgc.PROVINCE) as channelGroupProvince
        from sms_sp_info sp
                 left join sms_province_group spg on sp.pro_group_id = spg.id
                 left join sms_province_group_config spgc on spg.id = spgc.group_id
        <where>
            sp.status = 1
            <if test="reqVO.spId != null">
                and sp.id = #{reqVO.spId}
            </if>
            <if test="reqVO.spName != null and reqVO.spName != ''">
                and sp.sp_name like CONCAT('%', #{reqVO.spName}, '%')
            </if>
            <if test="reqVO.channelId != null and reqVO.channelId != '-1'">
                and sp.id in (select spid
                              from sms_channel_province_config
                              where mobilechannel = #{reqVO.channelId}
                                 or unicomchannel = #{reqVO.channelId}
                                 or telecomchannel = #{reqVO.channelId}
                              union
                              select spi.id spid
                              from sms_province_group_config spgc,
                                   sms_sp_info spi
                              where spgc.group_id = spi.pro_group_id
                                and (spgc.mobilechannel = #{reqVO.channelId} or
                                     spgc.unicomchannel = #{reqVO.channelId} or
                                     spgc.telecomchannel = #{reqVO.channelId}))
            </if>
        </where>
        group by sp.id
    </select>

    <select id="listSpChannelProvinceByIds" resultType="cn.roadm.admin.dto.ChannelGroupProvinceSpListDTO">
        select spid as spId, group_concat(province) as spChannelProvince from sms_channel_province_config where SPID in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        group by spid
    </select>

    <select id="getChannelProvinceGroupOptions" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as value, name as label from sms_province_group where status = 1
    </select>

    <select id="getChannelGroupProvinceDetailBySp" resultType="cn.roadm.admin.dto.SmsProvinceGroupConfigTable">
        select id, province, mobileChannel, unicomChannel, telecomChannel, spId from sms_channel_province_config where SPID = #{spId}
    </select>

    <delete id="deleteThisSpChannelProvinces">
        delete from sms_channel_province_config where SPID = #{spId}
    </delete>

    <insert id="insertSpChannelProvinces">
        insert into sms_channel_province_config (SPID, PROVINCE, MOBILECHANNEL, UNICOMCHANNEL, TELECOMCHANNEL, CREATETIME) values
            <foreach collection="channelGroupProvinceSpAddVO.provinceChannels" item="item" separator=",">
                (#{channelGroupProvinceSpAddVO.spId}, #{item.province}, #{item.mobileChannel}, #{item.unicomChannel}, #{item.telecomChannel}, now())
            </foreach>
    </insert>

    <select id="listPageQueryChannelGroupProvinceChannelList" resultType="cn.roadm.admin.dto.ChannelProvinceGroupDTO">
        SELECT g.id,
               g.name,
               DATE_FORMAT(g.createtime, '%Y-%m-%d %H:%i:%s') as createTime,
               g.`desc` as description,
               GROUP_CONCAT(gc.province)                      as provinces,
               GROUP_CONCAT(gc.mobilechannel)                 as mobileChannels,
               GROUP_CONCAT(gc.unicomchannel)                 as unicomchannels,
               GROUP_CONCAT(gc.telecomchannel)                as telecomchannels
        FROM sms_province_group g
                 left join sms_province_group_config gc on g.id = gc.group_id
        <where>
            STATUS = 1
            <if test="channelProvinceGroupReqVO.groupName != null and channelProvinceGroupReqVO.groupName != '' ">
                and g.name like CONCAT('%', #{channelProvinceGroupReqVO.groupName}, '%')
            </if>
        </where>
        GROUP BY g.id
    </select>

    <select id="getChannelGroupProvinceChannelDetail" resultType="cn.roadm.admin.dto.SmsProvinceGroupConfigTable">
        select id, province, mobileChannel, unicomChannel, telecomChannel from sms_province_group_config where group_id = #{groupId}
    </select>


    <select id="listPageChannelVideoGroupList" resultType="cn.roadm.admin.table.MmsChannelGroupTable">
        select id, name, network, platform, biz, strategy, channels, status, weight, strategytype, modifytime, overstocks
        from mms_channel_group g
        <where>
            g.status = '1'
            <if test="queryVO.network != null and queryVO.network != ''">
                and g.network = #{queryVO.network}
            </if>
            <if test="queryVO.name != null and queryVO.name != ''">
                and g.name like CONCAT('%', #{queryVO.name}, '%')
            </if>
            <if test="queryVO.channelId != null and queryVO.channelId != ''">
                and CONCAT(',', channels, ',') like CONCAT('%,', #{queryVO.channelId}, ',%')
            </if>
        </where>
        order by g.id desc
    </select>

    <select id="getChannelVideoGroupDetail" resultType="cn.roadm.admin.table.MmsChannelGroupTable">
        select id, name, network, platform, biz, strategy, channels, status, weight, strategytype, modifytime, overstocks
        from mms_channel_group g where id = #{channelGroupId}
    </select>

    <insert id="addChannelVideoGroup">
        insert into mms_channel_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test = "id != null">
                id,
            </if>
            <if test = "network != '' and network!=null">
                network,
            </if>
            <if test = "name != null and name!= ''">
                name,
            </if>
            <if test = "biz != null and biz != ''">
                biz,
            </if>
            <if test = "channels != null and channels != ''">
                channels,
            </if>
            <if test = "weights != null and weights != ''">
                weight,
            </if>
            <if test = "overstocks != null and overstocks != ''">
                overstocks,
            </if>
            modifyTime,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test = "id != null">
                #{id, jdbcType=INTEGER},
            </if>
            <if test = "network != null and network != ''">
                #{network, jdbcType=VARCHAR},
            </if>
            <if test = "name != null and name != ''">
                #{name, jdbcType=VARCHAR},
            </if>
            <if test = "biz != null and biz != ''">
                #{biz, jdbcType=VARCHAR},
            </if>
            <if test = "channels != null and channels != ''">
                #{channels, jdbcType=VARCHAR},
            </if>
            <if test = "weights != null and weights != ''">
                #{weights, jdbcType=VARCHAR},
            </if>
            <if test = "overstocks != null and overstocks != ''">
                #{overstocks, jdbcType=VARCHAR},
            </if>
            sysdate(),
        </trim>
    </insert>

    <update id="updateChannelVideoGroup">
        update mms_channel_group
        <set>
            <if test = "name != null and name != ''">
                name = #{name, jdbcType=VARCHAR},
            </if>
            <if test = "network != null and network != ''">
                network = #{network, jdbcType=VARCHAR},
            </if>
            <if test = "biz != null and biz != ''">
                biz = #{biz, jdbcType=VARCHAR},
            </if>
            <if test = "channels != null and channels != ''">
                channels = #{channels, jdbcType=VARCHAR},
            </if>
            <if test = "weights != null and weights != ''">
                weight = #{weights, jdbcType=VARCHAR},
            </if>
            <if test = "overstocks != null and overstocks != ''">
                overstocks = #{overstocks, jdbcType=VARCHAR},
            </if>
            modifyTime = sysdate(),
        </set>
        where id = #{id, jdbcType=INTEGER}
    </update>

    <update id="updateChannelVideoGroupStatus">
        update mms_channel_group
        set status = '0'
        where id = #{id, jdbcType=INTEGER}
    </update>

    <update id="updateChannelUpdateTime">
        update sms_send_channel_info
        set update_time = now()
        where id = #{id}
    </update>

</mapper>
