<?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="com.tsi.api.server.mybatis.TscSsipApiMapper">

    <select id="getApiAuthorizedInfo" parameterType="java.util.HashMap" resultType="com.tsi.api.server.vo.AuthorizedVo">
        <![CDATA[
        SELECT TRIM(a.ipaddr) AS accessIpAddress,
               IF (a.expiredt >= now(), 1, 0) AS isAuthorized
          FROM tb_tsc_api_client a, tb_tsc_api_acl b
         WHERE a.useyn    = 'Y'
           AND a.state    = 'C'
           AND a.apitoken = #{apiToken}
           AND a.apitoken = b.apitoken
           AND b.apiid    = #{apiId}
           AND b.acl      = 'A'
        ]]>
    </select>

    <select id="getApiKafkaAuthorizedInfo" parameterType="java.util.HashMap" resultType="com.tsi.api.server.vo.KafkaAuthorizedVo">
        <![CDATA[
        SELECT TRIM(a.ipaddr) AS accessIpAddress,
               IF (a.expiredt >= now(), 1, 0) AS isAuthorized,
               NVL(broker_id, 'kAfKAiD') AS brokerId,
               NVL(broker_pwd, 'kAfKApWd') AS brokerPwd
        FROM tb_tsc_api_client a
        WHERE a.useyn    = 'Y'
          AND a.state    = 'C'
          AND a.apitoken = #{apiToken}
        ]]>
    </select>

    <select id="getNodeInfoList" resultType="com.tsi.api.server.vo.NodeVo">
        <![CDATA[
        SELECT nodeid AS nodeId,
               TRIM(name) AS nodeName,
               FORMAT(latitude, 10) AS lat,
               FORMAT(longitude, 9) AS lng
          FROM tb_tsc_node
         WHERE useyn  = 'Y'
        /*  AND nodeyn = 'Y'*/
     ]]>
   </select>

   <select id="getBrokerInfo" parameterType="java.lang.String" resultType="com.tsi.api.server.vo.BrokerVo">
        <![CDATA[
        SELECT TRIM(b.host) AS ipAddr, b.port AS port, b.username AS id, b.password AS pwd
          FROM tb_tsc_api_client a, tb_tsc_broker b
         WHERE a.apitoken = #{apiToken}
           AND a.expiredt >= now()
           AND a.brokerid = b.broker_id
           AND b.useyn    = 'Y'
     ]]>
   </select>

    <insert id="insertInvokeHs" parameterType="com.tsi.api.server.vo.ApiInvokeVo">
        <![CDATA[
        INSERT INTO tb_tsc_api_invoke_hs (apiid, apitoken, ipaddr, eventdt, error)
        VALUES (#{apiId}, #{apiToken}, #{ipAddr}, now(), #{error})
        ]]>
    </insert>

    <insert id="insertSystemInfo" parameterType="com.tsi.api.server.dto.SystemStatusDto">
        <![CDATA[
        INSERT INTO tb_tsc_system (host_name, os_type, os_name, os_version, os_arch, kernel_release, internal_ip, external_ip, total_mem, swap_mem, total_disk, cpu_cnt, uptimes)
        VALUES (#{info.hostName}, #{info.osType}, #{info.osName}, #{info.osVersion}, #{info.osArch}, #{info.kernelRelease}, #{info.internalIp}, #{info.externalIp},
                #{info.totalMem}, #{info.swapMem}, #{info.totalDisk}, #{info.cpuCnt}, #{info.upTimes})
            ON DUPLICATE KEY UPDATE
                                 os_type = #{info.osType},
                                 os_name = #{info.osName},
                                 os_version = #{info.osVersion},
                                 os_arch = #{info.osArch},
                                 kernel_release = #{info.kernelRelease},
                                 internal_ip = #{info.internalIp},
                                 external_ip = #{info.externalIp},
                                 total_mem = #{info.totalMem},
                                 swap_mem = #{info.swapMem},
                                 total_disk = #{info.totalDisk},
                                 cpu_cnt = #{info.cpuCnt},
                                 uptimes = #{info.upTimes}
        ]]>
    </insert>

    <insert id="insertSystemStatusHs" parameterType="com.tsi.api.server.vo.SystemStatusVo">
        <![CDATA[
        INSERT INTO tb_tsc_system_status_hs (clct_dt, host_name, total_mem, use_mem, total_disk, use_disk, use_mem_rate, use_cpu_rate, use_disk_rate, task_cnt, thread_cnt, load_avg)
        VALUES (STR_TO_DATE(#{info.clctDt}, '%Y%m%d%H%i%s'), #{info.hostName}, #{info.totalMem}, #{info.useMem}, #{info.totalDisk}, #{info.useDisk}, #{info.useMemRate}, #{info.useCpuRate}, #{info.useDiskRate}, #{info.tasks}, #{info.threads}, #{info.loadAvg})
        ]]>
    </insert>

    <insert id="insertSystemStatusHsxx" parameterType="com.tsi.api.server.vo.SystemStatusVo">
        <![CDATA[
        INSERT INTO tb_tsc_system_status_hs (clct_dt, host_name, total_mem, use_mem, total_disk, use_disk, use_mem_rate, use_cpu_rate, use_disk_rate, task_cnt, thread_cnt, load_avg)
        VALUES (DATE_SUB(NOW(), INTERVAL MOD(NOW(), 10) SECOND), #{info.hostName}, #{info.totalMem}, #{info.useMem}, #{info.totalDisk}, #{info.useDisk}, #{info.useMemRate}, #{info.useCpuRate}, #{info.useDiskRate}, #{info.tasks}, #{info.threads}, #{info.loadAvg})
        ]]>
    </insert>

    <select id="getAlarmConfig" parameterType="java.util.HashMap" resultType="com.tsi.api.server.mybatis.vo.AlarmConfigVo">
        <![CDATA[
        select a.alarm_code as alarmCode, a.desc,
               a.value, a.useyn
        from tb_tsc_alarm_config a
        ]]>
    </select>

</mapper>
