using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using StartServerWPF.Models;
using StartServerWPF.Modules.Main.Models;

namespace StartServerWPF.Modules.Main
{
    public class MySqlHelper
    {
        public RemoteDataBase rdb;
        public MySqlConnection conn;
        public string ConnectionString;
        public MySqlHelper(string serverAddress, string serverPort,
            string userName, string passWord,
            string dataBase, string tableName)
        {
            rdb = new RemoteDataBase();
            rdb.ServerAddress = serverAddress;
            rdb.ServerPort = serverPort;
            rdb.UserName = userName;
            rdb.Password = passWord;
            rdb.DataBase = dataBase;
            rdb.TableName = tableName;
            ConnectionString = "server=" + rdb.ServerAddress + ";port=" + rdb.ServerPort +
                ";user=" + rdb.UserName + ";password=" + rdb.Password + "; database=" + rdb.DataBase + ";";
            conn = new MySqlConnection(ConnectionString);
        }
        public MySqlHelper(RemoteDataBase remotedb)
        {
            rdb = remotedb;
            ConnectionString = "server=" + rdb.ServerAddress + ";port=" + rdb.ServerPort +
             ";user=" + rdb.UserName + ";password=" + rdb.Password + "; database=" + rdb.DataBase + ";";
            conn = new MySqlConnection(ConnectionString);
        }
        public int MySqlExcute(string cmdStr)
        {
            conn.Open();
            MySqlCommand mcmd = new MySqlCommand(cmdStr, conn);
            int rtn = mcmd.ExecuteNonQuery();
            conn.Close();
            return rtn;
        }
        public bool InsertEvent(MsEventModel msEvent)
        {
            conn.Open();
            //检查是否有重复记录
            string timeStr = msEvent.OriginTime.ToString("yyyy-MM-ddTHH:mm:ss.fff");
            string checkExist = "select COUNT('ETime') from " + rdb.TableName + " where ETime='" + timeStr + "'";
            //Console.WriteLine(timeStr);
            MySqlCommand mcmd = new MySqlCommand(checkExist, conn);
            int rtn = Convert.ToInt32(mcmd.ExecuteScalar());
            //0=没有，则新增记录
            if (rtn == 0)
            {
                string insStr = "insert into " + rdb.TableName + "(ETime,X,Y,Z,ML,LocSta,MLSta,Rms)" +
                    "values('" +
                    msEvent.OriginTime.ToString("yyyy-MM-ddTHH:mm:ss.fff") + "','" +
                    msEvent.EastCoordinate.ToString() + "','" +
                    msEvent.NorthCoordinate.ToString() + "','" +
                    msEvent.Depth.ToString() + "','" +
                    msEvent.ML.ToString() + "','" ;
                mcmd = new MySqlCommand(insStr, conn);
                int insertRow = mcmd.ExecuteNonQuery();
                if (insertRow > 0)
                {
                    conn.Close();
                    return true;
                }
                else
                {
                    conn.Close();
                    return false;
                }
            }
            else
            {
                conn.Close();
                return false;
            }
        }
        public DataSet MySqlSelect(string cmdStr)
        {
            conn.Open();
            MySqlCommand mcmd = new MySqlCommand(cmdStr, conn);
            MySqlDataAdapter msda = new MySqlDataAdapter(mcmd);
            DataSet ds = new DataSet();
            msda.Fill(ds, rdb.TableName);
            //int rtn = mcmd.ExecuteNonQuery();
            conn.Close();
            return ds;
        }

    }
}
