<?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.comm.server.dao.maraidb.mapper.TsiCommServerMapper">

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

    <insert id="insertAlarmOccrHs" parameterType="com.tsi.comm.server.vo.mariadb.AlarmOccrVo">
    <![CDATA[
        insert into tb_tsc_alarm_hs(occr_dt, alarm_code, alarm_target, alarm_value)
        values(STR_TO_DATE(#{vo.eventDt},'%Y%m%d%H%i%S'), #{vo.alarmCode}, #{vo.alarmTarget}, #{vo.alarmValue})
        ]]>
    </insert>

    <select id="getNodeInfoList" parameterType="java.util.HashMap" resultType="com.tsi.comm.server.vo.mariadb.NodeVo">
        <![CDATA[
        select a.nodeid as nodeId, nvl(trim(a.ipaddr), '0.0.0.0') as ipAddr,
               useyn as useYn, cvimyn as cvimYn, nodeyn as nodeYn, testyn as testYn
        from tb_tsc_node a
        where a.useyn  = 'Y'
        ]]>
    </select>

    <resultMap id="result-nodeAdd" type="com.tsi.comm.server.vo.mariadb.NodeAddVo">
        <id column="origin_nodeid" property="originNodeId"/>
        <collection column="IFSC_ID" property="addDetailList" ofType="com.tsi.comm.server.vo.mariadb.NodeAddDetailVo" javaType="java.util.ArrayList">
            <result column="nodeid"          property="nodeId" />
            <result column="origin_dir_code" property="originDirCode" />
            <result column="add_node_order"  property="addNodeOrder" />
            <result column="add_dir_code"    property="addDirCode" />
        </collection>
    </resultMap>

    <select id="getNodeAddInfoList" resultMap="result-nodeAdd">
        <![CDATA[
        select a.origin_nodeid,
               a.nodeid,
               a.origin_dir_code,
               a.add_node_order,
               trim(a.add_dir_code) as add_dir_code
        from tb_tsc_node_add a, tb_tsc_node b, tb_tsc_node c
        where a.nodeid        = b.nodeid
          and a.origin_nodeid = c.nodeid
          and b.useyn         = 'Y'
          and c.useyn         = 'Y'
          and trim(a.add_dir_code) is not null
        order by a.origin_nodeid, a.nodeid, a.add_node_order
        ]]>
    </select>

    <update id="updateNodeIpAddr" parameterType="com.tsi.comm.server.vo.mariadb.NodeIpAddrVo">
    <![CDATA[
        update tb_tsc_node
        set ipaddr = #{node.ipAddr}
        where nodeid = #{node.nodeId}
        ]]>
    </update>

    <insert id="insertNodeStatusInitialize">
    <![CDATA[
        insert into tb_tsc_node_status(nodeid, status, eventdt, serverid)
        select nodeid, 0, NOW(), NULL
        from tb_tsc_node
        where nodeid not in (select nodeid from tb_tsc_node_status)
        ]]>
    </insert>

    <update id="updateNodeStatusTerm" parameterType="java.lang.Integer">
    <![CDATA[
        update tb_tsc_node_status
        set status = 0,
            eventdt = NOW()
        where serverid = #{serverId}
        ]]>
    </update>

    <update id="updateNodeStatus_MERGE" parameterType="com.tsi.comm.server.vo.mariadb.NodeStatusVo">
    <![CDATA[
        INSERT INTO tb_tsc_node_status (
            nodeid,
            status,
            eventdt,
            ipAddr,
            serverid
        ) VALUES (
             #{stts.nodeId},
             #{stts.status},
             NOW(),
             #{stts.ipAddr},
             #{stts.serverId}
         )
        ON DUPLICATE KEY UPDATE
             status   = VALUES(status),
             eventdt  = VALUES(eventdt),
             ipAddr   = VALUES(ipAddr),
             serverid = VALUES(serverid);
        ]]>
    </update>

    <update id="updateNodeStatus" parameterType="com.tsi.comm.server.vo.mariadb.NodeStatusVo">
    <![CDATA[
        update tb_tsc_node_status
        set status  = #{stts.status},
            eventdt = NOW(),
            ipAddr  = #{stts.ipAddr},
            serverid = #{stts.serverId}
        where nodeid = #{stts.nodeId}
        ]]>
    </update>

    <insert id="insertKafkaTransHs" parameterType="com.tsi.comm.server.vo.mariadb.KafkaTransVo">
        <![CDATA[
        INSERT INTO tb_tsc_kafka_trans_hs (clct_dt, host_name, status, send_tm, recv_tm)
        VALUES (DATE_SUB(NOW(), INTERVAL MOD(NOW(), 5) SECOND), #{stat.hostName}, #{stat.status}, #{stat.sendTm}, #{stat.recvTm})
        ]]>
    </insert>

    <insert id="insertCommTpsHs" parameterType="com.tsi.comm.server.vo.mariadb.CommStatusVo">
        <![CDATA[
        INSERT INTO tb_tsc_comm_tps_hs (prcn_dt, host_name, sessions, trans, bytes)
        VALUES (STR_TO_DATE(#{stat.eventDt},'%Y%m%d%H%i%S'), LEFT(#{stat.hostName}, 30), #{stat.sessions}, #{stat.trans}, #{stat.bytes})
        ]]>
    </insert>

    <insert id="insertCommTpsHs_OLD" parameterType="com.tsi.comm.server.vo.mariadb.CommStatusVo">
        <![CDATA[
        INSERT INTO tb_tsc_comm_tps_hs (prcn_dt, sessions, trans, bytes)
        VALUES (STR_TO_DATE(#{stat.eventDt},'%Y%m%d%H%i%S'), #{stat.sessions}, #{stat.trans}, #{stat.bytes})
            ON DUPLICATE KEY UPDATE
            sessions = sessions + #{stat.sessions},
            trans = trans + #{stat.trans},
            bytes = bytes + #{stat.bytes}
        ]]>
    </insert>

    <update id="updateProcessStatusStart" parameterType="com.tsi.comm.server.vo.mariadb.ProcessStateVo">
    <![CDATA[
        update tb_tsc_process
        set process_state = 1,
            update_tm = NOW(),
            start_tm = NOW()
        where process_id = #{stts.processId}
        ]]>
    </update>
    <update id="updateProcessStatusStop" parameterType="com.tsi.comm.server.vo.mariadb.ProcessStateVo">
    <![CDATA[
        update tb_tsc_process
        set process_state = 0,
            update_tm = NOW(),
            end_tm = NOW()
        where process_id = #{stts.processId}
        ]]>
    </update>
    <update id="updateProcessStatusRun" parameterType="com.tsi.comm.server.vo.mariadb.ProcessStateVo">
    <![CDATA[
        update tb_tsc_process
        set process_state = 1,
            update_tm = NOW()
        where process_id = #{stts.processId}
        ]]>
    </update>

</mapper>
