package com.cesgroup.cesmonitor.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.cesgroup.cesmonitor.bean.FaultWarningDto;
import com.cesgroup.cesmonitor.common.JSON;
import com.cesgroup.cesmonitor.service.MediaService;
import com.cesgroup.cesmonitor.service.OverviewService;

public class MediaTest extends MyTest {

    @Autowired
    MediaService mediaService;

    @Autowired
    OverviewService overviewService;

    //@Test
    public void testDB() {
        Connection conn = null;
        Statement stmt = null;
        try {
            //Map<String, Object> map = mapper.readValue(json, Map.class);
            //             root root  "show status like 'Threads_connected%'"
            //            dbCheck.sh["oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@10.250.190.148:1521:ORCL","JIRA603","JIRA603"]
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://10.250.198.114:3306/wotalk?useUnicode=true&characterEncoding=utf-8";
            String username = "root";
            String password = "root";
            String sql = "show status like 'Threads_connected%'";
            String column = "2";
            if (column == null || column.trim().equals("")) {
                column = "1";
            }
            int c = Integer.parseInt(column);
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            if (rs.next()) {
                System.out.println(rs.getString(c));
            } else {
                System.out.println("0");
            }

        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Throwable se) {
            se.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testQuery() {

        Connection conn = null;
        Statement stmt = null;
        try {
            String driver = "oracle.jdbc.driver.OracleDriver";
            String url = "jdbc:oracle:thin:@10.250.198.158:1521:ORCL";
            String username = "system";
            String password = "system";
            String sql = "select count(*) from v$session";
            String column = "1";
            if (column == null || column.trim().equals("")) {
                column = "1";
            }
            int c = Integer.parseInt(column);
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            stmt = conn.createStatement();
            //            String sql = (String) map.get("sql");
            ResultSet rs = stmt.executeQuery(sql);
            if (rs.next()) {
                System.out.println("结果：" + rs.getString(c));
            } else {
                System.out.println("无数据");
            }

        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Throwable se) {
            se.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //@Test
    public void sendByWeixin() {
        try {
            logger.debug(mediaService.sendByWeixin("张二伟", "测试消息", "aaa测试"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //@Test
    public void sendByMail() {
        try {
            mediaService.sendByMail("zhang.erwei@cesgroup.com.cn", "测试aaaa", "测试bbbb");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //@Test
    public void listsResourceAlarmInfo() {
        try {
            List<FaultWarningDto> lists = overviewService.listsResourceAlarmInfo(
                new Date(new Date().getTime() / 1000 - 3600), new Date(new Date().getTime() / 1000));
            System.out.println(JSON.toJson(lists));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
