<?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.SpChannelConfigMapper">
    <select id="getChannelPageSelectDataByOtype" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsStringVO">
        select id as value, channel_name as label
        from sms_send_channel_info
        <where>
            STATUS = 1
            <if test="isSupportUnicom != null and isSupportUnicom > 0">
                and is_support_unicom = #{isSupportUnicom}
            </if>
            <if test="isSupportMobile != null and isSupportMobile > 0">
                and is_support_mobile = #{isSupportMobile}
            </if>
            <if test="isSupportTelecom != null and isSupportTelecom > 0">
                and is_support_telecom = #{isSupportTelecom}
            </if>
            <if test="isSupportInternational != null and isSupportInternational > 0">
                and is_support_international = #{isSupportInternational}
            </if>
        </where>
    </select>

    <select id="getAllOtypeChannelGroupData" resultType="cn.roadm.admin.entity.PaGroupConfig">
        select id,
               name,
               network
        from sms_channel_group g
        where g.status = '1'
    </select>

    <select id="getEditSpChannelConfig" resultType="cn.roadm.admin.table.SmsSpChannelConfigTable">
        select id,
               network,
               type,
               mobile,
               unicom,
               telecom,
               spId,
               mobileGroup,
               unicomGroup,
               telecomGroup,
               internation
        from sms_sp_channel_config
        where spId = #{spId}
    </select>

    <select id="getSpPrice" resultType="java.lang.Double">
        select a.saleprice
        from sms_api_setting a
        where a.sp_id = #{spid}
    </select>

    <select id="getChannelPriceById" resultType="string">
        select price
        from sms_send_channel_info
        where id = #{id}
    </select>

    <select id="getSpChannelConfigId" resultType="cn.roadm.admin.table.SmsSpChannelConfigTable">
        select id,
               network,
               type,
               mobile,
               unicom,
               telecom,
               spId,
               mobileGroup,
               unicomGroup,
               telecomGroup,
               internation
        from sms_sp_channel_config g
        where g.spId = #{spId}
    </select>

    <insert id="insertSpChannelConfig">
        insert into sms_sp_channel_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="network != '' and network != null">
                network,
            </if>
            <if test="type != '' and type != null">
                type,
            </if>
            <if test="spId != '' and spId != null">
                spId,
            </if>
            <if test="mobile != '' and mobile != null">
                mobile,
            </if>
            <if test="unicom != '' and unicom != null">
                unicom,
            </if>
            <if test="telecom != '' and telecom != null">
                telecom,
            </if>
            <if test="mobileGroup != '' and mobileGroup != null">
                mobileGroup,
            </if>
            <if test="unicomGroup != '' and unicomGroup != null">
                unicomGroup,
            </if>
            <if test="telecomGroup != '' and telecomGroup != null">
                telecomGroup,
            </if>
            <if test="internation != '' and internation">
                internation,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="network != null and network != ''">
                #{network},
            </if>
            <if test="type != null and type != ''">
                #{type},
            </if>
            <if test="spId != null and spId != ''">
                #{spId},
            </if>
            <if test="mobile != null and mobile != ''">
                #{mobile},
            </if>
            <if test="unicom != null and unicom != ''">
                #{unicom},
            </if>
            <if test="telecom != null and telecom != ''">
                #{telecom},
            </if>
            <if test="mobileGroup != null and mobileGroup != ''">
                #{mobileGroup},
            </if>
            <if test="unicomGroup != null and unicomGroup != ''">
                #{unicomGroup},
            </if>
            <if test="telecomGroup != null and telecomGroup != ''">
                #{telecomGroup},
            </if>
            <if test="internation != null and internation != ''">
                #{internation},
            </if>
        </trim>
    </insert>

    <update id="updateSpChannelConfig">
        update sms_sp_channel_config
        <set>
            <if test="network != null">
                network = #{network},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="spId != null">
                spId = #{spId},
            </if>
            <if test="mobile != null">
                mobile = #{mobile},
            </if>
            <if test="unicom != null">
                unicom = #{unicom},
            </if>
            <if test="telecom != null">
                telecom = #{telecom},
            </if>
            <if test="mobileGroup != null">
                mobileGroup = #{mobileGroup},
            </if>
            <if test="unicomGroup != null">
                unicomGroup = #{unicomGroup},
            </if>
            <if test="telecomGroup != null">
                telecomGroup = #{telecomGroup},
            </if>
            <if test="internation != null">
                internation = #{internation},
            </if>
        </set>
        where id = #{id, jdbcType=INTEGER}
    </update>

    <select id="getChannelConfigInfo" resultType="cn.roadm.admin.entity.ChannelConfig">
        select cg.id,
               cg.opertor_type as operator_type,
               cg.channelid,
               cg.sp_id,
               cg.createtime
        from sms_chennel_config cg
        <where>
            <if test="sp_id != null and sp_id > 0">
                cg.sp_id = #{sp_id}
            </if>
            <if test="operator_type != null and operator_type > 0">
                and cg.opertor_type = #{operator_type}
            </if>
        </where>
    </select>

    <update id="updateChannelConfig">
        update sms_chennel_config
        set channelid=#{channelId}
        where id = #{id}
    </update>

    <insert id="insertChannelConfig">
        insert into sms_chennel_config (opertor_type, channelid, sp_id, createtime)
        values (#{operator_type}, #{channelId}, #{sp_id}, sysdate())
    </insert>

    <update id="updateSpInfoOfChannelConfig">
        update sms_sp_info
        <set>
            <if test="mobilePrefix != null">
                mobile_prefix=#{mobilePrefix,jdbcType=VARCHAR},
            </if>
            <if test="mobileSuffix != null">
                mobile_suffix=#{mobileSuffix,jdbcType=VARCHAR},
            </if>
            <if test="signature != null">
                signature=#{signature,jdbcType=VARCHAR},
            </if>
            <if test="mobileNum != null">
                mobile_num=#{mobileNum,jdbcType=VARCHAR},
            </if>
            <if test="unicomPrefix != null">
                unicom_prefix=#{unicomPrefix,jdbcType=VARCHAR},
            </if>
            <if test="unicomSuffix != null">
                unicom_suffix=#{unicomSuffix,jdbcType=VARCHAR},
            </if>
            <if test="unicomNum != null">
                unicom_num=#{unicomNum,jdbcType=VARCHAR},
            </if>
            <if test="telecomPrefix != null">
                telecom_prefix=#{telecomPrefix,jdbcType=VARCHAR},
            </if>
            <if test="telecomSuffix != null">
                telecom_suffix=#{telecomSuffix,jdbcType=VARCHAR},
            </if>
            <if test="telecomNum != null">
                telecom_num=#{telecomNum,jdbcType=VARCHAR},
            </if>
            <if test="internationalPrefix != null">
                international_prefix=#{internationalPrefix,jdbcType=VARCHAR},
            </if>
            <if test="internationalSuffix != null">
                international_suffix=#{internationalSuffix,jdbcType=VARCHAR},
            </if>
            <if test="internationalNum != null">
                international_num=#{internationalNum,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id}
    </update>

    <select id="getSpMainUserName" resultType="java.lang.String">
        select username
        from sms_sp_info sp
                 left join sms_sp_user_info user on sp.id = user.sp_id
        where sp.id = #{spId}
          and sp.status = 1
          and user.role_id = 0
        limit 1
    </select>

    <select id="getChannelInfoRepeat" resultType="java.lang.Integer">
        select count(1)
        from sms_sp_info sp,
             sms_api_setting sa
        where sp.id = sa.sp_id
          and sp.status = 1
        <if test="spId != null and spId > 0">
            AND sp.id != #{spId}
        </if>
        <if test="channelNum != null and channelNum != ''">
            <if test="otype == 1">
                and sp.unicom_num = #{channelNum}
            </if>
            <if test="otype == 2">
                and sp.mobile_num = #{channelNum}
            </if>
            <if test="otype == 3">
                and sp.telecom_num = #{channelNum}
            </if>
        </if>
    </select>

    <update id="updateSpApiSettingOtypeLimit">
        update sms_api_setting
        set otype_limit = #{otypeLimit}
        where sp_id = #{spId}
    </update>

    <insert id="insertSpApiSettingOtypeLimitIfAbsent">
        insert into sms_api_setting (sp_id, sp_audit_type, return_type, content_type, saleprice, sp_audit_number, otype_limit)
        select #{spId}, 0, 2, 1, 0, 0, #{otypeLimit}
        from dual
        where not exists (select 1 from sms_api_setting where sp_id = #{spId})
    </insert>
</mapper>
