<?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.mybatis.TsiDatabaseMapper">

    <select id="getAlarmConfig" parameterType="java.util.HashMap" resultType="com.tsi.comm.server.mybatis.vo.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.mybatis.vo.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.mybatis.vo.NodeVo">
        <![CDATA[
            select a.nodeid, nvl(trim(a.ipaddr), '0'),
                   useyn, cvimyn, nodeyn, testyn
              from tb_tsc_node a
             where a.useyn  = 'Y'
        ]]>
    </select>

    <resultMap id="result-nodeAdd" type="com.tsi.comm.server.mybatis.vo.NodeAddVo">
        <id column="origin_nodeid" property="originNodeId"/>
        <collection column="IFSC_ID" property="addDetailList" ofType="com.tsi.comm.server.mybatis.vo.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, a.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'
             order by a.origin_nodeid, a.nodeid, a.add_node_order
        ]]>
    </select>

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

    <update id="updateNodeStatusTerm">
    <![CDATA[
		update tb_tsc_node_status
		   set status = 0,
		       eventdt = NOW()
    ]]>
    </update>

    <update id="updateNodeStatusLoginTempX" parameterType="com.tsi.comm.server.mybatis.vo.NodeStatusVo">
    <![CDATA[
        update tb_tsc_node_status
        set status  = 1,
            eventdt = STR_TO_DATE(#{stts.eventDt},'%Y%m%d%H%i%S'),
            ipAddr  = #{stts.ipAddr}
        where nodeid = #{stts.nodeId}
        ]]>
    </update>

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


    <insert id="insertKafkaTransHs" parameterType="com.tsi.comm.server.mybatis.vo.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.mybatis.vo.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})
        ]]>
    </insert>

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

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

    <update id="updateProcessStatusStart" parameterType="com.tsi.comm.server.mybatis.vo.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.mybatis.vo.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.mybatis.vo.ProcessStateVo">
    <![CDATA[
        update tb_tsc_process
        set process_state = 1,
            update_tm = NOW()
        where process_id = #{stts.processId}
        ]]>
    </update>

</mapper>
