<?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.SmsSocketSpMapper">
    <resultMap id="smsSocketListMap" type="cn.roadm.admin.dto.SmsSocketSpListDO">
        <id column="clientId" property="clientId"/>
        <result column="max_connect" property="maxConnect"/>
        <result column="max_speed" property="maxSpeed"/>
        <result column="userId" property="userId"/>
        <result column="reporttype" property="reportType"/>
        <result column="spId" property="spId"/>
        <result column="sp_name" property="spName"/>
        <result column="callmdn" property="callMdn"/>
        <result column="password" property="password"/>
    </resultMap>

    <select id="listPageQuerySmsSocketSp" resultMap="smsSocketListMap">
        SELECT s.clientid clientId,
               s.max_connect,
               s.max_speed,
               su.id as userId,
               s.reporttype,
               sp.id as spId,
               sp.sp_name,
               sp.ip whiteIp,
               s.`password`,
               s.callmdn
        FROM sms_api_socket_sp s
                 LEFT JOIN sms_sp_info sp
                           ON s.spid = sp.id
                 LEFT JOIN sms_sp_user_info su
                           ON sp.id = su.sp_id
        <where>
            su.role_id = 0 AND su.status = 1
            <if test="queryVO.spId != null and queryVO.spId != ''">
                and sp.id = #{queryVO.spId}
            </if>
            <if test="queryVO.spName != null and queryVO.spName != ''">
                and sp.sp_name like CONCAT('%', #{queryVO.spName}, '%')
            </if>
            <if test="queryVO.userId != null and queryVO.userId != ''">
                and s.clientid = #{queryVO.userId}
            </if>
        </where>

    </select>

    <select id="getSmsSocketSpById" resultMap="smsSocketListMap">
         SELECT s.clientid,
               s.max_connect,
               s.max_speed,
               su.id as userId,
               s.reporttype,
               sp.id as spid,
               sp.sp_name,
               sp.ip whiteIp,
               s.`password`,
               s.callmdn
        FROM sms_api_socket_sp s
                 LEFT JOIN sms_sp_info sp
                           ON s.spid = sp.id
                 LEFT JOIN sms_sp_user_info su
                           ON sp.id = su.sp_id
        <where>
            su.role_id = 0 AND su.status = 1
                and sp.id = #{id}
        </where>
    </select>

    <select id="getSpUserIdById" resultType="int">
        SELECT
        su.id as userId
        FROM sms_sp_info sp
        LEFT JOIN sms_sp_user_info su
        ON sp.id = su.sp_id
        <where>
            su.role_id = 0 AND su.status = 1
            and sp.id = #{id}
        </where>
    </select>

    <select id="getSpApiSocketLastCmppId" resultType="string">
        select clientid
        FROM sms_api_socket_sp s
        ORDER BY s.clientid DESC
        LIMIT 1
    </select>

    <select id="getSpApiSocketLastCallMdn" resultType="java.lang.String">
        SELECT s.callmdn FROM sms_api_socket_sp s
        <where>
        	<if test="callMdnLength != null">
        		and LENGTH(callmdn) = #{callMdnLength}
        	</if>
        </where>
        order by s.callmdn desc
        limit 1
    </select>

    <insert id="insertSpApiSocket">
        INSERT INTO sms_api_socket_sp (clientid,user_id,reporttype,spid,password,callmdn,max_speed,max_connect) VALUES
        (#{clientId},#{userId},#{reportType},#{spId},#{password},#{callMdn},#{maxSpeed}, #{maxConnect})
    </insert>

    <update id="updateSpApiSetting">
         update
        sms_api_setting c
        set
        content_type=#{spType,jdbcType=VARCHAR}
        ,send_province=#{sendProvince,jdbcType=VARCHAR}
        ,yuexin_push = #{yuexinPush,jdbcType=VARCHAR}
        ,mms_sign_report_push = #{mmsSignReportPush,jdbcType=VARCHAR}
        ,content_length_limit = #{contentLengthLimit}
        ,mms_reply_url = #{mmsReplyUrl,jdbcType=VARCHAR}
        <if test="salePrice!=null and salePrice!=''">
	        ,c.saleprice = #{salePrice}
        </if>
        where
        c.sp_id = #{id}
    </update>

    <update id="updateSpApiSocket" >
        update
        sms_api_socket_sp
        <set>
            <if test="clientId!=null and clientId!= ''">
                clientid=#{clientId},
            </if>
            <if test="userId!=null ">
                user_id=#{userId},
            </if>
            <if test="reportType!=null ">
                reporttype=#{reportType},
            </if>
            <if test="password!=null and password!= ''">
                password=#{password},
            </if>
            <if test="callMdn!=null and callMdn!= ''">
                callmdn=#{callMdn},
            </if>
            <if test="maxConnect !=null and maxConnect != ''">
                max_connect=#{maxConnect},
            </if>
            <if test="maxSpeed !=null and maxSpeed != ''">
                max_speed=#{maxSpeed},
            </if>
        </set>
        where
        spid=#{spId}
    </update>
    <insert id="deleteSpApiSocket" parameterType="int">
        DELETE  FROM  sms_api_socket_sp  WHERE spid = #{spid}
    </insert>

    <select id="listCmppSpeed"  resultType="cn.roadm.admin.dto.SmsSocketSpSpeedDO">
        SELECT
        s.spid spId,
        s.speed,
        s.monitortime monitorTime
        FROM
        sms_cmpp_speed  s
        where 1=1
        <if test="spId !=null and spId !=''">
            and s.spid= #{spId}
        </if>
        <if test="startTime!=null and startTime!=''">
            and s.createtime &gt;= STR_TO_DATE(#{startTime},'%Y-%m-%d %H:%i:%s')
        </if>
        <if test="endTime!=null and endTime!=''">
            and s.createtime &lt;= STR_TO_DATE(#{endTime},'%Y-%m-%d %H:%i:%s')
        </if>
        order by s.monitortime
    </select>


    <select id="listCmppSpeedAll"  resultType="cn.roadm.admin.dto.SmsSocketSpSpeedDO">
        SELECT
        sum(s.speed) speed,
        s.monitortime monitorTime
        FROM
        sms_cmpp_speed  s
        <where>
            <if test="spId !=null and spId !=''">
                s.spid= #{spId}
            </if>
            <if test="startTime!=null and startTime!=''">
                and s.createtime &gt;= STR_TO_DATE(#{startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endTime!=null and endTime!=''">
                and s.createtime &lt;= STR_TO_DATE(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
        group by s.createtime,s.monitortime
    </select>
    <select id="listCmppSpeedTop" resultType="cn.roadm.admin.dto.SmsSocketSpSpeedDO">
        SELECT
        avg(IFNULL(s.speed, 0)) as speed,
        s.spid monitorTime
        FROM
        sms_cmpp_speed  s
        <where>
            <if test="spId !=null and spId !=''">
                s.spid= #{spId}
            </if>
            <if test="startTime!=null and startTime!=''">
                and s.createtime &gt;= STR_TO_DATE(#{startTime},'%Y-%m-%d %H:%i:%s')

            </if>
            <if test="endTime!=null and endTime!=''">
                and s.createtime &lt;= STR_TO_DATE(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
        GROUP BY s.spid order by speed desc limit 10
    </select>
</mapper>
