package com.cnd3b.common.data.db;

import com.cnd3b.common.parameter.parameter;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Properties;

public class SQLiteJDBC {
    public Connection c = null;
    Statement stmt = null;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");


    public void open() {
        try {
            OperatingSystemMXBean system = ManagementFactory.getOperatingSystemMXBean();
            String OSName = system.getName();
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:" + parameter.SQLiteFilePath());
            stmt = c.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void createP2DataConfig(String datakey, String driver, String loginid, String password, String url) {
        open();
        String sql1 = "delete from datasource where datakey='" + datakey + "'";
        String sql2 = "insert into datasource(datakey,driver,username,password,url) values('" + datakey + "','" + driver + "','" + loginid
                + "','" + password + "','" + url + "')";
        try {
            stmt.execute(sql1);
            stmt.execute(sql2);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }


    public Properties getDataSource(String datakey) {
        Properties prop = new Properties();
        open();
        String sql = "select driver,username,password,url from datasource where datakey='" + datakey + "'";
        try {
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                prop.setProperty("driver", rs.getString("driver"));
                prop.setProperty("username", rs.getString("username"));
                prop.setProperty("password", rs.getString("password"));
                prop.setProperty("url", rs.getString("url"));
            }
            rs.close();
            rs = null;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return prop;
    }

    public void InsertLogMsg(String keyname, String msg, String type) {
        open();
        String sql1 = "delete from log where keyname='" + keyname + "' and msg='" + msg + "' and type='" + type + "'";
        String date = format.format(Calendar.getInstance().getTime());
        String sql2 = "insert into log(createtime,keyname,msg,type) values('" + date + "','" + keyname + "','" + msg
                + "','" + type + "')";
        try {
            stmt.execute(sql1);
            stmt.execute(sql2);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }

    public ArrayList<HashMap<String, Object>> getLog() {
        open();
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        String sql1 = "select createtime,type,keyname,msg from log order by createtime desc ";
        try {
            ResultSet rs = stmt.executeQuery(sql1);

            while (rs.next()) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("createtime", rs.getString("createtime"));
                map.put("type", rs.getString("type"));
                map.put("keyname", rs.getString("keyname"));
                map.put("msg", rs.getString("msg"));
                list.add(map);
            }
            rs.close();
            rs = null;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return list;
    }

    public ArrayList<HashMap<String, Object>> getServices() {
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        open();
        String sql = "select classname,intervaltime,used,remarks,lastruntime from services";
        try {
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("classname", rs.getString("classname"));
                map.put("intervaltime", rs.getLong("intervaltime"));
                map.put("used", rs.getBoolean("used"));
                map.put("remarks", rs.getString("remarks"));
                map.put("lastruntime", rs.getString("lastruntime"));
                list.add(map);
            }
            rs.close();
            rs = null;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return list;
    }

    public boolean checkServicesUsed(String classname, Boolean islimittime) {
        if (islimittime && Calendar.getInstance().get(Calendar.HOUR_OF_DAY) > 0 && Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 8) {
            return false;
        }
        boolean isused = false;
        open();
        String sql = "select used from services where classname='" + classname + "' and used=1 ";
        try {
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                isused = true;
                String date = format.format(Calendar.getInstance().getTime());
                String sql2 = "update services set lastruntime='" + date + "' where classname='" + classname + "'";
                stmt.execute(sql2);
            }
            rs.close();
            rs = null;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return isused;
    }

    public void close() {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (c != null) {
                c.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SQLiteJDBC jdbc = new SQLiteJDBC();
        jdbc.open();
        jdbc.close();
    }


}
