<?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.mapper.SpApiSocketMapper">

    <resultMap type="SpApiSocket" id="SpApiSocketResultMap">
        <id column="clientid" property="clientid" />
        <id column="user_id" property="user_id" />
        <id column="reporttype" property="reporttype" />
        <result column="spid" property="spid" />
        <result column="password" property="password" />
        <result column="callmdn" property="callmdn" />
    </resultMap>

    <resultMap type="cn.roadm.bo.CmppSendSpeed" id="CmppSendSpeedMap">
        <result column="spid" property="spid" />
        <result column="speed" property="speed" />
        <result column="monitortime" property="monitortime" />
        <result column="createtime" property="createtime" />
    </resultMap>
    <select id="getSpApiSocket" parameterType="int"
            resultMap="SpApiSocketResultMap">
        SELECT
        s.clientid,
         s.max_connect,
          s.max_speed,
        su.id user_id,
        s.reporttype,
        sp.id spid,
        sp.sp_name,
        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="spid!=null and spid!=''">
                and sp.id= #{spid}
            </if>
        </where>
        limit 1
    </select>
    <select id="getSpApiSocketId" parameterType="int"
            resultMap="SpApiSocketResultMap">
        SELECT
        s.clientid,
         s.max_connect,
          s.max_speed,
        s.reporttype,
        s.`password`,
         s.callmdn
        FROM
        sms_api_socket_sp s
        order by s.clientid+0 desc
        limit 1
    </select>
    <select id="getSpApiSocketCallmdn" parameterType="int"
            resultMap="SpApiSocketResultMap">
        SELECT
        s.clientid,
         s.max_connect,
          s.max_speed,
        s.reporttype,
        s.`password`,
         s.callmdn
        FROM
        sms_api_socket_sp s
        <where>
        	<if test="callmdnLength!=null and callmdnLength!=''">
        		and LENGTH(callmdn) = #{callmdnLength}
        	</if>
            <if test="callmdnStartPre!=null and callmdnStartPre!=''">
                and callmdn like CONCAT( #{callmdnStartPre} ,'%')
            </if>
        </where>
        order by s.callmdn+0 desc
        limit 1
    </select>
    <select id="listPageSpApiSocket" parameterType="SpApiSocket"
            resultMap="SpApiSocketResultMap">
        SELECT
        s.clientid,
         s.max_connect,
          s.max_speed,
        su.id user_id,
        s.reporttype,
        sp.id spid,
        sp.sp_name,
        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="spid!=null and spid!=''">
                and sp.id= #{spid}
            </if>
            <if test="sp_name!=null and sp_name!=''">
                and sp.sp_name  like CONCAT('%',#{sp_name},'%')
            </if>
            <if test="user_id != null and user_id != ''">
                and s.clientid = #{user_id}
            </if>
            <if test="sp_saler!=null and sp_saler!=''">
                and sp.sp_saler = #{sp_saler}
            </if>
            <if test="salerList!=null and salerList.size()>0">
                and sp.sp_saler in
                <foreach collection="salerList" index="index"  item="item" open="(" separator="," close=")">
                    '${item}'
                </foreach>
            </if>
        </where>
	order by ${order}
    </select>
    <update id="updateSpApiSocket" parameterType="SpApiSocket">
        update
        sms_api_socket_sp
        <set>
            <if test="clientid!=null and clientid!= ''">
                clientid=#{clientid},
            </if>
            <if test="user_id!=null ">
                user_id=#{user_id},
            </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="max_connect !=null and max_connect != ''">
                max_connect=#{max_connect},
            </if>
            <if test="max_speed !=null and max_speed != ''">
                max_speed=#{max_speed},
            </if>
        </set>
        where
        spid=#{spid}
    </update>
    <insert id="insertSpApiSocket" parameterType="SpApiSocket">
        INSERT INTO sms_api_socket_sp (clientid,user_id,reporttype,spid,password,callmdn,max_speed,max_connect) VALUES
        (#{clientid},#{user_id},#{reporttype},#{spid},#{password},#{callmdn},#{max_speed},#{max_connect})
    </insert>
    <insert id="deleteSpApiSocket" parameterType="int">
        DELETE  FROM  sms_api_socket_sp  WHERE spid = #{spid}
    </insert>


    <select id="listCmppSpeed" parameterType="cn.roadm.bo.CmppSendSpeed" resultMap="CmppSendSpeedMap">
        SELECT
        s.spid,
        s.speed,
        s.monitortime
        FROM
        sms_cmpp_speed  s
        where 1=1
        <if test="spid !=null and spid !=''">
            and s.spid= #{spid}
        </if>

        <if test="createTime!=null and createTime!=''">
        	and s.createtime &gt;= STR_TO_DATE(#{createTime},'%Y-%m-%d %H:%i:%s')
            <!-- and DATE_FORMAT(s.createtime,'%Y-%m-%d')=#{createTime} -->
        </if>
		<if test="createTime1!=null and createTime1!=''">
        	and s.createtime &lt;= STR_TO_DATE(#{createTime1},'%Y-%m-%d %H:%i:%s')
        </if>
        order by s.createtime
    </select>


     <select id="listCmppSpeedAll" parameterType="cn.roadm.bo.CmppSendSpeed" resultMap="CmppSendSpeedMap">
        SELECT
        sum(s.speed) speed,
        s.monitortime
        FROM
        sms_cmpp_speed  s
        <where>
       <if test="spid !=null and spid !=''">
            s.spid= #{spid}
        </if>
        <if test="createTime!=null and createTime!=''">
        	and s.createtime &gt;= STR_TO_DATE(#{createTime},'%Y-%m-%d %H:%i:%s')
            <!-- and DATE_FORMAT(s.createtime,'%Y-%m-%d')=#{createTime} -->
        </if>
		<if test="createTime1!=null and createTime1!=''">
        	and s.createtime &lt;= STR_TO_DATE(#{createTime1},'%Y-%m-%d %H:%i:%s')
        </if>
        </where>
        group by s.createtime,s.monitortime
         order by s.createtime
    </select>

    <!-- select spid as spId,avg(speed) as speed  from sms_cmpp_speed where
createtime>'2023-10-10 09:00:00' and createtime<'2023-10-10 10:00:00'
GROUP BY spid ORDER BY speed desc limit 10 -->
    <select id="listCmppSpeedTop" parameterType="cn.roadm.bo.CmppSendSpeed" resultType="cn.roadm.bo.CmppSendSpeed">
        SELECT
        avg(IFNULL(s.speed, 0)) as averageSpeed,
        s.spid
        FROM
        sms_cmpp_speed  s
        <where>
            <if test="spid !=null and spid !=''">
                s.spid= #{spid}
            </if>
            <if test="createTime!=null and createTime!=''">
                and s.createtime &gt;= STR_TO_DATE(#{createTime},'%Y-%m-%d %H:%i:%s')
                <!-- and DATE_FORMAT(s.createtime,'%Y-%m-%d')=#{createTime} -->
            </if>
            <if test="createTime1!=null and createTime1!=''">
                and s.createtime &lt;= STR_TO_DATE(#{createTime1},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
        GROUP BY s.spid order by averageSpeed desc limit 10
    </select>

</mapper>
